Hacker Newsnew | past | comments | ask | show | jobs | submit | SigmundA's commentslogin

Would be nice if PG supported clustered indexes (Index Organized Tables in Oracle speak) as an option if you have a table thats accessed mostly the same way you can get a index without the write amplification because the table is the index.

Clustered indexes only save up to 2x write amplification in the very rare case where you're indexing the entire table (e.g. if it has very few columns).

However, that is usually the least of your concerns with write amplification. If you don't batch your writes, you can easily get 100x write amplification. For any primary key or any other index not strongly correlated with your INSERTs, you can get perhaps another 100x write amplification even if you batch you writes.


For inserts, you cannot escape writing into the base table and all indexes. However, my understanding is that for updates PostgreSQL has a write amplification problem due to the fact that each time a row is updated this creates a new row (to implement MVCC), and a new physical location in the heap, so all indexes need to be updated to point to the new location, even those not containing the updated columns.

OTOH, with a heap-less (aka. clustered, aka. index organized) table, you would only have to update the indexes containing the columns that are actually being updated. You don't need to touch any other index. Furthermore, only if you are updating a key column would you physically "move" the entry into a different part of the B-tree. If you update an included column (PK columns are automatically "included" in all secondary indexes, even if not explicitly mentioned in the index definition), you can do that in-place, without moving the entry.

Here is how this works in SQL Server - consider the following example:

    CREATE TABLE T (

        ID int,
        NAME nvarchar(255) NOT NULL,
        AMOUNT int NOT NULL,

        CONSTRAINT T_PK PRIMARY KEY (ID)

    );

    GO

    CREATE INDEX T_I1 ON T (NAME);

    GO

    CREATE INDEX T_I2 ON T (AMOUNT);
Now, doing this...

    UPDATE T SET AMOUNT = 42 WHERE ID = 100;
...will only write to T_PK and T_I2, but not T_I1. Furthermore T_PK's entry will not need to be moved to a different place in the B-tree. SQL Server uses row versioning similar to PostgreSQL, so it's conceivable that PostgreSQL could behave similarly to SQL Server if it supported clustered (index-organized) tables.

>in the very rare case where you're indexing the entire table (e.g. if it has very few columns).

Not sure I follow most tables are accessed primarily in one way (primary key) while maybe sometimes in others for analysis. Having the PK written twice because it's almost always indexed is normally a waste and good candidate for a clustered index. So much so that many DB's like SQLite and MySql always do clustered indexes on primary key because their storage engine is built such that tables are a b-tree anyway vs PG that has separate b-tree indexes and heap tables. MSSQL and Oracle give you a choice whether the table is a index structure or a heap.

If you have very specific use case tables they can typically have a clustered index and no secondary indexes, you can still scan them for ad-hoc analysis but you get better insert performance and space usage because you aren't double writing to the heap and a PK index like you would in PG.

As far as batch writes that is a separate issue and has to due with whether that even makes sense for durability, if you need to commit a single random row due to something occurring you can't batch that up and maintain consistency, if your bulk loading data sure and is common practice to do commit batches there, clustered indexes could still be a 100 vs 200x write amplification if you have to insert both an index row and heap row vs just a single clustered index row.


Clustered indexes aren't just about write amplification. They also reduce the reads needed to get the data. Sometimes by quite a bit.

That's true for seeks into the clustered (primary) index because that index includes all fields, so you don't need to "jump" to the heap to get them.

However, seeking into a secondary index, and then reading a column not included in that index incurs an additional index seek (into the clustered index), which may be somewhat slower than what would happen in a heap-based table.

So there are pros and cons, as usual...


I have found very minimal penalty on secondary index reads in practice such that it has never made a difference.

Remember some databases always use clustered index internally (SQLite, MySql) such that even if you have no primary key they will create a hidden one instead for use with the index.

https://www.sqlite.org/rowidtable.html

It is nice to have the choice which way to go and would be nice if PG implemented this. It can have significant space savings on narrow table with one primary index and performance advantages.


