News for hackers. Stuff that matters.
Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @02:56PM (#16081150)
We've found that periodic invocations of pg_repack are necessary to keep things from bloating beyond the inflection point where vacuuming just can't keep up. I'd love to see VACUUM somehow able to take care of that, because the WAL volume of repacking multi-terabyte databases — even when you're just targeting the worst culprit tables — is absurd (especially when you're keeping all your WALs around forever, for PITR purposes). I'm just not sure what more it can do.
Re:The State of Vacuum in Postgres by ioltas on Saturday January 06, 2018 @01:45AM (#16084179)
There has been in the community for a couple of years now a set of patches to support REINDEX CONCURRENTLY, and there have been discussions for having a CLUSTER CONCURRENTLY, but nothing has materialized. Pushing forward into having such patches would make sense. There are many users asking for the possibility to do tuple reordering without holding an exclusive lock on the relations involved.
Re:The State of Vacuum in Postgres by gregw2 on Friday January 05, 2018 @03:38PM (#16081446)
Where do things stand with the status of vacuum in Redshift which derived from Postgres?

What version of postgres vacuum implementation are they derived from? Have they implemented or incorporated any of these improvements since their fork? (Obviously Redshift doesn't need the btree index ones since it uses distribution keys and zone maps.)

I ask because my experience is that Redshift vacuum implementation is awful on large heavily used data warehouses, and I wonder if there is any hope for improvement.

Re:The State of Vacuum in Postgres by scapecast on Friday January 05, 2018 @10:56PM (#16083756)
Yeah, true. Even though Redshift is based on Postgres, it does not leverage the automatic vacuuming facility of postgres.

So the vacuuming of tables to recover disk space from deleted rows needs to be done manually.

There are 3 types of vacuuming in Redshift (DELETE ONLY, SORT, REINDEX). The vacuum SORT operation is done on tables that have a sort key. To the extent that a vacuum SORT is an expensive (high IO) operation, we recommend when possible, to avoid the need to vacuum by loading the rows in sort order. If you do that, you will not need to vacuum the table, and this is the optimal solution for very long tables.

When dealing with a very long table (>10b rows) that has a sort key, it’s useful to partition the table into multiple tables where the table names are modified to indicate the partition. This is useful when pruning the size of the table, you don’t have to run VACUUM DELETE ONLY.

The key is to vacuum on a regular basis, and keep your stats off below 10%. If you wait too long, you might end up in a place where you have to resize your cluster or do a deep copy.

One more thing: Run your vacuum jobs in the queue with the most memory.

Re:The State of Vacuum in Postgres by blaisio on Friday January 05, 2018 @09:29PM (#16083503)
Vacuum in Redshift is doing something entirely different from vacuum in Postgres. You really need to look at the documentation.
Re:The State of Vacuum in Postgres by bigtones on Friday January 05, 2018 @08:56PM (#16083397)
Amazon Redshift is not actually based in Postgres, it just supports a Postgres interface. It is built on top of technology from the massive parallel processing (MPP) data-warehouse company ParAccel which was acquired by Actian, and licenced by Amazon.
Re:The State of Vacuum in Postgres by Ralfp on Friday January 05, 2018 @10:08PM (#16083626)
> Amazon Redshift is based on PostgreSQL 8.0.2

Same wikipedia article you are quoting ;)

https://en.wikipedia.org/wiki/Amazon_Redshift

Re:The State of Vacuum in Postgres by scapecast on Friday January 05, 2018 @10:20PM (#16083655)
I can confirm that Redshift is based on Postgres. And I know that straight from the source: Dave Steinhoff, one of the founders of ParAccel, is on our team.

https://www.intermix.io/about-us/

Re:The State of Vacuum in Postgres by anarazel on Friday January 05, 2018 @09:21PM (#16083474)
Which in turn initially was based on postgres. But there indeed isn't much in common anymore...
Re:The State of Vacuum in Postgres by hinkley on Friday January 05, 2018 @03:48PM (#16081505)
A number of GC and other resource reclamation systems, especially soft real-time ones, have worked on accounting the cleanup costs to the parts of the system that make the mess in the first place.

In these systems, a function that is very careful with resource allocation will almost never experience a significant slowdown due to the background processing, because they either pay no cost or an amortized cost. For instance, one collector I knew of would garbage collect up to 10 objects on every allocation. If large allocations are front loaded then a full cycle almost never happens. In Swift or C++ or Rust you pay for your allocations as soon as they go out of scope.

In the context of an MVCC system, running a long query causes considerably more versioned data to pile up. If part of the cost of retiring that data was payed at the end of the transaction, several things happen. One, other transactions are less impacted by the overhead. Two, you end up throttling the bad actors, which encourages the problem to be fixed, and in the meantime the high water mark for vacuuming is reduced.

Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @03:52PM (#16081525)
Are you legit suggesting tying the cost of GC to transaction commit in a database platform that is very commonly used for OLTP workloads?
Re:The State of Vacuum in Postgres by hinkley on Friday January 05, 2018 @04:05PM (#16081629)
Those are exactly the systems that are falling over already, aren’t they? They’re already tied together whether that’s inconvenient or not. Right now (or at least, recently) the performance simply falls off a cliff instead of degrading.

A lot of the tools we have for dealing with down services also can deal with degraded ones. If for instance throttling votes per second keeps the system usable for everyone else then that has been done before and can be done again. But you have to do it before the database has a seizure, not after.

[edit: I’m talking about back pressure. This is not a new or crazy concept. Your incredulity and $5 will buy me a cup of coffee]

Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @04:12PM (#16081687)
I wouldn't characterize things as "falling over", except perhaps in cases of operator incompetence.

There's certainly room for improvement — The Fine Article wouldn't exist otherwise — and backpressure might be worth exploring as a means of improving things. If that backpressure causes the system to slow down in a way that adversely affects customer or end-user interactions, however, it will often just be turned off, mooting it — or making things worse.

Re:The State of Vacuum in Postgres by hinkley on Friday January 05, 2018 @06:21PM (#16082659)
Like a lot of people, I was barely aware of VACUUMing before several high profile OLTP systems had unplanned outages because Postgres' requests per second dropped off dramatically and suddenly due to this arcane form of resource exhaustion. That's a performance cliff. I didn't mean it is hyperbole but as jargon. System hits a bottleneck, rate-of-change alters drastically/unmanageably.
Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @08:30PM (#16083281)
That sounds like operator error to me. If you (the general "you") use a database in production without knowing what sorts of maintenance it requires, that's neither the database's nor its authors' fault.

I guess you could argue the tool is somehow "flawed" for needing maintenance. I'd counter with the invitation to show me tools that don't.

Re:The State of Vacuum in Postgres by quickben on Friday January 05, 2018 @05:02PM (#16082120)
new and delete are fast.

What's slow are stuff like java GC Eden management where deletes are an afterthought to what is a time-stamped LRU reference management server, and a lot more.

So, no. I don't think you can compare C++ with systems that run full fledged GCs and threat them in the same bullpark.

Re:The State of Vacuum in Postgres by devit on Friday January 05, 2018 @04:44PM (#16081966)
Is it really necessary to have a vacuum procedure?

Isn't it possible to just use a data structure to index rows by transaction ids, and on each transaction commit efficiently find all rows that aren't visible to any transaction, and add them to a free list?

Seems kind of a bad design to rely on periodic full data scans.

Re:The State of Vacuum in Postgres by jandrewrogers on Friday January 05, 2018 @06:11PM (#16082609)
Efficient resource recovery while preserving consistency and minimally impacting workload throughput is one of THE central problems in database engine design. You can move the pain around but it never goes away, and a poor solution will very negatively impact the throughput of normal workloads. People have been thinking about this problem for as long as we've been building database engines.

This is not to say vacuum-like mechanisms are necessarily the best method but it was a common architectural idiom for database engines from the 1990s because it works well with sequential storage devices. Newer techniques tend to put resource recovery inline with the workload rather than outside of it but that has other disadvantages.

Indexing all rows in a database by transaction id would cause an extreme loss of throughput. That just creates a continuously mutated (and therefore locking) structure every thread constantly uses that is also paged to disk. And deletion of individual records is a problem for indexes generally.

Re:The State of Vacuum in Postgres by lobster_johnson on Friday January 05, 2018 @10:28PM (#16083676)
Oracle, with its undo log and some clever in-place tuple modification and space reclamation, seems to have sidestepped the issue to a much greater degree than Postgres. Oracle is MVCC-like, but sacrifices technical elegance for performance.

For example, as I recall, Oracle actually writes new row data to the page the row lives in, overwriting the old data in place on commit. It also writes the modifying transaction ID to the row header. If a different transaction finds the row, it will see that the transaction ID doesn't match (it's newer than itself), which forces the database to go to the undo log to look for the older data.

I'm not privy to the technical details of how the undo log is implemented or how it avoids the vacuum problem. I suspect it's related to the fact that the undo log is separate from the tuple data, so undo bloat doesn't affect table performance to the same extent as with Postgres.

Re:The State of Vacuum in Postgres by Tostino on Saturday January 06, 2018 @01:57PM (#16086829)
That is correct, it also means that things like rollbacks, which are pretty much instant in Postgres can take significant time in Oracle.
Re:The State of Vacuum in Postgres by acdha on Friday January 05, 2018 @05:10PM (#16082191)
In general, when a respected team sticks with a design for a long time it's safe to assume it's not a bad design but that the problem is harder than it might first appear.

In this case, I'd want to think carefully about the data volume: many places may have hundreds of transactions per second but also have queries which run for much longer periods of time and the visible state has to be accurate for all of them. That sounds a lot of contention for that shared data structure and “efficiently find all rows that aren't visible to any transaction” sounds decidedly non-trivial for busy servers with lots of data, which are generally the only ones where this matters.

Re:The State of Vacuum in Postgres by al_james on Friday January 05, 2018 @04:58PM (#16082090)
Sadly it's a bit more complex than that. Because currently running different transactions may hold a view of the database as it was before the transaction commits, the actual view of what does can be reclaimed depends on what transactions are currently active. Thus upon commit, it may be that some tuples release by the committed transaction are still visible to others.

Vacuum actually works by looking for tuples where no running transactions can see them anymore. Postgres, in effect, maintains a minimum and maximum transaction ID that any tuple is visible for, and vacuum scans over all of those that have a max visible transaction ID (suggesting it's available to be reclaimed) and which is less than the minimum active transaction ID.

Re:The State of Vacuum in Postgres by devit on Friday January 05, 2018 @05:40PM (#16082399)
Sure, but the DB knows what transactions are active.

Assuming active transactions {T}_i ordered by id, then if you are committing T_i, any row whose [Tmin, Tmax] visibility interval is fully contained in (T_(i-1), T_(i+1)) (i.e. for which Tmin > T_(i-1) && T_(i+1) < Tmax) is now dead (taking -inf and a large value for the previous/next transaction ids if there are none).

I believe this sort of query can be efficiently handled in time O(k polylog n) with several data structures, like an interval tree such as a B-tree augmented with maximum values or several kinds of 2D search trees.

It's also possible to use a simple index and just reclaim those rows whose Tmax is lower than the oldest active transaction, although this means that a single never-closed transaction blocks all row reclamation forever, which seems a bad design for a production-quality database.

Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @05:50PM (#16082471)
...all of which structures you'd somehow need to synchronize writing to, to keep them accurate and fresh. Global write-locks do not scale, no matter how fast it might be in the toy case.
Re:The State of Vacuum in Postgres by devit on Friday January 05, 2018 @05:56PM (#16082514)
It's just an index on the transaction id fields of the rows, so it should scale like any other index on the table.
Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @06:07PM (#16082581)
So, basically the already-existing Visibility Map?

Indexes in postgres point to the page, not the tuple. The VM keeps two bits per page: "does this page have any 'dead' tuples?", and "does this page need to be touched for xid wraparound?"

Vacuuming doesn't touch a page unless the VM indicates it's necessary.

Re:The State of Vacuum in Postgres by rhaas on Saturday January 06, 2018 @10:33AM (#16085644)
Uh, indexes in postgres point to the tuple, not the page.

Except for BRIN.

Re:The State of Vacuum in Postgres by rosser on Saturday January 06, 2018 @04:38PM (#16087603)
Thanks for the correction, Robert! Now I'm curious where I got that notion.
Re:The State of Vacuum in Postgres by anarazel on Friday January 05, 2018 @06:00PM (#16082551)
> Seems kind of a bad design to rely on periodic full data scans.

Note that it's not full scans - only pages that have been modified since the last vacuum, or were in a state last vacuum that they couldn't be processed, are vacuumed again. So there essentially is a block-level index for this.

https://www.postgresql.org/docs/current/static/storage-vm.ht...

Re:The State of Vacuum in Postgres by grogers on Friday January 05, 2018 @06:55PM (#16082830)
This is essentially what innodb does, it calls this operation "purge" as in purging dead mvcc rows, and it calls the data structure the history list (IIRC). I don't know what the exact data structure is though to find purgable rows and how it avoids contention. The main difference is that innodb runs purge in background thread(s) as opposed to blocking foreground operations.

It has potential pitfalls too though. On a system with very high mutation rate, purging could fall behind. This mainly would happen if the history list overflows available buffer pool space and starts getting paged to disk (e.g. from one very long lived transection). I don't know if newer MySQL versions make purge running off disk more efficient, but 5.5 added a config for multiple purge threads.

Re:The State of Vacuum in Postgres by ketralnis on Friday January 05, 2018 @06:18PM (#16082646)
> Seems kind of a bad design

Many things can seem this way from the armchair, but you can lend some benefit of the doubt. Postgres is worked on by very smart people and it's not the only system that works this way

Everything has tradeoffs.

Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @05:00PM (#16082102)
Wouldn't a write lock on that structure become a hotly contended resource? Globally shared, globally writable structures tend not to scale very well.

Postgres already has the notion of creating and destroying transaction ids for each row version attached to that tuple (row version). That distributes your "structure" across the rows involved in it. Just because you delete a row version (whether by deleting that row, or by updating it — which in MVCC is synchronously deleting the old version and inserting a new one) doesn't mean that row version isn't still visible to other concurrent activity, specifically including read-only activity. You'd therefore have to write to this proposed structure on completing every read or write.

Re:The State of Vacuum in Postgres by porker on Friday January 05, 2018 @05:23PM (#16082287)
How do other databases such as MS-SQL handle this?
Re:The State of Vacuum in Postgres by anarazel on Friday January 05, 2018 @06:06PM (#16082578)
Largely through undo logging.
Re:The State of Vacuum in Postgres by antoaravinth on Saturday January 06, 2018 @10:08AM (#16085553)
Well this might not answer your question directly, but it shows how VACCUM solves problem such as transaction etc. Its a great video to watch, covers topic in depth : https://www.youtube.com/watch?v=ZxhBkBNxvR0
Re:The State of Vacuum in Postgres by hinkley on Friday January 05, 2018 @03:58PM (#16081581)
Why isn’t the rate of vacuuming in Postgres determined by the rate of churn instead of things like wall clock time?

These weights and heuristics are either going to punish small databases of sabotage large ones, unless the factor in a predictor or how much there is to vacuum.

Re:The State of Vacuum in Postgres by anarazel on Friday January 05, 2018 @06:04PM (#16082571)
> Why isn’t the rate of vacuuming in Postgres determined by the rate of churn instead of things like wall clock time?

It is determined by the rate of churn.

Re:The State of Vacuum in Postgres by firasd on Friday January 05, 2018 @02:56PM (#16081148)
Related: Why Uber Engineering Switched from Postgres to MySQL https://eng.uber.com/mysql-migration/

Edit: This is above my technical paygrade so I'll let others be the judge of whether these are in fact related! I believed so, from my earlier readings about the issue. Here's an article from the same blog in the OP about the Uber migration: http://rhaas.blogspot.in/2016/08/ubers-move-away-from-postgr...

Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @02:57PM (#16081153)
Vacuuming is not why Uber flounced off PostgreSQL. They were naïvely using it as a KV store, and indexing every column, which caused a write-amplification cascade.

Edit, re: the parent's edit: Quoting that article, "Perhaps the thorniest problem which Uber raises is that of write amplification caused by secondary index updates." The word "vacuum" is mentioned exactly once, in the context of keeping up with index insertions (writes).

Re:The State of Vacuum in Postgres by warent on Friday January 05, 2018 @03:16PM (#16081293)
Lol. If that's true (and I'm not disputing it) then how would switching to MySQL fix that problem?
Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @03:19PM (#16081316)
Writes in postgres touch all indexes. When you index every column, you have to touch every column's index for every write.

Writes in MySQL (using InnoDB) just touch the pkey index, to which all other indexes point, or something like that. (Can someone who knows MySQL innards better — I avoid it like it's contagious — please confirm or clarify?)

Re:The State of Vacuum in Postgres by blaisio on Friday January 05, 2018 @09:32PM (#16083513)
That's correct.
Re:The State of Vacuum in Postgres by jtc331 on Friday January 05, 2018 @07:29PM (#16083008)
The other thing here they downplay in the article is that they didn’t switch to MySQL per se; they implemented a columnar storage backend within MySQL.

It’d have been more accurate to say they abandoned traditional relational databases entirely.

Re:The State of Vacuum in Postgres by dragonwriter on Friday January 05, 2018 @04:57PM (#16082080)
Postgres’s MVCC implementation with immutable tuples means every write to a table affecting any index touches every index on that table. Excessive indexing will have a performance hit in any DB, but combined with Postgres’s architecture it’s particularly bad.
Re:The State of Vacuum in Postgres by anarazel on Friday January 05, 2018 @06:04PM (#16082566)
> Postgres’s MVCC implementation with immutable tuples means every write (even updates to a column not in any indexes) to a table touches every index on that table.

The parenthetical comment is not true. If no indexed columns are updated, and there's space on the same page, a so called "HOT" update is performed. Basically a redirection is inserted triggering index scans to follow the update chain to the newest version:

https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f...

Re:The State of Vacuum in Postgres by dragonwriter on Friday January 05, 2018 @06:12PM (#16082612)
Edited the comment to correct the error in my original understanding; thanks for pointing it out!
Re:The State of Vacuum in Postgres by noncoml on Friday January 05, 2018 @03:19PM (#16081313)
> Lol. If that's true (and I'm not disputing it) then how would switching to MySQL fix that problem?

It's explained in the blog the great-grandparent linked:

https://eng.uber.com/mysql-migration/

Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @03:21PM (#16081330)
Uber's explanation of the problem neglects to consider their poor usage of the tool as contributory to the problem.
Re:The State of Vacuum in Postgres by dozzie on Friday January 05, 2018 @03:27PM (#16081377)
That's why there were so many rebuttals to their arguments.
Re:The State of Vacuum in Postgres by Thaxll on Friday January 05, 2018 @07:18PM (#16082938)
Even Postgres dev ack the problem they had, don't try to dismiss the issue it's real.
Re:The State of Vacuum in Postgres by rosser on Friday January 05, 2018 @08:25PM (#16083253)
Can you point out where I dismissed anything?

PostgreSQL has a write-amplification issue — especially when you index every column on a table. My point is: if you don't do that, it doesn't hurt.

Re:The State of Vacuum in Postgres by jtc331 on Friday January 05, 2018 @07:30PM (#16083012)
There is a real issue, yes, but it’s heavily lessened by using sane scheme design, while Uber basically used the worst possible design.