1 /*-------------------------------------------------------------------------
4 * The postgres vacuum cleaner.
6 * This file now includes only control and dispatch code for VACUUM and
7 * ANALYZE commands. Regular VACUUM is implemented in vacuumlazy.c,
8 * ANALYZE in analyze.c, and VACUUM FULL is a variant of CLUSTER, handled
12 * Portions Copyright (c) 1996-2011, PostgreSQL Global Development Group
13 * Portions Copyright (c) 1994, Regents of the University of California
17 * src/backend/commands/vacuum.c
19 *-------------------------------------------------------------------------
23 #include "access/clog.h"
24 #include "access/genam.h"
25 #include "access/heapam.h"
26 #include "access/transam.h"
27 #include "access/xact.h"
28 #include "catalog/namespace.h"
29 #include "catalog/pg_database.h"
30 #include "catalog/pg_namespace.h"
31 #include "commands/cluster.h"
32 #include "commands/vacuum.h"
33 #include "miscadmin.h"
35 #include "postmaster/autovacuum.h"
36 #include "storage/bufmgr.h"
37 #include "storage/lmgr.h"
38 #include "storage/proc.h"
39 #include "storage/procarray.h"
40 #include "utils/acl.h"
41 #include "utils/fmgroids.h"
42 #include "utils/guc.h"
43 #include "utils/memutils.h"
44 #include "utils/snapmgr.h"
45 #include "utils/syscache.h"
46 #include "utils/tqual.h"
52 int vacuum_freeze_min_age;
53 int vacuum_freeze_table_age;
56 /* A few variables that don't seem worth passing around as parameters */
57 static MemoryContext vac_context = NULL;
58 static BufferAccessStrategy vac_strategy;
61 /* non-export function prototypes */
62 static List *get_rel_oids(Oid relid, const RangeVar *vacrel);
63 static void vac_truncate_clog(TransactionId frozenXID);
64 static void vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast,
65 bool for_wraparound, bool *scanned_all);
69 * Primary entry point for VACUUM and ANALYZE commands.
71 * relid is normally InvalidOid; if it is not, then it provides the relation
72 * OID to be processed, and vacstmt->relation is ignored. (The non-invalid
73 * case is currently only used by autovacuum.)
75 * do_toast is passed as FALSE by autovacuum, because it processes TOAST
78 * for_wraparound is used by autovacuum to let us know when it's forcing
79 * a vacuum for wraparound, which should not be auto-cancelled.
81 * bstrategy is normally given as NULL, but in autovacuum it can be passed
82 * in to use the same buffer strategy object across multiple vacuum() calls.
84 * isTopLevel should be passed down from ProcessUtility.
86 * It is the caller's responsibility that vacstmt and bstrategy
87 * (if given) be allocated in a memory context that won't disappear
88 * at transaction commit.
91 vacuum(VacuumStmt *vacstmt, Oid relid, bool do_toast,
92 BufferAccessStrategy bstrategy, bool for_wraparound, bool isTopLevel)
95 volatile bool all_rels,
100 /* sanity checks on options */
101 Assert(vacstmt->options & (VACOPT_VACUUM | VACOPT_ANALYZE));
102 Assert((vacstmt->options & VACOPT_VACUUM) ||
103 !(vacstmt->options & (VACOPT_FULL | VACOPT_FREEZE)));
104 Assert((vacstmt->options & VACOPT_ANALYZE) || vacstmt->va_cols == NIL);
106 stmttype = (vacstmt->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
109 * We cannot run VACUUM inside a user transaction block; if we were inside
110 * a transaction, then our commit- and start-transaction-command calls
111 * would not have the intended effect! There are numerous other subtle
112 * dependencies on this, too.
114 * ANALYZE (without VACUUM) can run either way.
116 if (vacstmt->options & VACOPT_VACUUM)
118 PreventTransactionChain(isTopLevel, stmttype);
119 in_outer_xact = false;
122 in_outer_xact = IsInTransactionChain(isTopLevel);
125 * Send info about dead objects to the statistics collector, unless we are
126 * in autovacuum --- autovacuum.c does this for itself.
128 if ((vacstmt->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
129 pgstat_vacuum_stat();
132 * Create special memory context for cross-transaction storage.
134 * Since it is a child of PortalContext, it will go away eventually even
135 * if we suffer an error; there's no need for special abort cleanup logic.
137 vac_context = AllocSetContextCreate(PortalContext,
139 ALLOCSET_DEFAULT_MINSIZE,
140 ALLOCSET_DEFAULT_INITSIZE,
141 ALLOCSET_DEFAULT_MAXSIZE);
144 * If caller didn't give us a buffer strategy object, make one in the
145 * cross-transaction memory context.
147 if (bstrategy == NULL)
149 MemoryContext old_context = MemoryContextSwitchTo(vac_context);
151 bstrategy = GetAccessStrategy(BAS_VACUUM);
152 MemoryContextSwitchTo(old_context);
154 vac_strategy = bstrategy;
156 /* Remember whether we are processing everything in the DB */
157 all_rels = (!OidIsValid(relid) && vacstmt->relation == NULL);
160 * Build list of relations to process, unless caller gave us one. (If we
161 * build one, we put it in vac_context for safekeeping.)
163 relations = get_rel_oids(relid, vacstmt->relation);
166 * Decide whether we need to start/commit our own transactions.
168 * For VACUUM (with or without ANALYZE): always do so, so that we can
169 * release locks as soon as possible. (We could possibly use the outer
170 * transaction for a one-table VACUUM, but handling TOAST tables would be
173 * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
174 * start/commit our own transactions. Also, there's no need to do so if
175 * only processing one relation. For multiple relations when not within a
176 * transaction block, and also in an autovacuum worker, use own
177 * transactions so we can release locks sooner.
179 if (vacstmt->options & VACOPT_VACUUM)
180 use_own_xacts = true;
183 Assert(vacstmt->options & VACOPT_ANALYZE);
184 if (IsAutoVacuumWorkerProcess())
185 use_own_xacts = true;
186 else if (in_outer_xact)
187 use_own_xacts = false;
188 else if (list_length(relations) > 1)
189 use_own_xacts = true;
191 use_own_xacts = false;
195 * vacuum_rel expects to be entered with no transaction active; it will
196 * start and commit its own transaction. But we are called by an SQL
197 * command, and so we are executing inside a transaction already. We
198 * commit the transaction started in PostgresMain() here, and start
199 * another one before exiting to match the commit waiting for us back in
204 /* ActiveSnapshot is not set by autovacuum */
205 if (ActiveSnapshotSet())
208 /* matches the StartTransaction in PostgresMain() */
209 CommitTransactionCommand();
212 /* Turn vacuum cost accounting on or off */
217 VacuumCostActive = (VacuumCostDelay > 0);
218 VacuumCostBalance = 0;
221 * Loop to process each selected relation.
223 foreach(cur, relations)
225 Oid relid = lfirst_oid(cur);
226 bool scanned_all = false;
228 if (vacstmt->options & VACOPT_VACUUM)
229 vacuum_rel(relid, vacstmt, do_toast, for_wraparound,
232 if (vacstmt->options & VACOPT_ANALYZE)
235 * If using separate xacts, start one for analyze. Otherwise,
236 * we can use the outer transaction.
240 StartTransactionCommand();
241 /* functions in indexes may want a snapshot set */
242 PushActiveSnapshot(GetTransactionSnapshot());
245 analyze_rel(relid, vacstmt, vac_strategy, !scanned_all);
250 CommitTransactionCommand();
257 /* Make sure cost accounting is turned off after error */
258 VacuumCostActive = false;
263 /* Turn off vacuum cost accounting */
264 VacuumCostActive = false;
267 * Finish up processing.
271 /* here, we are not in a transaction */
274 * This matches the CommitTransaction waiting for us in
277 StartTransactionCommand();
280 if ((vacstmt->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
283 * Update pg_database.datfrozenxid, and truncate pg_clog if possible.
284 * (autovacuum.c does this for itself.)
286 vac_update_datfrozenxid();
290 * Clean up working storage --- note we must do this after
291 * StartTransactionCommand, else we might be trying to delete the active
294 MemoryContextDelete(vac_context);
299 * Build a list of Oids for each relation to be processed
301 * The list is built in vac_context so that it will survive across our
302 * per-relation transactions.
305 get_rel_oids(Oid relid, const RangeVar *vacrel)
307 List *oid_list = NIL;
308 MemoryContext oldcontext;
310 /* OID supplied by VACUUM's caller? */
311 if (OidIsValid(relid))
313 oldcontext = MemoryContextSwitchTo(vac_context);
314 oid_list = lappend_oid(oid_list, relid);
315 MemoryContextSwitchTo(oldcontext);
319 /* Process a specific relation */
322 relid = RangeVarGetRelid(vacrel, false);
324 /* Make a relation list entry for this guy */
325 oldcontext = MemoryContextSwitchTo(vac_context);
326 oid_list = lappend_oid(oid_list, relid);
327 MemoryContextSwitchTo(oldcontext);
331 /* Process all plain relations listed in pg_class */
338 Anum_pg_class_relkind,
339 BTEqualStrategyNumber, F_CHAREQ,
340 CharGetDatum(RELKIND_RELATION));
342 pgclass = heap_open(RelationRelationId, AccessShareLock);
344 scan = heap_beginscan(pgclass, SnapshotNow, 1, &key);
346 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
348 /* Make a relation list entry for this guy */
349 oldcontext = MemoryContextSwitchTo(vac_context);
350 oid_list = lappend_oid(oid_list, HeapTupleGetOid(tuple));
351 MemoryContextSwitchTo(oldcontext);
355 heap_close(pgclass, AccessShareLock);
362 * vacuum_set_xid_limits() -- compute oldest-Xmin and freeze cutoff points
365 vacuum_set_xid_limits(int freeze_min_age,
366 int freeze_table_age,
368 TransactionId *oldestXmin,
369 TransactionId *freezeLimit,
370 TransactionId *freezeTableLimit)
374 TransactionId safeLimit;
377 * We can always ignore processes running lazy vacuum. This is because we
378 * use these values only for deciding which tuples we must keep in the
379 * tables. Since lazy vacuum doesn't write its XID anywhere, it's safe to
380 * ignore it. In theory it could be problematic to ignore lazy vacuums in
381 * a full vacuum, but keep in mind that only one vacuum process can be
382 * working on a particular table at any time, and that each vacuum is
383 * always an independent transaction.
385 *oldestXmin = GetOldestXmin(sharedRel, true);
387 Assert(TransactionIdIsNormal(*oldestXmin));
390 * Determine the minimum freeze age to use: as specified by the caller, or
391 * vacuum_freeze_min_age, but in any case not more than half
392 * autovacuum_freeze_max_age, so that autovacuums to prevent XID
393 * wraparound won't occur too frequently.
395 freezemin = freeze_min_age;
397 freezemin = vacuum_freeze_min_age;
398 freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);
399 Assert(freezemin >= 0);
402 * Compute the cutoff XID, being careful not to generate a "permanent" XID
404 limit = *oldestXmin - freezemin;
405 if (!TransactionIdIsNormal(limit))
406 limit = FirstNormalTransactionId;
409 * If oldestXmin is very far back (in practice, more than
410 * autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum
411 * freeze age of zero.
413 safeLimit = ReadNewTransactionId() - autovacuum_freeze_max_age;
414 if (!TransactionIdIsNormal(safeLimit))
415 safeLimit = FirstNormalTransactionId;
417 if (TransactionIdPrecedes(limit, safeLimit))
420 (errmsg("oldest xmin is far in the past"),
421 errhint("Close open transactions soon to avoid wraparound problems.")));
425 *freezeLimit = limit;
427 if (freezeTableLimit != NULL)
432 * Determine the table freeze age to use: as specified by the caller,
433 * or vacuum_freeze_table_age, but in any case not more than
434 * autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
435 * VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples
436 * before anti-wraparound autovacuum is launched.
438 freezetable = freeze_min_age;
440 freezetable = vacuum_freeze_table_age;
441 freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);
442 Assert(freezetable >= 0);
445 * Compute the cutoff XID, being careful not to generate a "permanent"
448 limit = ReadNewTransactionId() - freezetable;
449 if (!TransactionIdIsNormal(limit))
450 limit = FirstNormalTransactionId;
452 *freezeTableLimit = limit;
458 * vac_update_relstats() -- update statistics for one relation
460 * Update the whole-relation statistics that are kept in its pg_class
461 * row. There are additional stats that will be updated if we are
462 * doing ANALYZE, but we always update these stats. This routine works
463 * for both index and heap relation entries in pg_class.
465 * We violate transaction semantics here by overwriting the rel's
466 * existing pg_class tuple with the new values. This is reasonably
467 * safe since the new values are correct whether or not this transaction
468 * commits. The reason for this is that if we updated these tuples in
469 * the usual way, vacuuming pg_class itself wouldn't work very well ---
470 * by the time we got done with a vacuum cycle, most of the tuples in
471 * pg_class would've been obsoleted. Of course, this only works for
472 * fixed-size never-null columns, but these are.
474 * Note another assumption: that two VACUUMs/ANALYZEs on a table can't
475 * run in parallel, nor can VACUUM/ANALYZE run in parallel with a
476 * schema alteration such as adding an index, rule, or trigger. Otherwise
477 * our updates of relhasindex etc might overwrite uncommitted updates.
479 * Another reason for doing it this way is that when we are in a lazy
480 * VACUUM and have PROC_IN_VACUUM set, we mustn't do any updates ---
481 * somebody vacuuming pg_class might think they could delete a tuple
482 * marked with xmin = our xid.
484 * This routine is shared by VACUUM and stand-alone ANALYZE.
487 vac_update_relstats(Relation relation,
488 BlockNumber num_pages, double num_tuples,
489 bool hasindex, TransactionId frozenxid)
491 Oid relid = RelationGetRelid(relation);
494 Form_pg_class pgcform;
497 rd = heap_open(RelationRelationId, RowExclusiveLock);
499 /* Fetch a copy of the tuple to scribble on */
500 ctup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
501 if (!HeapTupleIsValid(ctup))
502 elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
504 pgcform = (Form_pg_class) GETSTRUCT(ctup);
506 /* Apply required updates, if any, to copied tuple */
509 if (pgcform->relpages != (int32) num_pages)
511 pgcform->relpages = (int32) num_pages;
514 if (pgcform->reltuples != (float4) num_tuples)
516 pgcform->reltuples = (float4) num_tuples;
519 if (pgcform->relhasindex != hasindex)
521 pgcform->relhasindex = hasindex;
526 * If we have discovered that there are no indexes, then there's no
527 * primary key either, nor any exclusion constraints. This could be done
532 if (pgcform->relhaspkey)
534 pgcform->relhaspkey = false;
537 if (pgcform->relhasexclusion && pgcform->relkind != RELKIND_INDEX)
539 pgcform->relhasexclusion = false;
544 /* We also clear relhasrules and relhastriggers if needed */
545 if (pgcform->relhasrules && relation->rd_rules == NULL)
547 pgcform->relhasrules = false;
550 if (pgcform->relhastriggers && relation->trigdesc == NULL)
552 pgcform->relhastriggers = false;
557 * relfrozenxid should never go backward. Caller can pass
558 * InvalidTransactionId if it has no new data.
560 if (TransactionIdIsNormal(frozenxid) &&
561 TransactionIdPrecedes(pgcform->relfrozenxid, frozenxid))
563 pgcform->relfrozenxid = frozenxid;
567 /* If anything changed, write out the tuple. */
569 heap_inplace_update(rd, ctup);
571 heap_close(rd, RowExclusiveLock);
576 * vac_update_datfrozenxid() -- update pg_database.datfrozenxid for our DB
578 * Update pg_database's datfrozenxid entry for our database to be the
579 * minimum of the pg_class.relfrozenxid values. If we are able to
580 * advance pg_database.datfrozenxid, also try to truncate pg_clog.
582 * We violate transaction semantics here by overwriting the database's
583 * existing pg_database tuple with the new value. This is reasonably
584 * safe since the new value is correct whether or not this transaction
585 * commits. As with vac_update_relstats, this avoids leaving dead tuples
586 * behind after a VACUUM.
589 vac_update_datfrozenxid(void)
592 Form_pg_database dbform;
596 TransactionId newFrozenXid;
600 * Initialize the "min" calculation with GetOldestXmin, which is a
601 * reasonable approximation to the minimum relfrozenxid for not-yet-
602 * committed pg_class entries for new tables; see AddNewRelationTuple().
603 * Se we cannot produce a wrong minimum by starting with this.
605 newFrozenXid = GetOldestXmin(true, true);
608 * We must seqscan pg_class to find the minimum Xid, because there is no
609 * index that can help us here.
611 relation = heap_open(RelationRelationId, AccessShareLock);
613 scan = systable_beginscan(relation, InvalidOid, false,
614 SnapshotNow, 0, NULL);
616 while ((classTup = systable_getnext(scan)) != NULL)
618 Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
621 * Only consider heap and TOAST tables (anything else should have
622 * InvalidTransactionId in relfrozenxid anyway.)
624 if (classForm->relkind != RELKIND_RELATION &&
625 classForm->relkind != RELKIND_TOASTVALUE)
628 Assert(TransactionIdIsNormal(classForm->relfrozenxid));
630 if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
631 newFrozenXid = classForm->relfrozenxid;
634 /* we're done with pg_class */
635 systable_endscan(scan);
636 heap_close(relation, AccessShareLock);
638 Assert(TransactionIdIsNormal(newFrozenXid));
640 /* Now fetch the pg_database tuple we need to update. */
641 relation = heap_open(DatabaseRelationId, RowExclusiveLock);
643 /* Fetch a copy of the tuple to scribble on */
644 tuple = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
645 if (!HeapTupleIsValid(tuple))
646 elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
647 dbform = (Form_pg_database) GETSTRUCT(tuple);
650 * Don't allow datfrozenxid to go backward (probably can't happen anyway);
651 * and detect the common case where it doesn't go forward either.
653 if (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid))
655 dbform->datfrozenxid = newFrozenXid;
660 heap_inplace_update(relation, tuple);
662 heap_freetuple(tuple);
663 heap_close(relation, RowExclusiveLock);
666 * If we were able to advance datfrozenxid, see if we can truncate
667 * pg_clog. Also do it if the shared XID-wrap-limit info is stale, since
668 * this action will update that too.
670 if (dirty || ForceTransactionIdLimitUpdate())
671 vac_truncate_clog(newFrozenXid);
676 * vac_truncate_clog() -- attempt to truncate the commit log
678 * Scan pg_database to determine the system-wide oldest datfrozenxid,
679 * and use it to truncate the transaction commit log (pg_clog).
680 * Also update the XID wrap limit info maintained by varsup.c.
682 * The passed XID is simply the one I just wrote into my pg_database
683 * entry. It's used to initialize the "min" calculation.
685 * This routine is only invoked when we've managed to change our
686 * DB's datfrozenxid entry, or we found that the shared XID-wrap-limit
690 vac_truncate_clog(TransactionId frozenXID)
692 TransactionId myXID = GetCurrentTransactionId();
697 bool frozenAlreadyWrapped = false;
699 /* init oldest_datoid to sync with my frozenXID */
700 oldest_datoid = MyDatabaseId;
703 * Scan pg_database to compute the minimum datfrozenxid
705 * Note: we need not worry about a race condition with new entries being
706 * inserted by CREATE DATABASE. Any such entry will have a copy of some
707 * existing DB's datfrozenxid, and that source DB cannot be ours because
708 * of the interlock against copying a DB containing an active backend.
709 * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
710 * concurrently modify the datfrozenxid's of different databases, the
711 * worst possible outcome is that pg_clog is not truncated as aggressively
714 relation = heap_open(DatabaseRelationId, AccessShareLock);
716 scan = heap_beginscan(relation, SnapshotNow, 0, NULL);
718 while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
720 Form_pg_database dbform = (Form_pg_database) GETSTRUCT(tuple);
722 Assert(TransactionIdIsNormal(dbform->datfrozenxid));
724 if (TransactionIdPrecedes(myXID, dbform->datfrozenxid))
725 frozenAlreadyWrapped = true;
726 else if (TransactionIdPrecedes(dbform->datfrozenxid, frozenXID))
728 frozenXID = dbform->datfrozenxid;
729 oldest_datoid = HeapTupleGetOid(tuple);
735 heap_close(relation, AccessShareLock);
738 * Do not truncate CLOG if we seem to have suffered wraparound already;
739 * the computed minimum XID might be bogus. This case should now be
740 * impossible due to the defenses in GetNewTransactionId, but we keep the
743 if (frozenAlreadyWrapped)
746 (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
747 errdetail("You might have already suffered transaction-wraparound data loss.")));
751 /* Truncate CLOG to the oldest frozenxid */
752 TruncateCLOG(frozenXID);
755 * Update the wrap limit for GetNewTransactionId. Note: this function
756 * will also signal the postmaster for an(other) autovac cycle if needed.
758 SetTransactionIdLimit(frozenXID, oldest_datoid);
763 * vacuum_rel() -- vacuum one heap relation
765 * Doing one heap at a time incurs extra overhead, since we need to
766 * check that the heap exists again just before we vacuum it. The
767 * reason that we do this is so that vacuuming can be spread across
768 * many small transactions. Otherwise, two-phase locking would require
769 * us to lock the entire database during one pass of the vacuum cleaner.
771 * We'll return true in *scanned_all if the vacuum scanned all heap
772 * pages, and updated pg_class.
774 * At entry and exit, we are not inside a transaction.
777 vacuum_rel(Oid relid, VacuumStmt *vacstmt, bool do_toast, bool for_wraparound,
785 int save_sec_context;
789 *scanned_all = false;
791 /* Begin a transaction for vacuuming this relation */
792 StartTransactionCommand();
795 * Functions in indexes may want a snapshot set. Also, setting a snapshot
796 * ensures that RecentGlobalXmin is kept truly recent.
798 PushActiveSnapshot(GetTransactionSnapshot());
800 if (!(vacstmt->options & VACOPT_FULL))
803 * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
804 * other concurrent VACUUMs know that they can ignore this one while
805 * determining their OldestXmin. (The reason we don't set it during a
806 * full VACUUM is exactly that we may have to run user-defined
807 * functions for functional indexes, and we want to make sure that if
808 * they use the snapshot set above, any tuples it requires can't get
809 * removed from other tables. An index function that depends on the
810 * contents of other tables is arguably broken, but we won't break it
811 * here by violating transaction semantics.)
813 * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
814 * autovacuum; it's used to avoid cancelling a vacuum that was invoked
817 * Note: these flags remain set until CommitTransaction or
818 * AbortTransaction. We don't want to clear them until we reset
819 * MyProc->xid/xmin, else OldestXmin might appear to go backwards,
820 * which is probably Not Good.
822 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
823 MyProc->vacuumFlags |= PROC_IN_VACUUM;
825 MyProc->vacuumFlags |= PROC_VACUUM_FOR_WRAPAROUND;
826 LWLockRelease(ProcArrayLock);
830 * Check for user-requested abort. Note we want this to be inside a
831 * transaction, so xact.c doesn't issue useless WARNING.
833 CHECK_FOR_INTERRUPTS();
836 * Determine the type of lock we want --- hard exclusive lock for a FULL
837 * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
838 * way, we can be sure that no other backend is vacuuming the same table.
840 lmode = (vacstmt->options & VACOPT_FULL) ? AccessExclusiveLock : ShareUpdateExclusiveLock;
843 * Open the relation and get the appropriate lock on it.
845 * There's a race condition here: the rel may have gone away since the
846 * last time we saw it. If so, we don't need to vacuum it.
848 onerel = try_relation_open(relid, lmode);
853 CommitTransactionCommand();
860 * We allow the user to vacuum a table if he is superuser, the table
861 * owner, or the database owner (but in the latter case, only if it's not
862 * a shared relation). pg_class_ownercheck includes the superuser case.
864 * Note we choose to treat permissions failure as a WARNING and keep
865 * trying to vacuum the rest of the DB --- is this appropriate?
867 if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
868 (pg_database_ownercheck(MyDatabaseId, GetUserId()) && !onerel->rd_rel->relisshared)))
870 if (onerel->rd_rel->relisshared)
872 (errmsg("skipping \"%s\" --- only superuser can vacuum it",
873 RelationGetRelationName(onerel))));
874 else if (onerel->rd_rel->relnamespace == PG_CATALOG_NAMESPACE)
876 (errmsg("skipping \"%s\" --- only superuser or database owner can vacuum it",
877 RelationGetRelationName(onerel))));
880 (errmsg("skipping \"%s\" --- only table or database owner can vacuum it",
881 RelationGetRelationName(onerel))));
882 relation_close(onerel, lmode);
884 CommitTransactionCommand();
889 * Check that it's a vacuumable table; we used to do this in
890 * get_rel_oids() but seems safer to check after we've locked the
893 if (onerel->rd_rel->relkind != RELKIND_RELATION &&
894 onerel->rd_rel->relkind != RELKIND_TOASTVALUE)
897 (errmsg("skipping \"%s\" --- cannot vacuum indexes, views, or special system tables",
898 RelationGetRelationName(onerel))));
899 relation_close(onerel, lmode);
901 CommitTransactionCommand();
906 * Silently ignore tables that are temp tables of other backends ---
907 * trying to vacuum these will lead to great unhappiness, since their
908 * contents are probably not up-to-date on disk. (We don't throw a
909 * warning here; it would just lead to chatter during a database-wide
912 if (RELATION_IS_OTHER_TEMP(onerel))
914 relation_close(onerel, lmode);
916 CommitTransactionCommand();
921 * Get a session-level lock too. This will protect our access to the
922 * relation across multiple transactions, so that we can vacuum the
923 * relation's TOAST table (if any) secure in the knowledge that no one is
924 * deleting the parent relation.
926 * NOTE: this cannot block, even if someone else is waiting for access,
927 * because the lock manager knows that both lock requests are from the
930 onerelid = onerel->rd_lockInfo.lockRelId;
931 LockRelationIdForSession(&onerelid, lmode);
934 * Remember the relation's TOAST relation for later, if the caller asked
935 * us to process it. In VACUUM FULL, though, the toast table is
936 * automatically rebuilt by cluster_rel so we shouldn't recurse to it.
938 if (do_toast && !(vacstmt->options & VACOPT_FULL))
939 toast_relid = onerel->rd_rel->reltoastrelid;
941 toast_relid = InvalidOid;
944 * Switch to the table owner's userid, so that any index functions are run
945 * as that user. Also lock down security-restricted operations and
946 * arrange to make GUC variable changes local to this command. (This is
947 * unnecessary, but harmless, for lazy VACUUM.)
949 GetUserIdAndSecContext(&save_userid, &save_sec_context);
950 SetUserIdAndSecContext(onerel->rd_rel->relowner,
951 save_sec_context | SECURITY_RESTRICTED_OPERATION);
952 save_nestlevel = NewGUCNestLevel();
955 * Do the actual work --- either FULL or "lazy" vacuum
957 if (vacstmt->options & VACOPT_FULL)
959 /* close relation before vacuuming, but hold lock until commit */
960 relation_close(onerel, NoLock);
963 /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
964 cluster_rel(relid, InvalidOid, false,
965 (vacstmt->options & VACOPT_VERBOSE) != 0,
966 vacstmt->freeze_min_age, vacstmt->freeze_table_age);
969 lazy_vacuum_rel(onerel, vacstmt, vac_strategy, scanned_all);
971 /* Roll back any GUC changes executed by index functions */
972 AtEOXact_GUC(false, save_nestlevel);
974 /* Restore userid and security context */
975 SetUserIdAndSecContext(save_userid, save_sec_context);
977 /* all done with this class, but hold lock until commit */
979 relation_close(onerel, NoLock);
982 * Complete the transaction and free all temporary memory used.
985 CommitTransactionCommand();
988 * If the relation has a secondary toast rel, vacuum that too while we
989 * still hold the session lock on the master table. Note however that
990 * "analyze" will not get done on the toast table. This is good, because
991 * the toaster always uses hardcoded index access and statistics are
992 * totally unimportant for toast relations.
994 if (toast_relid != InvalidOid)
995 vacuum_rel(toast_relid, vacstmt, false, for_wraparound, NULL);
998 * Now release the session-level lock on the master table.
1000 UnlockRelationIdForSession(&onerelid, lmode);
1005 * Open all the indexes of the given relation, obtaining the specified kind
1006 * of lock on each. Return an array of Relation pointers for the indexes
1007 * into *Irel, and the number of indexes into *nindexes.
1010 vac_open_indexes(Relation relation, LOCKMODE lockmode,
1011 int *nindexes, Relation **Irel)
1014 ListCell *indexoidscan;
1017 Assert(lockmode != NoLock);
1019 indexoidlist = RelationGetIndexList(relation);
1021 *nindexes = list_length(indexoidlist);
1024 *Irel = (Relation *) palloc(*nindexes * sizeof(Relation));
1029 foreach(indexoidscan, indexoidlist)
1031 Oid indexoid = lfirst_oid(indexoidscan);
1033 (*Irel)[i++] = index_open(indexoid, lockmode);
1036 list_free(indexoidlist);
1040 * Release the resources acquired by vac_open_indexes. Optionally release
1041 * the locks (say NoLock to keep 'em).
1044 vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
1051 Relation ind = Irel[nindexes];
1053 index_close(ind, lockmode);
1059 * vacuum_delay_point --- check for interrupts and cost-based delay.
1061 * This should be called in each major loop of VACUUM processing,
1062 * typically once per page processed.
1065 vacuum_delay_point(void)
1067 /* Always check for interrupts */
1068 CHECK_FOR_INTERRUPTS();
1070 /* Nap if appropriate */
1071 if (VacuumCostActive && !InterruptPending &&
1072 VacuumCostBalance >= VacuumCostLimit)
1076 msec = VacuumCostDelay * VacuumCostBalance / VacuumCostLimit;
1077 if (msec > VacuumCostDelay * 4)
1078 msec = VacuumCostDelay * 4;
1080 pg_usleep(msec * 1000L);
1082 VacuumCostBalance = 0;
1084 /* update balance values for workers */
1085 AutoVacuumUpdateDelay();
1087 /* Might have gotten an interrupt while sleeping */
1088 CHECK_FOR_INTERRUPTS();