Another option would be a good way of placing indexes on a different physical disk. You could use fast, ephemeral storage like you can for a WAL without amplifying the writes to the same device that is your expensive bottleneck. You could rebuild on data loss.

But it would add complexity to detect out-of-sync indexes and tables.



Maybe? I wasn’t under the impression these could be reliably lost or out of sync without risking data loss?

Wasn't aware you could put a WAL on a unreliable storage system either without risking data loss?

Would be interesting for indexes say put them on ram drive and rebuild them on restart if they aren't there just fallback to table scans.

MSSQL has memory optimized tables that do this sort of thing: https://learn.microsoft.com/en-us/sql/relational-databases/i...


If you lose the WAL you lose the data since the last merge but there’s no risk of corruption. The WAL handles missed syncs fine, too, missing losing just that window of data.

I don’t know if or how Postgres records the transaction number in the index to be able to notice if it’s out of date. If it does, I don’t know of any solution to “catch up” the index besides recreating it, which would be ok if that’s the only issue but from my experience with out-of-date indexes (libc or icu updates, where Postgres doesn’t know if anything IS broken and just reports that it could be), there’s no guarantee you’d even notice and your app could be running completely broken until you rebuild.


>If you lose the WAL you lose the data since the last merge but there’s no risk of corruption.

That is not my understanding:

https://www.postgresql.org/docs/current/app-pgresetwal.html

>After running this command on a data directory with corrupted WAL or a corrupted control file, it should be possible to start the server, but bear in mind that the database might contain inconsistent data due to partially-committed transactions. You should immediately dump your data, run initdb, and restore. After restore, check for inconsistencies and repair as needed.


I guess that depends on the definition of corrupted. It just describes what to do when the WAL is corrupt, but doesn’t say what conditions are considered corrupt. (We use ZFS so I haven’t run into torn writes.)

It does support index organized tables with the CLUSTER command, or you meant something else?

CLUSTER command is not the same as index organized tables, it's a one-time "physical sort" operation. New data is not organized until you run CLUSTER again. Index organized tables are maintained automatically by Oracle/SQL Server.

Interesting, i didnt know that. Thanks!

Not just maintained automatically, clustered indexes have no heap at all, the table is an index.

The CLUSTER command in PG just moves rows around in the heap so they match the still separate index order which can help a little bit with range operations because rows are close on disk, but otherwise doesn't do much.

So they are completely separate things that just happen to use the same term.


>Currently, constraint exclusion is enabled by default only for cases that are often used to implement table partitioning via inheritance trees. Turning it on for all tables imposes extra planning overhead that is quite noticeable on simple queries, and most often will yield no benefit for simple queries.

PG's lack of plan caching strikes again, this sort of thing is not a concern in other DB's that reuse query plans.


PG does reuse plans, but only if you prepare a query and run it more than 5 times on that connection. See plan_cache_mode[0] and the PREPARE docs it links to. This works great on simple queries that run all the time.

It sometimes really stinks on some queries since the generic plan can't "see" the parameter values anymore. E.g. if you have an index on (customer_id, item_id) and run a query where `customer_id = $1 AND item_id = ANY($2)` ($2 is an array parameter), the generic query plan doesn't know how many elements are in the array and can decide to do an elaborate plan like a bitmap index scan instead of a nested loop join. I've seen the generic plan flip-flop in a situation like this and have a >100x load difference.

The plan cache is also per-connection, so you still have to plan a query multiple times. This is another reason why consolidating connections in PG is important.

0: https://www.postgresql.org/docs/current/runtime-config-query...


Yes manual query preparation by client [1] is what you did in MSSQL server up until v7.0 I believe, which was 1998 when it started doing automatic caching based on statement text. I believe it also cached stored procedures before v7.0 which is one reason they were recommended for all application code access to the database back then.

MSSQL server also does parameter sniffing now days and can have multiple plans based on the parameters values it also has a hint to guide or disable sniffing because many times a generic plan is actually better, again something else PG doesn't have, HINTS [2].

