PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
vacuum.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * vacuum.c
4  * The postgres vacuum cleaner.
5  *
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
9  * in cluster.c.
10  *
11  *
12  * Portions Copyright (c) 1996-2017, PostgreSQL Global Development Group
13  * Portions Copyright (c) 1994, Regents of the University of California
14  *
15  *
16  * IDENTIFICATION
17  * src/backend/commands/vacuum.c
18  *
19  *-------------------------------------------------------------------------
20  */
21 #include "postgres.h"
22 
23 #include <math.h>
24 
25 #include "access/clog.h"
26 #include "access/commit_ts.h"
27 #include "access/genam.h"
28 #include "access/heapam.h"
29 #include "access/htup_details.h"
30 #include "access/multixact.h"
31 #include "access/transam.h"
32 #include "access/xact.h"
33 #include "catalog/namespace.h"
34 #include "catalog/pg_database.h"
35 #include "catalog/pg_namespace.h"
36 #include "commands/cluster.h"
37 #include "commands/vacuum.h"
38 #include "miscadmin.h"
39 #include "pgstat.h"
40 #include "postmaster/autovacuum.h"
41 #include "storage/bufmgr.h"
42 #include "storage/lmgr.h"
43 #include "storage/proc.h"
44 #include "storage/procarray.h"
45 #include "utils/acl.h"
46 #include "utils/fmgroids.h"
47 #include "utils/guc.h"
48 #include "utils/memutils.h"
49 #include "utils/snapmgr.h"
50 #include "utils/syscache.h"
51 #include "utils/tqual.h"
52 
53 
54 /*
55  * GUC parameters
56  */
61 
62 
63 /* A few variables that don't seem worth passing around as parameters */
66 
67 
68 /* non-export function prototypes */
69 static List *get_rel_oids(Oid relid, const RangeVar *vacrel);
70 static void vac_truncate_clog(TransactionId frozenXID,
71  MultiXactId minMulti,
72  TransactionId lastSaneFrozenXid,
73  MultiXactId lastSaneMinMulti);
74 static bool vacuum_rel(Oid relid, RangeVar *relation, int options,
75  VacuumParams *params);
76 
77 /*
78  * Primary entry point for manual VACUUM and ANALYZE commands
79  *
80  * This is mainly a preparation wrapper for the real operations that will
81  * happen in vacuum().
82  */
83 void
84 ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel)
85 {
86  VacuumParams params;
87 
88  /* sanity checks on options */
90  Assert((vacstmt->options & VACOPT_VACUUM) ||
91  !(vacstmt->options & (VACOPT_FULL | VACOPT_FREEZE)));
92  Assert((vacstmt->options & VACOPT_ANALYZE) || vacstmt->va_cols == NIL);
93  Assert(!(vacstmt->options & VACOPT_SKIPTOAST));
94 
95  /*
96  * All freeze ages are zero if the FREEZE option is given; otherwise pass
97  * them as -1 which means to use the default values.
98  */
99  if (vacstmt->options & VACOPT_FREEZE)
100  {
101  params.freeze_min_age = 0;
102  params.freeze_table_age = 0;
103  params.multixact_freeze_min_age = 0;
104  params.multixact_freeze_table_age = 0;
105  }
106  else
107  {
108  params.freeze_min_age = -1;
109  params.freeze_table_age = -1;
110  params.multixact_freeze_min_age = -1;
111  params.multixact_freeze_table_age = -1;
112  }
113 
114  /* user-invoked vacuum is never "for wraparound" */
115  params.is_wraparound = false;
116 
117  /* user-invoked vacuum never uses this parameter */
118  params.log_min_duration = -1;
119 
120  /* Now go through the common routine */
121  vacuum(vacstmt->options, vacstmt->relation, InvalidOid, &params,
122  vacstmt->va_cols, NULL, isTopLevel);
123 }
124 
125 /*
126  * Primary entry point for VACUUM and ANALYZE commands.
127  *
128  * options is a bitmask of VacuumOption flags, indicating what to do.
129  *
130  * relid, if not InvalidOid, indicate the relation to process; otherwise,
131  * the RangeVar is used. (The latter must always be passed, because it's
132  * used for error messages.)
133  *
134  * params contains a set of parameters that can be used to customize the
135  * behavior.
136  *
137  * va_cols is a list of columns to analyze, or NIL to process them all.
138  *
139  * bstrategy is normally given as NULL, but in autovacuum it can be passed
140  * in to use the same buffer strategy object across multiple vacuum() calls.
141  *
142  * isTopLevel should be passed down from ProcessUtility.
143  *
144  * It is the caller's responsibility that all parameters are allocated in a
145  * memory context that will not disappear at transaction commit.
146  */
147 void
148 vacuum(int options, RangeVar *relation, Oid relid, VacuumParams *params,
149  List *va_cols, BufferAccessStrategy bstrategy, bool isTopLevel)
150 {
151  const char *stmttype;
152  volatile bool in_outer_xact,
153  use_own_xacts;
154  List *relations;
155  static bool in_vacuum = false;
156 
157  Assert(params != NULL);
158 
159  stmttype = (options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
160 
161  /*
162  * We cannot run VACUUM inside a user transaction block; if we were inside
163  * a transaction, then our commit- and start-transaction-command calls
164  * would not have the intended effect! There are numerous other subtle
165  * dependencies on this, too.
166  *
167  * ANALYZE (without VACUUM) can run either way.
168  */
169  if (options & VACOPT_VACUUM)
170  {
171  PreventTransactionChain(isTopLevel, stmttype);
172  in_outer_xact = false;
173  }
174  else
175  in_outer_xact = IsInTransactionChain(isTopLevel);
176 
177  /*
178  * Due to static variables vac_context, anl_context and vac_strategy,
179  * vacuum() is not reentrant. This matters when VACUUM FULL or ANALYZE
180  * calls a hostile index expression that itself calls ANALYZE.
181  */
182  if (in_vacuum)
183  ereport(ERROR,
184  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
185  errmsg("%s cannot be executed from VACUUM or ANALYZE",
186  stmttype)));
187 
188  /*
189  * Sanity check DISABLE_PAGE_SKIPPING option.
190  */
191  if ((options & VACOPT_FULL) != 0 &&
192  (options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
193  ereport(ERROR,
194  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
195  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
196 
197  /*
198  * Send info about dead objects to the statistics collector, unless we are
199  * in autovacuum --- autovacuum.c does this for itself.
200  */
201  if ((options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
203 
204  /*
205  * Create special memory context for cross-transaction storage.
206  *
207  * Since it is a child of PortalContext, it will go away eventually even
208  * if we suffer an error; there's no need for special abort cleanup logic.
209  */
210  vac_context = AllocSetContextCreate(PortalContext,
211  "Vacuum",
213 
214  /*
215  * If caller didn't give us a buffer strategy object, make one in the
216  * cross-transaction memory context.
217  */
218  if (bstrategy == NULL)
219  {
220  MemoryContext old_context = MemoryContextSwitchTo(vac_context);
221 
222  bstrategy = GetAccessStrategy(BAS_VACUUM);
223  MemoryContextSwitchTo(old_context);
224  }
225  vac_strategy = bstrategy;
226 
227  /*
228  * Build list of relations to process, unless caller gave us one. (If we
229  * build one, we put it in vac_context for safekeeping.)
230  */
231  relations = get_rel_oids(relid, relation);
232 
233  /*
234  * Decide whether we need to start/commit our own transactions.
235  *
236  * For VACUUM (with or without ANALYZE): always do so, so that we can
237  * release locks as soon as possible. (We could possibly use the outer
238  * transaction for a one-table VACUUM, but handling TOAST tables would be
239  * problematic.)
240  *
241  * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
242  * start/commit our own transactions. Also, there's no need to do so if
243  * only processing one relation. For multiple relations when not within a
244  * transaction block, and also in an autovacuum worker, use own
245  * transactions so we can release locks sooner.
246  */
247  if (options & VACOPT_VACUUM)
248  use_own_xacts = true;
249  else
250  {
251  Assert(options & VACOPT_ANALYZE);
253  use_own_xacts = true;
254  else if (in_outer_xact)
255  use_own_xacts = false;
256  else if (list_length(relations) > 1)
257  use_own_xacts = true;
258  else
259  use_own_xacts = false;
260  }
261 
262  /*
263  * vacuum_rel expects to be entered with no transaction active; it will
264  * start and commit its own transaction. But we are called by an SQL
265  * command, and so we are executing inside a transaction already. We
266  * commit the transaction started in PostgresMain() here, and start
267  * another one before exiting to match the commit waiting for us back in
268  * PostgresMain().
269  */
270  if (use_own_xacts)
271  {
272  Assert(!in_outer_xact);
273 
274  /* ActiveSnapshot is not set by autovacuum */
275  if (ActiveSnapshotSet())
277 
278  /* matches the StartTransaction in PostgresMain() */
280  }
281 
282  /* Turn vacuum cost accounting on or off */
283  PG_TRY();
284  {
285  ListCell *cur;
286 
287  in_vacuum = true;
289  VacuumCostBalance = 0;
290  VacuumPageHit = 0;
291  VacuumPageMiss = 0;
292  VacuumPageDirty = 0;
293 
294  /*
295  * Loop to process each selected relation.
296  */
297  foreach(cur, relations)
298  {
299  Oid relid = lfirst_oid(cur);
300 
301  if (options & VACOPT_VACUUM)
302  {
303  if (!vacuum_rel(relid, relation, options, params))
304  continue;
305  }
306 
307  if (options & VACOPT_ANALYZE)
308  {
309  /*
310  * If using separate xacts, start one for analyze. Otherwise,
311  * we can use the outer transaction.
312  */
313  if (use_own_xacts)
314  {
316  /* functions in indexes may want a snapshot set */
318  }
319 
320  analyze_rel(relid, relation, options, params,
321  va_cols, in_outer_xact, vac_strategy);
322 
323  if (use_own_xacts)
324  {
327  }
328  }
329  }
330  }
331  PG_CATCH();
332  {
333  in_vacuum = false;
334  VacuumCostActive = false;
335  PG_RE_THROW();
336  }
337  PG_END_TRY();
338 
339  in_vacuum = false;
340  VacuumCostActive = false;
341 
342  /*
343  * Finish up processing.
344  */
345  if (use_own_xacts)
346  {
347  /* here, we are not in a transaction */
348 
349  /*
350  * This matches the CommitTransaction waiting for us in
351  * PostgresMain().
352  */
354  }
355 
356  if ((options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
357  {
358  /*
359  * Update pg_database.datfrozenxid, and truncate pg_clog if possible.
360  * (autovacuum.c does this for itself.)
361  */
363  }
364 
365  /*
366  * Clean up working storage --- note we must do this after
367  * StartTransactionCommand, else we might be trying to delete the active
368  * context!
369  */
370  MemoryContextDelete(vac_context);
371  vac_context = NULL;
372 }
373 
374 /*
375  * Build a list of Oids for each relation to be processed
376  *
377  * The list is built in vac_context so that it will survive across our
378  * per-relation transactions.
379  */
380 static List *
381 get_rel_oids(Oid relid, const RangeVar *vacrel)
382 {
383  List *oid_list = NIL;
384  MemoryContext oldcontext;
385 
386  /* OID supplied by VACUUM's caller? */
387  if (OidIsValid(relid))
388  {
389  oldcontext = MemoryContextSwitchTo(vac_context);
390  oid_list = lappend_oid(oid_list, relid);
391  MemoryContextSwitchTo(oldcontext);
392  }
393  else if (vacrel)
394  {
395  /* Process a specific relation */
396  Oid relid;
397 
398  /*
399  * Since we don't take a lock here, the relation might be gone, or the
400  * RangeVar might no longer refer to the OID we look up here. In the
401  * former case, VACUUM will do nothing; in the latter case, it will
402  * process the OID we looked up here, rather than the new one. Neither
403  * is ideal, but there's little practical alternative, since we're
404  * going to commit this transaction and begin a new one between now
405  * and then.
406  */
407  relid = RangeVarGetRelid(vacrel, NoLock, false);
408 
409  /* Make a relation list entry for this guy */
410  oldcontext = MemoryContextSwitchTo(vac_context);
411  oid_list = lappend_oid(oid_list, relid);
412  MemoryContextSwitchTo(oldcontext);
413  }
414  else
415  {
416  /*
417  * Process all plain relations and materialized views listed in
418  * pg_class
419  */
420  Relation pgclass;
421  HeapScanDesc scan;
422  HeapTuple tuple;
423 
425 
426  scan = heap_beginscan_catalog(pgclass, 0, NULL);
427 
428  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
429  {
430  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
431 
432  if (classForm->relkind != RELKIND_RELATION &&
433  classForm->relkind != RELKIND_MATVIEW)
434  continue;
435 
436  /* Make a relation list entry for this guy */
437  oldcontext = MemoryContextSwitchTo(vac_context);
438  oid_list = lappend_oid(oid_list, HeapTupleGetOid(tuple));
439  MemoryContextSwitchTo(oldcontext);
440  }
441 
442  heap_endscan(scan);
443  heap_close(pgclass, AccessShareLock);
444  }
445 
446  return oid_list;
447 }
448 
449 /*
450  * vacuum_set_xid_limits() -- compute oldest-Xmin and freeze cutoff points
451  *
452  * The output parameters are:
453  * - oldestXmin is the cutoff value used to distinguish whether tuples are
454  * DEAD or RECENTLY_DEAD (see HeapTupleSatisfiesVacuum).
455  * - freezeLimit is the Xid below which all Xids are replaced by
456  * FrozenTransactionId during vacuum.
457  * - xidFullScanLimit (computed from table_freeze_age parameter)
458  * represents a minimum Xid value; a table whose relfrozenxid is older than
459  * this will have a full-table vacuum applied to it, to freeze tuples across
460  * the whole table. Vacuuming a table younger than this value can use a
461  * partial scan.
462  * - multiXactCutoff is the value below which all MultiXactIds are removed from
463  * Xmax.
464  * - mxactFullScanLimit is a value against which a table's relminmxid value is
465  * compared to produce a full-table vacuum, as with xidFullScanLimit.
466  *
467  * xidFullScanLimit and mxactFullScanLimit can be passed as NULL if caller is
468  * not interested.
469  */
470 void
472  int freeze_min_age,
473  int freeze_table_age,
474  int multixact_freeze_min_age,
475  int multixact_freeze_table_age,
476  TransactionId *oldestXmin,
477  TransactionId *freezeLimit,
478  TransactionId *xidFullScanLimit,
479  MultiXactId *multiXactCutoff,
480  MultiXactId *mxactFullScanLimit)
481 {
482  int freezemin;
483  int mxid_freezemin;
484  int effective_multixact_freeze_max_age;
485  TransactionId limit;
486  TransactionId safeLimit;
487  MultiXactId mxactLimit;
488  MultiXactId safeMxactLimit;
489 
490  /*
491  * We can always ignore processes running lazy vacuum. This is because we
492  * use these values only for deciding which tuples we must keep in the
493  * tables. Since lazy vacuum doesn't write its XID anywhere, it's safe to
494  * ignore it. In theory it could be problematic to ignore lazy vacuums in
495  * a full vacuum, but keep in mind that only one vacuum process can be
496  * working on a particular table at any time, and that each vacuum is
497  * always an independent transaction.
498  */
499  *oldestXmin =
501 
502  Assert(TransactionIdIsNormal(*oldestXmin));
503 
504  /*
505  * Determine the minimum freeze age to use: as specified by the caller, or
506  * vacuum_freeze_min_age, but in any case not more than half
507  * autovacuum_freeze_max_age, so that autovacuums to prevent XID
508  * wraparound won't occur too frequently.
509  */
510  freezemin = freeze_min_age;
511  if (freezemin < 0)
512  freezemin = vacuum_freeze_min_age;
513  freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);
514  Assert(freezemin >= 0);
515 
516  /*
517  * Compute the cutoff XID, being careful not to generate a "permanent" XID
518  */
519  limit = *oldestXmin - freezemin;
520  if (!TransactionIdIsNormal(limit))
521  limit = FirstNormalTransactionId;
522 
523  /*
524  * If oldestXmin is very far back (in practice, more than
525  * autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum
526  * freeze age of zero.
527  */
529  if (!TransactionIdIsNormal(safeLimit))
530  safeLimit = FirstNormalTransactionId;
531 
532  if (TransactionIdPrecedes(limit, safeLimit))
533  {
535  (errmsg("oldest xmin is far in the past"),
536  errhint("Close open transactions soon to avoid wraparound problems.")));
537  limit = *oldestXmin;
538  }
539 
540  *freezeLimit = limit;
541 
542  /*
543  * Compute the multixact age for which freezing is urgent. This is
544  * normally autovacuum_multixact_freeze_max_age, but may be less if we are
545  * short of multixact member space.
546  */
547  effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
548 
549  /*
550  * Determine the minimum multixact freeze age to use: as specified by
551  * caller, or vacuum_multixact_freeze_min_age, but in any case not more
552  * than half effective_multixact_freeze_max_age, so that autovacuums to
553  * prevent MultiXact wraparound won't occur too frequently.
554  */
555  mxid_freezemin = multixact_freeze_min_age;
556  if (mxid_freezemin < 0)
557  mxid_freezemin = vacuum_multixact_freeze_min_age;
558  mxid_freezemin = Min(mxid_freezemin,
559  effective_multixact_freeze_max_age / 2);
560  Assert(mxid_freezemin >= 0);
561 
562  /* compute the cutoff multi, being careful to generate a valid value */
563  mxactLimit = GetOldestMultiXactId() - mxid_freezemin;
564  if (mxactLimit < FirstMultiXactId)
565  mxactLimit = FirstMultiXactId;
566 
567  safeMxactLimit =
568  ReadNextMultiXactId() - effective_multixact_freeze_max_age;
569  if (safeMxactLimit < FirstMultiXactId)
570  safeMxactLimit = FirstMultiXactId;
571 
572  if (MultiXactIdPrecedes(mxactLimit, safeMxactLimit))
573  {
575  (errmsg("oldest multixact is far in the past"),
576  errhint("Close open transactions with multixacts soon to avoid wraparound problems.")));
577  mxactLimit = safeMxactLimit;
578  }
579 
580  *multiXactCutoff = mxactLimit;
581 
582  if (xidFullScanLimit != NULL)
583  {
584  int freezetable;
585 
586  Assert(mxactFullScanLimit != NULL);
587 
588  /*
589  * Determine the table freeze age to use: as specified by the caller,
590  * or vacuum_freeze_table_age, but in any case not more than
591  * autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
592  * VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples
593  * before anti-wraparound autovacuum is launched.
594  */
595  freezetable = freeze_table_age;
596  if (freezetable < 0)
597  freezetable = vacuum_freeze_table_age;
598  freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);
599  Assert(freezetable >= 0);
600 
601  /*
602  * Compute XID limit causing a full-table vacuum, being careful not to
603  * generate a "permanent" XID.
604  */
605  limit = ReadNewTransactionId() - freezetable;
606  if (!TransactionIdIsNormal(limit))
607  limit = FirstNormalTransactionId;
608 
609  *xidFullScanLimit = limit;
610 
611  /*
612  * Similar to the above, determine the table freeze age to use for
613  * multixacts: as specified by the caller, or
614  * vacuum_multixact_freeze_table_age, but in any case not more than
615  * autovacuum_multixact_freeze_table_age * 0.95, so that if you have
616  * e.g. nightly VACUUM schedule, the nightly VACUUM gets a chance to
617  * freeze multixacts before anti-wraparound autovacuum is launched.
618  */
619  freezetable = multixact_freeze_table_age;
620  if (freezetable < 0)
621  freezetable = vacuum_multixact_freeze_table_age;
622  freezetable = Min(freezetable,
623  effective_multixact_freeze_max_age * 0.95);
624  Assert(freezetable >= 0);
625 
626  /*
627  * Compute MultiXact limit causing a full-table vacuum, being careful
628  * to generate a valid MultiXact value.
629  */
630  mxactLimit = ReadNextMultiXactId() - freezetable;
631  if (mxactLimit < FirstMultiXactId)
632  mxactLimit = FirstMultiXactId;
633 
634  *mxactFullScanLimit = mxactLimit;
635  }
636  else
637  {
638  Assert(mxactFullScanLimit == NULL);
639  }
640 }
641 
642 /*
643  * vac_estimate_reltuples() -- estimate the new value for pg_class.reltuples
644  *
645  * If we scanned the whole relation then we should just use the count of
646  * live tuples seen; but if we did not, we should not trust the count
647  * unreservedly, especially not in VACUUM, which may have scanned a quite
648  * nonrandom subset of the table. When we have only partial information,
649  * we take the old value of pg_class.reltuples as a measurement of the
650  * tuple density in the unscanned pages.
651  *
652  * This routine is shared by VACUUM and ANALYZE.
653  */
654 double
655 vac_estimate_reltuples(Relation relation, bool is_analyze,
656  BlockNumber total_pages,
657  BlockNumber scanned_pages,
658  double scanned_tuples)
659 {
660  BlockNumber old_rel_pages = relation->rd_rel->relpages;
661  double old_rel_tuples = relation->rd_rel->reltuples;
662  double old_density;
663  double new_density;
664  double multiplier;
665  double updated_density;
666 
667  /* If we did scan the whole table, just use the count as-is */
668  if (scanned_pages >= total_pages)
669  return scanned_tuples;
670 
671  /*
672  * If scanned_pages is zero but total_pages isn't, keep the existing value
673  * of reltuples. (Note: callers should avoid updating the pg_class
674  * statistics in this situation, since no new information has been
675  * provided.)
676  */
677  if (scanned_pages == 0)
678  return old_rel_tuples;
679 
680  /*
681  * If old value of relpages is zero, old density is indeterminate; we
682  * can't do much except scale up scanned_tuples to match total_pages.
683  */
684  if (old_rel_pages == 0)
685  return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
686 
687  /*
688  * Okay, we've covered the corner cases. The normal calculation is to
689  * convert the old measurement to a density (tuples per page), then update
690  * the density using an exponential-moving-average approach, and finally
691  * compute reltuples as updated_density * total_pages.
692  *
693  * For ANALYZE, the moving average multiplier is just the fraction of the
694  * table's pages we scanned. This is equivalent to assuming that the
695  * tuple density in the unscanned pages didn't change. Of course, it
696  * probably did, if the new density measurement is different. But over
697  * repeated cycles, the value of reltuples will converge towards the
698  * correct value, if repeated measurements show the same new density.
699  *
700  * For VACUUM, the situation is a bit different: we have looked at a
701  * nonrandom sample of pages, but we know for certain that the pages we
702  * didn't look at are precisely the ones that haven't changed lately.
703  * Thus, there is a reasonable argument for doing exactly the same thing
704  * as for the ANALYZE case, that is use the old density measurement as the
705  * value for the unscanned pages.
706  *
707  * This logic could probably use further refinement.
708  */
709  old_density = old_rel_tuples / old_rel_pages;
710  new_density = scanned_tuples / scanned_pages;
711  multiplier = (double) scanned_pages / (double) total_pages;
712  updated_density = old_density + (new_density - old_density) * multiplier;
713  return floor(updated_density * total_pages + 0.5);
714 }
715 
716 
717 /*
718  * vac_update_relstats() -- update statistics for one relation
719  *
720  * Update the whole-relation statistics that are kept in its pg_class
721  * row. There are additional stats that will be updated if we are
722  * doing ANALYZE, but we always update these stats. This routine works
723  * for both index and heap relation entries in pg_class.
724  *
725  * We violate transaction semantics here by overwriting the rel's
726  * existing pg_class tuple with the new values. This is reasonably
727  * safe as long as we're sure that the new values are correct whether or
728  * not this transaction commits. The reason for doing this is that if
729  * we updated these tuples in the usual way, vacuuming pg_class itself
730  * wouldn't work very well --- by the time we got done with a vacuum
731  * cycle, most of the tuples in pg_class would've been obsoleted. Of
732  * course, this only works for fixed-size not-null columns, but these are.
733  *
734  * Another reason for doing it this way is that when we are in a lazy
735  * VACUUM and have PROC_IN_VACUUM set, we mustn't do any regular updates.
736  * Somebody vacuuming pg_class might think they could delete a tuple
737  * marked with xmin = our xid.
738  *
739  * In addition to fundamentally nontransactional statistics such as
740  * relpages and relallvisible, we try to maintain certain lazily-updated
741  * DDL flags such as relhasindex, by clearing them if no longer correct.
742  * It's safe to do this in VACUUM, which can't run in parallel with
743  * CREATE INDEX/RULE/TRIGGER and can't be part of a transaction block.
744  * However, it's *not* safe to do it in an ANALYZE that's within an
745  * outer transaction, because for example the current transaction might
746  * have dropped the last index; then we'd think relhasindex should be
747  * cleared, but if the transaction later rolls back this would be wrong.
748  * So we refrain from updating the DDL flags if we're inside an outer
749  * transaction. This is OK since postponing the flag maintenance is
750  * always allowable.
751  *
752  * This routine is shared by VACUUM and ANALYZE.
753  */
754 void
756  BlockNumber num_pages, double num_tuples,
757  BlockNumber num_all_visible_pages,
758  bool hasindex, TransactionId frozenxid,
759  MultiXactId minmulti,
760  bool in_outer_xact)
761 {
762  Oid relid = RelationGetRelid(relation);
763  Relation rd;
764  HeapTuple ctup;
765  Form_pg_class pgcform;
766  bool dirty;
767 
769 
770  /* Fetch a copy of the tuple to scribble on */
772  if (!HeapTupleIsValid(ctup))
773  elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
774  relid);
775  pgcform = (Form_pg_class) GETSTRUCT(ctup);
776 
777  /* Apply statistical updates, if any, to copied tuple */
778 
779  dirty = false;
780  if (pgcform->relpages != (int32) num_pages)
781  {
782  pgcform->relpages = (int32) num_pages;
783  dirty = true;
784  }
785  if (pgcform->reltuples != (float4) num_tuples)
786  {
787  pgcform->reltuples = (float4) num_tuples;
788  dirty = true;
789  }
790  if (pgcform->relallvisible != (int32) num_all_visible_pages)
791  {
792  pgcform->relallvisible = (int32) num_all_visible_pages;
793  dirty = true;
794  }
795 
796  /* Apply DDL updates, but not inside an outer transaction (see above) */
797 
798  if (!in_outer_xact)
799  {
800  /*
801  * If we didn't find any indexes, reset relhasindex.
802  */
803  if (pgcform->relhasindex && !hasindex)
804  {
805  pgcform->relhasindex = false;
806  dirty = true;
807  }
808 
809  /*
810  * If we have discovered that there are no indexes, then there's no
811  * primary key either. This could be done more thoroughly...
812  */
813  if (pgcform->relhaspkey && !hasindex)
814  {
815  pgcform->relhaspkey = false;
816  dirty = true;
817  }
818 
819  /* We also clear relhasrules and relhastriggers if needed */
820  if (pgcform->relhasrules && relation->rd_rules == NULL)
821  {
822  pgcform->relhasrules = false;
823  dirty = true;
824  }
825  if (pgcform->relhastriggers && relation->trigdesc == NULL)
826  {
827  pgcform->relhastriggers = false;
828  dirty = true;
829  }
830  }
831 
832  /*
833  * Update relfrozenxid, unless caller passed InvalidTransactionId
834  * indicating it has no new data.
835  *
836  * Ordinarily, we don't let relfrozenxid go backwards: if things are
837  * working correctly, the only way the new frozenxid could be older would
838  * be if a previous VACUUM was done with a tighter freeze_min_age, in
839  * which case we don't want to forget the work it already did. However,
840  * if the stored relfrozenxid is "in the future", then it must be corrupt
841  * and it seems best to overwrite it with the cutoff we used this time.
842  * This should match vac_update_datfrozenxid() concerning what we consider
843  * to be "in the future".
844  */
845  if (TransactionIdIsNormal(frozenxid) &&
846  pgcform->relfrozenxid != frozenxid &&
847  (TransactionIdPrecedes(pgcform->relfrozenxid, frozenxid) ||
849  pgcform->relfrozenxid)))
850  {
851  pgcform->relfrozenxid = frozenxid;
852  dirty = true;
853  }
854 
855  /* Similarly for relminmxid */
856  if (MultiXactIdIsValid(minmulti) &&
857  pgcform->relminmxid != minmulti &&
858  (MultiXactIdPrecedes(pgcform->relminmxid, minmulti) ||
859  MultiXactIdPrecedes(ReadNextMultiXactId(), pgcform->relminmxid)))
860  {
861  pgcform->relminmxid = minmulti;
862  dirty = true;
863  }
864 
865  /* If anything changed, write out the tuple. */
866  if (dirty)
867  heap_inplace_update(rd, ctup);
868 
870 }
871 
872 
873 /*
874  * vac_update_datfrozenxid() -- update pg_database.datfrozenxid for our DB
875  *
876  * Update pg_database's datfrozenxid entry for our database to be the
877  * minimum of the pg_class.relfrozenxid values.
878  *
879  * Similarly, update our datminmxid to be the minimum of the
880  * pg_class.relminmxid values.
881  *
882  * If we are able to advance either pg_database value, also try to
883  * truncate pg_clog and pg_multixact.
884  *
885  * We violate transaction semantics here by overwriting the database's
886  * existing pg_database tuple with the new values. This is reasonably
887  * safe since the new values are correct whether or not this transaction
888  * commits. As with vac_update_relstats, this avoids leaving dead tuples
889  * behind after a VACUUM.
890  */
891 void
893 {
894  HeapTuple tuple;
895  Form_pg_database dbform;
896  Relation relation;
897  SysScanDesc scan;
898  HeapTuple classTup;
899  TransactionId newFrozenXid;
900  MultiXactId newMinMulti;
901  TransactionId lastSaneFrozenXid;
902  MultiXactId lastSaneMinMulti;
903  bool bogus = false;
904  bool dirty = false;
905 
906  /*
907  * Initialize the "min" calculation with GetOldestXmin, which is a
908  * reasonable approximation to the minimum relfrozenxid for not-yet-
909  * committed pg_class entries for new tables; see AddNewRelationTuple().
910  * So we cannot produce a wrong minimum by starting with this.
911  */
912  newFrozenXid = GetOldestXmin(NULL, true);
913 
914  /*
915  * Similarly, initialize the MultiXact "min" with the value that would be
916  * used on pg_class for new tables. See AddNewRelationTuple().
917  */
918  newMinMulti = GetOldestMultiXactId();
919 
920  /*
921  * Identify the latest relfrozenxid and relminmxid values that we could
922  * validly see during the scan. These are conservative values, but it's
923  * not really worth trying to be more exact.
924  */
925  lastSaneFrozenXid = ReadNewTransactionId();
926  lastSaneMinMulti = ReadNextMultiXactId();
927 
928  /*
929  * We must seqscan pg_class to find the minimum Xid, because there is no
930  * index that can help us here.
931  */
933 
934  scan = systable_beginscan(relation, InvalidOid, false,
935  NULL, 0, NULL);
936 
937  while ((classTup = systable_getnext(scan)) != NULL)
938  {
939  Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
940 
941  /*
942  * Only consider relations able to hold unfrozen XIDs (anything else
943  * should have InvalidTransactionId in relfrozenxid anyway.)
944  */
945  if (classForm->relkind != RELKIND_RELATION &&
946  classForm->relkind != RELKIND_MATVIEW &&
947  classForm->relkind != RELKIND_TOASTVALUE)
948  continue;
949 
950  Assert(TransactionIdIsNormal(classForm->relfrozenxid));
951  Assert(MultiXactIdIsValid(classForm->relminmxid));
952 
953  /*
954  * If things are working properly, no relation should have a
955  * relfrozenxid or relminmxid that is "in the future". However, such
956  * cases have been known to arise due to bugs in pg_upgrade. If we
957  * see any entries that are "in the future", chicken out and don't do
958  * anything. This ensures we won't truncate clog before those
959  * relations have been scanned and cleaned up.
960  */
961  if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid) ||
962  MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
963  {
964  bogus = true;
965  break;
966  }
967 
968  if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
969  newFrozenXid = classForm->relfrozenxid;
970 
971  if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
972  newMinMulti = classForm->relminmxid;
973  }
974 
975  /* we're done with pg_class */
976  systable_endscan(scan);
977  heap_close(relation, AccessShareLock);
978 
979  /* chicken out if bogus data found */
980  if (bogus)
981  return;
982 
983  Assert(TransactionIdIsNormal(newFrozenXid));
984  Assert(MultiXactIdIsValid(newMinMulti));
985 
986  /* Now fetch the pg_database tuple we need to update. */
988 
989  /* Fetch a copy of the tuple to scribble on */
991  if (!HeapTupleIsValid(tuple))
992  elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
993  dbform = (Form_pg_database) GETSTRUCT(tuple);
994 
995  /*
996  * As in vac_update_relstats(), we ordinarily don't want to let
997  * datfrozenxid go backward; but if it's "in the future" then it must be
998  * corrupt and it seems best to overwrite it.
999  */
1000  if (dbform->datfrozenxid != newFrozenXid &&
1001  (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1002  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1003  {
1004  dbform->datfrozenxid = newFrozenXid;
1005  dirty = true;
1006  }
1007  else
1008  newFrozenXid = dbform->datfrozenxid;
1009 
1010  /* Ditto for datminmxid */
1011  if (dbform->datminmxid != newMinMulti &&
1012  (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1013  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1014  {
1015  dbform->datminmxid = newMinMulti;
1016  dirty = true;
1017  }
1018  else
1019  newMinMulti = dbform->datminmxid;
1020 
1021  if (dirty)
1022  heap_inplace_update(relation, tuple);
1023 
1024  heap_freetuple(tuple);
1025  heap_close(relation, RowExclusiveLock);
1026 
1027  /*
1028  * If we were able to advance datfrozenxid or datminmxid, see if we can
1029  * truncate pg_clog and/or pg_multixact. Also do it if the shared
1030  * XID-wrap-limit info is stale, since this action will update that too.
1031  */
1032  if (dirty || ForceTransactionIdLimitUpdate())
1033  vac_truncate_clog(newFrozenXid, newMinMulti,
1034  lastSaneFrozenXid, lastSaneMinMulti);
1035 }
1036 
1037 
1038 /*
1039  * vac_truncate_clog() -- attempt to truncate the commit log
1040  *
1041  * Scan pg_database to determine the system-wide oldest datfrozenxid,
1042  * and use it to truncate the transaction commit log (pg_clog).
1043  * Also update the XID wrap limit info maintained by varsup.c.
1044  * Likewise for datminmxid.
1045  *
1046  * The passed frozenXID and minMulti are the updated values for my own
1047  * pg_database entry. They're used to initialize the "min" calculations.
1048  * The caller also passes the "last sane" XID and MXID, since it has
1049  * those at hand already.
1050  *
1051  * This routine is only invoked when we've managed to change our
1052  * DB's datfrozenxid/datminmxid values, or we found that the shared
1053  * XID-wrap-limit info is stale.
1054  */
1055 static void
1057  MultiXactId minMulti,
1058  TransactionId lastSaneFrozenXid,
1059  MultiXactId lastSaneMinMulti)
1060 {
1061  TransactionId nextXID = ReadNewTransactionId();
1062  Relation relation;
1063  HeapScanDesc scan;
1064  HeapTuple tuple;
1065  Oid oldestxid_datoid;
1066  Oid minmulti_datoid;
1067  bool bogus = false;
1068  bool frozenAlreadyWrapped = false;
1069 
1070  /* init oldest datoids to sync with my frozenXID/minMulti values */
1071  oldestxid_datoid = MyDatabaseId;
1072  minmulti_datoid = MyDatabaseId;
1073 
1074  /*
1075  * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1076  *
1077  * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1078  * the values could change while we look at them. Fetch each one just
1079  * once to ensure sane behavior of the comparison logic. (Here, as in
1080  * many other places, we assume that fetching or updating an XID in shared
1081  * storage is atomic.)
1082  *
1083  * Note: we need not worry about a race condition with new entries being
1084  * inserted by CREATE DATABASE. Any such entry will have a copy of some
1085  * existing DB's datfrozenxid, and that source DB cannot be ours because
1086  * of the interlock against copying a DB containing an active backend.
1087  * Hence the new entry will not reduce the minimum. Also, if two VACUUMs
1088  * concurrently modify the datfrozenxid's of different databases, the
1089  * worst possible outcome is that pg_clog is not truncated as aggressively
1090  * as it could be.
1091  */
1093 
1094  scan = heap_beginscan_catalog(relation, 0, NULL);
1095 
1096  while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1097  {
1098  volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1099  TransactionId datfrozenxid = dbform->datfrozenxid;
1100  TransactionId datminmxid = dbform->datminmxid;
1101 
1102  Assert(TransactionIdIsNormal(datfrozenxid));
1103  Assert(MultiXactIdIsValid(datminmxid));
1104 
1105  /*
1106  * If things are working properly, no database should have a
1107  * datfrozenxid or datminmxid that is "in the future". However, such
1108  * cases have been known to arise due to bugs in pg_upgrade. If we
1109  * see any entries that are "in the future", chicken out and don't do
1110  * anything. This ensures we won't truncate clog before those
1111  * databases have been scanned and cleaned up. (We will issue the
1112  * "already wrapped" warning if appropriate, though.)
1113  */
1114  if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1115  MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1116  bogus = true;
1117 
1118  if (TransactionIdPrecedes(nextXID, datfrozenxid))
1119  frozenAlreadyWrapped = true;
1120  else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1121  {
1122  frozenXID = datfrozenxid;
1123  oldestxid_datoid = HeapTupleGetOid(tuple);
1124  }
1125 
1126  if (MultiXactIdPrecedes(datminmxid, minMulti))
1127  {
1128  minMulti = datminmxid;
1129  minmulti_datoid = HeapTupleGetOid(tuple);
1130  }
1131  }
1132 
1133  heap_endscan(scan);
1134 
1135  heap_close(relation, AccessShareLock);
1136 
1137  /*
1138  * Do not truncate CLOG if we seem to have suffered wraparound already;
1139  * the computed minimum XID might be bogus. This case should now be
1140  * impossible due to the defenses in GetNewTransactionId, but we keep the
1141  * test anyway.
1142  */
1143  if (frozenAlreadyWrapped)
1144  {
1145  ereport(WARNING,
1146  (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1147  errdetail("You might have already suffered transaction-wraparound data loss.")));
1148  return;
1149  }
1150 
1151  /* chicken out if data is bogus in any other way */
1152  if (bogus)
1153  return;
1154 
1155  /*
1156  * Advance the oldest value for commit timestamps before truncating, so
1157  * that if a user requests a timestamp for a transaction we're truncating
1158  * away right after this point, they get NULL instead of an ugly "file not
1159  * found" error from slru.c. This doesn't matter for xact/multixact
1160  * because they are not subject to arbitrary lookups from users.
1161  */
1162  AdvanceOldestCommitTsXid(frozenXID);
1163 
1164  /*
1165  * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1166  */
1167  TruncateCLOG(frozenXID);
1168  TruncateCommitTs(frozenXID);
1169  TruncateMultiXact(minMulti, minmulti_datoid);
1170 
1171  /*
1172  * Update the wrap limit for GetNewTransactionId and creation of new
1173  * MultiXactIds. Note: these functions will also signal the postmaster
1174  * for an(other) autovac cycle if needed. XXX should we avoid possibly
1175  * signalling twice?
1176  */
1177  SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1178  SetMultiXactIdLimit(minMulti, minmulti_datoid);
1179 }
1180 
1181 
1182 /*
1183  * vacuum_rel() -- vacuum one heap relation
1184  *
1185  * Doing one heap at a time incurs extra overhead, since we need to
1186  * check that the heap exists again just before we vacuum it. The
1187  * reason that we do this is so that vacuuming can be spread across
1188  * many small transactions. Otherwise, two-phase locking would require
1189  * us to lock the entire database during one pass of the vacuum cleaner.
1190  *
1191  * At entry and exit, we are not inside a transaction.
1192  */
1193 static bool
1194 vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params)
1195 {
1196  LOCKMODE lmode;
1197  Relation onerel;
1198  LockRelId onerelid;
1199  Oid toast_relid;
1200  Oid save_userid;
1201  int save_sec_context;
1202  int save_nestlevel;
1203 
1204  Assert(params != NULL);
1205 
1206  /* Begin a transaction for vacuuming this relation */
1208 
1209  /*
1210  * Functions in indexes may want a snapshot set. Also, setting a snapshot
1211  * ensures that RecentGlobalXmin is kept truly recent.
1212  */
1214 
1215  if (!(options & VACOPT_FULL))
1216  {
1217  /*
1218  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1219  * other concurrent VACUUMs know that they can ignore this one while
1220  * determining their OldestXmin. (The reason we don't set it during a
1221  * full VACUUM is exactly that we may have to run user-defined
1222  * functions for functional indexes, and we want to make sure that if
1223  * they use the snapshot set above, any tuples it requires can't get
1224  * removed from other tables. An index function that depends on the
1225  * contents of other tables is arguably broken, but we won't break it
1226  * here by violating transaction semantics.)
1227  *
1228  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
1229  * autovacuum; it's used to avoid canceling a vacuum that was invoked
1230  * in an emergency.
1231  *
1232  * Note: these flags remain set until CommitTransaction or
1233  * AbortTransaction. We don't want to clear them until we reset
1234  * MyPgXact->xid/xmin, else OldestXmin might appear to go backwards,
1235  * which is probably Not Good.
1236  */
1237  LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1239  if (params->is_wraparound)
1241  LWLockRelease(ProcArrayLock);
1242  }
1243 
1244  /*
1245  * Check for user-requested abort. Note we want this to be inside a
1246  * transaction, so xact.c doesn't issue useless WARNING.
1247  */
1249 
1250  /*
1251  * Determine the type of lock we want --- hard exclusive lock for a FULL
1252  * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
1253  * way, we can be sure that no other backend is vacuuming the same table.
1254  */
1256 
1257  /*
1258  * Open the relation and get the appropriate lock on it.
1259  *
1260  * There's a race condition here: the rel may have gone away since the
1261  * last time we saw it. If so, we don't need to vacuum it.
1262  *
1263  * If we've been asked not to wait for the relation lock, acquire it first
1264  * in non-blocking mode, before calling try_relation_open().
1265  */
1266  if (!(options & VACOPT_NOWAIT))
1267  onerel = try_relation_open(relid, lmode);
1268  else if (ConditionalLockRelationOid(relid, lmode))
1269  onerel = try_relation_open(relid, NoLock);
1270  else
1271  {
1272  onerel = NULL;
1273  if (IsAutoVacuumWorkerProcess() && params->log_min_duration >= 0)
1274  ereport(LOG,
1275  (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
1276  errmsg("skipping vacuum of \"%s\" --- lock not available",
1277  relation->relname)));
1278  }
1279 
1280  if (!onerel)
1281  {
1284  return false;
1285  }
1286 
1287  /*
1288  * Check permissions.
1289  *
1290  * We allow the user to vacuum a table if he is superuser, the table
1291  * owner, or the database owner (but in the latter case, only if it's not
1292  * a shared relation). pg_class_ownercheck includes the superuser case.
1293  *
1294  * Note we choose to treat permissions failure as a WARNING and keep
1295  * trying to vacuum the rest of the DB --- is this appropriate?
1296  */
1297  if (!(pg_class_ownercheck(RelationGetRelid(onerel), GetUserId()) ||
1298  (pg_database_ownercheck(MyDatabaseId, GetUserId()) && !onerel->rd_rel->relisshared)))
1299  {
1300  if (onerel->rd_rel->relisshared)
1301  ereport(WARNING,
1302  (errmsg("skipping \"%s\" --- only superuser can vacuum it",
1303  RelationGetRelationName(onerel))));
1304  else if (onerel->rd_rel->relnamespace == PG_CATALOG_NAMESPACE)
1305  ereport(WARNING,
1306  (errmsg("skipping \"%s\" --- only superuser or database owner can vacuum it",
1307  RelationGetRelationName(onerel))));
1308  else
1309  ereport(WARNING,
1310  (errmsg("skipping \"%s\" --- only table or database owner can vacuum it",
1311  RelationGetRelationName(onerel))));
1312  relation_close(onerel, lmode);
1315  return false;
1316  }
1317 
1318  /*
1319  * Check that it's a vacuumable relation; we used to do this in
1320  * get_rel_oids() but seems safer to check after we've locked the
1321  * relation.
1322  */
1323  if (onerel->rd_rel->relkind != RELKIND_RELATION &&
1324  onerel->rd_rel->relkind != RELKIND_MATVIEW &&
1325  onerel->rd_rel->relkind != RELKIND_TOASTVALUE &&
1326  onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1327  {
1328  ereport(WARNING,
1329  (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
1330  RelationGetRelationName(onerel))));
1331  relation_close(onerel, lmode);
1334  return false;
1335  }
1336 
1337  /*
1338  * Silently ignore tables that are temp tables of other backends ---
1339  * trying to vacuum these will lead to great unhappiness, since their
1340  * contents are probably not up-to-date on disk. (We don't throw a
1341  * warning here; it would just lead to chatter during a database-wide
1342  * VACUUM.)
1343  */
1344  if (RELATION_IS_OTHER_TEMP(onerel))
1345  {
1346  relation_close(onerel, lmode);
1349  return false;
1350  }
1351 
1352  /*
1353  * Get a session-level lock too. This will protect our access to the
1354  * relation across multiple transactions, so that we can vacuum the
1355  * relation's TOAST table (if any) secure in the knowledge that no one is
1356  * deleting the parent relation.
1357  *
1358  * NOTE: this cannot block, even if someone else is waiting for access,
1359  * because the lock manager knows that both lock requests are from the
1360  * same process.
1361  */
1362  onerelid = onerel->rd_lockInfo.lockRelId;
1363  LockRelationIdForSession(&onerelid, lmode);
1364 
1365  /*
1366  * Remember the relation's TOAST relation for later, if the caller asked
1367  * us to process it. In VACUUM FULL, though, the toast table is
1368  * automatically rebuilt by cluster_rel so we shouldn't recurse to it.
1369  */
1370  if (!(options & VACOPT_SKIPTOAST) && !(options & VACOPT_FULL))
1371  toast_relid = onerel->rd_rel->reltoastrelid;
1372  else
1373  toast_relid = InvalidOid;
1374 
1375  /*
1376  * Switch to the table owner's userid, so that any index functions are run
1377  * as that user. Also lock down security-restricted operations and
1378  * arrange to make GUC variable changes local to this command. (This is
1379  * unnecessary, but harmless, for lazy VACUUM.)
1380  */
1381  GetUserIdAndSecContext(&save_userid, &save_sec_context);
1382  SetUserIdAndSecContext(onerel->rd_rel->relowner,
1383  save_sec_context | SECURITY_RESTRICTED_OPERATION);
1384  save_nestlevel = NewGUCNestLevel();
1385 
1386  /*
1387  * Do the actual work --- either FULL or "lazy" vacuum
1388  */
1389  if (options & VACOPT_FULL)
1390  {
1391  /* close relation before vacuuming, but hold lock until commit */
1392  relation_close(onerel, NoLock);
1393  onerel = NULL;
1394 
1395  /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
1396  cluster_rel(relid, InvalidOid, false,
1397  (options & VACOPT_VERBOSE) != 0);
1398  }
1399  else
1400  lazy_vacuum_rel(onerel, options, params, vac_strategy);
1401 
1402  /* Roll back any GUC changes executed by index functions */
1403  AtEOXact_GUC(false, save_nestlevel);
1404 
1405  /* Restore userid and security context */
1406  SetUserIdAndSecContext(save_userid, save_sec_context);
1407 
1408  /* all done with this class, but hold lock until commit */
1409  if (onerel)
1410  relation_close(onerel, NoLock);
1411 
1412  /*
1413  * Complete the transaction and free all temporary memory used.
1414  */
1417 
1418  /*
1419  * If the relation has a secondary toast rel, vacuum that too while we
1420  * still hold the session lock on the master table. Note however that
1421  * "analyze" will not get done on the toast table. This is good, because
1422  * the toaster always uses hardcoded index access and statistics are
1423  * totally unimportant for toast relations.
1424  */
1425  if (toast_relid != InvalidOid)
1426  vacuum_rel(toast_relid, relation, options, params);
1427 
1428  /*
1429  * Now release the session-level lock on the master table.
1430  */
1431  UnlockRelationIdForSession(&onerelid, lmode);
1432 
1433  /* Report that we really did it. */
1434  return true;
1435 }
1436 
1437 
1438 /*
1439  * Open all the vacuumable indexes of the given relation, obtaining the
1440  * specified kind of lock on each. Return an array of Relation pointers for
1441  * the indexes into *Irel, and the number of indexes into *nindexes.
1442  *
1443  * We consider an index vacuumable if it is marked insertable (IndexIsReady).
1444  * If it isn't, probably a CREATE INDEX CONCURRENTLY command failed early in
1445  * execution, and what we have is too corrupt to be processable. We will
1446  * vacuum even if the index isn't indisvalid; this is important because in a
1447  * unique index, uniqueness checks will be performed anyway and had better not
1448  * hit dangling index pointers.
1449  */
1450 void
1452  int *nindexes, Relation **Irel)
1453 {
1454  List *indexoidlist;
1455  ListCell *indexoidscan;
1456  int i;
1457 
1458  Assert(lockmode != NoLock);
1459 
1460  indexoidlist = RelationGetIndexList(relation);
1461 
1462  /* allocate enough memory for all indexes */
1463  i = list_length(indexoidlist);
1464 
1465  if (i > 0)
1466  *Irel = (Relation *) palloc(i * sizeof(Relation));
1467  else
1468  *Irel = NULL;
1469 
1470  /* collect just the ready indexes */
1471  i = 0;
1472  foreach(indexoidscan, indexoidlist)
1473  {
1474  Oid indexoid = lfirst_oid(indexoidscan);
1475  Relation indrel;
1476 
1477  indrel = index_open(indexoid, lockmode);
1478  if (IndexIsReady(indrel->rd_index))
1479  (*Irel)[i++] = indrel;
1480  else
1481  index_close(indrel, lockmode);
1482  }
1483 
1484  *nindexes = i;
1485 
1486  list_free(indexoidlist);
1487 }
1488 
1489 /*
1490  * Release the resources acquired by vac_open_indexes. Optionally release
1491  * the locks (say NoLock to keep 'em).
1492  */
1493 void
1494 vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
1495 {
1496  if (Irel == NULL)
1497  return;
1498 
1499  while (nindexes--)
1500  {
1501  Relation ind = Irel[nindexes];
1502 
1503  index_close(ind, lockmode);
1504  }
1505  pfree(Irel);
1506 }
1507 
1508 /*
1509  * vacuum_delay_point --- check for interrupts and cost-based delay.
1510  *
1511  * This should be called in each major loop of VACUUM processing,
1512  * typically once per page processed.
1513  */
1514 void
1516 {
1517  /* Always check for interrupts */
1519 
1520  /* Nap if appropriate */
1523  {
1524  int msec;
1525 
1527  if (msec > VacuumCostDelay * 4)
1528  msec = VacuumCostDelay * 4;
1529 
1530  pg_usleep(msec * 1000L);
1531 
1532  VacuumCostBalance = 0;
1533 
1534  /* update balance values for workers */
1536 
1537  /* Might have gotten an interrupt while sleeping */
1539  }
1540 }
BufferAccessStrategy GetAccessStrategy(BufferAccessStrategyType btype)
Definition: freelist.c:525
#define NIL
Definition: pg_list.h:69
bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode)
Definition: lmgr.c:138
int multixact_freeze_table_age
Definition: vacuum.h:142
void vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
Definition: vacuum.c:1494
LockRelId lockRelId
Definition: rel.h:44
void MemoryContextDelete(MemoryContext context)
Definition: mcxt.c:200
int errhint(const char *fmt,...)
Definition: elog.c:987
void systable_endscan(SysScanDesc sysscan)
Definition: genam.c:493
#define GETSTRUCT(TUP)
Definition: htup_details.h:656
int VacuumCostBalance
Definition: globals.c:138
int vacuum_multixact_freeze_table_age
Definition: vacuum.c:60
void heap_endscan(HeapScanDesc scan)
Definition: heapam.c:1581
double vac_estimate_reltuples(Relation relation, bool is_analyze, BlockNumber total_pages, BlockNumber scanned_pages, double scanned_tuples)
Definition: vacuum.c:655
uint32 TransactionId
Definition: c.h:393
#define SECURITY_RESTRICTED_OPERATION
Definition: miscadmin.h:292
void vac_update_datfrozenxid(void)
Definition: vacuum.c:892
void SetUserIdAndSecContext(Oid userid, int sec_context)
Definition: miscinit.c:395
int LOCKMODE
Definition: lockdefs.h:26
Relation try_relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1150
Oid GetUserId(void)
Definition: miscinit.c:283
FormData_pg_database * Form_pg_database
Definition: pg_database.h:57
#define RangeVarGetRelid(relation, lockmode, missing_ok)
Definition: namespace.h:53
int VacuumPageHit
Definition: globals.c:134
#define IndexIsReady(indexForm)
Definition: pg_index.h:108
#define DatabaseRelationId
Definition: pg_database.h:29
void CommitTransactionCommand(void)
Definition: xact.c:2745
#define RelationRelationId
Definition: pg_class.h:29
#define Min(x, y)
Definition: c.h:801
TransactionId TransactionIdLimitedForOldSnapshots(TransactionId recentXmin, Relation relation)
Definition: snapmgr.c:1686
#define PROC_VACUUM_FOR_WRAPAROUND
Definition: proc.h:46
#define RELKIND_MATVIEW
Definition: pg_class.h:167
void analyze_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params, List *va_cols, bool in_outer_xact, BufferAccessStrategy bstrategy)
Definition: analyze.c:105
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
#define AccessShareLock
Definition: lockdefs.h:36
static BufferAccessStrategy vac_strategy
Definition: vacuum.c:65
struct cursor * cur
Definition: ecpg.c:28
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1263
void vacuum_set_xid_limits(Relation rel, int freeze_min_age, int freeze_table_age, int multixact_freeze_min_age, int multixact_freeze_table_age, TransactionId *oldestXmin, TransactionId *freezeLimit, TransactionId *xidFullScanLimit, MultiXactId *multiXactCutoff, MultiXactId *mxactFullScanLimit)
Definition: vacuum.c:471
uint32 BlockNumber
Definition: block.h:31
void PopActiveSnapshot(void)
Definition: snapmgr.c:807
#define heap_close(r, l)
Definition: heapam.h:97
#define LOG
Definition: elog.h:26
Form_pg_class rd_rel
Definition: rel.h:113
void heap_freetuple(HeapTuple htup)
Definition: heaptuple.c:1374
unsigned int Oid
Definition: postgres_ext.h:31
List * lappend_oid(List *list, Oid datum)
Definition: list.c:164
Snapshot GetTransactionSnapshot(void)
Definition: snapmgr.c:300
#define OidIsValid(objectId)
Definition: c.h:533
void AdvanceOldestCommitTsXid(TransactionId oldestXact)
Definition: commit_ts.c:864
int freeze_table_age
Definition: vacuum.h:139
static List * get_rel_oids(Oid relid, const RangeVar *vacrel)
Definition: vacuum.c:381
SysScanDesc systable_beginscan(Relation heapRelation, Oid indexId, bool indexOK, Snapshot snapshot, int nkeys, ScanKey key)
Definition: genam.c:322
signed int int32
Definition: c.h:253
void SetMultiXactIdLimit(MultiXactId oldest_datminmxid, Oid oldest_datoid)
Definition: multixact.c:2191
PGXACT * MyPgXact
Definition: proc.c:68
uint8 vacuumFlags
Definition: proc.h:208
MemoryContext PortalContext
Definition: mcxt.c:52
void LWLockRelease(LWLock *lock)
Definition: lwlock.c:1714
char * relname
Definition: primnodes.h:67
bool ForceTransactionIdLimitUpdate(void)
Definition: varsup.c:408
void pg_usleep(long microsec)
Definition: signal.c:53
Form_pg_index rd_index
Definition: rel.h:155
HeapTuple systable_getnext(SysScanDesc sysscan)
Definition: genam.c:410
void pfree(void *pointer)
Definition: mcxt.c:992
#define PROC_IN_VACUUM
Definition: proc.h:44
#define FirstNormalTransactionId
Definition: transam.h:34
void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:312
#define ObjectIdGetDatum(X)
Definition: postgres.h:515
#define ERROR
Definition: elog.h:43
void cluster_rel(Oid tableOid, Oid indexOid, bool recheck, bool verbose)
Definition: cluster.c:260
Definition: rel.h:36
int VacuumCostLimit
Definition: globals.c:131
void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode)
Definition: lmgr.c:299
int autovacuum_freeze_max_age
Definition: autovacuum.c:119
int freeze_min_age
Definition: vacuum.h:138
void lazy_vacuum_rel(Relation onerel, int options, VacuumParams *params, BufferAccessStrategy bstrategy)
Definition: vacuumlazy.c:182
int vacuum_multixact_freeze_min_age
Definition: vacuum.c:59
TriggerDesc * trigdesc
Definition: rel.h:119
#define ALLOCSET_DEFAULT_SIZES
Definition: memutils.h:145
bool is_wraparound
Definition: vacuum.h:144
List * va_cols
Definition: parsenodes.h:2986
#define NoLock
Definition: lockdefs.h:34
LockInfoData rd_lockInfo
Definition: rel.h:116
void PushActiveSnapshot(Snapshot snap)
Definition: snapmgr.c:728
void GetUserIdAndSecContext(Oid *userid, int *sec_context)
Definition: miscinit.c:388
#define RowExclusiveLock
Definition: lockdefs.h:38
void ExecVacuum(VacuumStmt *vacstmt, bool isTopLevel)
Definition: vacuum.c:84
void AtEOXact_GUC(bool isCommit, int nestLevel)
Definition: guc.c:5040
int errdetail(const char *fmt,...)
Definition: elog.c:873
static MemoryContext vac_context
Definition: vacuum.c:64
#define RelationGetRelationName(relation)
Definition: rel.h:433
TransactionId ReadNewTransactionId(void)
Definition: varsup.c:250
#define MultiXactIdIsValid(multi)
Definition: multixact.h:27
bool ActiveSnapshotSet(void)
Definition: snapmgr.c:846
#define FirstMultiXactId
Definition: multixact.h:24
HeapScanDesc heap_beginscan_catalog(Relation relation, int nkeys, ScanKey key)
Definition: heapam.c:1402
#define PG_CATALOG_NAMESPACE
Definition: pg_namespace.h:71
bool IsAutoVacuumWorkerProcess(void)
Definition: autovacuum.c:2989
#define ereport(elevel, rest)
Definition: elog.h:122
void pgstat_vacuum_stat(void)
Definition: pgstat.c:946
int MultiXactMemberFreezeThreshold(void)
Definition: multixact.c:2812
bool pg_database_ownercheck(Oid db_oid, Oid roleid)
Definition: aclchk.c:4939
bool TransactionIdPrecedes(TransactionId id1, TransactionId id2)
Definition: transam.c:300
void TruncateCommitTs(TransactionId oldestXact)
Definition: commit_ts.c:811
#define WARNING
Definition: elog.h:40
void vac_open_indexes(Relation relation, LOCKMODE lockmode, int *nindexes, Relation **Irel)
Definition: vacuum.c:1451
float float4
Definition: c.h:376
void TruncateCLOG(TransactionId oldestXact)
Definition: clog.c:643
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
int VacuumPageDirty
Definition: globals.c:136
MultiXactId GetOldestMultiXactId(void)
Definition: multixact.c:2487
MemoryContext AllocSetContextCreate(MemoryContext parent, const char *name, Size minContextSize, Size initBlockSize, Size maxBlockSize)
Definition: aset.c:440
#define RELKIND_TOASTVALUE
Definition: pg_class.h:163
Oid MyDatabaseId
Definition: globals.c:76
HeapTuple heap_getnext(HeapScanDesc scan, ScanDirection direction)
Definition: heapam.c:1781
Relation heap_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1287
#define InvalidOid
Definition: postgres_ext.h:36
void vacuum(int options, RangeVar *relation, Oid relid, VacuumParams *params, List *va_cols, BufferAccessStrategy bstrategy, bool isTopLevel)
Definition: vacuum.c:148
int VacuumCostDelay
Definition: globals.c:132
volatile bool InterruptPending
Definition: globals.c:29
TransactionId GetOldestXmin(Relation rel, bool ignoreVacuum)
Definition: procarray.c:1305
TransactionId MultiXactId
Definition: c.h:403
#define PG_CATCH()
Definition: elog.h:293
#define ShareUpdateExclusiveLock
Definition: lockdefs.h:39
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define NULL
Definition: c.h:226
#define Assert(condition)
Definition: c.h:670
static bool vacuum_rel(Oid relid, RangeVar *relation, int options, VacuumParams *params)
Definition: vacuum.c:1194
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:530
RangeVar * relation
Definition: parsenodes.h:2985
bool pg_class_ownercheck(Oid class_oid, Oid roleid)
Definition: aclchk.c:4521
void StartTransactionCommand(void)
Definition: xact.c:2675
RuleLock * rd_rules
Definition: rel.h:117
void SetTransactionIdLimit(TransactionId oldest_datfrozenxid, Oid oldest_datoid)
Definition: varsup.c:267
static int list_length(const List *l)
Definition: pg_list.h:89
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition: lwlock.c:1110
int vacuum_freeze_min_age
Definition: vacuum.c:57
int log_min_duration
Definition: vacuum.h:145
#define PG_RE_THROW()
Definition: elog.h:314
bool MultiXactIdPrecedes(MultiXactId multi1, MultiXactId multi2)
Definition: multixact.c:3135
static void vac_truncate_clog(TransactionId frozenXID, MultiXactId minMulti, TransactionId lastSaneFrozenXid, MultiXactId lastSaneMinMulti)
Definition: vacuum.c:1056
List * RelationGetIndexList(Relation relation)
Definition: relcache.c:4336
int vacuum_freeze_table_age
Definition: vacuum.c:58
void index_close(Relation relation, LOCKMODE lockmode)
Definition: indexam.c:176
FormData_pg_class * Form_pg_class
Definition: pg_class.h:95
#define SearchSysCacheCopy1(cacheId, key1)
Definition: syscache.h:158
#define AccessExclusiveLock
Definition: lockdefs.h:46
int NewGUCNestLevel(void)
Definition: guc.c:5026
void * palloc(Size size)
Definition: mcxt.c:891
int errmsg(const char *fmt,...)
Definition: elog.c:797
void heap_inplace_update(Relation relation, HeapTuple tuple)
Definition: heapam.c:6233
void list_free(List *list)
Definition: list.c:1133
bool IsInTransactionChain(bool isTopLevel)
Definition: xact.c:3268
int i
void AutoVacuumUpdateDelay(void)
Definition: autovacuum.c:1714
FormData_pg_database
Definition: pg_database.h:50
#define CHECK_FOR_INTERRUPTS()
Definition: miscadmin.h:97
int VacuumPageMiss
Definition: globals.c:135
#define elog
Definition: elog.h:219
#define HeapTupleGetOid(tuple)
Definition: htup_details.h:695
void vacuum_delay_point(void)
Definition: vacuum.c:1515
#define TransactionIdIsNormal(xid)
Definition: transam.h:42
#define PG_TRY()
Definition: elog.h:284
void TruncateMultiXact(MultiXactId newOldestMulti, Oid newOldestMultiDB)
Definition: multixact.c:2928
#define RELKIND_RELATION
Definition: pg_class.h:160
void vac_update_relstats(Relation relation, BlockNumber num_pages, double num_tuples, BlockNumber num_all_visible_pages, bool hasindex, TransactionId frozenxid, MultiXactId minmulti, bool in_outer_xact)
Definition: vacuum.c:755
Definition: pg_list.h:45
#define RelationGetRelid(relation)
Definition: rel.h:413
int multixact_freeze_min_age
Definition: vacuum.h:140
Relation index_open(Oid relationId, LOCKMODE lockmode)
Definition: indexam.c:151
#define PG_END_TRY()
Definition: elog.h:300
#define lfirst_oid(lc)
Definition: pg_list.h:108
void PreventTransactionChain(bool isTopLevel, const char *stmtType)
Definition: xact.c:3152
bool VacuumCostActive
Definition: globals.c:139
MultiXactId ReadNextMultiXactId(void)
Definition: multixact.c:721