]> granicus.if.org Git - postgresql/blob - src/backend/commands/vacuum.c
Allow existing VACUUM options to take a Boolean argument.
[postgresql] / src / backend / commands / vacuum.c
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-2019, 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/tableam.h"
32 #include "access/transam.h"
33 #include "access/xact.h"
34 #include "catalog/namespace.h"
35 #include "catalog/pg_database.h"
36 #include "catalog/pg_inherits.h"
37 #include "catalog/pg_namespace.h"
38 #include "commands/cluster.h"
39 #include "commands/defrem.h"
40 #include "commands/vacuum.h"
41 #include "miscadmin.h"
42 #include "nodes/makefuncs.h"
43 #include "pgstat.h"
44 #include "postmaster/autovacuum.h"
45 #include "storage/bufmgr.h"
46 #include "storage/lmgr.h"
47 #include "storage/proc.h"
48 #include "storage/procarray.h"
49 #include "utils/acl.h"
50 #include "utils/fmgroids.h"
51 #include "utils/guc.h"
52 #include "utils/memutils.h"
53 #include "utils/snapmgr.h"
54 #include "utils/syscache.h"
55
56
57 /*
58  * GUC parameters
59  */
60 int                     vacuum_freeze_min_age;
61 int                     vacuum_freeze_table_age;
62 int                     vacuum_multixact_freeze_min_age;
63 int                     vacuum_multixact_freeze_table_age;
64
65
66 /* A few variables that don't seem worth passing around as parameters */
67 static MemoryContext vac_context = NULL;
68 static BufferAccessStrategy vac_strategy;
69
70
71 /* non-export function prototypes */
72 static List *expand_vacuum_rel(VacuumRelation *vrel, int options);
73 static List *get_all_vacuum_rels(int options);
74 static void vac_truncate_clog(TransactionId frozenXID,
75                                   MultiXactId minMulti,
76                                   TransactionId lastSaneFrozenXid,
77                                   MultiXactId lastSaneMinMulti);
78 static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params);
79
80 /*
81  * Primary entry point for manual VACUUM and ANALYZE commands
82  *
83  * This is mainly a preparation wrapper for the real operations that will
84  * happen in vacuum().
85  */
86 void
87 ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
88 {
89         VacuumParams params;
90         bool verbose = false;
91         bool skip_locked = false;
92         bool analyze = false;
93         bool freeze = false;
94         bool full = false;
95         bool disable_page_skipping = false;
96         ListCell        *lc;
97
98         /* Parse options list */
99         foreach(lc, vacstmt->options)
100         {
101                 DefElem *opt = (DefElem *) lfirst(lc);
102
103                 /* Parse common options for VACUUM and ANALYZE */
104                 if (strcmp(opt->defname, "verbose") == 0)
105                         verbose = defGetBoolean(opt);
106                 else if (strcmp(opt->defname, "skip_locked") == 0)
107                         skip_locked = defGetBoolean(opt);
108                 else if (!vacstmt->is_vacuumcmd)
109                         ereport(ERROR,
110                                         (errcode(ERRCODE_SYNTAX_ERROR),
111                                          errmsg("unrecognized ANALYZE option \"%s\"", opt->defname),
112                                          parser_errposition(pstate, opt->location)));
113
114                 /* Parse options available on VACUUM */
115                 else if (strcmp(opt->defname, "analyze") == 0)
116                         analyze = defGetBoolean(opt);
117                 else if (strcmp(opt->defname, "freeze") == 0)
118                         freeze = defGetBoolean(opt);
119                 else if (strcmp(opt->defname, "full") == 0)
120                         full = defGetBoolean(opt);
121                 else if (strcmp(opt->defname, "disable_page_skipping") == 0)
122                         disable_page_skipping = defGetBoolean(opt);
123                 else
124                         ereport(ERROR,
125                                         (errcode(ERRCODE_SYNTAX_ERROR),
126                                          errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
127                                          parser_errposition(pstate, opt->location)));
128         }
129
130         /* Set vacuum options */
131         params.options =
132                 (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
133                 (verbose ? VACOPT_VERBOSE : 0) |
134                 (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
135                 (analyze ? VACOPT_ANALYZE : 0) |
136                 (freeze ? VACOPT_FREEZE : 0) |
137                 (full ? VACOPT_FULL : 0) |
138                 (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0);
139
140         /* sanity checks on options */
141         Assert(params.options & (VACOPT_VACUUM | VACOPT_ANALYZE));
142         Assert((params.options & VACOPT_VACUUM) ||
143                    !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
144         Assert(!(params.options & VACOPT_SKIPTOAST));
145
146         /*
147          * Make sure VACOPT_ANALYZE is specified if any column lists are present.
148          */
149         if (!(params.options & VACOPT_ANALYZE))
150         {
151                 ListCell   *lc;
152
153                 foreach(lc, vacstmt->rels)
154                 {
155                         VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
156
157                         if (vrel->va_cols != NIL)
158                                 ereport(ERROR,
159                                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
160                                                  errmsg("ANALYZE option must be specified when a column list is provided")));
161                 }
162         }
163
164         /*
165          * All freeze ages are zero if the FREEZE option is given; otherwise pass
166          * them as -1 which means to use the default values.
167          */
168         if (params.options & VACOPT_FREEZE)
169         {
170                 params.freeze_min_age = 0;
171                 params.freeze_table_age = 0;
172                 params.multixact_freeze_min_age = 0;
173                 params.multixact_freeze_table_age = 0;
174         }
175         else
176         {
177                 params.freeze_min_age = -1;
178                 params.freeze_table_age = -1;
179                 params.multixact_freeze_min_age = -1;
180                 params.multixact_freeze_table_age = -1;
181         }
182
183         /* user-invoked vacuum is never "for wraparound" */
184         params.is_wraparound = false;
185
186         /* user-invoked vacuum never uses this parameter */
187         params.log_min_duration = -1;
188
189         /* Now go through the common routine */
190         vacuum(vacstmt->rels, &params, NULL, isTopLevel);
191 }
192
193 /*
194  * Internal entry point for VACUUM and ANALYZE commands.
195  *
196  * relations, if not NIL, is a list of VacuumRelation to process; otherwise,
197  * we process all relevant tables in the database.  For each VacuumRelation,
198  * if a valid OID is supplied, the table with that OID is what to process;
199  * otherwise, the VacuumRelation's RangeVar indicates what to process.
200  *
201  * params contains a set of parameters that can be used to customize the
202  * behavior.
203  *
204  * bstrategy is normally given as NULL, but in autovacuum it can be passed
205  * in to use the same buffer strategy object across multiple vacuum() calls.
206  *
207  * isTopLevel should be passed down from ProcessUtility.
208  *
209  * It is the caller's responsibility that all parameters are allocated in a
210  * memory context that will not disappear at transaction commit.
211  */
212 void
213 vacuum(List *relations, VacuumParams *params,
214            BufferAccessStrategy bstrategy, bool isTopLevel)
215 {
216         static bool in_vacuum = false;
217
218         const char *stmttype;
219         volatile bool in_outer_xact,
220                                 use_own_xacts;
221
222         Assert(params != NULL);
223
224         stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
225
226         /*
227          * We cannot run VACUUM inside a user transaction block; if we were inside
228          * a transaction, then our commit- and start-transaction-command calls
229          * would not have the intended effect!  There are numerous other subtle
230          * dependencies on this, too.
231          *
232          * ANALYZE (without VACUUM) can run either way.
233          */
234         if (params->options & VACOPT_VACUUM)
235         {
236                 PreventInTransactionBlock(isTopLevel, stmttype);
237                 in_outer_xact = false;
238         }
239         else
240                 in_outer_xact = IsInTransactionBlock(isTopLevel);
241
242         /*
243          * Due to static variables vac_context, anl_context and vac_strategy,
244          * vacuum() is not reentrant.  This matters when VACUUM FULL or ANALYZE
245          * calls a hostile index expression that itself calls ANALYZE.
246          */
247         if (in_vacuum)
248                 ereport(ERROR,
249                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
250                                  errmsg("%s cannot be executed from VACUUM or ANALYZE",
251                                                 stmttype)));
252
253         /*
254          * Sanity check DISABLE_PAGE_SKIPPING option.
255          */
256         if ((params->options & VACOPT_FULL) != 0 &&
257                 (params->options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
258                 ereport(ERROR,
259                                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
260                                  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
261
262         /*
263          * Send info about dead objects to the statistics collector, unless we are
264          * in autovacuum --- autovacuum.c does this for itself.
265          */
266         if ((params->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
267                 pgstat_vacuum_stat();
268
269         /*
270          * Create special memory context for cross-transaction storage.
271          *
272          * Since it is a child of PortalContext, it will go away eventually even
273          * if we suffer an error; there's no need for special abort cleanup logic.
274          */
275         vac_context = AllocSetContextCreate(PortalContext,
276                                                                                 "Vacuum",
277                                                                                 ALLOCSET_DEFAULT_SIZES);
278
279         /*
280          * If caller didn't give us a buffer strategy object, make one in the
281          * cross-transaction memory context.
282          */
283         if (bstrategy == NULL)
284         {
285                 MemoryContext old_context = MemoryContextSwitchTo(vac_context);
286
287                 bstrategy = GetAccessStrategy(BAS_VACUUM);
288                 MemoryContextSwitchTo(old_context);
289         }
290         vac_strategy = bstrategy;
291
292         /*
293          * Build list of relation(s) to process, putting any new data in
294          * vac_context for safekeeping.
295          */
296         if (relations != NIL)
297         {
298                 List       *newrels = NIL;
299                 ListCell   *lc;
300
301                 foreach(lc, relations)
302                 {
303                         VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
304                         List       *sublist;
305                         MemoryContext old_context;
306
307                         sublist = expand_vacuum_rel(vrel, params->options);
308                         old_context = MemoryContextSwitchTo(vac_context);
309                         newrels = list_concat(newrels, sublist);
310                         MemoryContextSwitchTo(old_context);
311                 }
312                 relations = newrels;
313         }
314         else
315                 relations = get_all_vacuum_rels(params->options);
316
317         /*
318          * Decide whether we need to start/commit our own transactions.
319          *
320          * For VACUUM (with or without ANALYZE): always do so, so that we can
321          * release locks as soon as possible.  (We could possibly use the outer
322          * transaction for a one-table VACUUM, but handling TOAST tables would be
323          * problematic.)
324          *
325          * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
326          * start/commit our own transactions.  Also, there's no need to do so if
327          * only processing one relation.  For multiple relations when not within a
328          * transaction block, and also in an autovacuum worker, use own
329          * transactions so we can release locks sooner.
330          */
331         if (params->options & VACOPT_VACUUM)
332                 use_own_xacts = true;
333         else
334         {
335                 Assert(params->options & VACOPT_ANALYZE);
336                 if (IsAutoVacuumWorkerProcess())
337                         use_own_xacts = true;
338                 else if (in_outer_xact)
339                         use_own_xacts = false;
340                 else if (list_length(relations) > 1)
341                         use_own_xacts = true;
342                 else
343                         use_own_xacts = false;
344         }
345
346         /*
347          * vacuum_rel expects to be entered with no transaction active; it will
348          * start and commit its own transaction.  But we are called by an SQL
349          * command, and so we are executing inside a transaction already. We
350          * commit the transaction started in PostgresMain() here, and start
351          * another one before exiting to match the commit waiting for us back in
352          * PostgresMain().
353          */
354         if (use_own_xacts)
355         {
356                 Assert(!in_outer_xact);
357
358                 /* ActiveSnapshot is not set by autovacuum */
359                 if (ActiveSnapshotSet())
360                         PopActiveSnapshot();
361
362                 /* matches the StartTransaction in PostgresMain() */
363                 CommitTransactionCommand();
364         }
365
366         /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
367         PG_TRY();
368         {
369                 ListCell   *cur;
370
371                 in_vacuum = true;
372                 VacuumCostActive = (VacuumCostDelay > 0);
373                 VacuumCostBalance = 0;
374                 VacuumPageHit = 0;
375                 VacuumPageMiss = 0;
376                 VacuumPageDirty = 0;
377
378                 /*
379                  * Loop to process each selected relation.
380                  */
381                 foreach(cur, relations)
382                 {
383                         VacuumRelation *vrel = lfirst_node(VacuumRelation, cur);
384
385                         if (params->options & VACOPT_VACUUM)
386                         {
387                                 if (!vacuum_rel(vrel->oid, vrel->relation, params))
388                                         continue;
389                         }
390
391                         if (params->options & VACOPT_ANALYZE)
392                         {
393                                 /*
394                                  * If using separate xacts, start one for analyze. Otherwise,
395                                  * we can use the outer transaction.
396                                  */
397                                 if (use_own_xacts)
398                                 {
399                                         StartTransactionCommand();
400                                         /* functions in indexes may want a snapshot set */
401                                         PushActiveSnapshot(GetTransactionSnapshot());
402                                 }
403
404                                 analyze_rel(vrel->oid, vrel->relation, params,
405                                                         vrel->va_cols, in_outer_xact, vac_strategy);
406
407                                 if (use_own_xacts)
408                                 {
409                                         PopActiveSnapshot();
410                                         CommitTransactionCommand();
411                                 }
412                         }
413                 }
414         }
415         PG_CATCH();
416         {
417                 in_vacuum = false;
418                 VacuumCostActive = false;
419                 PG_RE_THROW();
420         }
421         PG_END_TRY();
422
423         in_vacuum = false;
424         VacuumCostActive = false;
425
426         /*
427          * Finish up processing.
428          */
429         if (use_own_xacts)
430         {
431                 /* here, we are not in a transaction */
432
433                 /*
434                  * This matches the CommitTransaction waiting for us in
435                  * PostgresMain().
436                  */
437                 StartTransactionCommand();
438         }
439
440         if ((params->options & VACOPT_VACUUM) && !IsAutoVacuumWorkerProcess())
441         {
442                 /*
443                  * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
444                  * (autovacuum.c does this for itself.)
445                  */
446                 vac_update_datfrozenxid();
447         }
448
449         /*
450          * Clean up working storage --- note we must do this after
451          * StartTransactionCommand, else we might be trying to delete the active
452          * context!
453          */
454         MemoryContextDelete(vac_context);
455         vac_context = NULL;
456 }
457
458 /*
459  * Check if a given relation can be safely vacuumed or analyzed.  If the
460  * user is not the relation owner, issue a WARNING log message and return
461  * false to let the caller decide what to do with this relation.  This
462  * routine is used to decide if a relation can be processed for VACUUM or
463  * ANALYZE.
464  */
465 bool
466 vacuum_is_relation_owner(Oid relid, Form_pg_class reltuple, int options)
467 {
468         char       *relname;
469
470         Assert((options & (VACOPT_VACUUM | VACOPT_ANALYZE)) != 0);
471
472         /*
473          * Check permissions.
474          *
475          * We allow the user to vacuum or analyze a table if he is superuser, the
476          * table owner, or the database owner (but in the latter case, only if
477          * it's not a shared relation).  pg_class_ownercheck includes the
478          * superuser case.
479          *
480          * Note we choose to treat permissions failure as a WARNING and keep
481          * trying to vacuum or analyze the rest of the DB --- is this appropriate?
482          */
483         if (pg_class_ownercheck(relid, GetUserId()) ||
484                 (pg_database_ownercheck(MyDatabaseId, GetUserId()) && !reltuple->relisshared))
485                 return true;
486
487         relname = NameStr(reltuple->relname);
488
489         if ((options & VACOPT_VACUUM) != 0)
490         {
491                 if (reltuple->relisshared)
492                         ereport(WARNING,
493                                         (errmsg("skipping \"%s\" --- only superuser can vacuum it",
494                                                         relname)));
495                 else if (reltuple->relnamespace == PG_CATALOG_NAMESPACE)
496                         ereport(WARNING,
497                                         (errmsg("skipping \"%s\" --- only superuser or database owner can vacuum it",
498                                                         relname)));
499                 else
500                         ereport(WARNING,
501                                         (errmsg("skipping \"%s\" --- only table or database owner can vacuum it",
502                                                         relname)));
503
504                 /*
505                  * For VACUUM ANALYZE, both logs could show up, but just generate
506                  * information for VACUUM as that would be the first one to be
507                  * processed.
508                  */
509                 return false;
510         }
511
512         if ((options & VACOPT_ANALYZE) != 0)
513         {
514                 if (reltuple->relisshared)
515                         ereport(WARNING,
516                                         (errmsg("skipping \"%s\" --- only superuser can analyze it",
517                                                         relname)));
518                 else if (reltuple->relnamespace == PG_CATALOG_NAMESPACE)
519                         ereport(WARNING,
520                                         (errmsg("skipping \"%s\" --- only superuser or database owner can analyze it",
521                                                         relname)));
522                 else
523                         ereport(WARNING,
524                                         (errmsg("skipping \"%s\" --- only table or database owner can analyze it",
525                                                         relname)));
526         }
527
528         return false;
529 }
530
531
532 /*
533  * vacuum_open_relation
534  *
535  * This routine is used for attempting to open and lock a relation which
536  * is going to be vacuumed or analyzed.  If the relation cannot be opened
537  * or locked, a log is emitted if possible.
538  */
539 Relation
540 vacuum_open_relation(Oid relid, RangeVar *relation, int options,
541                                          bool verbose, LOCKMODE lmode)
542 {
543         Relation        onerel;
544         bool            rel_lock = true;
545         int                     elevel;
546
547         Assert((options & (VACOPT_VACUUM | VACOPT_ANALYZE)) != 0);
548
549         /*
550          * Open the relation and get the appropriate lock on it.
551          *
552          * There's a race condition here: the relation may have gone away since
553          * the last time we saw it.  If so, we don't need to vacuum or analyze it.
554          *
555          * If we've been asked not to wait for the relation lock, acquire it first
556          * in non-blocking mode, before calling try_relation_open().
557          */
558         if (!(options & VACOPT_SKIP_LOCKED))
559                 onerel = try_relation_open(relid, lmode);
560         else if (ConditionalLockRelationOid(relid, lmode))
561                 onerel = try_relation_open(relid, NoLock);
562         else
563         {
564                 onerel = NULL;
565                 rel_lock = false;
566         }
567
568         /* if relation is opened, leave */
569         if (onerel)
570                 return onerel;
571
572         /*
573          * Relation could not be opened, hence generate if possible a log
574          * informing on the situation.
575          *
576          * If the RangeVar is not defined, we do not have enough information to
577          * provide a meaningful log statement.  Chances are that the caller has
578          * intentionally not provided this information so that this logging is
579          * skipped, anyway.
580          */
581         if (relation == NULL)
582                 return NULL;
583
584         /*
585          * Determine the log level.
586          *
587          * For manual VACUUM or ANALYZE, we emit a WARNING to match the log statements
588          * in the permission checks; otherwise, only log if the caller so requested.
589          */
590         if (!IsAutoVacuumWorkerProcess())
591                 elevel = WARNING;
592         else if (verbose)
593                 elevel = LOG;
594         else
595                 return NULL;
596
597         if ((options & VACOPT_VACUUM) != 0)
598         {
599                 if (!rel_lock)
600                         ereport(elevel,
601                                         (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
602                                          errmsg("skipping vacuum of \"%s\" --- lock not available",
603                                                         relation->relname)));
604                 else
605                         ereport(elevel,
606                                         (errcode(ERRCODE_UNDEFINED_TABLE),
607                                          errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
608                                                         relation->relname)));
609
610                 /*
611                  * For VACUUM ANALYZE, both logs could show up, but just generate
612                  * information for VACUUM as that would be the first one to be
613                  * processed.
614                  */
615                 return NULL;
616         }
617
618         if ((options & VACOPT_ANALYZE) != 0)
619         {
620                 if (!rel_lock)
621                         ereport(elevel,
622                                         (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
623                                          errmsg("skipping analyze of \"%s\" --- lock not available",
624                                                         relation->relname)));
625                 else
626                         ereport(elevel,
627                                         (errcode(ERRCODE_UNDEFINED_TABLE),
628                                          errmsg("skipping analyze of \"%s\" --- relation no longer exists",
629                                                         relation->relname)));
630         }
631
632         return NULL;
633 }
634
635
636 /*
637  * Given a VacuumRelation, fill in the table OID if it wasn't specified,
638  * and optionally add VacuumRelations for partitions of the table.
639  *
640  * If a VacuumRelation does not have an OID supplied and is a partitioned
641  * table, an extra entry will be added to the output for each partition.
642  * Presently, only autovacuum supplies OIDs when calling vacuum(), and
643  * it does not want us to expand partitioned tables.
644  *
645  * We take care not to modify the input data structure, but instead build
646  * new VacuumRelation(s) to return.  (But note that they will reference
647  * unmodified parts of the input, eg column lists.)  New data structures
648  * are made in vac_context.
649  */
650 static List *
651 expand_vacuum_rel(VacuumRelation *vrel, int options)
652 {
653         List       *vacrels = NIL;
654         MemoryContext oldcontext;
655
656         /* If caller supplied OID, there's nothing we need do here. */
657         if (OidIsValid(vrel->oid))
658         {
659                 oldcontext = MemoryContextSwitchTo(vac_context);
660                 vacrels = lappend(vacrels, vrel);
661                 MemoryContextSwitchTo(oldcontext);
662         }
663         else
664         {
665                 /* Process a specific relation, and possibly partitions thereof */
666                 Oid                     relid;
667                 HeapTuple       tuple;
668                 Form_pg_class classForm;
669                 bool            include_parts;
670                 int                     rvr_opts;
671
672                 /*
673                  * Since autovacuum workers supply OIDs when calling vacuum(), no
674                  * autovacuum worker should reach this code.
675                  */
676                 Assert(!IsAutoVacuumWorkerProcess());
677
678                 /*
679                  * We transiently take AccessShareLock to protect the syscache lookup
680                  * below, as well as find_all_inheritors's expectation that the caller
681                  * holds some lock on the starting relation.
682                  */
683                 rvr_opts = (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0;
684                 relid = RangeVarGetRelidExtended(vrel->relation,
685                                                                                  AccessShareLock,
686                                                                                  rvr_opts,
687                                                                                  NULL, NULL);
688
689                 /*
690                  * If the lock is unavailable, emit the same log statement that
691                  * vacuum_rel() and analyze_rel() would.
692                  */
693                 if (!OidIsValid(relid))
694                 {
695                         if (options & VACOPT_VACUUM)
696                                 ereport(WARNING,
697                                                 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
698                                                  errmsg("skipping vacuum of \"%s\" --- lock not available",
699                                                                 vrel->relation->relname)));
700                         else
701                                 ereport(WARNING,
702                                                 (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
703                                                  errmsg("skipping analyze of \"%s\" --- lock not available",
704                                                                 vrel->relation->relname)));
705                         return vacrels;
706                 }
707
708                 /*
709                  * To check whether the relation is a partitioned table and its
710                  * ownership, fetch its syscache entry.
711                  */
712                 tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
713                 if (!HeapTupleIsValid(tuple))
714                         elog(ERROR, "cache lookup failed for relation %u", relid);
715                 classForm = (Form_pg_class) GETSTRUCT(tuple);
716
717                 /*
718                  * Make a returnable VacuumRelation for this rel if user is a proper
719                  * owner.
720                  */
721                 if (vacuum_is_relation_owner(relid, classForm, options))
722                 {
723                         oldcontext = MemoryContextSwitchTo(vac_context);
724                         vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
725                                                                                                                   relid,
726                                                                                                                   vrel->va_cols));
727                         MemoryContextSwitchTo(oldcontext);
728                 }
729
730
731                 include_parts = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
732                 ReleaseSysCache(tuple);
733
734                 /*
735                  * If it is, make relation list entries for its partitions.  Note that
736                  * the list returned by find_all_inheritors() includes the passed-in
737                  * OID, so we have to skip that.  There's no point in taking locks on
738                  * the individual partitions yet, and doing so would just add
739                  * unnecessary deadlock risk.  For this last reason we do not check
740                  * yet the ownership of the partitions, which get added to the list to
741                  * process.  Ownership will be checked later on anyway.
742                  */
743                 if (include_parts)
744                 {
745                         List       *part_oids = find_all_inheritors(relid, NoLock, NULL);
746                         ListCell   *part_lc;
747
748                         foreach(part_lc, part_oids)
749                         {
750                                 Oid                     part_oid = lfirst_oid(part_lc);
751
752                                 if (part_oid == relid)
753                                         continue;       /* ignore original table */
754
755                                 /*
756                                  * We omit a RangeVar since it wouldn't be appropriate to
757                                  * complain about failure to open one of these relations
758                                  * later.
759                                  */
760                                 oldcontext = MemoryContextSwitchTo(vac_context);
761                                 vacrels = lappend(vacrels, makeVacuumRelation(NULL,
762                                                                                                                           part_oid,
763                                                                                                                           vrel->va_cols));
764                                 MemoryContextSwitchTo(oldcontext);
765                         }
766                 }
767
768                 /*
769                  * Release lock again.  This means that by the time we actually try to
770                  * process the table, it might be gone or renamed.  In the former case
771                  * we'll silently ignore it; in the latter case we'll process it
772                  * anyway, but we must beware that the RangeVar doesn't necessarily
773                  * identify it anymore.  This isn't ideal, perhaps, but there's little
774                  * practical alternative, since we're typically going to commit this
775                  * transaction and begin a new one between now and then.  Moreover,
776                  * holding locks on multiple relations would create significant risk
777                  * of deadlock.
778                  */
779                 UnlockRelationOid(relid, AccessShareLock);
780         }
781
782         return vacrels;
783 }
784
785 /*
786  * Construct a list of VacuumRelations for all vacuumable rels in
787  * the current database.  The list is built in vac_context.
788  */
789 static List *
790 get_all_vacuum_rels(int options)
791 {
792         List       *vacrels = NIL;
793         Relation        pgclass;
794         TableScanDesc scan;
795         HeapTuple       tuple;
796
797         pgclass = table_open(RelationRelationId, AccessShareLock);
798
799         scan = table_beginscan_catalog(pgclass, 0, NULL);
800
801         while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
802         {
803                 Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
804                 MemoryContext oldcontext;
805                 Oid                     relid = classForm->oid;
806
807                 /* check permissions of relation */
808                 if (!vacuum_is_relation_owner(relid, classForm, options))
809                         continue;
810
811                 /*
812                  * We include partitioned tables here; depending on which operation is
813                  * to be performed, caller will decide whether to process or ignore
814                  * them.
815                  */
816                 if (classForm->relkind != RELKIND_RELATION &&
817                         classForm->relkind != RELKIND_MATVIEW &&
818                         classForm->relkind != RELKIND_PARTITIONED_TABLE)
819                         continue;
820
821                 /*
822                  * Build VacuumRelation(s) specifying the table OIDs to be processed.
823                  * We omit a RangeVar since it wouldn't be appropriate to complain
824                  * about failure to open one of these relations later.
825                  */
826                 oldcontext = MemoryContextSwitchTo(vac_context);
827                 vacrels = lappend(vacrels, makeVacuumRelation(NULL,
828                                                                                                           relid,
829                                                                                                           NIL));
830                 MemoryContextSwitchTo(oldcontext);
831         }
832
833         table_endscan(scan);
834         table_close(pgclass, AccessShareLock);
835
836         return vacrels;
837 }
838
839 /*
840  * vacuum_set_xid_limits() -- compute oldest-Xmin and freeze cutoff points
841  *
842  * The output parameters are:
843  * - oldestXmin is the cutoff value used to distinguish whether tuples are
844  *       DEAD or RECENTLY_DEAD (see HeapTupleSatisfiesVacuum).
845  * - freezeLimit is the Xid below which all Xids are replaced by
846  *       FrozenTransactionId during vacuum.
847  * - xidFullScanLimit (computed from table_freeze_age parameter)
848  *       represents a minimum Xid value; a table whose relfrozenxid is older than
849  *       this will have a full-table vacuum applied to it, to freeze tuples across
850  *       the whole table.  Vacuuming a table younger than this value can use a
851  *       partial scan.
852  * - multiXactCutoff is the value below which all MultiXactIds are removed from
853  *       Xmax.
854  * - mxactFullScanLimit is a value against which a table's relminmxid value is
855  *       compared to produce a full-table vacuum, as with xidFullScanLimit.
856  *
857  * xidFullScanLimit and mxactFullScanLimit can be passed as NULL if caller is
858  * not interested.
859  */
860 void
861 vacuum_set_xid_limits(Relation rel,
862                                           int freeze_min_age,
863                                           int freeze_table_age,
864                                           int multixact_freeze_min_age,
865                                           int multixact_freeze_table_age,
866                                           TransactionId *oldestXmin,
867                                           TransactionId *freezeLimit,
868                                           TransactionId *xidFullScanLimit,
869                                           MultiXactId *multiXactCutoff,
870                                           MultiXactId *mxactFullScanLimit)
871 {
872         int                     freezemin;
873         int                     mxid_freezemin;
874         int                     effective_multixact_freeze_max_age;
875         TransactionId limit;
876         TransactionId safeLimit;
877         MultiXactId mxactLimit;
878         MultiXactId safeMxactLimit;
879
880         /*
881          * We can always ignore processes running lazy vacuum.  This is because we
882          * use these values only for deciding which tuples we must keep in the
883          * tables.  Since lazy vacuum doesn't write its XID anywhere, it's safe to
884          * ignore it.  In theory it could be problematic to ignore lazy vacuums in
885          * a full vacuum, but keep in mind that only one vacuum process can be
886          * working on a particular table at any time, and that each vacuum is
887          * always an independent transaction.
888          */
889         *oldestXmin =
890                 TransactionIdLimitedForOldSnapshots(GetOldestXmin(rel, PROCARRAY_FLAGS_VACUUM), rel);
891
892         Assert(TransactionIdIsNormal(*oldestXmin));
893
894         /*
895          * Determine the minimum freeze age to use: as specified by the caller, or
896          * vacuum_freeze_min_age, but in any case not more than half
897          * autovacuum_freeze_max_age, so that autovacuums to prevent XID
898          * wraparound won't occur too frequently.
899          */
900         freezemin = freeze_min_age;
901         if (freezemin < 0)
902                 freezemin = vacuum_freeze_min_age;
903         freezemin = Min(freezemin, autovacuum_freeze_max_age / 2);
904         Assert(freezemin >= 0);
905
906         /*
907          * Compute the cutoff XID, being careful not to generate a "permanent" XID
908          */
909         limit = *oldestXmin - freezemin;
910         if (!TransactionIdIsNormal(limit))
911                 limit = FirstNormalTransactionId;
912
913         /*
914          * If oldestXmin is very far back (in practice, more than
915          * autovacuum_freeze_max_age / 2 XIDs old), complain and force a minimum
916          * freeze age of zero.
917          */
918         safeLimit = ReadNewTransactionId() - autovacuum_freeze_max_age;
919         if (!TransactionIdIsNormal(safeLimit))
920                 safeLimit = FirstNormalTransactionId;
921
922         if (TransactionIdPrecedes(limit, safeLimit))
923         {
924                 ereport(WARNING,
925                                 (errmsg("oldest xmin is far in the past"),
926                                  errhint("Close open transactions soon to avoid wraparound problems.\n"
927                                                  "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
928                 limit = *oldestXmin;
929         }
930
931         *freezeLimit = limit;
932
933         /*
934          * Compute the multixact age for which freezing is urgent.  This is
935          * normally autovacuum_multixact_freeze_max_age, but may be less if we are
936          * short of multixact member space.
937          */
938         effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
939
940         /*
941          * Determine the minimum multixact freeze age to use: as specified by
942          * caller, or vacuum_multixact_freeze_min_age, but in any case not more
943          * than half effective_multixact_freeze_max_age, so that autovacuums to
944          * prevent MultiXact wraparound won't occur too frequently.
945          */
946         mxid_freezemin = multixact_freeze_min_age;
947         if (mxid_freezemin < 0)
948                 mxid_freezemin = vacuum_multixact_freeze_min_age;
949         mxid_freezemin = Min(mxid_freezemin,
950                                                  effective_multixact_freeze_max_age / 2);
951         Assert(mxid_freezemin >= 0);
952
953         /* compute the cutoff multi, being careful to generate a valid value */
954         mxactLimit = GetOldestMultiXactId() - mxid_freezemin;
955         if (mxactLimit < FirstMultiXactId)
956                 mxactLimit = FirstMultiXactId;
957
958         safeMxactLimit =
959                 ReadNextMultiXactId() - effective_multixact_freeze_max_age;
960         if (safeMxactLimit < FirstMultiXactId)
961                 safeMxactLimit = FirstMultiXactId;
962
963         if (MultiXactIdPrecedes(mxactLimit, safeMxactLimit))
964         {
965                 ereport(WARNING,
966                                 (errmsg("oldest multixact is far in the past"),
967                                  errhint("Close open transactions with multixacts soon to avoid wraparound problems.")));
968                 mxactLimit = safeMxactLimit;
969         }
970
971         *multiXactCutoff = mxactLimit;
972
973         if (xidFullScanLimit != NULL)
974         {
975                 int                     freezetable;
976
977                 Assert(mxactFullScanLimit != NULL);
978
979                 /*
980                  * Determine the table freeze age to use: as specified by the caller,
981                  * or vacuum_freeze_table_age, but in any case not more than
982                  * autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
983                  * VACUUM schedule, the nightly VACUUM gets a chance to freeze tuples
984                  * before anti-wraparound autovacuum is launched.
985                  */
986                 freezetable = freeze_table_age;
987                 if (freezetable < 0)
988                         freezetable = vacuum_freeze_table_age;
989                 freezetable = Min(freezetable, autovacuum_freeze_max_age * 0.95);
990                 Assert(freezetable >= 0);
991
992                 /*
993                  * Compute XID limit causing a full-table vacuum, being careful not to
994                  * generate a "permanent" XID.
995                  */
996                 limit = ReadNewTransactionId() - freezetable;
997                 if (!TransactionIdIsNormal(limit))
998                         limit = FirstNormalTransactionId;
999
1000                 *xidFullScanLimit = limit;
1001
1002                 /*
1003                  * Similar to the above, determine the table freeze age to use for
1004                  * multixacts: as specified by the caller, or
1005                  * vacuum_multixact_freeze_table_age, but in any case not more than
1006                  * autovacuum_multixact_freeze_table_age * 0.95, so that if you have
1007                  * e.g. nightly VACUUM schedule, the nightly VACUUM gets a chance to
1008                  * freeze multixacts before anti-wraparound autovacuum is launched.
1009                  */
1010                 freezetable = multixact_freeze_table_age;
1011                 if (freezetable < 0)
1012                         freezetable = vacuum_multixact_freeze_table_age;
1013                 freezetable = Min(freezetable,
1014                                                   effective_multixact_freeze_max_age * 0.95);
1015                 Assert(freezetable >= 0);
1016
1017                 /*
1018                  * Compute MultiXact limit causing a full-table vacuum, being careful
1019                  * to generate a valid MultiXact value.
1020                  */
1021                 mxactLimit = ReadNextMultiXactId() - freezetable;
1022                 if (mxactLimit < FirstMultiXactId)
1023                         mxactLimit = FirstMultiXactId;
1024
1025                 *mxactFullScanLimit = mxactLimit;
1026         }
1027         else
1028         {
1029                 Assert(mxactFullScanLimit == NULL);
1030         }
1031 }
1032
1033 /*
1034  * vac_estimate_reltuples() -- estimate the new value for pg_class.reltuples
1035  *
1036  *              If we scanned the whole relation then we should just use the count of
1037  *              live tuples seen; but if we did not, we should not blindly extrapolate
1038  *              from that number, since VACUUM may have scanned a quite nonrandom
1039  *              subset of the table.  When we have only partial information, we take
1040  *              the old value of pg_class.reltuples as a measurement of the
1041  *              tuple density in the unscanned pages.
1042  *
1043  *              Note: scanned_tuples should count only *live* tuples, since
1044  *              pg_class.reltuples is defined that way.
1045  */
1046 double
1047 vac_estimate_reltuples(Relation relation,
1048                                            BlockNumber total_pages,
1049                                            BlockNumber scanned_pages,
1050                                            double scanned_tuples)
1051 {
1052         BlockNumber old_rel_pages = relation->rd_rel->relpages;
1053         double          old_rel_tuples = relation->rd_rel->reltuples;
1054         double          old_density;
1055         double          unscanned_pages;
1056         double          total_tuples;
1057
1058         /* If we did scan the whole table, just use the count as-is */
1059         if (scanned_pages >= total_pages)
1060                 return scanned_tuples;
1061
1062         /*
1063          * If scanned_pages is zero but total_pages isn't, keep the existing value
1064          * of reltuples.  (Note: callers should avoid updating the pg_class
1065          * statistics in this situation, since no new information has been
1066          * provided.)
1067          */
1068         if (scanned_pages == 0)
1069                 return old_rel_tuples;
1070
1071         /*
1072          * If old value of relpages is zero, old density is indeterminate; we
1073          * can't do much except scale up scanned_tuples to match total_pages.
1074          */
1075         if (old_rel_pages == 0)
1076                 return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
1077
1078         /*
1079          * Okay, we've covered the corner cases.  The normal calculation is to
1080          * convert the old measurement to a density (tuples per page), then
1081          * estimate the number of tuples in the unscanned pages using that figure,
1082          * and finally add on the number of tuples in the scanned pages.
1083          */
1084         old_density = old_rel_tuples / old_rel_pages;
1085         unscanned_pages = (double) total_pages - (double) scanned_pages;
1086         total_tuples = old_density * unscanned_pages + scanned_tuples;
1087         return floor(total_tuples + 0.5);
1088 }
1089
1090
1091 /*
1092  *      vac_update_relstats() -- update statistics for one relation
1093  *
1094  *              Update the whole-relation statistics that are kept in its pg_class
1095  *              row.  There are additional stats that will be updated if we are
1096  *              doing ANALYZE, but we always update these stats.  This routine works
1097  *              for both index and heap relation entries in pg_class.
1098  *
1099  *              We violate transaction semantics here by overwriting the rel's
1100  *              existing pg_class tuple with the new values.  This is reasonably
1101  *              safe as long as we're sure that the new values are correct whether or
1102  *              not this transaction commits.  The reason for doing this is that if
1103  *              we updated these tuples in the usual way, vacuuming pg_class itself
1104  *              wouldn't work very well --- by the time we got done with a vacuum
1105  *              cycle, most of the tuples in pg_class would've been obsoleted.  Of
1106  *              course, this only works for fixed-size not-null columns, but these are.
1107  *
1108  *              Another reason for doing it this way is that when we are in a lazy
1109  *              VACUUM and have PROC_IN_VACUUM set, we mustn't do any regular updates.
1110  *              Somebody vacuuming pg_class might think they could delete a tuple
1111  *              marked with xmin = our xid.
1112  *
1113  *              In addition to fundamentally nontransactional statistics such as
1114  *              relpages and relallvisible, we try to maintain certain lazily-updated
1115  *              DDL flags such as relhasindex, by clearing them if no longer correct.
1116  *              It's safe to do this in VACUUM, which can't run in parallel with
1117  *              CREATE INDEX/RULE/TRIGGER and can't be part of a transaction block.
1118  *              However, it's *not* safe to do it in an ANALYZE that's within an
1119  *              outer transaction, because for example the current transaction might
1120  *              have dropped the last index; then we'd think relhasindex should be
1121  *              cleared, but if the transaction later rolls back this would be wrong.
1122  *              So we refrain from updating the DDL flags if we're inside an outer
1123  *              transaction.  This is OK since postponing the flag maintenance is
1124  *              always allowable.
1125  *
1126  *              Note: num_tuples should count only *live* tuples, since
1127  *              pg_class.reltuples is defined that way.
1128  *
1129  *              This routine is shared by VACUUM and ANALYZE.
1130  */
1131 void
1132 vac_update_relstats(Relation relation,
1133                                         BlockNumber num_pages, double num_tuples,
1134                                         BlockNumber num_all_visible_pages,
1135                                         bool hasindex, TransactionId frozenxid,
1136                                         MultiXactId minmulti,
1137                                         bool in_outer_xact)
1138 {
1139         Oid                     relid = RelationGetRelid(relation);
1140         Relation        rd;
1141         HeapTuple       ctup;
1142         Form_pg_class pgcform;
1143         bool            dirty;
1144
1145         rd = table_open(RelationRelationId, RowExclusiveLock);
1146
1147         /* Fetch a copy of the tuple to scribble on */
1148         ctup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
1149         if (!HeapTupleIsValid(ctup))
1150                 elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
1151                          relid);
1152         pgcform = (Form_pg_class) GETSTRUCT(ctup);
1153
1154         /* Apply statistical updates, if any, to copied tuple */
1155
1156         dirty = false;
1157         if (pgcform->relpages != (int32) num_pages)
1158         {
1159                 pgcform->relpages = (int32) num_pages;
1160                 dirty = true;
1161         }
1162         if (pgcform->reltuples != (float4) num_tuples)
1163         {
1164                 pgcform->reltuples = (float4) num_tuples;
1165                 dirty = true;
1166         }
1167         if (pgcform->relallvisible != (int32) num_all_visible_pages)
1168         {
1169                 pgcform->relallvisible = (int32) num_all_visible_pages;
1170                 dirty = true;
1171         }
1172
1173         /* Apply DDL updates, but not inside an outer transaction (see above) */
1174
1175         if (!in_outer_xact)
1176         {
1177                 /*
1178                  * If we didn't find any indexes, reset relhasindex.
1179                  */
1180                 if (pgcform->relhasindex && !hasindex)
1181                 {
1182                         pgcform->relhasindex = false;
1183                         dirty = true;
1184                 }
1185
1186                 /* We also clear relhasrules and relhastriggers if needed */
1187                 if (pgcform->relhasrules && relation->rd_rules == NULL)
1188                 {
1189                         pgcform->relhasrules = false;
1190                         dirty = true;
1191                 }
1192                 if (pgcform->relhastriggers && relation->trigdesc == NULL)
1193                 {
1194                         pgcform->relhastriggers = false;
1195                         dirty = true;
1196                 }
1197         }
1198
1199         /*
1200          * Update relfrozenxid, unless caller passed InvalidTransactionId
1201          * indicating it has no new data.
1202          *
1203          * Ordinarily, we don't let relfrozenxid go backwards: if things are
1204          * working correctly, the only way the new frozenxid could be older would
1205          * be if a previous VACUUM was done with a tighter freeze_min_age, in
1206          * which case we don't want to forget the work it already did.  However,
1207          * if the stored relfrozenxid is "in the future", then it must be corrupt
1208          * and it seems best to overwrite it with the cutoff we used this time.
1209          * This should match vac_update_datfrozenxid() concerning what we consider
1210          * to be "in the future".
1211          */
1212         if (TransactionIdIsNormal(frozenxid) &&
1213                 pgcform->relfrozenxid != frozenxid &&
1214                 (TransactionIdPrecedes(pgcform->relfrozenxid, frozenxid) ||
1215                  TransactionIdPrecedes(ReadNewTransactionId(),
1216                                                            pgcform->relfrozenxid)))
1217         {
1218                 pgcform->relfrozenxid = frozenxid;
1219                 dirty = true;
1220         }
1221
1222         /* Similarly for relminmxid */
1223         if (MultiXactIdIsValid(minmulti) &&
1224                 pgcform->relminmxid != minmulti &&
1225                 (MultiXactIdPrecedes(pgcform->relminmxid, minmulti) ||
1226                  MultiXactIdPrecedes(ReadNextMultiXactId(), pgcform->relminmxid)))
1227         {
1228                 pgcform->relminmxid = minmulti;
1229                 dirty = true;
1230         }
1231
1232         /* If anything changed, write out the tuple. */
1233         if (dirty)
1234                 heap_inplace_update(rd, ctup);
1235
1236         table_close(rd, RowExclusiveLock);
1237 }
1238
1239
1240 /*
1241  *      vac_update_datfrozenxid() -- update pg_database.datfrozenxid for our DB
1242  *
1243  *              Update pg_database's datfrozenxid entry for our database to be the
1244  *              minimum of the pg_class.relfrozenxid values.
1245  *
1246  *              Similarly, update our datminmxid to be the minimum of the
1247  *              pg_class.relminmxid values.
1248  *
1249  *              If we are able to advance either pg_database value, also try to
1250  *              truncate pg_xact and pg_multixact.
1251  *
1252  *              We violate transaction semantics here by overwriting the database's
1253  *              existing pg_database tuple with the new values.  This is reasonably
1254  *              safe since the new values are correct whether or not this transaction
1255  *              commits.  As with vac_update_relstats, this avoids leaving dead tuples
1256  *              behind after a VACUUM.
1257  */
1258 void
1259 vac_update_datfrozenxid(void)
1260 {
1261         HeapTuple       tuple;
1262         Form_pg_database dbform;
1263         Relation        relation;
1264         SysScanDesc scan;
1265         HeapTuple       classTup;
1266         TransactionId newFrozenXid;
1267         MultiXactId newMinMulti;
1268         TransactionId lastSaneFrozenXid;
1269         MultiXactId lastSaneMinMulti;
1270         bool            bogus = false;
1271         bool            dirty = false;
1272
1273         /*
1274          * Initialize the "min" calculation with GetOldestXmin, which is a
1275          * reasonable approximation to the minimum relfrozenxid for not-yet-
1276          * committed pg_class entries for new tables; see AddNewRelationTuple().
1277          * So we cannot produce a wrong minimum by starting with this.
1278          */
1279         newFrozenXid = GetOldestXmin(NULL, PROCARRAY_FLAGS_VACUUM);
1280
1281         /*
1282          * Similarly, initialize the MultiXact "min" with the value that would be
1283          * used on pg_class for new tables.  See AddNewRelationTuple().
1284          */
1285         newMinMulti = GetOldestMultiXactId();
1286
1287         /*
1288          * Identify the latest relfrozenxid and relminmxid values that we could
1289          * validly see during the scan.  These are conservative values, but it's
1290          * not really worth trying to be more exact.
1291          */
1292         lastSaneFrozenXid = ReadNewTransactionId();
1293         lastSaneMinMulti = ReadNextMultiXactId();
1294
1295         /*
1296          * We must seqscan pg_class to find the minimum Xid, because there is no
1297          * index that can help us here.
1298          */
1299         relation = table_open(RelationRelationId, AccessShareLock);
1300
1301         scan = systable_beginscan(relation, InvalidOid, false,
1302                                                           NULL, 0, NULL);
1303
1304         while ((classTup = systable_getnext(scan)) != NULL)
1305         {
1306                 Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
1307
1308                 /*
1309                  * Only consider relations able to hold unfrozen XIDs (anything else
1310                  * should have InvalidTransactionId in relfrozenxid anyway.)
1311                  */
1312                 if (classForm->relkind != RELKIND_RELATION &&
1313                         classForm->relkind != RELKIND_MATVIEW &&
1314                         classForm->relkind != RELKIND_TOASTVALUE)
1315                         continue;
1316
1317                 Assert(TransactionIdIsNormal(classForm->relfrozenxid));
1318                 Assert(MultiXactIdIsValid(classForm->relminmxid));
1319
1320                 /*
1321                  * If things are working properly, no relation should have a
1322                  * relfrozenxid or relminmxid that is "in the future".  However, such
1323                  * cases have been known to arise due to bugs in pg_upgrade.  If we
1324                  * see any entries that are "in the future", chicken out and don't do
1325                  * anything.  This ensures we won't truncate clog before those
1326                  * relations have been scanned and cleaned up.
1327                  */
1328                 if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid) ||
1329                         MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
1330                 {
1331                         bogus = true;
1332                         break;
1333                 }
1334
1335                 if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
1336                         newFrozenXid = classForm->relfrozenxid;
1337
1338                 if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
1339                         newMinMulti = classForm->relminmxid;
1340         }
1341
1342         /* we're done with pg_class */
1343         systable_endscan(scan);
1344         table_close(relation, AccessShareLock);
1345
1346         /* chicken out if bogus data found */
1347         if (bogus)
1348                 return;
1349
1350         Assert(TransactionIdIsNormal(newFrozenXid));
1351         Assert(MultiXactIdIsValid(newMinMulti));
1352
1353         /* Now fetch the pg_database tuple we need to update. */
1354         relation = table_open(DatabaseRelationId, RowExclusiveLock);
1355
1356         /* Fetch a copy of the tuple to scribble on */
1357         tuple = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
1358         if (!HeapTupleIsValid(tuple))
1359                 elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
1360         dbform = (Form_pg_database) GETSTRUCT(tuple);
1361
1362         /*
1363          * As in vac_update_relstats(), we ordinarily don't want to let
1364          * datfrozenxid go backward; but if it's "in the future" then it must be
1365          * corrupt and it seems best to overwrite it.
1366          */
1367         if (dbform->datfrozenxid != newFrozenXid &&
1368                 (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
1369                  TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
1370         {
1371                 dbform->datfrozenxid = newFrozenXid;
1372                 dirty = true;
1373         }
1374         else
1375                 newFrozenXid = dbform->datfrozenxid;
1376
1377         /* Ditto for datminmxid */
1378         if (dbform->datminmxid != newMinMulti &&
1379                 (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
1380                  MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
1381         {
1382                 dbform->datminmxid = newMinMulti;
1383                 dirty = true;
1384         }
1385         else
1386                 newMinMulti = dbform->datminmxid;
1387
1388         if (dirty)
1389                 heap_inplace_update(relation, tuple);
1390
1391         heap_freetuple(tuple);
1392         table_close(relation, RowExclusiveLock);
1393
1394         /*
1395          * If we were able to advance datfrozenxid or datminmxid, see if we can
1396          * truncate pg_xact and/or pg_multixact.  Also do it if the shared
1397          * XID-wrap-limit info is stale, since this action will update that too.
1398          */
1399         if (dirty || ForceTransactionIdLimitUpdate())
1400                 vac_truncate_clog(newFrozenXid, newMinMulti,
1401                                                   lastSaneFrozenXid, lastSaneMinMulti);
1402 }
1403
1404
1405 /*
1406  *      vac_truncate_clog() -- attempt to truncate the commit log
1407  *
1408  *              Scan pg_database to determine the system-wide oldest datfrozenxid,
1409  *              and use it to truncate the transaction commit log (pg_xact).
1410  *              Also update the XID wrap limit info maintained by varsup.c.
1411  *              Likewise for datminmxid.
1412  *
1413  *              The passed frozenXID and minMulti are the updated values for my own
1414  *              pg_database entry. They're used to initialize the "min" calculations.
1415  *              The caller also passes the "last sane" XID and MXID, since it has
1416  *              those at hand already.
1417  *
1418  *              This routine is only invoked when we've managed to change our
1419  *              DB's datfrozenxid/datminmxid values, or we found that the shared
1420  *              XID-wrap-limit info is stale.
1421  */
1422 static void
1423 vac_truncate_clog(TransactionId frozenXID,
1424                                   MultiXactId minMulti,
1425                                   TransactionId lastSaneFrozenXid,
1426                                   MultiXactId lastSaneMinMulti)
1427 {
1428         TransactionId nextXID = ReadNewTransactionId();
1429         Relation        relation;
1430         TableScanDesc scan;
1431         HeapTuple       tuple;
1432         Oid                     oldestxid_datoid;
1433         Oid                     minmulti_datoid;
1434         bool            bogus = false;
1435         bool            frozenAlreadyWrapped = false;
1436
1437         /* init oldest datoids to sync with my frozenXID/minMulti values */
1438         oldestxid_datoid = MyDatabaseId;
1439         minmulti_datoid = MyDatabaseId;
1440
1441         /*
1442          * Scan pg_database to compute the minimum datfrozenxid/datminmxid
1443          *
1444          * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
1445          * the values could change while we look at them.  Fetch each one just
1446          * once to ensure sane behavior of the comparison logic.  (Here, as in
1447          * many other places, we assume that fetching or updating an XID in shared
1448          * storage is atomic.)
1449          *
1450          * Note: we need not worry about a race condition with new entries being
1451          * inserted by CREATE DATABASE.  Any such entry will have a copy of some
1452          * existing DB's datfrozenxid, and that source DB cannot be ours because
1453          * of the interlock against copying a DB containing an active backend.
1454          * Hence the new entry will not reduce the minimum.  Also, if two VACUUMs
1455          * concurrently modify the datfrozenxid's of different databases, the
1456          * worst possible outcome is that pg_xact is not truncated as aggressively
1457          * as it could be.
1458          */
1459         relation = table_open(DatabaseRelationId, AccessShareLock);
1460
1461         scan = table_beginscan_catalog(relation, 0, NULL);
1462
1463         while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
1464         {
1465                 volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
1466                 TransactionId datfrozenxid = dbform->datfrozenxid;
1467                 TransactionId datminmxid = dbform->datminmxid;
1468
1469                 Assert(TransactionIdIsNormal(datfrozenxid));
1470                 Assert(MultiXactIdIsValid(datminmxid));
1471
1472                 /*
1473                  * If things are working properly, no database should have a
1474                  * datfrozenxid or datminmxid that is "in the future".  However, such
1475                  * cases have been known to arise due to bugs in pg_upgrade.  If we
1476                  * see any entries that are "in the future", chicken out and don't do
1477                  * anything.  This ensures we won't truncate clog before those
1478                  * databases have been scanned and cleaned up.  (We will issue the
1479                  * "already wrapped" warning if appropriate, though.)
1480                  */
1481                 if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
1482                         MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
1483                         bogus = true;
1484
1485                 if (TransactionIdPrecedes(nextXID, datfrozenxid))
1486                         frozenAlreadyWrapped = true;
1487                 else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
1488                 {
1489                         frozenXID = datfrozenxid;
1490                         oldestxid_datoid = dbform->oid;
1491                 }
1492
1493                 if (MultiXactIdPrecedes(datminmxid, minMulti))
1494                 {
1495                         minMulti = datminmxid;
1496                         minmulti_datoid = dbform->oid;
1497                 }
1498         }
1499
1500         table_endscan(scan);
1501
1502         table_close(relation, AccessShareLock);
1503
1504         /*
1505          * Do not truncate CLOG if we seem to have suffered wraparound already;
1506          * the computed minimum XID might be bogus.  This case should now be
1507          * impossible due to the defenses in GetNewTransactionId, but we keep the
1508          * test anyway.
1509          */
1510         if (frozenAlreadyWrapped)
1511         {
1512                 ereport(WARNING,
1513                                 (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
1514                                  errdetail("You might have already suffered transaction-wraparound data loss.")));
1515                 return;
1516         }
1517
1518         /* chicken out if data is bogus in any other way */
1519         if (bogus)
1520                 return;
1521
1522         /*
1523          * Advance the oldest value for commit timestamps before truncating, so
1524          * that if a user requests a timestamp for a transaction we're truncating
1525          * away right after this point, they get NULL instead of an ugly "file not
1526          * found" error from slru.c.  This doesn't matter for xact/multixact
1527          * because they are not subject to arbitrary lookups from users.
1528          */
1529         AdvanceOldestCommitTsXid(frozenXID);
1530
1531         /*
1532          * Truncate CLOG, multixact and CommitTs to the oldest computed value.
1533          */
1534         TruncateCLOG(frozenXID, oldestxid_datoid);
1535         TruncateCommitTs(frozenXID);
1536         TruncateMultiXact(minMulti, minmulti_datoid);
1537
1538         /*
1539          * Update the wrap limit for GetNewTransactionId and creation of new
1540          * MultiXactIds.  Note: these functions will also signal the postmaster
1541          * for an(other) autovac cycle if needed.   XXX should we avoid possibly
1542          * signalling twice?
1543          */
1544         SetTransactionIdLimit(frozenXID, oldestxid_datoid);
1545         SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
1546 }
1547
1548
1549 /*
1550  *      vacuum_rel() -- vacuum one heap relation
1551  *
1552  *              relid identifies the relation to vacuum.  If relation is supplied,
1553  *              use the name therein for reporting any failure to open/lock the rel;
1554  *              do not use it once we've successfully opened the rel, since it might
1555  *              be stale.
1556  *
1557  *              Returns true if it's okay to proceed with a requested ANALYZE
1558  *              operation on this table.
1559  *
1560  *              Doing one heap at a time incurs extra overhead, since we need to
1561  *              check that the heap exists again just before we vacuum it.  The
1562  *              reason that we do this is so that vacuuming can be spread across
1563  *              many small transactions.  Otherwise, two-phase locking would require
1564  *              us to lock the entire database during one pass of the vacuum cleaner.
1565  *
1566  *              At entry and exit, we are not inside a transaction.
1567  */
1568 static bool
1569 vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params)
1570 {
1571         LOCKMODE        lmode;
1572         Relation        onerel;
1573         LockRelId       onerelid;
1574         Oid                     toast_relid;
1575         Oid                     save_userid;
1576         int                     save_sec_context;
1577         int                     save_nestlevel;
1578
1579         Assert(params != NULL);
1580
1581         /* Begin a transaction for vacuuming this relation */
1582         StartTransactionCommand();
1583
1584         /*
1585          * Functions in indexes may want a snapshot set.  Also, setting a snapshot
1586          * ensures that RecentGlobalXmin is kept truly recent.
1587          */
1588         PushActiveSnapshot(GetTransactionSnapshot());
1589
1590         if (!(params->options & VACOPT_FULL))
1591         {
1592                 /*
1593                  * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
1594                  * other concurrent VACUUMs know that they can ignore this one while
1595                  * determining their OldestXmin.  (The reason we don't set it during a
1596                  * full VACUUM is exactly that we may have to run user-defined
1597                  * functions for functional indexes, and we want to make sure that if
1598                  * they use the snapshot set above, any tuples it requires can't get
1599                  * removed from other tables.  An index function that depends on the
1600                  * contents of other tables is arguably broken, but we won't break it
1601                  * here by violating transaction semantics.)
1602                  *
1603                  * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
1604                  * autovacuum; it's used to avoid canceling a vacuum that was invoked
1605                  * in an emergency.
1606                  *
1607                  * Note: these flags remain set until CommitTransaction or
1608                  * AbortTransaction.  We don't want to clear them until we reset
1609                  * MyPgXact->xid/xmin, else OldestXmin might appear to go backwards,
1610                  * which is probably Not Good.
1611                  */
1612                 LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
1613                 MyPgXact->vacuumFlags |= PROC_IN_VACUUM;
1614                 if (params->is_wraparound)
1615                         MyPgXact->vacuumFlags |= PROC_VACUUM_FOR_WRAPAROUND;
1616                 LWLockRelease(ProcArrayLock);
1617         }
1618
1619         /*
1620          * Check for user-requested abort.  Note we want this to be inside a
1621          * transaction, so xact.c doesn't issue useless WARNING.
1622          */
1623         CHECK_FOR_INTERRUPTS();
1624
1625         /*
1626          * Determine the type of lock we want --- hard exclusive lock for a FULL
1627          * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
1628          * way, we can be sure that no other backend is vacuuming the same table.
1629          */
1630         lmode = (params->options & VACOPT_FULL) ?
1631                 AccessExclusiveLock : ShareUpdateExclusiveLock;
1632
1633         /* open the relation and get the appropriate lock on it */
1634         onerel = vacuum_open_relation(relid, relation, params->options,
1635                                                                   params->log_min_duration >= 0, lmode);
1636
1637         /* leave if relation could not be opened or locked */
1638         if (!onerel)
1639         {
1640                 PopActiveSnapshot();
1641                 CommitTransactionCommand();
1642                 return false;
1643         }
1644
1645         /*
1646          * Check if relation needs to be skipped based on ownership.  This check
1647          * happens also when building the relation list to vacuum for a manual
1648          * operation, and needs to be done additionally here as VACUUM could
1649          * happen across multiple transactions where relation ownership could have
1650          * changed in-between.  Make sure to only generate logs for VACUUM in this
1651          * case.
1652          */
1653         if (!vacuum_is_relation_owner(RelationGetRelid(onerel),
1654                                                                   onerel->rd_rel,
1655                                                                   params->options & VACOPT_VACUUM))
1656         {
1657                 relation_close(onerel, lmode);
1658                 PopActiveSnapshot();
1659                 CommitTransactionCommand();
1660                 return false;
1661         }
1662
1663         /*
1664          * Check that it's of a vacuumable relkind.
1665          */
1666         if (onerel->rd_rel->relkind != RELKIND_RELATION &&
1667                 onerel->rd_rel->relkind != RELKIND_MATVIEW &&
1668                 onerel->rd_rel->relkind != RELKIND_TOASTVALUE &&
1669                 onerel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
1670         {
1671                 ereport(WARNING,
1672                                 (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
1673                                                 RelationGetRelationName(onerel))));
1674                 relation_close(onerel, lmode);
1675                 PopActiveSnapshot();
1676                 CommitTransactionCommand();
1677                 return false;
1678         }
1679
1680         /*
1681          * Silently ignore tables that are temp tables of other backends ---
1682          * trying to vacuum these will lead to great unhappiness, since their
1683          * contents are probably not up-to-date on disk.  (We don't throw a
1684          * warning here; it would just lead to chatter during a database-wide
1685          * VACUUM.)
1686          */
1687         if (RELATION_IS_OTHER_TEMP(onerel))
1688         {
1689                 relation_close(onerel, lmode);
1690                 PopActiveSnapshot();
1691                 CommitTransactionCommand();
1692                 return false;
1693         }
1694
1695         /*
1696          * Silently ignore partitioned tables as there is no work to be done.  The
1697          * useful work is on their child partitions, which have been queued up for
1698          * us separately.
1699          */
1700         if (onerel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
1701         {
1702                 relation_close(onerel, lmode);
1703                 PopActiveSnapshot();
1704                 CommitTransactionCommand();
1705                 /* It's OK to proceed with ANALYZE on this table */
1706                 return true;
1707         }
1708
1709         /*
1710          * Get a session-level lock too. This will protect our access to the
1711          * relation across multiple transactions, so that we can vacuum the
1712          * relation's TOAST table (if any) secure in the knowledge that no one is
1713          * deleting the parent relation.
1714          *
1715          * NOTE: this cannot block, even if someone else is waiting for access,
1716          * because the lock manager knows that both lock requests are from the
1717          * same process.
1718          */
1719         onerelid = onerel->rd_lockInfo.lockRelId;
1720         LockRelationIdForSession(&onerelid, lmode);
1721
1722         /*
1723          * Remember the relation's TOAST relation for later, if the caller asked
1724          * us to process it.  In VACUUM FULL, though, the toast table is
1725          * automatically rebuilt by cluster_rel so we shouldn't recurse to it.
1726          */
1727         if (!(params->options & VACOPT_SKIPTOAST) && !(params->options & VACOPT_FULL))
1728                 toast_relid = onerel->rd_rel->reltoastrelid;
1729         else
1730                 toast_relid = InvalidOid;
1731
1732         /*
1733          * Switch to the table owner's userid, so that any index functions are run
1734          * as that user.  Also lock down security-restricted operations and
1735          * arrange to make GUC variable changes local to this command. (This is
1736          * unnecessary, but harmless, for lazy VACUUM.)
1737          */
1738         GetUserIdAndSecContext(&save_userid, &save_sec_context);
1739         SetUserIdAndSecContext(onerel->rd_rel->relowner,
1740                                                    save_sec_context | SECURITY_RESTRICTED_OPERATION);
1741         save_nestlevel = NewGUCNestLevel();
1742
1743         /*
1744          * Do the actual work --- either FULL or "lazy" vacuum
1745          */
1746         if (params->options & VACOPT_FULL)
1747         {
1748                 int                     cluster_options = 0;
1749
1750                 /* close relation before vacuuming, but hold lock until commit */
1751                 relation_close(onerel, NoLock);
1752                 onerel = NULL;
1753
1754                 if ((params->options & VACOPT_VERBOSE) != 0)
1755                         cluster_options |= CLUOPT_VERBOSE;
1756
1757                 /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
1758                 cluster_rel(relid, InvalidOid, cluster_options);
1759         }
1760         else
1761                 heap_vacuum_rel(onerel, params, vac_strategy);
1762
1763         /* Roll back any GUC changes executed by index functions */
1764         AtEOXact_GUC(false, save_nestlevel);
1765
1766         /* Restore userid and security context */
1767         SetUserIdAndSecContext(save_userid, save_sec_context);
1768
1769         /* all done with this class, but hold lock until commit */
1770         if (onerel)
1771                 relation_close(onerel, NoLock);
1772
1773         /*
1774          * Complete the transaction and free all temporary memory used.
1775          */
1776         PopActiveSnapshot();
1777         CommitTransactionCommand();
1778
1779         /*
1780          * If the relation has a secondary toast rel, vacuum that too while we
1781          * still hold the session lock on the master table.  Note however that
1782          * "analyze" will not get done on the toast table.  This is good, because
1783          * the toaster always uses hardcoded index access and statistics are
1784          * totally unimportant for toast relations.
1785          */
1786         if (toast_relid != InvalidOid)
1787                 vacuum_rel(toast_relid, NULL, params);
1788
1789         /*
1790          * Now release the session-level lock on the master table.
1791          */
1792         UnlockRelationIdForSession(&onerelid, lmode);
1793
1794         /* Report that we really did it. */
1795         return true;
1796 }
1797
1798
1799 /*
1800  * Open all the vacuumable indexes of the given relation, obtaining the
1801  * specified kind of lock on each.  Return an array of Relation pointers for
1802  * the indexes into *Irel, and the number of indexes into *nindexes.
1803  *
1804  * We consider an index vacuumable if it is marked insertable (indisready).
1805  * If it isn't, probably a CREATE INDEX CONCURRENTLY command failed early in
1806  * execution, and what we have is too corrupt to be processable.  We will
1807  * vacuum even if the index isn't indisvalid; this is important because in a
1808  * unique index, uniqueness checks will be performed anyway and had better not
1809  * hit dangling index pointers.
1810  */
1811 void
1812 vac_open_indexes(Relation relation, LOCKMODE lockmode,
1813                                  int *nindexes, Relation **Irel)
1814 {
1815         List       *indexoidlist;
1816         ListCell   *indexoidscan;
1817         int                     i;
1818
1819         Assert(lockmode != NoLock);
1820
1821         indexoidlist = RelationGetIndexList(relation);
1822
1823         /* allocate enough memory for all indexes */
1824         i = list_length(indexoidlist);
1825
1826         if (i > 0)
1827                 *Irel = (Relation *) palloc(i * sizeof(Relation));
1828         else
1829                 *Irel = NULL;
1830
1831         /* collect just the ready indexes */
1832         i = 0;
1833         foreach(indexoidscan, indexoidlist)
1834         {
1835                 Oid                     indexoid = lfirst_oid(indexoidscan);
1836                 Relation        indrel;
1837
1838                 indrel = index_open(indexoid, lockmode);
1839                 if (indrel->rd_index->indisready)
1840                         (*Irel)[i++] = indrel;
1841                 else
1842                         index_close(indrel, lockmode);
1843         }
1844
1845         *nindexes = i;
1846
1847         list_free(indexoidlist);
1848 }
1849
1850 /*
1851  * Release the resources acquired by vac_open_indexes.  Optionally release
1852  * the locks (say NoLock to keep 'em).
1853  */
1854 void
1855 vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
1856 {
1857         if (Irel == NULL)
1858                 return;
1859
1860         while (nindexes--)
1861         {
1862                 Relation        ind = Irel[nindexes];
1863
1864                 index_close(ind, lockmode);
1865         }
1866         pfree(Irel);
1867 }
1868
1869 /*
1870  * vacuum_delay_point --- check for interrupts and cost-based delay.
1871  *
1872  * This should be called in each major loop of VACUUM processing,
1873  * typically once per page processed.
1874  */
1875 void
1876 vacuum_delay_point(void)
1877 {
1878         /* Always check for interrupts */
1879         CHECK_FOR_INTERRUPTS();
1880
1881         /* Nap if appropriate */
1882         if (VacuumCostActive && !InterruptPending &&
1883                 VacuumCostBalance >= VacuumCostLimit)
1884         {
1885                 double          msec;
1886
1887                 msec = VacuumCostDelay * VacuumCostBalance / VacuumCostLimit;
1888                 if (msec > VacuumCostDelay * 4)
1889                         msec = VacuumCostDelay * 4;
1890
1891                 pg_usleep((long) (msec * 1000));
1892
1893                 VacuumCostBalance = 0;
1894
1895                 /* update balance values for workers */
1896                 AutoVacuumUpdateDelay();
1897
1898                 /* Might have gotten an interrupt while sleeping */
1899                 CHECK_FOR_INTERRUPTS();
1900         }
1901 }