PG being process based per connection instead of thread based makes it much more difficult to share plans between connections and it also has no plan serialization ability. Where MSSQL can save plans to xml and they can be loaded on other servers and "frozen" to use that plan if desired, they can also be loaded into plan inspection tools that way as well [3].

1. https://learn.microsoft.com/en-us/sql/relational-databases/n...

2. https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-tr...

3. https://learn.microsoft.com/en-us/sql/t-sql/queries/hints-tr...


PostgreSQL shares other caches between processes so they probably could have a global plan cache if they wanted. I wonder why they don’t though.

One possible reason is that the planner configuration can be different per connection, so the plans might not transfer


> PostgreSQL shares other caches between processes so they probably could have a global plan cache if they wanted. I wonder why they don’t though.

> One possible reason is that the planner configuration can be different per connection, so the plans might not transfer

That's part of it, another big part is that the transactional DDL makes it more complicated, as different sessions might require different plans.


In MSSQL Server part of the plan match is the various session/connection options, if they are different there are different plans cached.

I believe the plan data structure PG is intimately tied to process space memory addresses since it was never thought to share between them and can even contain executable code that was generated.

This makes it difficult to share between processes without a heavy redesign but would be a good change IMO.


>South of Spain to the North of Sweden to take 50 hours, more or less the same.

That would be like driving from Key West to Prudhoe Bay which looks to be 91 hours.

Sorry the US is big spread out place, but I also agree it's not really an excuse for what's happening.


> That would be like driving from Key West to Prudhoe Bay which looks to be 91 hours.

Haha, yeah, at least I got a laugh from it, thank you :) A fair comparison then I guess would be from Canary Islands to Svalbard, if we're aiming to make it as far as possible to make some imaginary point no one cares about :)


Well if we're including islands then Hawai'i is pretty far away...

All the money managements apps (Monarch etc) use one of the big three bank gateways, Plaid, MX or Fincity.

They all have issues mainly the banks want constant reauth two factor rather than just trusting forever.

Mint was better they had their own connection supposedly and now that’s gone but wasn’t an open api.

I get the feeling banks don’t want this sort of thing and are fine making it annoying to use


There is 400 amp residential service you can get 80 amp 19.2 kw level 2 chargers.

You would need 5 80 amp charger to approach 100kw but with other loads in a large house, I have seen large HVAC systems and elaborate pools with lazy rivers etc that can add up very quickly which is why they had 400 amp service.

100kw isn't really that much, a modern EV can put out 3 times that from its battery pack into the motor for short bursts and easily sustain 100kw until drained.

480v 200 amp 3 phase commercial supply can provide 100kw continuous and would be some thing used in a medium sized office building.


A watt of power multiplied by a second of time has an agreed upon name called joule, but a watt second is also a perfectly valid SI name.

A watt is a joule of energy divided by a second of time, this is a rate, joule per second is also a valid name similar to nautical mile per hour and knot being the same unit.

Multiplication vs division, quantity vs rate, see the relationship? Units may have different names but are equivalent, both the proper name and compound name are acceptable.

A watt hour is 3600 joules, it’s more convenient to use and matches more closely with how electrical energy is typically consumed. Kilowatt hour is again more directly relatable than 3.6 megajoules.

Newton meter and Coulomb volt are other names for the joule. In pure base units it is a kilogram-meter squared per second squared.


So when I torque all 20 of my car's lug bolts to 120 n-M, I've exerted 2/3 of a W-h? So if it takes me 4 minutes, I'm averaging 10 watts? That's neat. I wonder what the peak wattage (right as the torque wrench clicks) would be; it must depend on angular velocity.


Newton meter as a unit of energy is not the same as the newton meter unit of force for torque.

The energy unit meter is distance moved, while the force unit meter is the length of the moment arm.

This is confusing even though valid, so the energy unit version is rarely used.

You can exert newton meters of force while using no energy, say by standing on a lug nut wrench allowing gravity to exert the force indefinitely unless the nut breaks loose.


Ah! I guess that explains the "f" for "force" in the imperial abbreviation "ft-lbf", to distinguish it from work. I wonder if there's ever been an analogous variant for metric such as "Nmf"...


