Why VACUUM?
© xkcd.xom (Randall Munroe) under the Creative Commons Attribution-NonCommercial 2.5 License
Whenever rows in a PostgreSQL table are updated or deleted, dead rows are left behind. VACUUM gets rid of them so that the space can be reused. If a table doesn’t get vacuumed, it will get bloated, which wastes disk space and slows down sequential table scans (and – to a smaller extent – index scans).
VACUUM also takes care of freezing table rows so to avoid problems when the transaction ID counter wraps around, but that’s a different story.
Normally you don’t have to take care of all that, because the autovacuum daemon built into PostgreSQL does that for you.
The problem
If your tables get bloated, the first thing you check is whether autovacuum has processed them or not:
| 1 2 3 4 5 6 7 8 |
|
If your bloated table does not show up here, n_dead_tup is zero and last_autovacuum is NULL, you might have a problem with the statistics collector.
If the bloated table is right there on top, but last_autovacuum is NULL, you might need to configure autovacuum to be more aggressive so that it gets done with the table.
But sometimes the result will look like this:
|
|
Here autovacuum has recently run, but it didn’t free the dead tuples!
We can verify the problem by running VACUUM (VERBOSE):
|
|
Why won’t VACUUM remove the dead rows?
VACUUM can only remove those row versions (also known as “tuples”) that are not needed any more. A tuple is not needed if the transaction ID of the deleting transaction (as stored in the xmax system column) is older than the oldest transaction still active in the PostgreSQL database (or the whole cluster for shared tables).
This value (22300 in the VACUUM output above) is called the “xmin horizon”.
There are three things that can hold back this xmin horizon in a PostgreSQL cluster:
-
Long-running transactions:
You can find those and their
xminvalue with the following query:1
2
3
4
SELECTpid, datname, usename, state, backend_xminFROMpg_stat_activityWHEREbackend_xminISNOTNULLORDERBYage(backend_xmin)DESC;You can use the
pg_terminate_backend()function to terminate the database session that is blocking yourVACUUM. -
Abandoned replication slots:
A replication slot is a data structure that keeps the PostgreSQL server from discarding information that is still needed by a standby server to catch up with the primary.
If replication is delayed or the standby server is down, the replication slot will prevent
VACUUMfrom deleting old rows.You can find all replication slots and their
xminvalue with this query:1
2
3
SELECTslot_name, slot_type,database, xminFROMpg_replication_slotsORDERBYage(xmin)DESC;Use the
pg_drop_replication_slot()function to drop replication slots that are no longer needed.Note: This can only happen with physical replication if
hot_standby_feedback = on. For logical replication there is a similar hazard, but only system catalogs are affected. Examine the columncatalog_xminin that case. -
Orphaned prepared transactions:
During two-phase commit, a distributed transaction is first prepared with the
PREPAREstatement and then committed with theCOMMIT PREPAREDstatement.Once a transaction has been prepared, it is kept “hanging around” until it is committed or aborted. It even has to survive a server restart! Normally, transactions don’t remain in the prepared state for long, but sometimes things go wrong and a prepared transaction has to be removed manually by an administrator.
You can find all prepared transactions and their
xminvalue with the following query:1
2
3
SELECTgid, prepared, owner,database,transactionASxminFROMpg_prepared_xactsORDERBYage(transaction)DESC;Use the
ROLLBACK PREPAREDSQL statement to remove prepared transactions.

564

被折叠的 条评论
为什么被折叠?