Hmm, I thought lbf was to distinguish the force unit from the mass unit (1 lbf = G * 1lb mass)


It seems the common thread is that the f means to introduce G, but not exactly. In my own research, the AI summaries are about as sloppy as I've ever seen, due to the vague and often regional differences (with the difference between ft-lb and lb-ft sometimes being described as relevant, as well).


You must still commit the WAL to disk, this is why the WAL exists it writes ahead to the log on durable storage. Its doesn't have to commit the main storage to disk only the WAL which is better since its just an append to end rather than placing correctly in the table storage which is slower.

You must have a single flushed write to disk to be durable, but it doesn't need the second write.


The acid in lead acid is sulfuric acid and if overcharged vents hydrogen gas, thats why they need a ventilated space typically. Sealed lead acid have safety vents that might pop if enough pressure builds.

They are most certainly not inert, they just have well established safety and charging protocols and are not used in very high quantities together because of their low energy density and cycle life.

LFP batteries which have iron phosphate cathodes are very stable compared to colbalt based batteries that tend to have catastrophic failures due to overcharge causing cathode failure. LFP have higher cycle life and are cheaper and typically whats used for storage and application where the loss in erergy density is not a big deal.


Hardly anything unless in a major city, no way to easily tell if there is any coverage other than randomly clicking until it shows, also doesn't tell you the date taken.

Google street view has the 2d overlay letting you know where there is coverage, shows the date taken along with previous imagery, and they have coverage nearly everywhere in the US at a least, although some of its pretty old.

Apple Maps does seem to have more up to date satellite / aerial imagery though.

Hard to overstate how valuable all that street view coverage is on the Google side.


My little Swedish village has full Look Around coverage, and clicking on the ⋯ icon shows an “Imagery” menu item that tells me the month and year the coverage was last updated. I think you’re underestimating where they’re currently at.


In the US is has basically zero coverage outside any major city. Google on the other hand has exentiqive coverage into rural areas, albeit some of it old, at least its there, where it has newer coverage it usually has multiple one at different times allowing one to look back in time as well, very useful.


I just double-checked my village. Every single road and cul-de-sac that I could find, with no exceptions, has full coverage on Apple. Google on the other hand, has coverage for maybe 50-55% of the roads. The worst example is a residential area on the outskirts where they’ve driven the car in, down one side-street, then given up and gone home.

On the other hand, they do have historical coverage, have to give them that.


Yeah so not sure why but Look around coverage is much better in Europe than the US for some reason which seems odd since Apple is US based.

You can see the very poor US coverage here: https://brilliantmaps.com/apple-look-around/

Of course compared to Google Street view there is no comparison on a world wide basis as you can see on the same page.


In areas with partial coverage Apple Maps has basically the same overlay showing where Look Around is available. It just doesn't have a great indicator as to why the option is greyed out when there's no coverage.


I mean in Google Maps you can drag the little man over the map and it has a map layer that highlights all the roads available, so you can easily see where it is and is not. Not randomly picking a point and seeing if indicator is available.


Unless amelius is stronger than you, or has better weapons, or commands a gang that is bigger than your gang, then you can't stop them.

Its almost like you need some sort of power structure with the monopoly on violence to enforce agreed upon freedoms, they could be called the "government" which enforces "laws".


>stronger than you, or has better weapons, or commands a gang that is bigger than your gang, then you can't stop them

How do you not realize you're literally describing government?


How can you not realize that’s the point? Monopoly on violence is just that, the definition of the state.

Anarchy is not a stable system, you have no property rights or freedoms without a way to enforce them.

You provide no alternative, a government will form from a power vacuum made up of whoever has the most physical power around you.


>government will form from a power vacuum made up of whoever has the most physical power around you.

Yup! My issue with the current system is that The Powers That Be pretend to act in the interest of their subjects(or, actually my issue is that people believe it) instead of being a gang of thugs imposing their will.


It can also be companies who put cameras in your home and abuse them.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: