LCOV - differential code coverage report
Current view: top level - src/backend/commands - vacuum.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: Differential Code Coverage 16@8cea358b128 vs 17@8cea358b128 Lines: 90.1 % 748 674 1 2 71 5 20 649 1 42
Current Date: 2024-04-14 14:21:10 Functions: 95.5 % 22 21 1 11 10 3
Baseline: 16@8cea358b128 Branches: 76.4 % 656 501 6 1 148 6 18 477
Baseline Date: 2024-04-14 14:21:09 Line coverage date bins:
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed [..60] days: 100.0 % 24 24 20 4
(120,180] days: 0.0 % 1 0 1
(240..) days: 89.9 % 723 650 2 71 5 645
Function coverage date bins:
[..60] days: 100.0 % 1 1 1
(240..) days: 95.2 % 21 20 1 10 10
Branch coverage date bins:
[..60] days: 80.0 % 30 24 6 18 6
(240..) days: 76.2 % 626 477 1 148 6 471

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * vacuum.c
                                  4                 :                :  *    The postgres vacuum cleaner.
                                  5                 :                :  *
                                  6                 :                :  * This file includes (a) control and dispatch code for VACUUM and ANALYZE
                                  7                 :                :  * commands, (b) code to compute various vacuum thresholds, and (c) index
                                  8                 :                :  * vacuum code.
                                  9                 :                :  *
                                 10                 :                :  * VACUUM for heap AM is implemented in vacuumlazy.c, parallel vacuum in
                                 11                 :                :  * vacuumparallel.c, ANALYZE in analyze.c, and VACUUM FULL is a variant of
                                 12                 :                :  * CLUSTER, handled in cluster.c.
                                 13                 :                :  *
                                 14                 :                :  *
                                 15                 :                :  * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group
                                 16                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 17                 :                :  *
                                 18                 :                :  *
                                 19                 :                :  * IDENTIFICATION
                                 20                 :                :  *    src/backend/commands/vacuum.c
                                 21                 :                :  *
                                 22                 :                :  *-------------------------------------------------------------------------
                                 23                 :                :  */
                                 24                 :                : #include "postgres.h"
                                 25                 :                : 
                                 26                 :                : #include <math.h>
                                 27                 :                : 
                                 28                 :                : #include "access/clog.h"
                                 29                 :                : #include "access/commit_ts.h"
                                 30                 :                : #include "access/genam.h"
                                 31                 :                : #include "access/heapam.h"
                                 32                 :                : #include "access/htup_details.h"
                                 33                 :                : #include "access/multixact.h"
                                 34                 :                : #include "access/tableam.h"
                                 35                 :                : #include "access/transam.h"
                                 36                 :                : #include "access/xact.h"
                                 37                 :                : #include "catalog/index.h"
                                 38                 :                : #include "catalog/namespace.h"
                                 39                 :                : #include "catalog/pg_database.h"
                                 40                 :                : #include "catalog/pg_inherits.h"
                                 41                 :                : #include "commands/cluster.h"
                                 42                 :                : #include "commands/defrem.h"
                                 43                 :                : #include "commands/vacuum.h"
                                 44                 :                : #include "miscadmin.h"
                                 45                 :                : #include "nodes/makefuncs.h"
                                 46                 :                : #include "pgstat.h"
                                 47                 :                : #include "postmaster/autovacuum.h"
                                 48                 :                : #include "postmaster/bgworker_internals.h"
                                 49                 :                : #include "postmaster/interrupt.h"
                                 50                 :                : #include "storage/bufmgr.h"
                                 51                 :                : #include "storage/lmgr.h"
                                 52                 :                : #include "storage/pmsignal.h"
                                 53                 :                : #include "storage/proc.h"
                                 54                 :                : #include "storage/procarray.h"
                                 55                 :                : #include "utils/acl.h"
                                 56                 :                : #include "utils/fmgroids.h"
                                 57                 :                : #include "utils/guc.h"
                                 58                 :                : #include "utils/guc_hooks.h"
                                 59                 :                : #include "utils/memutils.h"
                                 60                 :                : #include "utils/snapmgr.h"
                                 61                 :                : #include "utils/syscache.h"
                                 62                 :                : 
                                 63                 :                : 
                                 64                 :                : /*
                                 65                 :                :  * GUC parameters
                                 66                 :                :  */
                                 67                 :                : int         vacuum_freeze_min_age;
                                 68                 :                : int         vacuum_freeze_table_age;
                                 69                 :                : int         vacuum_multixact_freeze_min_age;
                                 70                 :                : int         vacuum_multixact_freeze_table_age;
                                 71                 :                : int         vacuum_failsafe_age;
                                 72                 :                : int         vacuum_multixact_failsafe_age;
                                 73                 :                : 
                                 74                 :                : /*
                                 75                 :                :  * Variables for cost-based vacuum delay. The defaults differ between
                                 76                 :                :  * autovacuum and vacuum. They should be set with the appropriate GUC value in
                                 77                 :                :  * vacuum code. They are initialized here to the defaults for client backends
                                 78                 :                :  * executing VACUUM or ANALYZE.
                                 79                 :                :  */
                                 80                 :                : double      vacuum_cost_delay = 0;
                                 81                 :                : int         vacuum_cost_limit = 200;
                                 82                 :                : 
                                 83                 :                : /*
                                 84                 :                :  * VacuumFailsafeActive is a defined as a global so that we can determine
                                 85                 :                :  * whether or not to re-enable cost-based vacuum delay when vacuuming a table.
                                 86                 :                :  * If failsafe mode has been engaged, we will not re-enable cost-based delay
                                 87                 :                :  * for the table until after vacuuming has completed, regardless of other
                                 88                 :                :  * settings.
                                 89                 :                :  *
                                 90                 :                :  * Only VACUUM code should inspect this variable and only table access methods
                                 91                 :                :  * should set it to true. In Table AM-agnostic VACUUM code, this variable is
                                 92                 :                :  * inspected to determine whether or not to allow cost-based delays. Table AMs
                                 93                 :                :  * are free to set it if they desire this behavior, but it is false by default
                                 94                 :                :  * and reset to false in between vacuuming each relation.
                                 95                 :                :  */
                                 96                 :                : bool        VacuumFailsafeActive = false;
                                 97                 :                : 
                                 98                 :                : /*
                                 99                 :                :  * Variables for cost-based parallel vacuum.  See comments atop
                                100                 :                :  * compute_parallel_delay to understand how it works.
                                101                 :                :  */
                                102                 :                : pg_atomic_uint32 *VacuumSharedCostBalance = NULL;
                                103                 :                : pg_atomic_uint32 *VacuumActiveNWorkers = NULL;
                                104                 :                : int         VacuumCostBalanceLocal = 0;
                                105                 :                : 
                                106                 :                : /* non-export function prototypes */
                                107                 :                : static List *expand_vacuum_rel(VacuumRelation *vrel,
                                108                 :                :                                MemoryContext vac_context, int options);
                                109                 :                : static List *get_all_vacuum_rels(MemoryContext vac_context, int options);
                                110                 :                : static void vac_truncate_clog(TransactionId frozenXID,
                                111                 :                :                               MultiXactId minMulti,
                                112                 :                :                               TransactionId lastSaneFrozenXid,
                                113                 :                :                               MultiXactId lastSaneMinMulti);
                                114                 :                : static bool vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
                                115                 :                :                        BufferAccessStrategy bstrategy);
                                116                 :                : static double compute_parallel_delay(void);
                                117                 :                : static VacOptValue get_vacoptval_from_boolean(DefElem *def);
                                118                 :                : static bool vac_tid_reaped(ItemPointer itemptr, void *state);
                                119                 :                : 
                                120                 :                : /*
                                121                 :                :  * GUC check function to ensure GUC value specified is within the allowable
                                122                 :                :  * range.
                                123                 :                :  */
                                124                 :                : bool
  373 drowley@postgresql.o      125                 :CBC         928 : check_vacuum_buffer_usage_limit(int *newval, void **extra,
                                126                 :                :                                 GucSource source)
                                127                 :                : {
                                128                 :                :     /* Value upper and lower hard limits are inclusive */
                                129   [ +  -  +  - ]:            928 :     if (*newval == 0 || (*newval >= MIN_BAS_VAC_RING_SIZE_KB &&
                                130         [ +  - ]:            928 :                          *newval <= MAX_BAS_VAC_RING_SIZE_KB))
                                131                 :            928 :         return true;
                                132                 :                : 
                                133                 :                :     /* Value does not fall within any allowable range */
  136 michael@paquier.xyz       134                 :UNC           0 :     GUC_check_errdetail("vacuum_buffer_usage_limit must be 0 or between %d kB and %d kB",
                                135                 :                :                         MIN_BAS_VAC_RING_SIZE_KB, MAX_BAS_VAC_RING_SIZE_KB);
                                136                 :                : 
  373 drowley@postgresql.o      137                 :UBC           0 :     return false;
                                138                 :                : }
                                139                 :                : 
                                140                 :                : /*
                                141                 :                :  * Primary entry point for manual VACUUM and ANALYZE commands
                                142                 :                :  *
                                143                 :                :  * This is mainly a preparation wrapper for the real operations that will
                                144                 :                :  * happen in vacuum().
                                145                 :                :  */
                                146                 :                : void
 1854 rhaas@postgresql.org      147                 :CBC        5140 : ExecVacuum(ParseState *pstate, VacuumStmt *vacstmt, bool isTopLevel)
                                148                 :                : {
                                149                 :                :     VacuumParams params;
  374 drowley@postgresql.o      150                 :           5140 :     BufferAccessStrategy bstrategy = NULL;
 1789 tgl@sss.pgh.pa.us         151                 :           5140 :     bool        verbose = false;
                                152                 :           5140 :     bool        skip_locked = false;
                                153                 :           5140 :     bool        analyze = false;
                                154                 :           5140 :     bool        freeze = false;
                                155                 :           5140 :     bool        full = false;
                                156                 :           5140 :     bool        disable_page_skipping = false;
  405 michael@paquier.xyz       157                 :           5140 :     bool        process_main = true;
 1160                           158                 :           5140 :     bool        process_toast = true;
                                159                 :                :     int         ring_size;
  464 tgl@sss.pgh.pa.us         160                 :           5140 :     bool        skip_database_stats = false;
                                161                 :           5140 :     bool        only_database_stats = false;
                                162                 :                :     MemoryContext vac_context;
                                163                 :                :     ListCell   *lc;
                                164                 :                : 
                                165                 :                :     /* index_cleanup and truncate values unspecified for now */
 1031 pg@bowt.ie                166                 :           5140 :     params.index_cleanup = VACOPTVALUE_UNSPECIFIED;
                                167                 :           5140 :     params.truncate = VACOPTVALUE_UNSPECIFIED;
                                168                 :                : 
                                169                 :                :     /* By default parallel vacuum is enabled */
 1546 akapila@postgresql.o      170                 :           5140 :     params.nworkers = 0;
                                171                 :                : 
                                172                 :                :     /* Will be set later if we recurse to a TOAST table. */
   32 nathan@postgresql.or      173                 :GNC        5140 :     params.toast_parent = InvalidOid;
                                174                 :                : 
                                175                 :                :     /*
                                176                 :                :      * Set this to an invalid value so it is clear whether or not a
                                177                 :                :      * BUFFER_USAGE_LIMIT was specified when making the access strategy.
                                178                 :                :      */
  373 drowley@postgresql.o      179                 :CBC        5140 :     ring_size = -1;
                                180                 :                : 
                                181                 :                :     /* Parse options list */
 1854 rhaas@postgresql.org      182   [ +  +  +  +  :           9541 :     foreach(lc, vacstmt->options)
                                              +  + ]
                                183                 :                :     {
 1789 tgl@sss.pgh.pa.us         184                 :           4419 :         DefElem    *opt = (DefElem *) lfirst(lc);
                                185                 :                : 
                                186                 :                :         /* Parse common options for VACUUM and ANALYZE */
 1854 rhaas@postgresql.org      187         [ +  + ]:           4419 :         if (strcmp(opt->defname, "verbose") == 0)
 1843                           188                 :             19 :             verbose = defGetBoolean(opt);
 1854                           189         [ +  + ]:           4400 :         else if (strcmp(opt->defname, "skip_locked") == 0)
 1843                           190                 :            167 :             skip_locked = defGetBoolean(opt);
  373 drowley@postgresql.o      191         [ +  + ]:           4233 :         else if (strcmp(opt->defname, "buffer_usage_limit") == 0)
                                192                 :                :         {
                                193                 :                :             const char *hintmsg;
                                194                 :                :             int         result;
                                195                 :                :             char       *vac_buffer_size;
                                196                 :                : 
                                197                 :             27 :             vac_buffer_size = defGetString(opt);
                                198                 :                : 
                                199                 :                :             /*
                                200                 :                :              * Check that the specified value is valid and the size falls
                                201                 :                :              * within the hard upper and lower limits if it is not 0.
                                202                 :                :              */
  369                           203         [ +  + ]:             27 :             if (!parse_int(vac_buffer_size, &result, GUC_UNIT_KB, &hintmsg) ||
                                204         [ +  + ]:             24 :                 (result != 0 &&
                                205   [ +  +  +  + ]:             18 :                  (result < MIN_BAS_VAC_RING_SIZE_KB || result > MAX_BAS_VAC_RING_SIZE_KB)))
                                206                 :                :             {
  373                           207   [ +  -  +  + ]:              9 :                 ereport(ERROR,
                                208                 :                :                         (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                                209                 :                :                          errmsg("BUFFER_USAGE_LIMIT option must be 0 or between %d kB and %d kB",
                                210                 :                :                                 MIN_BAS_VAC_RING_SIZE_KB, MAX_BAS_VAC_RING_SIZE_KB),
                                211                 :                :                          hintmsg ? errhint("%s", _(hintmsg)) : 0));
                                212                 :                :             }
                                213                 :                : 
                                214                 :             18 :             ring_size = result;
                                215                 :                :         }
 1854 rhaas@postgresql.org      216         [ +  + ]:           4206 :         else if (!vacstmt->is_vacuumcmd)
                                217         [ +  - ]:              3 :             ereport(ERROR,
                                218                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                219                 :                :                      errmsg("unrecognized ANALYZE option \"%s\"", opt->defname),
                                220                 :                :                      parser_errposition(pstate, opt->location)));
                                221                 :                : 
                                222                 :                :         /* Parse options available on VACUUM */
                                223         [ +  + ]:           4203 :         else if (strcmp(opt->defname, "analyze") == 0)
 1843                           224                 :            685 :             analyze = defGetBoolean(opt);
 1854                           225         [ +  + ]:           3518 :         else if (strcmp(opt->defname, "freeze") == 0)
 1843                           226                 :            573 :             freeze = defGetBoolean(opt);
 1854                           227         [ +  + ]:           2945 :         else if (strcmp(opt->defname, "full") == 0)
 1843                           228                 :            178 :             full = defGetBoolean(opt);
 1854                           229         [ +  + ]:           2767 :         else if (strcmp(opt->defname, "disable_page_skipping") == 0)
 1843                           230                 :             91 :             disable_page_skipping = defGetBoolean(opt);
 1837                           231         [ +  + ]:           2676 :         else if (strcmp(opt->defname, "index_cleanup") == 0)
                                232                 :                :         {
                                233                 :                :             /* Interpret no string as the default, which is 'auto' */
 1031 pg@bowt.ie                234         [ -  + ]:             87 :             if (!opt->arg)
 1031 pg@bowt.ie                235                 :UBC           0 :                 params.index_cleanup = VACOPTVALUE_AUTO;
                                236                 :                :             else
                                237                 :                :             {
 1031 pg@bowt.ie                238                 :CBC          87 :                 char       *sval = defGetString(opt);
                                239                 :                : 
                                240                 :                :                 /* Try matching on 'auto' string, or fall back on boolean */
                                241         [ +  + ]:             87 :                 if (pg_strcasecmp(sval, "auto") == 0)
                                242                 :              3 :                     params.index_cleanup = VACOPTVALUE_AUTO;
                                243                 :                :                 else
                                244                 :             84 :                     params.index_cleanup = get_vacoptval_from_boolean(opt);
                                245                 :                :             }
                                246                 :                :         }
  405 michael@paquier.xyz       247         [ +  + ]:           2589 :         else if (strcmp(opt->defname, "process_main") == 0)
                                248                 :             77 :             process_main = defGetBoolean(opt);
 1160                           249         [ +  + ]:           2512 :         else if (strcmp(opt->defname, "process_toast") == 0)
                                250                 :             80 :             process_toast = defGetBoolean(opt);
 1803 fujii@postgresql.org      251         [ +  + ]:           2432 :         else if (strcmp(opt->defname, "truncate") == 0)
 1031 pg@bowt.ie                252                 :             74 :             params.truncate = get_vacoptval_from_boolean(opt);
 1546 akapila@postgresql.o      253         [ +  + ]:           2358 :         else if (strcmp(opt->defname, "parallel") == 0)
                                254                 :                :         {
                                255         [ +  + ]:            169 :             if (opt->arg == NULL)
                                256                 :                :             {
                                257         [ +  - ]:              3 :                 ereport(ERROR,
                                258                 :                :                         (errcode(ERRCODE_SYNTAX_ERROR),
                                259                 :                :                          errmsg("parallel option requires a value between 0 and %d",
                                260                 :                :                                 MAX_PARALLEL_WORKER_LIMIT),
                                261                 :                :                          parser_errposition(pstate, opt->location)));
                                262                 :                :             }
                                263                 :                :             else
                                264                 :                :             {
                                265                 :                :                 int         nworkers;
                                266                 :                : 
                                267                 :            166 :                 nworkers = defGetInt32(opt);
                                268   [ +  +  -  + ]:            166 :                 if (nworkers < 0 || nworkers > MAX_PARALLEL_WORKER_LIMIT)
                                269         [ +  - ]:              3 :                     ereport(ERROR,
                                270                 :                :                             (errcode(ERRCODE_SYNTAX_ERROR),
                                271                 :                :                              errmsg("parallel workers for vacuum must be between 0 and %d",
                                272                 :                :                                     MAX_PARALLEL_WORKER_LIMIT),
                                273                 :                :                              parser_errposition(pstate, opt->location)));
                                274                 :                : 
                                275                 :                :                 /*
                                276                 :                :                  * Disable parallel vacuum, if user has specified parallel
                                277                 :                :                  * degree as zero.
                                278                 :                :                  */
                                279         [ +  + ]:            163 :                 if (nworkers == 0)
                                280                 :             77 :                     params.nworkers = -1;
                                281                 :                :                 else
                                282                 :             86 :                     params.nworkers = nworkers;
                                283                 :                :             }
                                284                 :                :         }
  464 tgl@sss.pgh.pa.us         285         [ +  + ]:           2189 :         else if (strcmp(opt->defname, "skip_database_stats") == 0)
                                286                 :           2131 :             skip_database_stats = defGetBoolean(opt);
                                287         [ +  - ]:             58 :         else if (strcmp(opt->defname, "only_database_stats") == 0)
                                288                 :             58 :             only_database_stats = defGetBoolean(opt);
                                289                 :                :         else
 1854 rhaas@postgresql.org      290         [ #  # ]:UBC           0 :             ereport(ERROR,
                                291                 :                :                     (errcode(ERRCODE_SYNTAX_ERROR),
                                292                 :                :                      errmsg("unrecognized VACUUM option \"%s\"", opt->defname),
                                293                 :                :                      parser_errposition(pstate, opt->location)));
                                294                 :                :     }
                                295                 :                : 
                                296                 :                :     /* Set vacuum options */
 1843 rhaas@postgresql.org      297                 :CBC        5122 :     params.options =
                                298         [ +  + ]:           5122 :         (vacstmt->is_vacuumcmd ? VACOPT_VACUUM : VACOPT_ANALYZE) |
                                299         [ +  + ]:           5122 :         (verbose ? VACOPT_VERBOSE : 0) |
                                300         [ +  + ]:           5122 :         (skip_locked ? VACOPT_SKIP_LOCKED : 0) |
                                301         [ +  + ]:           5122 :         (analyze ? VACOPT_ANALYZE : 0) |
                                302         [ +  + ]:           5122 :         (freeze ? VACOPT_FREEZE : 0) |
                                303         [ +  + ]:           5122 :         (full ? VACOPT_FULL : 0) |
 1160 michael@paquier.xyz       304         [ +  + ]:           5122 :         (disable_page_skipping ? VACOPT_DISABLE_PAGE_SKIPPING : 0) |
  405                           305         [ +  + ]:           5122 :         (process_main ? VACOPT_PROCESS_MAIN : 0) |
  464 tgl@sss.pgh.pa.us         306         [ +  + ]:           5122 :         (process_toast ? VACOPT_PROCESS_TOAST : 0) |
                                307         [ +  + ]:           5122 :         (skip_database_stats ? VACOPT_SKIP_DATABASE_STATS : 0) |
                                308         [ +  + ]:           5122 :         (only_database_stats ? VACOPT_ONLY_DATABASE_STATS : 0);
                                309                 :                : 
                                310                 :                :     /* sanity checks on options */
 1854 rhaas@postgresql.org      311         [ -  + ]:           5122 :     Assert(params.options & (VACOPT_VACUUM | VACOPT_ANALYZE));
                                312   [ +  +  -  + ]:           5122 :     Assert((params.options & VACOPT_VACUUM) ||
                                313                 :                :            !(params.options & (VACOPT_FULL | VACOPT_FREEZE)));
                                314                 :                : 
 1459 akapila@postgresql.o      315   [ +  +  +  + ]:           5122 :     if ((params.options & VACOPT_FULL) && params.nworkers > 0)
 1546                           316         [ +  - ]:              3 :         ereport(ERROR,
                                317                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                318                 :                :                  errmsg("VACUUM FULL cannot be performed in parallel")));
                                319                 :                : 
                                320                 :                :     /*
                                321                 :                :      * BUFFER_USAGE_LIMIT does nothing for VACUUM (FULL) so just raise an
                                322                 :                :      * ERROR for that case.  VACUUM (FULL, ANALYZE) does make use of it, so
                                323                 :                :      * we'll permit that.
                                324                 :                :      */
  373 drowley@postgresql.o      325   [ +  +  +  + ]:           5119 :     if (ring_size != -1 && (params.options & VACOPT_FULL) &&
                                326         [ +  - ]:              3 :         !(params.options & VACOPT_ANALYZE))
                                327         [ +  - ]:              3 :         ereport(ERROR,
                                328                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                329                 :                :                  errmsg("BUFFER_USAGE_LIMIT cannot be specified for VACUUM FULL")));
                                330                 :                : 
                                331                 :                :     /*
                                332                 :                :      * Make sure VACOPT_ANALYZE is specified if any column lists are present.
                                333                 :                :      */
 1854 rhaas@postgresql.org      334         [ +  + ]:           5116 :     if (!(params.options & VACOPT_ANALYZE))
                                335                 :                :     {
 2385 tgl@sss.pgh.pa.us         336   [ +  +  +  +  :           4372 :         foreach(lc, vacstmt->rels)
                                              +  + ]
                                337                 :                :         {
                                338                 :           2147 :             VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
                                339                 :                : 
                                340         [ +  + ]:           2147 :             if (vrel->va_cols != NIL)
                                341         [ +  - ]:              3 :                 ereport(ERROR,
                                342                 :                :                         (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                343                 :                :                          errmsg("ANALYZE option must be specified when a column list is provided")));
                                344                 :                :         }
                                345                 :                :     }
                                346                 :                : 
                                347                 :                : 
                                348                 :                :     /*
                                349                 :                :      * Sanity check DISABLE_PAGE_SKIPPING option.
                                350                 :                :      */
  374 drowley@postgresql.o      351         [ +  + ]:           5113 :     if ((params.options & VACOPT_FULL) != 0 &&
                                352         [ -  + ]:            166 :         (params.options & VACOPT_DISABLE_PAGE_SKIPPING) != 0)
  374 drowley@postgresql.o      353         [ #  # ]:UBC           0 :         ereport(ERROR,
                                354                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                355                 :                :                  errmsg("VACUUM option DISABLE_PAGE_SKIPPING cannot be used with FULL")));
                                356                 :                : 
                                357                 :                :     /* sanity check for PROCESS_TOAST */
  374 drowley@postgresql.o      358         [ +  + ]:CBC        5113 :     if ((params.options & VACOPT_FULL) != 0 &&
                                359         [ +  + ]:            166 :         (params.options & VACOPT_PROCESS_TOAST) == 0)
                                360         [ +  - ]:              3 :         ereport(ERROR,
                                361                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                362                 :                :                  errmsg("PROCESS_TOAST required with VACUUM FULL")));
                                363                 :                : 
                                364                 :                :     /* sanity check for ONLY_DATABASE_STATS */
                                365         [ +  + ]:           5110 :     if (params.options & VACOPT_ONLY_DATABASE_STATS)
                                366                 :                :     {
                                367         [ -  + ]:             58 :         Assert(params.options & VACOPT_VACUUM);
                                368         [ +  + ]:             58 :         if (vacstmt->rels != NIL)
                                369         [ +  - ]:              3 :             ereport(ERROR,
                                370                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                371                 :                :                      errmsg("ONLY_DATABASE_STATS cannot be specified with a list of tables")));
                                372                 :                :         /* don't require people to turn off PROCESS_TOAST/MAIN explicitly */
                                373         [ -  + ]:             55 :         if (params.options & ~(VACOPT_VACUUM |
                                374                 :                :                                VACOPT_VERBOSE |
                                375                 :                :                                VACOPT_PROCESS_MAIN |
                                376                 :                :                                VACOPT_PROCESS_TOAST |
                                377                 :                :                                VACOPT_ONLY_DATABASE_STATS))
  374 drowley@postgresql.o      378         [ #  # ]:UBC           0 :             ereport(ERROR,
                                379                 :                :                     (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                380                 :                :                      errmsg("ONLY_DATABASE_STATS cannot be specified with other VACUUM options")));
                                381                 :                :     }
                                382                 :                : 
                                383                 :                :     /*
                                384                 :                :      * All freeze ages are zero if the FREEZE option is given; otherwise pass
                                385                 :                :      * them as -1 which means to use the default values.
                                386                 :                :      */
 1854 rhaas@postgresql.org      387         [ +  + ]:CBC        5107 :     if (params.options & VACOPT_FREEZE)
                                388                 :                :     {
 3315 alvherre@alvh.no-ip.      389                 :            573 :         params.freeze_min_age = 0;
                                390                 :            573 :         params.freeze_table_age = 0;
                                391                 :            573 :         params.multixact_freeze_min_age = 0;
                                392                 :            573 :         params.multixact_freeze_table_age = 0;
                                393                 :                :     }
                                394                 :                :     else
                                395                 :                :     {
                                396                 :           4534 :         params.freeze_min_age = -1;
                                397                 :           4534 :         params.freeze_table_age = -1;
                                398                 :           4534 :         params.multixact_freeze_min_age = -1;
                                399                 :           4534 :         params.multixact_freeze_table_age = -1;
                                400                 :                :     }
                                401                 :                : 
                                402                 :                :     /* user-invoked vacuum is never "for wraparound" */
                                403                 :           5107 :     params.is_wraparound = false;
                                404                 :                : 
                                405                 :                :     /* user-invoked vacuum uses VACOPT_VERBOSE instead of log_min_duration */
 3299                           406                 :           5107 :     params.log_min_duration = -1;
                                407                 :                : 
                                408                 :                :     /*
                                409                 :                :      * Create special memory context for cross-transaction storage.
                                410                 :                :      *
                                411                 :                :      * Since it is a child of PortalContext, it will go away eventually even
                                412                 :                :      * if we suffer an error; there's no need for special abort cleanup logic.
                                413                 :                :      */
  374 drowley@postgresql.o      414                 :           5107 :     vac_context = AllocSetContextCreate(PortalContext,
                                415                 :                :                                         "Vacuum",
                                416                 :                :                                         ALLOCSET_DEFAULT_SIZES);
                                417                 :                : 
                                418                 :                :     /*
                                419                 :                :      * Make a buffer strategy object in the cross-transaction memory context.
                                420                 :                :      * We needn't bother making this for VACUUM (FULL) or VACUUM
                                421                 :                :      * (ONLY_DATABASE_STATS) as they'll not make use of it.  VACUUM (FULL,
                                422                 :                :      * ANALYZE) is possible, so we'd better ensure that we make a strategy
                                423                 :                :      * when we see ANALYZE.
                                424                 :                :      */
                                425         [ +  + ]:           5107 :     if ((params.options & (VACOPT_ONLY_DATABASE_STATS |
                                426                 :            218 :                            VACOPT_FULL)) == 0 ||
                                427         [ +  + ]:            218 :         (params.options & VACOPT_ANALYZE) != 0)
                                428                 :                :     {
                                429                 :                : 
                                430                 :           4892 :         MemoryContext old_context = MemoryContextSwitchTo(vac_context);
                                431                 :                : 
  373                           432         [ -  + ]:           4892 :         Assert(ring_size >= -1);
                                433                 :                : 
                                434                 :                :         /*
                                435                 :                :          * If BUFFER_USAGE_LIMIT was specified by the VACUUM or ANALYZE
                                436                 :                :          * command, it overrides the value of VacuumBufferUsageLimit.  Either
                                437                 :                :          * value may be 0, in which case GetAccessStrategyWithSize() will
                                438                 :                :          * return NULL, effectively allowing full use of shared buffers.
                                439                 :                :          */
                                440         [ +  + ]:           4892 :         if (ring_size == -1)
                                441                 :           4877 :             ring_size = VacuumBufferUsageLimit;
                                442                 :                : 
                                443                 :           4892 :         bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, ring_size);
                                444                 :                : 
  374                           445                 :           4892 :         MemoryContextSwitchTo(old_context);
                                446                 :                :     }
                                447                 :                : 
                                448                 :                :     /* Now go through the common routine */
                                449                 :           5107 :     vacuum(vacstmt->rels, &params, bstrategy, vac_context, isTopLevel);
                                450                 :                : 
                                451                 :                :     /* Finally, clean up the vacuum memory context */
                                452                 :           5045 :     MemoryContextDelete(vac_context);
 3315 alvherre@alvh.no-ip.      453                 :           5045 : }
                                454                 :                : 
                                455                 :                : /*
                                456                 :                :  * Internal entry point for autovacuum and the VACUUM / ANALYZE commands.
                                457                 :                :  *
                                458                 :                :  * relations, if not NIL, is a list of VacuumRelation to process; otherwise,
                                459                 :                :  * we process all relevant tables in the database.  For each VacuumRelation,
                                460                 :                :  * if a valid OID is supplied, the table with that OID is what to process;
                                461                 :                :  * otherwise, the VacuumRelation's RangeVar indicates what to process.
                                462                 :                :  *
                                463                 :                :  * params contains a set of parameters that can be used to customize the
                                464                 :                :  * behavior.
                                465                 :                :  *
                                466                 :                :  * bstrategy may be passed in as NULL when the caller does not want to
                                467                 :                :  * restrict the number of shared_buffers that VACUUM / ANALYZE can use,
                                468                 :                :  * otherwise, the caller must build a BufferAccessStrategy with the number of
                                469                 :                :  * shared_buffers that VACUUM / ANALYZE should try to limit themselves to
                                470                 :                :  * using.
                                471                 :                :  *
                                472                 :                :  * isTopLevel should be passed down from ProcessUtility.
                                473                 :                :  *
                                474                 :                :  * It is the caller's responsibility that all parameters are allocated in a
                                475                 :                :  * memory context that will not disappear at transaction commit.
                                476                 :                :  */
                                477                 :                : void
  374 drowley@postgresql.o      478                 :          79515 : vacuum(List *relations, VacuumParams *params, BufferAccessStrategy bstrategy,
                                479                 :                :        MemoryContext vac_context, bool isTopLevel)
                                480                 :                : {
                                481                 :                :     static bool in_vacuum = false;
                                482                 :                : 
                                483                 :                :     const char *stmttype;
                                484                 :                :     volatile bool in_outer_xact,
                                485                 :                :                 use_own_xacts;
                                486                 :                : 
 3315 alvherre@alvh.no-ip.      487         [ -  + ]:          79515 :     Assert(params != NULL);
                                488                 :                : 
 1854 rhaas@postgresql.org      489         [ +  + ]:          79515 :     stmttype = (params->options & VACOPT_VACUUM) ? "VACUUM" : "ANALYZE";
                                490                 :                : 
                                491                 :                :     /*
                                492                 :                :      * We cannot run VACUUM inside a user transaction block; if we were inside
                                493                 :                :      * a transaction, then our commit- and start-transaction-command calls
                                494                 :                :      * would not have the intended effect!  There are numerous other subtle
                                495                 :                :      * dependencies on this, too.
                                496                 :                :      *
                                497                 :                :      * ANALYZE (without VACUUM) can run either way.
                                498                 :                :      */
                                499         [ +  + ]:          79515 :     if (params->options & VACOPT_VACUUM)
                                500                 :                :     {
 2249 peter_e@gmx.net           501                 :          77184 :         PreventInTransactionBlock(isTopLevel, stmttype);
 7267 tgl@sss.pgh.pa.us         502                 :          77178 :         in_outer_xact = false;
                                503                 :                :     }
                                504                 :                :     else
 2249 peter_e@gmx.net           505                 :           2331 :         in_outer_xact = IsInTransactionBlock(isTopLevel);
                                506                 :                : 
                                507                 :                :     /*
                                508                 :                :      * Check for and disallow recursive calls.  This could happen when VACUUM
                                509                 :                :      * FULL or ANALYZE calls a hostile index expression that itself calls
                                510                 :                :      * ANALYZE.
                                511                 :                :      */
 3385 noah@leadboat.com         512         [ +  + ]:          79509 :     if (in_vacuum)
 3178 tgl@sss.pgh.pa.us         513         [ +  - ]:              6 :         ereport(ERROR,
                                514                 :                :                 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                515                 :                :                  errmsg("%s cannot be executed from VACUUM or ANALYZE",
                                516                 :                :                         stmttype)));
                                517                 :                : 
                                518                 :                :     /*
                                519                 :                :      * Build list of relation(s) to process, putting any new data in
                                520                 :                :      * vac_context for safekeeping.
                                521                 :                :      */
  464                           522         [ +  + ]:          79503 :     if (params->options & VACOPT_ONLY_DATABASE_STATS)
                                523                 :                :     {
                                524                 :                :         /* We don't process any tables in this case */
                                525         [ -  + ]:             55 :         Assert(relations == NIL);
                                526                 :                :     }
                                527         [ +  + ]:          79448 :     else if (relations != NIL)
                                528                 :                :     {
 2385                           529                 :          79365 :         List       *newrels = NIL;
                                530                 :                :         ListCell   *lc;
                                531                 :                : 
                                532   [ +  -  +  +  :         158774 :         foreach(lc, relations)
                                              +  + ]
                                533                 :                :         {
                                534                 :          79427 :             VacuumRelation *vrel = lfirst_node(VacuumRelation, lc);
                                535                 :                :             List       *sublist;
                                536                 :                :             MemoryContext old_context;
                                537                 :                : 
  377 drowley@postgresql.o      538                 :          79427 :             sublist = expand_vacuum_rel(vrel, vac_context, params->options);
 2385 tgl@sss.pgh.pa.us         539                 :          79409 :             old_context = MemoryContextSwitchTo(vac_context);
                                540                 :          79409 :             newrels = list_concat(newrels, sublist);
                                541                 :          79409 :             MemoryContextSwitchTo(old_context);
                                542                 :                :         }
                                543                 :          79347 :         relations = newrels;
                                544                 :                :     }
                                545                 :                :     else
  377 drowley@postgresql.o      546                 :             83 :         relations = get_all_vacuum_rels(vac_context, params->options);
                                547                 :                : 
                                548                 :                :     /*
                                549                 :                :      * Decide whether we need to start/commit our own transactions.
                                550                 :                :      *
                                551                 :                :      * For VACUUM (with or without ANALYZE): always do so, so that we can
                                552                 :                :      * release locks as soon as possible.  (We could possibly use the outer
                                553                 :                :      * transaction for a one-table VACUUM, but handling TOAST tables would be
                                554                 :                :      * problematic.)
                                555                 :                :      *
                                556                 :                :      * For ANALYZE (no VACUUM): if inside a transaction block, we cannot
                                557                 :                :      * start/commit our own transactions.  Also, there's no need to do so if
                                558                 :                :      * only processing one relation.  For multiple relations when not within a
                                559                 :                :      * transaction block, and also in an autovacuum worker, use own
                                560                 :                :      * transactions so we can release locks sooner.
                                561                 :                :      */
 1854 rhaas@postgresql.org      562         [ +  + ]:          79485 :     if (params->options & VACOPT_VACUUM)
 7267 tgl@sss.pgh.pa.us         563                 :          77172 :         use_own_xacts = true;
                                564                 :                :     else
                                565                 :                :     {
 1854 rhaas@postgresql.org      566         [ -  + ]:           2313 :         Assert(params->options & VACOPT_ANALYZE);
   41 heikki.linnakangas@i      567         [ +  + ]:GNC        2313 :         if (AmAutoVacuumWorkerProcess())
 6149 alvherre@alvh.no-ip.      568                 :CBC         128 :             use_own_xacts = true;
                                569         [ +  + ]:           2185 :         else if (in_outer_xact)
 7267 tgl@sss.pgh.pa.us         570                 :            107 :             use_own_xacts = false;
 7263 neilc@samurai.com         571         [ +  + ]:           2078 :         else if (list_length(relations) > 1)
 7267 tgl@sss.pgh.pa.us         572                 :            312 :             use_own_xacts = true;
                                573                 :                :         else
                                574                 :           1766 :             use_own_xacts = false;
                                575                 :                :     }
                                576                 :                : 
                                577                 :                :     /*
                                578                 :                :      * vacuum_rel expects to be entered with no transaction active; it will
                                579                 :                :      * start and commit its own transaction.  But we are called by an SQL
                                580                 :                :      * command, and so we are executing inside a transaction already. We
                                581                 :                :      * commit the transaction started in PostgresMain() here, and start
                                582                 :                :      * another one before exiting to match the commit waiting for us back in
                                583                 :                :      * PostgresMain().
                                584                 :                :      */
                                585         [ +  + ]:          79485 :     if (use_own_xacts)
                                586                 :                :     {
 3454                           587         [ -  + ]:          77612 :         Assert(!in_outer_xact);
                                588                 :                : 
                                589                 :                :         /* ActiveSnapshot is not set by autovacuum */
 5816 alvherre@alvh.no-ip.      590         [ +  + ]:          77612 :         if (ActiveSnapshotSet())
                                591                 :           3204 :             PopActiveSnapshot();
                                592                 :                : 
                                593                 :                :         /* matches the StartTransaction in PostgresMain() */
 7641 tgl@sss.pgh.pa.us         594                 :          77612 :         CommitTransactionCommand();
                                595                 :                :     }
                                596                 :                : 
                                597                 :                :     /* Turn vacuum cost accounting on or off, and set/clear in_vacuum */
 7197                           598         [ +  + ]:          79485 :     PG_TRY();
                                599                 :                :     {
                                600                 :                :         ListCell   *cur;
                                601                 :                : 
 3385 noah@leadboat.com         602                 :          79485 :         in_vacuum = true;
  373 dgustafsson@postgres      603                 :          79485 :         VacuumFailsafeActive = false;
                                604                 :          79485 :         VacuumUpdateCosts();
 7197 tgl@sss.pgh.pa.us         605                 :          79485 :         VacuumCostBalance = 0;
 4524 alvherre@alvh.no-ip.      606                 :          79485 :         VacuumPageHit = 0;
                                607                 :          79485 :         VacuumPageMiss = 0;
                                608                 :          79485 :         VacuumPageDirty = 0;
 1546 akapila@postgresql.o      609                 :          79485 :         VacuumCostBalanceLocal = 0;
                                610                 :          79485 :         VacuumSharedCostBalance = NULL;
                                611                 :          79485 :         VacuumActiveNWorkers = NULL;
                                612                 :                : 
                                613                 :                :         /*
                                614                 :                :          * Loop to process each selected relation.
                                615                 :                :          */
 7197 tgl@sss.pgh.pa.us         616   [ +  +  +  +  :         165717 :         foreach(cur, relations)
                                              +  + ]
                                617                 :                :         {
 2385                           618                 :          86264 :             VacuumRelation *vrel = lfirst_node(VacuumRelation, cur);
                                619                 :                : 
 1854 rhaas@postgresql.org      620         [ +  + ]:          86264 :             if (params->options & VACOPT_VACUUM)
                                621                 :                :             {
  299 nathan@postgresql.or      622         [ +  + ]:          80700 :                 if (!vacuum_rel(vrel->oid, vrel->relation, params, bstrategy))
 4815 rhaas@postgresql.org      623                 :             49 :                     continue;
                                624                 :                :             }
                                625                 :                : 
 1854                           626         [ +  + ]:          86212 :             if (params->options & VACOPT_ANALYZE)
                                627                 :                :             {
                                628                 :                :                 /*
                                629                 :                :                  * If using separate xacts, start one for analyze. Otherwise,
                                630                 :                :                  * we can use the outer transaction.
                                631                 :                :                  */
 7197 tgl@sss.pgh.pa.us         632         [ +  + ]:           6397 :                 if (use_own_xacts)
                                633                 :                :                 {
                                634                 :           4555 :                     StartTransactionCommand();
                                635                 :                :                     /* functions in indexes may want a snapshot set */
 5816 alvherre@alvh.no-ip.      636                 :           4555 :                     PushActiveSnapshot(GetTransactionSnapshot());
                                637                 :                :                 }
                                638                 :                : 
 1854 rhaas@postgresql.org      639                 :           6397 :                 analyze_rel(vrel->oid, vrel->relation, params,
                                640                 :                :                             vrel->va_cols, in_outer_xact, bstrategy);
                                641                 :                : 
 7197 tgl@sss.pgh.pa.us         642         [ +  + ]:           6368 :                 if (use_own_xacts)
                                643                 :                :                 {
 5816 alvherre@alvh.no-ip.      644                 :           4536 :                     PopActiveSnapshot();
 7197 tgl@sss.pgh.pa.us         645                 :           4536 :                     CommitTransactionCommand();
                                646                 :                :                 }
                                647                 :                :                 else
                                648                 :                :                 {
                                649                 :                :                     /*
                                650                 :                :                      * If we're not using separate xacts, better separate the
                                651                 :                :                      * ANALYZE actions with CCIs.  This avoids trouble if user
                                652                 :                :                      * says "ANALYZE t, t".
                                653                 :                :                      */
 1709                           654                 :           1832 :                     CommandCounterIncrement();
                                655                 :                :                 }
                                656                 :                :             }
                                657                 :                : 
                                658                 :                :             /*
                                659                 :                :              * Ensure VacuumFailsafeActive has been reset before vacuuming the
                                660                 :                :              * next relation.
                                661                 :                :              */
  373 dgustafsson@postgres      662                 :          86183 :             VacuumFailsafeActive = false;
                                663                 :                :         }
                                664                 :                :     }
 1626 peter@eisentraut.org      665                 :             32 :     PG_FINALLY();
                                666                 :                :     {
 3385 noah@leadboat.com         667                 :          79485 :         in_vacuum = false;
 7197 tgl@sss.pgh.pa.us         668                 :          79485 :         VacuumCostActive = false;
  373 dgustafsson@postgres      669                 :          79485 :         VacuumFailsafeActive = false;
                                670                 :          79485 :         VacuumCostBalance = 0;
                                671                 :                :     }
 7197 tgl@sss.pgh.pa.us         672         [ +  + ]:          79485 :     PG_END_TRY();
                                673                 :                : 
                                674                 :                :     /*
                                675                 :                :      * Finish up processing.
                                676                 :                :      */
 7267                           677         [ +  + ]:          79453 :     if (use_own_xacts)
                                678                 :                :     {
                                679                 :                :         /* here, we are not in a transaction */
                                680                 :                : 
                                681                 :                :         /*
                                682                 :                :          * This matches the CommitTransaction waiting for us in
                                683                 :                :          * PostgresMain().
                                684                 :                :          */
 7641                           685                 :          77590 :         StartTransactionCommand();
                                686                 :                :     }
                                687                 :                : 
  464                           688         [ +  + ]:          79453 :     if ((params->options & VACOPT_VACUUM) &&
                                689         [ +  + ]:          77156 :         !(params->options & VACOPT_SKIP_DATABASE_STATS))
                                690                 :                :     {
                                691                 :                :         /*
                                692                 :                :          * Update pg_database.datfrozenxid, and truncate pg_xact if possible.
                                693                 :                :          */
 6370                           694                 :            746 :         vac_update_datfrozenxid();
                                695                 :                :     }
                                696                 :                : 
10141 scrappy@hub.org           697                 :          79453 : }
                                698                 :                : 
                                699                 :                : /*
                                700                 :                :  * Check if the current user has privileges to vacuum or analyze the relation.
                                701                 :                :  * If not, issue a WARNING log message and return false to let the caller
                                702                 :                :  * decide what to do with this relation.  This routine is used to decide if a
                                703                 :                :  * relation can be processed for VACUUM or ANALYZE.
                                704                 :                :  */
                                705                 :                : bool
   32 nathan@postgresql.or      706                 :GNC      101532 : vacuum_is_permitted_for_relation(Oid relid, Form_pg_class reltuple,
                                707                 :                :                                  bits32 options)
                                708                 :                : {
                                709                 :                :     char       *relname;
                                710                 :                : 
 2057 michael@paquier.xyz       711         [ -  + ]:CBC      101532 :     Assert((options & (VACOPT_VACUUM | VACOPT_ANALYZE)) != 0);
                                712                 :                : 
                                713                 :                :     /*----------
                                714                 :                :      * A role has privileges to vacuum or analyze the relation if any of the
                                715                 :                :      * following are true:
                                716                 :                :      *   - the role owns the current database and the relation is not shared
                                717                 :                :      *   - the role has the MAINTAIN privilege on the relation
                                718                 :                :      *----------
                                719                 :                :      */
   32 nathan@postgresql.or      720         [ +  + ]:GNC      101532 :     if ((object_ownercheck(DatabaseRelationId, MyDatabaseId, GetUserId()) &&
                                721   [ +  +  +  + ]:         118785 :          !reltuple->relisshared) ||
                                722                 :          17640 :         pg_class_aclcheck(relid, GetUserId(), ACL_MAINTAIN) == ACLCHECK_OK)
 2057 michael@paquier.xyz       723                 :CBC      101362 :         return true;
                                724                 :                : 
                                725                 :            170 :     relname = NameStr(reltuple->relname);
                                726                 :                : 
                                727         [ +  + ]:            170 :     if ((options & VACOPT_VACUUM) != 0)
                                728                 :                :     {
  508 andrew@dunslane.net       729         [ +  - ]:            112 :         ereport(WARNING,
                                730                 :                :                 (errmsg("permission denied to vacuum \"%s\", skipping it",
                                731                 :                :                         relname)));
                                732                 :                : 
                                733                 :                :         /*
                                734                 :                :          * For VACUUM ANALYZE, both logs could show up, but just generate
                                735                 :                :          * information for VACUUM as that would be the first one to be
                                736                 :                :          * processed.
                                737                 :                :          */
 2057 michael@paquier.xyz       738                 :            112 :         return false;
                                739                 :                :     }
                                740                 :                : 
                                741         [ +  - ]:             58 :     if ((options & VACOPT_ANALYZE) != 0)
  508 andrew@dunslane.net       742         [ +  - ]:             58 :         ereport(WARNING,
                                743                 :                :                 (errmsg("permission denied to analyze \"%s\", skipping it",
                                744                 :                :                         relname)));
                                745                 :                : 
 2057 michael@paquier.xyz       746                 :             58 :     return false;
                                747                 :                : }
                                748                 :                : 
                                749                 :                : 
                                750                 :                : /*
                                751                 :                :  * vacuum_open_relation
                                752                 :                :  *
                                753                 :                :  * This routine is used for attempting to open and lock a relation which
                                754                 :                :  * is going to be vacuumed or analyzed.  If the relation cannot be opened
                                755                 :                :  * or locked, a log is emitted if possible.
                                756                 :                :  */
                                757                 :                : Relation
 1182                           758                 :          90467 : vacuum_open_relation(Oid relid, RangeVar *relation, bits32 options,
                                759                 :                :                      bool verbose, LOCKMODE lmode)
                                760                 :                : {
                                761                 :                :     Relation    rel;
 2021                           762                 :          90467 :     bool        rel_lock = true;
                                763                 :                :     int         elevel;
                                764                 :                : 
                                765         [ -  + ]:          90467 :     Assert((options & (VACOPT_VACUUM | VACOPT_ANALYZE)) != 0);
                                766                 :                : 
                                767                 :                :     /*
                                768                 :                :      * Open the relation and get the appropriate lock on it.
                                769                 :                :      *
                                770                 :                :      * There's a race condition here: the relation may have gone away since
                                771                 :                :      * the last time we saw it.  If so, we don't need to vacuum or analyze it.
                                772                 :                :      *
                                773                 :                :      * If we've been asked not to wait for the relation lock, acquire it first
                                774                 :                :      * in non-blocking mode, before calling try_relation_open().
                                775                 :                :      */
                                776         [ +  + ]:          90467 :     if (!(options & VACOPT_SKIP_LOCKED))
 1105 pg@bowt.ie                777                 :          89814 :         rel = try_relation_open(relid, lmode);
 2021 michael@paquier.xyz       778         [ +  + ]:            653 :     else if (ConditionalLockRelationOid(relid, lmode))
 1105 pg@bowt.ie                779                 :            643 :         rel = try_relation_open(relid, NoLock);
                                780                 :                :     else
                                781                 :                :     {
                                782                 :             10 :         rel = NULL;
 2021 michael@paquier.xyz       783                 :             10 :         rel_lock = false;
                                784                 :                :     }
                                785                 :                : 
                                786                 :                :     /* if relation is opened, leave */
 1105 pg@bowt.ie                787         [ +  + ]:          90467 :     if (rel)
                                788                 :          90451 :         return rel;
                                789                 :                : 
                                790                 :                :     /*
                                791                 :                :      * Relation could not be opened, hence generate if possible a log
                                792                 :                :      * informing on the situation.
                                793                 :                :      *
                                794                 :                :      * If the RangeVar is not defined, we do not have enough information to
                                795                 :                :      * provide a meaningful log statement.  Chances are that the caller has
                                796                 :                :      * intentionally not provided this information so that this logging is
                                797                 :                :      * skipped, anyway.
                                798                 :                :      */
 2021 michael@paquier.xyz       799         [ +  + ]:             16 :     if (relation == NULL)
                                800                 :              9 :         return NULL;
                                801                 :                : 
                                802                 :                :     /*
                                803                 :                :      * Determine the log level.
                                804                 :                :      *
                                805                 :                :      * For manual VACUUM or ANALYZE, we emit a WARNING to match the log
                                806                 :                :      * statements in the permission checks; otherwise, only log if the caller
                                807                 :                :      * so requested.
                                808                 :                :      */
   41 heikki.linnakangas@i      809         [ +  - ]:GNC           7 :     if (!AmAutoVacuumWorkerProcess())
 2021 michael@paquier.xyz       810                 :CBC           7 :         elevel = WARNING;
 1854 rhaas@postgresql.org      811         [ #  # ]:LBC         (1) :     else if (verbose)
 2021 michael@paquier.xyz       812                 :            (1) :         elevel = LOG;
                                813                 :                :     else
 2021 michael@paquier.xyz       814                 :UBC           0 :         return NULL;
                                815                 :                : 
 2021 michael@paquier.xyz       816         [ +  + ]:CBC           7 :     if ((options & VACOPT_VACUUM) != 0)
                                817                 :                :     {
                                818         [ +  + ]:              5 :         if (!rel_lock)
                                819         [ +  - ]:              3 :             ereport(elevel,
                                820                 :                :                     (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                                821                 :                :                      errmsg("skipping vacuum of \"%s\" --- lock not available",
                                822                 :                :                             relation->relname)));
                                823                 :                :         else
                                824         [ +  - ]:              2 :             ereport(elevel,
                                825                 :                :                     (errcode(ERRCODE_UNDEFINED_TABLE),
                                826                 :                :                      errmsg("skipping vacuum of \"%s\" --- relation no longer exists",
                                827                 :                :                             relation->relname)));
                                828                 :                : 
                                829                 :                :         /*
                                830                 :                :          * For VACUUM ANALYZE, both logs could show up, but just generate
                                831                 :                :          * information for VACUUM as that would be the first one to be
                                832                 :                :          * processed.
                                833                 :                :          */
                                834                 :              5 :         return NULL;
                                835                 :                :     }
                                836                 :                : 
                                837         [ +  - ]:              2 :     if ((options & VACOPT_ANALYZE) != 0)
                                838                 :                :     {
                                839         [ +  + ]:              2 :         if (!rel_lock)
                                840         [ +  - ]:              1 :             ereport(elevel,
                                841                 :                :                     (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                                842                 :                :                      errmsg("skipping analyze of \"%s\" --- lock not available",
                                843                 :                :                             relation->relname)));
                                844                 :                :         else
                                845         [ +  - ]:              1 :             ereport(elevel,
                                846                 :                :                     (errcode(ERRCODE_UNDEFINED_TABLE),
                                847                 :                :                      errmsg("skipping analyze of \"%s\" --- relation no longer exists",
                                848                 :                :                             relation->relname)));
                                849                 :                :     }
                                850                 :                : 
                                851                 :              2 :     return NULL;
                                852                 :                : }
                                853                 :                : 
                                854                 :                : 
                                855                 :                : /*
                                856                 :                :  * Given a VacuumRelation, fill in the table OID if it wasn't specified,
                                857                 :                :  * and optionally add VacuumRelations for partitions of the table.
                                858                 :                :  *
                                859                 :                :  * If a VacuumRelation does not have an OID supplied and is a partitioned
                                860                 :                :  * table, an extra entry will be added to the output for each partition.
                                861                 :                :  * Presently, only autovacuum supplies OIDs when calling vacuum(), and
                                862                 :                :  * it does not want us to expand partitioned tables.
                                863                 :                :  *
                                864                 :                :  * We take care not to modify the input data structure, but instead build
                                865                 :                :  * new VacuumRelation(s) to return.  (But note that they will reference
                                866                 :                :  * unmodified parts of the input, eg column lists.)  New data structures
                                867                 :                :  * are made in vac_context.
                                868                 :                :  */
                                869                 :                : static List *
  377 drowley@postgresql.o      870                 :          79427 : expand_vacuum_rel(VacuumRelation *vrel, MemoryContext vac_context,
                                871                 :                :                   int options)
                                872                 :                : {
 2385 tgl@sss.pgh.pa.us         873                 :          79427 :     List       *vacrels = NIL;
                                874                 :                :     MemoryContext oldcontext;
                                875                 :                : 
                                876                 :                :     /* If caller supplied OID, there's nothing we need do here. */
                                877         [ +  + ]:          79427 :     if (OidIsValid(vrel->oid))
                                878                 :                :     {
 5792 alvherre@alvh.no-ip.      879                 :          74408 :         oldcontext = MemoryContextSwitchTo(vac_context);
 2385 tgl@sss.pgh.pa.us         880                 :          74408 :         vacrels = lappend(vacrels, vrel);
 5792 alvherre@alvh.no-ip.      881                 :          74408 :         MemoryContextSwitchTo(oldcontext);
                                882                 :                :     }
                                883                 :                :     else
                                884                 :                :     {
                                885                 :                :         /* Process a specific relation, and possibly partitions thereof */
                                886                 :                :         Oid         relid;
                                887                 :                :         HeapTuple   tuple;
                                888                 :                :         Form_pg_class classForm;
                                889                 :                :         bool        include_parts;
                                890                 :                :         int         rvr_opts;
                                891                 :                : 
                                892                 :                :         /*
                                893                 :                :          * Since autovacuum workers supply OIDs when calling vacuum(), no
                                894                 :                :          * autovacuum worker should reach this code.
                                895                 :                :          */
   41 heikki.linnakangas@i      896         [ -  + ]:GNC        5019 :         Assert(!AmAutoVacuumWorkerProcess());
                                897                 :                : 
                                898                 :                :         /*
                                899                 :                :          * We transiently take AccessShareLock to protect the syscache lookup
                                900                 :                :          * below, as well as find_all_inheritors's expectation that the caller
                                901                 :                :          * holds some lock on the starting relation.
                                902                 :                :          */
 2019 michael@paquier.xyz       903                 :CBC        5019 :         rvr_opts = (options & VACOPT_SKIP_LOCKED) ? RVR_SKIP_LOCKED : 0;
                                904                 :           5019 :         relid = RangeVarGetRelidExtended(vrel->relation,
                                905                 :                :                                          AccessShareLock,
                                906                 :                :                                          rvr_opts,
                                907                 :                :                                          NULL, NULL);
                                908                 :                : 
                                909                 :                :         /*
                                910                 :                :          * If the lock is unavailable, emit the same log statement that
                                911                 :                :          * vacuum_rel() and analyze_rel() would.
                                912                 :                :          */
                                913         [ +  + ]:           5001 :         if (!OidIsValid(relid))
                                914                 :                :         {
                                915         [ +  + ]:              4 :             if (options & VACOPT_VACUUM)
                                916         [ +  - ]:              3 :                 ereport(WARNING,
                                917                 :                :                         (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                                918                 :                :                          errmsg("skipping vacuum of \"%s\" --- lock not available",
                                919                 :                :                                 vrel->relation->relname)));
                                920                 :                :             else
                                921         [ +  - ]:              1 :                 ereport(WARNING,
                                922                 :                :                         (errcode(ERRCODE_LOCK_NOT_AVAILABLE),
                                923                 :                :                          errmsg("skipping analyze of \"%s\" --- lock not available",
                                924                 :                :                                 vrel->relation->relname)));
                                925                 :              4 :             return vacrels;
                                926                 :                :         }
                                927                 :                : 
                                928                 :                :         /*
                                929                 :                :          * To check whether the relation is a partitioned table and its
                                930                 :                :          * ownership, fetch its syscache entry.
                                931                 :                :          */
 2600 rhaas@postgresql.org      932                 :           4997 :         tuple = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
                                933         [ -  + ]:           4997 :         if (!HeapTupleIsValid(tuple))
 2600 rhaas@postgresql.org      934         [ #  # ]:UBC           0 :             elog(ERROR, "cache lookup failed for relation %u", relid);
 2600 rhaas@postgresql.org      935                 :CBC        4997 :         classForm = (Form_pg_class) GETSTRUCT(tuple);
                                936                 :                : 
                                937                 :                :         /*
                                938                 :                :          * Make a returnable VacuumRelation for this rel if the user has the
                                939                 :                :          * required privileges.
                                940                 :                :          */
   32 nathan@postgresql.or      941         [ +  + ]:GNC        4997 :         if (vacuum_is_permitted_for_relation(relid, classForm, options))
                                942                 :                :         {
 2057 michael@paquier.xyz       943                 :CBC        4881 :             oldcontext = MemoryContextSwitchTo(vac_context);
                                944                 :           4881 :             vacrels = lappend(vacrels, makeVacuumRelation(vrel->relation,
                                945                 :                :                                                           relid,
                                946                 :                :                                                           vrel->va_cols));
                                947                 :           4881 :             MemoryContextSwitchTo(oldcontext);
                                948                 :                :         }
                                949                 :                : 
                                950                 :                : 
 2600 rhaas@postgresql.org      951                 :           4997 :         include_parts = (classForm->relkind == RELKIND_PARTITIONED_TABLE);
                                952                 :           4997 :         ReleaseSysCache(tuple);
                                953                 :                : 
                                954                 :                :         /*
                                955                 :                :          * If it is, make relation list entries for its partitions.  Note that
                                956                 :                :          * the list returned by find_all_inheritors() includes the passed-in
                                957                 :                :          * OID, so we have to skip that.  There's no point in taking locks on
                                958                 :                :          * the individual partitions yet, and doing so would just add
                                959                 :                :          * unnecessary deadlock risk.  For this last reason we do not check
                                960                 :                :          * yet the ownership of the partitions, which get added to the list to
                                961                 :                :          * process.  Ownership will be checked later on anyway.
                                962                 :                :          */
                                963         [ +  + ]:           4997 :         if (include_parts)
                                964                 :                :         {
 2385 tgl@sss.pgh.pa.us         965                 :            342 :             List       *part_oids = find_all_inheritors(relid, NoLock, NULL);
                                966                 :                :             ListCell   *part_lc;
                                967                 :                : 
                                968   [ +  -  +  +  :           1599 :             foreach(part_lc, part_oids)
                                              +  + ]
                                969                 :                :             {
                                970                 :           1257 :                 Oid         part_oid = lfirst_oid(part_lc);
                                971                 :                : 
                                972         [ +  + ]:           1257 :                 if (part_oid == relid)
                                973                 :            342 :                     continue;   /* ignore original table */
                                974                 :                : 
                                975                 :                :                 /*
                                976                 :                :                  * We omit a RangeVar since it wouldn't be appropriate to
                                977                 :                :                  * complain about failure to open one of these relations
                                978                 :                :                  * later.
                                979                 :                :                  */
                                980                 :            915 :                 oldcontext = MemoryContextSwitchTo(vac_context);
                                981                 :            915 :                 vacrels = lappend(vacrels, makeVacuumRelation(NULL,
                                982                 :                :                                                               part_oid,
                                983                 :                :                                                               vrel->va_cols));
                                984                 :            915 :                 MemoryContextSwitchTo(oldcontext);
                                985                 :                :             }
                                986                 :                :         }
                                987                 :                : 
                                988                 :                :         /*
                                989                 :                :          * Release lock again.  This means that by the time we actually try to
                                990                 :                :          * process the table, it might be gone or renamed.  In the former case
                                991                 :                :          * we'll silently ignore it; in the latter case we'll process it
                                992                 :                :          * anyway, but we must beware that the RangeVar doesn't necessarily
                                993                 :                :          * identify it anymore.  This isn't ideal, perhaps, but there's little
                                994                 :                :          * practical alternative, since we're typically going to commit this
                                995                 :                :          * transaction and begin a new one between now and then.  Moreover,
                                996                 :                :          * holding locks on multiple relations would create significant risk
                                997                 :                :          * of deadlock.
                                998                 :                :          */
 2389                           999                 :           4997 :         UnlockRelationOid(relid, AccessShareLock);
                               1000                 :                :     }
                               1001                 :                : 
 2385                          1002                 :          79405 :     return vacrels;
                               1003                 :                : }
                               1004                 :                : 
                               1005                 :                : /*
                               1006                 :                :  * Construct a list of VacuumRelations for all vacuumable rels in
                               1007                 :                :  * the current database.  The list is built in vac_context.
                               1008                 :                :  */
                               1009                 :                : static List *
  377 drowley@postgresql.o     1010                 :             83 : get_all_vacuum_rels(MemoryContext vac_context, int options)
                               1011                 :                : {
 2385 tgl@sss.pgh.pa.us        1012                 :             83 :     List       *vacrels = NIL;
                               1013                 :                :     Relation    pgclass;
                               1014                 :                :     TableScanDesc scan;
                               1015                 :                :     HeapTuple   tuple;
                               1016                 :                : 
 1910 andres@anarazel.de       1017                 :             83 :     pgclass = table_open(RelationRelationId, AccessShareLock);
                               1018                 :                : 
 1861                          1019                 :             83 :     scan = table_beginscan_catalog(pgclass, 0, NULL);
                               1020                 :                : 
 2385 tgl@sss.pgh.pa.us        1021         [ +  + ]:          35571 :     while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
                               1022                 :                :     {
                               1023                 :          35488 :         Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
                               1024                 :                :         MemoryContext oldcontext;
 1972 andres@anarazel.de       1025                 :          35488 :         Oid         relid = classForm->oid;
                               1026                 :                : 
                               1027                 :                :         /*
                               1028                 :                :          * We include partitioned tables here; depending on which operation is
                               1029                 :                :          * to be performed, caller will decide whether to process or ignore
                               1030                 :                :          * them.
                               1031                 :                :          */
 2385 tgl@sss.pgh.pa.us        1032         [ +  + ]:          35488 :         if (classForm->relkind != RELKIND_RELATION &&
                               1033         [ +  + ]:          29428 :             classForm->relkind != RELKIND_MATVIEW &&
                               1034         [ +  + ]:          29425 :             classForm->relkind != RELKIND_PARTITIONED_TABLE)
                               1035                 :          29404 :             continue;
                               1036                 :                : 
                               1037                 :                :         /* check permissions of relation */
   32 nathan@postgresql.or     1038         [ -  + ]:GNC        6084 :         if (!vacuum_is_permitted_for_relation(relid, classForm, options))
  457 jdavis@postgresql.or     1039                 :UBC           0 :             continue;
                               1040                 :                : 
                               1041                 :                :         /*
                               1042                 :                :          * Build VacuumRelation(s) specifying the table OIDs to be processed.
                               1043                 :                :          * We omit a RangeVar since it wouldn't be appropriate to complain
                               1044                 :                :          * about failure to open one of these relations later.
                               1045                 :                :          */
 2385 tgl@sss.pgh.pa.us        1046                 :CBC        6084 :         oldcontext = MemoryContextSwitchTo(vac_context);
                               1047                 :           6084 :         vacrels = lappend(vacrels, makeVacuumRelation(NULL,
                               1048                 :                :                                                       relid,
                               1049                 :                :                                                       NIL));
                               1050                 :           6084 :         MemoryContextSwitchTo(oldcontext);
                               1051                 :                :     }
                               1052                 :                : 
 1861 andres@anarazel.de       1053                 :             83 :     table_endscan(scan);
 1910                          1054                 :             83 :     table_close(pgclass, AccessShareLock);
                               1055                 :                : 
 2385 tgl@sss.pgh.pa.us        1056                 :             83 :     return vacrels;
                               1057                 :                : }
                               1058                 :                : 
                               1059                 :                : /*
                               1060                 :                :  * vacuum_get_cutoffs() -- compute OldestXmin and freeze cutoff points
                               1061                 :                :  *
                               1062                 :                :  * The target relation and VACUUM parameters are our inputs.
                               1063                 :                :  *
                               1064                 :                :  * Output parameters are the cutoffs that VACUUM caller should use.
                               1065                 :                :  *
                               1066                 :                :  * Return value indicates if vacuumlazy.c caller should make its VACUUM
                               1067                 :                :  * operation aggressive.  An aggressive VACUUM must advance relfrozenxid up to
                               1068                 :                :  * FreezeLimit (at a minimum), and relminmxid up to MultiXactCutoff (at a
                               1069                 :                :  * minimum).
                               1070                 :                :  */
                               1071                 :                : bool
  479 pg@bowt.ie               1072                 :          83963 : vacuum_get_cutoffs(Relation rel, const VacuumParams *params,
                               1073                 :                :                    struct VacuumCutoffs *cutoffs)
                               1074                 :                : {
                               1075                 :                :     int         freeze_min_age,
                               1076                 :                :                 multixact_freeze_min_age,
                               1077                 :                :                 freeze_table_age,
                               1078                 :                :                 multixact_freeze_table_age,
                               1079                 :                :                 effective_multixact_freeze_max_age;
                               1080                 :                :     TransactionId nextXID,
                               1081                 :                :                 safeOldestXmin,
                               1082                 :                :                 aggressiveXIDCutoff;
                               1083                 :                :     MultiXactId nextMXID,
                               1084                 :                :                 safeOldestMxact,
                               1085                 :                :                 aggressiveMXIDCutoff;
                               1086                 :                : 
                               1087                 :                :     /* Use mutable copies of freeze age parameters */
  508                          1088                 :          83963 :     freeze_min_age = params->freeze_min_age;
                               1089                 :          83963 :     multixact_freeze_min_age = params->multixact_freeze_min_age;
                               1090                 :          83963 :     freeze_table_age = params->freeze_table_age;
                               1091                 :          83963 :     multixact_freeze_table_age = params->multixact_freeze_table_age;
                               1092                 :                : 
                               1093                 :                :     /* Set pg_class fields in cutoffs */
  479                          1094                 :          83963 :     cutoffs->relfrozenxid = rel->rd_rel->relfrozenxid;
                               1095                 :          83963 :     cutoffs->relminmxid = rel->rd_rel->relminmxid;
                               1096                 :                : 
                               1097                 :                :     /*
                               1098                 :                :      * Acquire OldestXmin.
                               1099                 :                :      *
                               1100                 :                :      * We can always ignore processes running lazy vacuum.  This is because we
                               1101                 :                :      * use these values only for deciding which tuples we must keep in the
                               1102                 :                :      * tables.  Since lazy vacuum doesn't write its XID anywhere (usually no
                               1103                 :                :      * XID assigned), it's safe to ignore it.  In theory it could be
                               1104                 :                :      * problematic to ignore lazy vacuums in a full vacuum, but keep in mind
                               1105                 :                :      * that only one vacuum process can be working on a particular table at
                               1106                 :                :      * any time, and that each vacuum is always an independent transaction.
                               1107                 :                :      */
                               1108                 :          83963 :     cutoffs->OldestXmin = GetOldestNonRemovableTransactionId(rel);
                               1109                 :                : 
                               1110         [ -  + ]:          83963 :     Assert(TransactionIdIsNormal(cutoffs->OldestXmin));
                               1111                 :                : 
                               1112                 :                :     /* Acquire OldestMxact */
                               1113                 :          83963 :     cutoffs->OldestMxact = GetOldestMultiXactId();
                               1114         [ -  + ]:          83963 :     Assert(MultiXactIdIsValid(cutoffs->OldestMxact));
                               1115                 :                : 
                               1116                 :                :     /* Acquire next XID/next MXID values used to apply age-based settings */
  592                          1117                 :          83963 :     nextXID = ReadNextTransactionId();
                               1118                 :          83963 :     nextMXID = ReadNextMultiXactId();
                               1119                 :                : 
                               1120                 :                :     /*
                               1121                 :                :      * Also compute the multixact age for which freezing is urgent.  This is
                               1122                 :                :      * normally autovacuum_multixact_freeze_max_age, but may be less if we are
                               1123                 :                :      * short of multixact member space.
                               1124                 :                :      */
  479                          1125                 :          83963 :     effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
                               1126                 :                : 
                               1127                 :                :     /*
                               1128                 :                :      * Almost ready to set freeze output parameters; check if OldestXmin or
                               1129                 :                :      * OldestMxact are held back to an unsafe degree before we start on that
                               1130                 :                :      */
                               1131                 :          83963 :     safeOldestXmin = nextXID - autovacuum_freeze_max_age;
                               1132         [ -  + ]:          83963 :     if (!TransactionIdIsNormal(safeOldestXmin))
  479 pg@bowt.ie               1133                 :UBC           0 :         safeOldestXmin = FirstNormalTransactionId;
  479 pg@bowt.ie               1134                 :CBC       83963 :     safeOldestMxact = nextMXID - effective_multixact_freeze_max_age;
                               1135         [ -  + ]:          83963 :     if (safeOldestMxact < FirstMultiXactId)
  479 pg@bowt.ie               1136                 :UBC           0 :         safeOldestMxact = FirstMultiXactId;
  479 pg@bowt.ie               1137         [ +  + ]:CBC       83963 :     if (TransactionIdPrecedes(cutoffs->OldestXmin, safeOldestXmin))
  479 pg@bowt.ie               1138         [ +  - ]:GBC       57496 :         ereport(WARNING,
                               1139                 :                :                 (errmsg("cutoff for removing and freezing tuples is far in the past"),
                               1140                 :                :                  errhint("Close open transactions soon to avoid wraparound problems.\n"
                               1141                 :                :                          "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
  479 pg@bowt.ie               1142         [ -  + ]:CBC       83963 :     if (MultiXactIdPrecedes(cutoffs->OldestMxact, safeOldestMxact))
  479 pg@bowt.ie               1143         [ #  # ]:UBC           0 :         ereport(WARNING,
                               1144                 :                :                 (errmsg("cutoff for freezing multixacts is far in the past"),
                               1145                 :                :                  errhint("Close open transactions soon to avoid wraparound problems.\n"
                               1146                 :                :                          "You might also need to commit or roll back old prepared transactions, or drop stale replication slots.")));
                               1147                 :                : 
                               1148                 :                :     /*
                               1149                 :                :      * Determine the minimum freeze age to use: as specified by the caller, or
                               1150                 :                :      * vacuum_freeze_min_age, but in any case not more than half
                               1151                 :                :      * autovacuum_freeze_max_age, so that autovacuums to prevent XID
                               1152                 :                :      * wraparound won't occur too frequently.
                               1153                 :                :      */
  592 pg@bowt.ie               1154         [ +  + ]:CBC       83963 :     if (freeze_min_age < 0)
                               1155                 :           4263 :         freeze_min_age = vacuum_freeze_min_age;
                               1156                 :          83963 :     freeze_min_age = Min(freeze_min_age, autovacuum_freeze_max_age / 2);
                               1157         [ -  + ]:          83963 :     Assert(freeze_min_age >= 0);
                               1158                 :                : 
                               1159                 :                :     /* Compute FreezeLimit, being careful to generate a normal XID */
  479                          1160                 :          83963 :     cutoffs->FreezeLimit = nextXID - freeze_min_age;
                               1161         [ -  + ]:          83963 :     if (!TransactionIdIsNormal(cutoffs->FreezeLimit))
  479 pg@bowt.ie               1162                 :UBC           0 :         cutoffs->FreezeLimit = FirstNormalTransactionId;
                               1163                 :                :     /* FreezeLimit must always be <= OldestXmin */
  479 pg@bowt.ie               1164         [ +  + ]:CBC       83963 :     if (TransactionIdPrecedes(cutoffs->OldestXmin, cutoffs->FreezeLimit))
                               1165                 :          68613 :         cutoffs->FreezeLimit = cutoffs->OldestXmin;
                               1166                 :                : 
                               1167                 :                :     /*
                               1168                 :                :      * Determine the minimum multixact freeze age to use: as specified by
                               1169                 :                :      * caller, or vacuum_multixact_freeze_min_age, but in any case not more
                               1170                 :                :      * than half effective_multixact_freeze_max_age, so that autovacuums to
                               1171                 :                :      * prevent MultiXact wraparound won't occur too frequently.
                               1172                 :                :      */
  592                          1173         [ +  + ]:          83963 :     if (multixact_freeze_min_age < 0)
                               1174                 :           4263 :         multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
                               1175                 :          83963 :     multixact_freeze_min_age = Min(multixact_freeze_min_age,
                               1176                 :                :                                    effective_multixact_freeze_max_age / 2);
                               1177         [ -  + ]:          83963 :     Assert(multixact_freeze_min_age >= 0);
                               1178                 :                : 
                               1179                 :                :     /* Compute MultiXactCutoff, being careful to generate a valid value */
  479                          1180                 :          83963 :     cutoffs->MultiXactCutoff = nextMXID - multixact_freeze_min_age;
                               1181         [ -  + ]:          83963 :     if (cutoffs->MultiXactCutoff < FirstMultiXactId)
  479 pg@bowt.ie               1182                 :UBC           0 :         cutoffs->MultiXactCutoff = FirstMultiXactId;
                               1183                 :                :     /* MultiXactCutoff must always be <= OldestMxact */
  479 pg@bowt.ie               1184         [ +  + ]:CBC       83963 :     if (MultiXactIdPrecedes(cutoffs->OldestMxact, cutoffs->MultiXactCutoff))
                               1185                 :              2 :         cutoffs->MultiXactCutoff = cutoffs->OldestMxact;
                               1186                 :                : 
                               1187                 :                :     /*
                               1188                 :                :      * Finally, figure out if caller needs to do an aggressive VACUUM or not.
                               1189                 :                :      *
                               1190                 :                :      * Determine the table freeze age to use: as specified by the caller, or
                               1191                 :                :      * the value of the vacuum_freeze_table_age GUC, but in any case not more
                               1192                 :                :      * than autovacuum_freeze_max_age * 0.95, so that if you have e.g nightly
                               1193                 :                :      * VACUUM schedule, the nightly VACUUM gets a chance to freeze XIDs before
                               1194                 :                :      * anti-wraparound autovacuum is launched.
                               1195                 :                :      */
  592                          1196         [ +  + ]:          83963 :     if (freeze_table_age < 0)
                               1197                 :           4263 :         freeze_table_age = vacuum_freeze_table_age;
                               1198         [ +  - ]:          83963 :     freeze_table_age = Min(freeze_table_age, autovacuum_freeze_max_age * 0.95);
                               1199         [ -  + ]:          83963 :     Assert(freeze_table_age >= 0);
                               1200                 :          83963 :     aggressiveXIDCutoff = nextXID - freeze_table_age;
                               1201         [ -  + ]:          83963 :     if (!TransactionIdIsNormal(aggressiveXIDCutoff))
  592 pg@bowt.ie               1202                 :UBC           0 :         aggressiveXIDCutoff = FirstNormalTransactionId;
  793 pg@bowt.ie               1203         [ +  + ]:CBC       83963 :     if (TransactionIdPrecedesOrEquals(rel->rd_rel->relfrozenxid,
                               1204                 :                :                                       aggressiveXIDCutoff))
                               1205                 :          79561 :         return true;
                               1206                 :                : 
                               1207                 :                :     /*
                               1208                 :                :      * Similar to the above, determine the table freeze age to use for
                               1209                 :                :      * multixacts: as specified by the caller, or the value of the
                               1210                 :                :      * vacuum_multixact_freeze_table_age GUC, but in any case not more than
                               1211                 :                :      * effective_multixact_freeze_max_age * 0.95, so that if you have e.g.
                               1212                 :                :      * nightly VACUUM schedule, the nightly VACUUM gets a chance to freeze
                               1213                 :                :      * multixacts before anti-wraparound autovacuum is launched.
                               1214                 :                :      */
  592                          1215         [ +  + ]:           4402 :     if (multixact_freeze_table_age < 0)
                               1216                 :           4258 :         multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
  592 pg@bowt.ie               1217                 :UBC           0 :     multixact_freeze_table_age =
  592 pg@bowt.ie               1218         [ +  - ]:CBC        4402 :         Min(multixact_freeze_table_age,
                               1219                 :                :             effective_multixact_freeze_max_age * 0.95);
                               1220         [ -  + ]:           4402 :     Assert(multixact_freeze_table_age >= 0);
                               1221                 :           4402 :     aggressiveMXIDCutoff = nextMXID - multixact_freeze_table_age;
                               1222         [ -  + ]:           4402 :     if (aggressiveMXIDCutoff < FirstMultiXactId)
  592 pg@bowt.ie               1223                 :UBC           0 :         aggressiveMXIDCutoff = FirstMultiXactId;
  793 pg@bowt.ie               1224         [ -  + ]:CBC        4402 :     if (MultiXactIdPrecedesOrEquals(rel->rd_rel->relminmxid,
                               1225                 :                :                                     aggressiveMXIDCutoff))
  793 pg@bowt.ie               1226                 :UBC           0 :         return true;
                               1227                 :                : 
                               1228                 :                :     /* Non-aggressive VACUUM */
  793 pg@bowt.ie               1229                 :CBC        4402 :     return false;
                               1230                 :                : }
                               1231                 :                : 
                               1232                 :                : /*
                               1233                 :                :  * vacuum_xid_failsafe_check() -- Used by VACUUM's wraparound failsafe
                               1234                 :                :  * mechanism to determine if its table's relfrozenxid and relminmxid are now
                               1235                 :                :  * dangerously far in the past.
                               1236                 :                :  *
                               1237                 :                :  * When we return true, VACUUM caller triggers the failsafe.
                               1238                 :                :  */
                               1239                 :                : bool
  479                          1240                 :          85242 : vacuum_xid_failsafe_check(const struct VacuumCutoffs *cutoffs)
                               1241                 :                : {
                               1242                 :          85242 :     TransactionId relfrozenxid = cutoffs->relfrozenxid;
                               1243                 :          85242 :     MultiXactId relminmxid = cutoffs->relminmxid;
                               1244                 :                :     TransactionId xid_skip_limit;
                               1245                 :                :     MultiXactId multi_skip_limit;
                               1246                 :                :     int         skip_index_vacuum;
                               1247                 :                : 
 1103                          1248         [ -  + ]:          85242 :     Assert(TransactionIdIsNormal(relfrozenxid));
                               1249         [ -  + ]:          85242 :     Assert(MultiXactIdIsValid(relminmxid));
                               1250                 :                : 
                               1251                 :                :     /*
                               1252                 :                :      * Determine the index skipping age to use. In any case no less than
                               1253                 :                :      * autovacuum_freeze_max_age * 1.05.
                               1254                 :                :      */
                               1255         [ +  - ]:          85242 :     skip_index_vacuum = Max(vacuum_failsafe_age, autovacuum_freeze_max_age * 1.05);
                               1256                 :                : 
                               1257                 :          85242 :     xid_skip_limit = ReadNextTransactionId() - skip_index_vacuum;
                               1258         [ -  + ]:          85242 :     if (!TransactionIdIsNormal(xid_skip_limit))
 1103 pg@bowt.ie               1259                 :UBC           0 :         xid_skip_limit = FirstNormalTransactionId;
                               1260                 :                : 
 1103 pg@bowt.ie               1261         [ +  + ]:CBC       85242 :     if (TransactionIdPrecedes(relfrozenxid, xid_skip_limit))
                               1262                 :                :     {
                               1263                 :                :         /* The table's relfrozenxid is too old */
 1103 pg@bowt.ie               1264                 :GBC       20492 :         return true;
                               1265                 :                :     }
                               1266                 :                : 
                               1267                 :                :     /*
                               1268                 :                :      * Similar to above, determine the index skipping age to use for
                               1269                 :                :      * multixact. In any case no less than autovacuum_multixact_freeze_max_age *
                               1270                 :                :      * 1.05.
                               1271                 :                :      */
 1103 pg@bowt.ie               1272         [ +  - ]:CBC       64750 :     skip_index_vacuum = Max(vacuum_multixact_failsafe_age,
                               1273                 :                :                             autovacuum_multixact_freeze_max_age * 1.05);
                               1274                 :                : 
                               1275                 :          64750 :     multi_skip_limit = ReadNextMultiXactId() - skip_index_vacuum;
                               1276         [ -  + ]:          64750 :     if (multi_skip_limit < FirstMultiXactId)
 1103 pg@bowt.ie               1277                 :UBC           0 :         multi_skip_limit = FirstMultiXactId;
                               1278                 :                : 
 1103 pg@bowt.ie               1279         [ -  + ]:CBC       64750 :     if (MultiXactIdPrecedes(relminmxid, multi_skip_limit))
                               1280                 :                :     {
                               1281                 :                :         /* The table's relminmxid is too old */
 1103 pg@bowt.ie               1282                 :UBC           0 :         return true;
                               1283                 :                :     }
                               1284                 :                : 
 1103 pg@bowt.ie               1285                 :CBC       64750 :     return false;
                               1286                 :                : }
                               1287                 :                : 
                               1288                 :                : /*
                               1289                 :                :  * vac_estimate_reltuples() -- estimate the new value for pg_class.reltuples
                               1290                 :                :  *
                               1291                 :                :  *      If we scanned the whole relation then we should just use the count of
                               1292                 :                :  *      live tuples seen; but if we did not, we should not blindly extrapolate
                               1293                 :                :  *      from that number, since VACUUM may have scanned a quite nonrandom
                               1294                 :                :  *      subset of the table.  When we have only partial information, we take
                               1295                 :                :  *      the old value of pg_class.reltuples/pg_class.relpages as a measurement
                               1296                 :                :  *      of the tuple density in the unscanned pages.
                               1297                 :                :  *
                               1298                 :                :  *      Note: scanned_tuples should count only *live* tuples, since
                               1299                 :                :  *      pg_class.reltuples is defined that way.
                               1300                 :                :  */
                               1301                 :                : double
 2224 tgl@sss.pgh.pa.us        1302                 :          83702 : vac_estimate_reltuples(Relation relation,
                               1303                 :                :                        BlockNumber total_pages,
                               1304                 :                :                        BlockNumber scanned_pages,
                               1305                 :                :                        double scanned_tuples)
                               1306                 :                : {
 4693 bruce@momjian.us         1307                 :          83702 :     BlockNumber old_rel_pages = relation->rd_rel->relpages;
 4703 tgl@sss.pgh.pa.us        1308                 :          83702 :     double      old_rel_tuples = relation->rd_rel->reltuples;
                               1309                 :                :     double      old_density;
                               1310                 :                :     double      unscanned_pages;
                               1311                 :                :     double      total_tuples;
                               1312                 :                : 
                               1313                 :                :     /* If we did scan the whole table, just use the count as-is */
                               1314         [ +  + ]:          83702 :     if (scanned_pages >= total_pages)
                               1315                 :          80779 :         return scanned_tuples;
                               1316                 :                : 
                               1317                 :                :     /*
                               1318                 :                :      * When successive VACUUM commands scan the same few pages again and
                               1319                 :                :      * again, without anything from the table really changing, there is a risk
                               1320                 :                :      * that our beliefs about tuple density will gradually become distorted.
                               1321                 :                :      * This might be caused by vacuumlazy.c implementation details, such as
                               1322                 :                :      * its tendency to always scan the last heap page.  Handle that here.
                               1323                 :                :      *
                               1324                 :                :      * If the relation is _exactly_ the same size according to the existing
                               1325                 :                :      * pg_class entry, and only a few of its pages (less than 2%) were
                               1326                 :                :      * scanned, keep the existing value of reltuples.  Also keep the existing
                               1327                 :                :      * value when only a subset of rel's pages <= a single page were scanned.
                               1328                 :                :      *
                               1329                 :                :      * (Note: we might be returning -1 here.)
                               1330                 :                :      */
  788 pg@bowt.ie               1331         [ +  + ]:           2923 :     if (old_rel_pages == total_pages &&
                               1332         [ +  + ]:           2908 :         scanned_pages < (double) total_pages * 0.02)
                               1333                 :           2110 :         return old_rel_tuples;
  604                          1334         [ +  + ]:            813 :     if (scanned_pages <= 1)
                               1335                 :            724 :         return old_rel_tuples;
                               1336                 :                : 
                               1337                 :                :     /*
                               1338                 :                :      * If old density is unknown, we can't do much except scale up
                               1339                 :                :      * scanned_tuples to match total_pages.
                               1340                 :                :      */
 1323 tgl@sss.pgh.pa.us        1341   [ +  +  -  + ]:             89 :     if (old_rel_tuples < 0 || old_rel_pages == 0)
 4703                          1342                 :              1 :         return floor((scanned_tuples / scanned_pages) * total_pages + 0.5);
                               1343                 :                : 
                               1344                 :                :     /*
                               1345                 :                :      * Okay, we've covered the corner cases.  The normal calculation is to
                               1346                 :                :      * convert the old measurement to a density (tuples per page), then
                               1347                 :                :      * estimate the number of tuples in the unscanned pages using that figure,
                               1348                 :                :      * and finally add on the number of tuples in the scanned pages.
                               1349                 :                :      */
                               1350                 :             88 :     old_density = old_rel_tuples / old_rel_pages;
 2224                          1351                 :             88 :     unscanned_pages = (double) total_pages - (double) scanned_pages;
                               1352                 :             88 :     total_tuples = old_density * unscanned_pages + scanned_tuples;
                               1353                 :             88 :     return floor(total_tuples + 0.5);
                               1354                 :                : }
                               1355                 :                : 
                               1356                 :                : 
                               1357                 :                : /*
                               1358                 :                :  *  vac_update_relstats() -- update statistics for one relation
                               1359                 :                :  *
                               1360                 :                :  *      Update the whole-relation statistics that are kept in its pg_class
                               1361                 :                :  *      row.  There are additional stats that will be updated if we are
                               1362                 :                :  *      doing ANALYZE, but we always update these stats.  This routine works
                               1363                 :                :  *      for both index and heap relation entries in pg_class.
                               1364                 :                :  *
                               1365                 :                :  *      We violate transaction semantics here by overwriting the rel's
                               1366                 :                :  *      existing pg_class tuple with the new values.  This is reasonably
                               1367                 :                :  *      safe as long as we're sure that the new values are correct whether or
                               1368                 :                :  *      not this transaction commits.  The reason for doing this is that if
                               1369                 :                :  *      we updated these tuples in the usual way, vacuuming pg_class itself
                               1370                 :                :  *      wouldn't work very well --- by the time we got done with a vacuum
                               1371                 :                :  *      cycle, most of the tuples in pg_class would've been obsoleted.  Of
                               1372                 :                :  *      course, this only works for fixed-size not-null columns, but these are.
                               1373                 :                :  *
                               1374                 :                :  *      Another reason for doing it this way is that when we are in a lazy
                               1375                 :                :  *      VACUUM and have PROC_IN_VACUUM set, we mustn't do any regular updates.
                               1376                 :                :  *      Somebody vacuuming pg_class might think they could delete a tuple
                               1377                 :                :  *      marked with xmin = our xid.
                               1378                 :                :  *
                               1379                 :                :  *      In addition to fundamentally nontransactional statistics such as
                               1380                 :                :  *      relpages and relallvisible, we try to maintain certain lazily-updated
                               1381                 :                :  *      DDL flags such as relhasindex, by clearing them if no longer correct.
                               1382                 :                :  *      It's safe to do this in VACUUM, which can't run in parallel with
                               1383                 :                :  *      CREATE INDEX/RULE/TRIGGER and can't be part of a transaction block.
                               1384                 :                :  *      However, it's *not* safe to do it in an ANALYZE that's within an
                               1385                 :                :  *      outer transaction, because for example the current transaction might
                               1386                 :                :  *      have dropped the last index; then we'd think relhasindex should be
                               1387                 :                :  *      cleared, but if the transaction later rolls back this would be wrong.
                               1388                 :                :  *      So we refrain from updating the DDL flags if we're inside an outer
                               1389                 :                :  *      transaction.  This is OK since postponing the flag maintenance is
                               1390                 :                :  *      always allowable.
                               1391                 :                :  *
                               1392                 :                :  *      Note: num_tuples should count only *live* tuples, since
                               1393                 :                :  *      pg_class.reltuples is defined that way.
                               1394                 :                :  *
                               1395                 :                :  *      This routine is shared by VACUUM and ANALYZE.
                               1396                 :                :  */
                               1397                 :                : void
 5634                          1398                 :          99770 : vac_update_relstats(Relation relation,
                               1399                 :                :                     BlockNumber num_pages, double num_tuples,
                               1400                 :                :                     BlockNumber num_all_visible_pages,
                               1401                 :                :                     bool hasindex, TransactionId frozenxid,
                               1402                 :                :                     MultiXactId minmulti,
                               1403                 :                :                     bool *frozenxid_updated, bool *minmulti_updated,
                               1404                 :                :                     bool in_outer_xact)
                               1405                 :                : {
                               1406                 :          99770 :     Oid         relid = RelationGetRelid(relation);
                               1407                 :                :     Relation    rd;
                               1408                 :                :     HeapTuple   ctup;
                               1409                 :                :     Form_pg_class pgcform;
                               1410                 :                :     bool        dirty,
                               1411                 :                :                 futurexid,
                               1412                 :                :                 futuremxid;
                               1413                 :                :     TransactionId oldfrozenxid;
                               1414                 :                :     MultiXactId oldminmulti;
                               1415                 :                : 
 1910 andres@anarazel.de       1416                 :          99770 :     rd = table_open(RelationRelationId, RowExclusiveLock);
                               1417                 :                : 
                               1418                 :                :     /* Fetch a copy of the tuple to scribble on */
 5173 rhaas@postgresql.org     1419                 :          99770 :     ctup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
 8312 tgl@sss.pgh.pa.us        1420         [ -  + ]:          99770 :     if (!HeapTupleIsValid(ctup))
 8312 tgl@sss.pgh.pa.us        1421         [ #  # ]:UBC           0 :         elog(ERROR, "pg_class entry for relid %u vanished during vacuuming",
                               1422                 :                :              relid);
 6549 tgl@sss.pgh.pa.us        1423                 :CBC       99770 :     pgcform = (Form_pg_class) GETSTRUCT(ctup);
                               1424                 :                : 
                               1425                 :                :     /* Apply statistical updates, if any, to copied tuple */
                               1426                 :                : 
                               1427                 :          99770 :     dirty = false;
                               1428         [ +  + ]:          99770 :     if (pgcform->relpages != (int32) num_pages)
                               1429                 :                :     {
                               1430                 :           3890 :         pgcform->relpages = (int32) num_pages;
                               1431                 :           3890 :         dirty = true;
                               1432                 :                :     }
                               1433         [ +  + ]:          99770 :     if (pgcform->reltuples != (float4) num_tuples)
                               1434                 :                :     {
                               1435                 :           8266 :         pgcform->reltuples = (float4) num_tuples;
                               1436                 :           8266 :         dirty = true;
                               1437                 :                :     }
 4566                          1438         [ +  + ]:          99770 :     if (pgcform->relallvisible != (int32) num_all_visible_pages)
                               1439                 :                :     {
                               1440                 :           2379 :         pgcform->relallvisible = (int32) num_all_visible_pages;
                               1441                 :           2379 :         dirty = true;
                               1442                 :                :     }
                               1443                 :                : 
                               1444                 :                :     /* Apply DDL updates, but not inside an outer transaction (see above) */
                               1445                 :                : 
 3454                          1446         [ +  + ]:          99770 :     if (!in_outer_xact)
                               1447                 :                :     {
                               1448                 :                :         /*
                               1449                 :                :          * If we didn't find any indexes, reset relhasindex.
                               1450                 :                :          */
 3455                          1451   [ +  +  +  + ]:          99631 :         if (pgcform->relhasindex && !hasindex)
                               1452                 :                :         {
                               1453                 :              9 :             pgcform->relhasindex = false;
                               1454                 :              9 :             dirty = true;
                               1455                 :                :         }
                               1456                 :                : 
                               1457                 :                :         /* We also clear relhasrules and relhastriggers if needed */
                               1458   [ +  +  -  + ]:          99631 :         if (pgcform->relhasrules && relation->rd_rules == NULL)
                               1459                 :                :         {
 3455 tgl@sss.pgh.pa.us        1460                 :UBC           0 :             pgcform->relhasrules = false;
                               1461                 :              0 :             dirty = true;
                               1462                 :                :         }
 3455 tgl@sss.pgh.pa.us        1463   [ +  +  +  + ]:CBC       99631 :         if (pgcform->relhastriggers && relation->trigdesc == NULL)
                               1464                 :                :         {
                               1465                 :              3 :             pgcform->relhastriggers = false;
                               1466                 :              3 :             dirty = true;
                               1467                 :                :         }
                               1468                 :                :     }
                               1469                 :                : 
                               1470                 :                :     /*
                               1471                 :                :      * Update relfrozenxid, unless caller passed InvalidTransactionId
                               1472                 :                :      * indicating it has no new data.
                               1473                 :                :      *
                               1474                 :                :      * Ordinarily, we don't let relfrozenxid go backwards.  However, if the
                               1475                 :                :      * stored relfrozenxid is "in the future" then it seems best to assume
                               1476                 :                :      * it's corrupt, and overwrite with the oldest remaining XID in the table.
                               1477                 :                :      * This should match vac_update_datfrozenxid() concerning what we consider
                               1478                 :                :      * to be "in the future".
                               1479                 :                :      */
  740 pg@bowt.ie               1480                 :          99770 :     oldfrozenxid = pgcform->relfrozenxid;
                               1481                 :          99770 :     futurexid = false;
  793                          1482         [ +  + ]:          99770 :     if (frozenxid_updated)
                               1483                 :          83700 :         *frozenxid_updated = false;
  740                          1484   [ +  +  +  + ]:          99770 :     if (TransactionIdIsNormal(frozenxid) && oldfrozenxid != frozenxid)
                               1485                 :                :     {
  703 tgl@sss.pgh.pa.us        1486                 :          25552 :         bool        update = false;
                               1487                 :                : 
  740 pg@bowt.ie               1488         [ +  + ]:          25552 :         if (TransactionIdPrecedes(oldfrozenxid, frozenxid))
                               1489                 :          25522 :             update = true;
                               1490         [ -  + ]:             30 :         else if (TransactionIdPrecedes(ReadNextTransactionId(), oldfrozenxid))
  740 pg@bowt.ie               1491                 :UBC           0 :             futurexid = update = true;
                               1492                 :                : 
  740 pg@bowt.ie               1493         [ +  + ]:CBC       25552 :         if (update)
                               1494                 :                :         {
                               1495                 :          25522 :             pgcform->relfrozenxid = frozenxid;
                               1496                 :          25522 :             dirty = true;
                               1497         [ +  - ]:          25522 :             if (frozenxid_updated)
                               1498                 :          25522 :                 *frozenxid_updated = true;
                               1499                 :                :         }
                               1500                 :                :     }
                               1501                 :                : 
                               1502                 :                :     /* Similarly for relminmxid */
                               1503                 :          99770 :     oldminmulti = pgcform->relminmxid;
                               1504                 :          99770 :     futuremxid = false;
  793                          1505         [ +  + ]:          99770 :     if (minmulti_updated)
                               1506                 :          83700 :         *minmulti_updated = false;
  740                          1507   [ +  +  +  + ]:          99770 :     if (MultiXactIdIsValid(minmulti) && oldminmulti != minmulti)
                               1508                 :                :     {
  703 tgl@sss.pgh.pa.us        1509                 :            146 :         bool        update = false;
                               1510                 :                : 
  740 pg@bowt.ie               1511         [ +  - ]:            146 :         if (MultiXactIdPrecedes(oldminmulti, minmulti))
                               1512                 :            146 :             update = true;
  740 pg@bowt.ie               1513         [ #  # ]:UBC           0 :         else if (MultiXactIdPrecedes(ReadNextMultiXactId(), oldminmulti))
                               1514                 :              0 :             futuremxid = update = true;
                               1515                 :                : 
  740 pg@bowt.ie               1516         [ +  - ]:CBC         146 :         if (update)
                               1517                 :                :         {
                               1518                 :            146 :             pgcform->relminmxid = minmulti;
                               1519                 :            146 :             dirty = true;
                               1520         [ +  - ]:            146 :             if (minmulti_updated)
                               1521                 :            146 :                 *minmulti_updated = true;
                               1522                 :                :         }
                               1523                 :                :     }
                               1524                 :                : 
                               1525                 :                :     /* If anything changed, write out the tuple. */
 6549 tgl@sss.pgh.pa.us        1526         [ +  + ]:          99770 :     if (dirty)
                               1527                 :          31372 :         heap_inplace_update(rd, ctup);
                               1528                 :                : 
 1910 andres@anarazel.de       1529                 :          99770 :     table_close(rd, RowExclusiveLock);
                               1530                 :                : 
  740 pg@bowt.ie               1531         [ -  + ]:          99770 :     if (futurexid)
  740 pg@bowt.ie               1532         [ #  # ]:UBC           0 :         ereport(WARNING,
                               1533                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                               1534                 :                :                  errmsg_internal("overwrote invalid relfrozenxid value %u with new value %u for table \"%s\"",
                               1535                 :                :                                  oldfrozenxid, frozenxid,
                               1536                 :                :                                  RelationGetRelationName(relation))));
  740 pg@bowt.ie               1537         [ -  + ]:CBC       99770 :     if (futuremxid)
  740 pg@bowt.ie               1538         [ #  # ]:UBC           0 :         ereport(WARNING,
                               1539                 :                :                 (errcode(ERRCODE_DATA_CORRUPTED),
                               1540                 :                :                  errmsg_internal("overwrote invalid relminmxid value %u with new value %u for table \"%s\"",
                               1541                 :                :                                  oldminmulti, minmulti,
                               1542                 :                :                                  RelationGetRelationName(relation))));
 8312 tgl@sss.pgh.pa.us        1543                 :CBC       99770 : }
                               1544                 :                : 
                               1545                 :                : 
                               1546                 :                : /*
                               1547                 :                :  *  vac_update_datfrozenxid() -- update pg_database.datfrozenxid for our DB
                               1548                 :                :  *
                               1549                 :                :  *      Update pg_database's datfrozenxid entry for our database to be the
                               1550                 :                :  *      minimum of the pg_class.relfrozenxid values.
                               1551                 :                :  *
                               1552                 :                :  *      Similarly, update our datminmxid to be the minimum of the
                               1553                 :                :  *      pg_class.relminmxid values.
                               1554                 :                :  *
                               1555                 :                :  *      If we are able to advance either pg_database value, also try to
                               1556                 :                :  *      truncate pg_xact and pg_multixact.
                               1557                 :                :  *
                               1558                 :                :  *      We violate transaction semantics here by overwriting the database's
                               1559                 :                :  *      existing pg_database tuple with the new values.  This is reasonably
                               1560                 :                :  *      safe since the new values are correct whether or not this transaction
                               1561                 :                :  *      commits.  As with vac_update_relstats, this avoids leaving dead tuples
                               1562                 :                :  *      behind after a VACUUM.
                               1563                 :                :  */
                               1564                 :                : void
 6370                          1565                 :           1496 : vac_update_datfrozenxid(void)
                               1566                 :                : {
                               1567                 :                :     HeapTuple   tuple;
                               1568                 :                :     Form_pg_database dbform;
                               1569                 :                :     Relation    relation;
                               1570                 :                :     SysScanDesc scan;
                               1571                 :                :     HeapTuple   classTup;
                               1572                 :                :     TransactionId newFrozenXid;
                               1573                 :                :     MultiXactId newMinMulti;
                               1574                 :                :     TransactionId lastSaneFrozenXid;
                               1575                 :                :     MultiXactId lastSaneMinMulti;
 3555                          1576                 :           1496 :     bool        bogus = false;
 6488 alvherre@alvh.no-ip.     1577                 :           1496 :     bool        dirty = false;
                               1578                 :                :     ScanKeyData key[1];
                               1579                 :                : 
                               1580                 :                :     /*
                               1581                 :                :      * Restrict this task to one backend per database.  This avoids race
                               1582                 :                :      * conditions that would move datfrozenxid or datminmxid backward.  It
                               1583                 :                :      * avoids calling vac_truncate_clog() with a datfrozenxid preceding a
                               1584                 :                :      * datfrozenxid passed to an earlier vac_truncate_clog() call.
                               1585                 :                :      */
 1338 noah@leadboat.com        1586                 :           1496 :     LockDatabaseFrozenIds(ExclusiveLock);
                               1587                 :                : 
                               1588                 :                :     /*
                               1589                 :                :      * Initialize the "min" calculation with
                               1590                 :                :      * GetOldestNonRemovableTransactionId(), which is a reasonable
                               1591                 :                :      * approximation to the minimum relfrozenxid for not-yet-committed
                               1592                 :                :      * pg_class entries for new tables; see AddNewRelationTuple().  So we
                               1593                 :                :      * cannot produce a wrong minimum by starting with this.
                               1594                 :                :      */
 1341 andres@anarazel.de       1595                 :           1496 :     newFrozenXid = GetOldestNonRemovableTransactionId(NULL);
                               1596                 :                : 
                               1597                 :                :     /*
                               1598                 :                :      * Similarly, initialize the MultiXact "min" with the value that would be
                               1599                 :                :      * used on pg_class for new tables.  See AddNewRelationTuple().
                               1600                 :                :      */
 3555 tgl@sss.pgh.pa.us        1601                 :           1496 :     newMinMulti = GetOldestMultiXactId();
                               1602                 :                : 
                               1603                 :                :     /*
                               1604                 :                :      * Identify the latest relfrozenxid and relminmxid values that we could
                               1605                 :                :      * validly see during the scan.  These are conservative values, but it's
                               1606                 :                :      * not really worth trying to be more exact.
                               1607                 :                :      */
 1154 tmunro@postgresql.or     1608                 :           1496 :     lastSaneFrozenXid = ReadNextTransactionId();
 3555 tgl@sss.pgh.pa.us        1609                 :           1496 :     lastSaneMinMulti = ReadNextMultiXactId();
                               1610                 :                : 
                               1611                 :                :     /*
                               1612                 :                :      * We must seqscan pg_class to find the minimum Xid, because there is no
                               1613                 :                :      * index that can help us here.
                               1614                 :                :      */
 1910 andres@anarazel.de       1615                 :           1496 :     relation = table_open(RelationRelationId, AccessShareLock);
                               1616                 :                : 
 6488 alvherre@alvh.no-ip.     1617                 :           1496 :     scan = systable_beginscan(relation, InvalidOid, false,
                               1618                 :                :                               NULL, 0, NULL);
                               1619                 :                : 
                               1620         [ +  + ]:         810169 :     while ((classTup = systable_getnext(scan)) != NULL)
                               1621                 :                :     {
 6370 tgl@sss.pgh.pa.us        1622                 :         808673 :         Form_pg_class classForm = (Form_pg_class) GETSTRUCT(classTup);
                               1623                 :                : 
                               1624                 :                :         /*
                               1625                 :                :          * Only consider relations able to hold unfrozen XIDs (anything else
                               1626                 :                :          * should have InvalidTransactionId in relfrozenxid anyway).
                               1627                 :                :          */
 6488 alvherre@alvh.no-ip.     1628         [ +  + ]:         808673 :         if (classForm->relkind != RELKIND_RELATION &&
 4060 kgrittn@postgresql.o     1629         [ +  + ]:         632420 :             classForm->relkind != RELKIND_MATVIEW &&
 6488 alvherre@alvh.no-ip.     1630         [ +  + ]:         631365 :             classForm->relkind != RELKIND_TOASTVALUE)
                               1631                 :                :         {
 1818 andres@anarazel.de       1632         [ -  + ]:         540470 :             Assert(!TransactionIdIsValid(classForm->relfrozenxid));
                               1633         [ -  + ]:         540470 :             Assert(!MultiXactIdIsValid(classForm->relminmxid));
 6488 alvherre@alvh.no-ip.     1634                 :         540470 :             continue;
                               1635                 :                :         }
                               1636                 :                : 
                               1637                 :                :         /*
                               1638                 :                :          * Some table AMs might not need per-relation xid / multixid horizons.
                               1639                 :                :          * It therefore seems reasonable to allow relfrozenxid and relminmxid
                               1640                 :                :          * to not be set (i.e. set to their respective Invalid*Id)
                               1641                 :                :          * independently. Thus validate and compute horizon for each only if
                               1642                 :                :          * set.
                               1643                 :                :          *
                               1644                 :                :          * If things are working properly, no relation should have a
                               1645                 :                :          * relfrozenxid or relminmxid that is "in the future".  However, such
                               1646                 :                :          * cases have been known to arise due to bugs in pg_upgrade.  If we
                               1647                 :                :          * see any entries that are "in the future", chicken out and don't do
                               1648                 :                :          * anything.  This ensures we won't truncate clog & multixact SLRUs
                               1649                 :                :          * before those relations have been scanned and cleaned up.
                               1650                 :                :          */
                               1651                 :                : 
 1818 andres@anarazel.de       1652         [ +  - ]:         268203 :         if (TransactionIdIsValid(classForm->relfrozenxid))
                               1653                 :                :         {
                               1654         [ -  + ]:         268203 :             Assert(TransactionIdIsNormal(classForm->relfrozenxid));
                               1655                 :                : 
                               1656                 :                :             /* check for values in the future */
                               1657         [ -  + ]:         268203 :             if (TransactionIdPrecedes(lastSaneFrozenXid, classForm->relfrozenxid))
                               1658                 :                :             {
 1818 andres@anarazel.de       1659                 :UBC           0 :                 bogus = true;
                               1660                 :              0 :                 break;
                               1661                 :                :             }
                               1662                 :                : 
                               1663                 :                :             /* determine new horizon */
 1818 andres@anarazel.de       1664         [ +  + ]:CBC      268203 :             if (TransactionIdPrecedes(classForm->relfrozenxid, newFrozenXid))
                               1665                 :           1466 :                 newFrozenXid = classForm->relfrozenxid;
                               1666                 :                :         }
                               1667                 :                : 
                               1668         [ +  - ]:         268203 :         if (MultiXactIdIsValid(classForm->relminmxid))
                               1669                 :                :         {
                               1670                 :                :             /* check for values in the future */
                               1671         [ -  + ]:         268203 :             if (MultiXactIdPrecedes(lastSaneMinMulti, classForm->relminmxid))
                               1672                 :                :             {
 1818 andres@anarazel.de       1673                 :UBC           0 :                 bogus = true;
                               1674                 :              0 :                 break;
                               1675                 :                :             }
                               1676                 :                : 
                               1677                 :                :             /* determine new horizon */
 1818 andres@anarazel.de       1678         [ +  + ]:CBC      268203 :             if (MultiXactIdPrecedes(classForm->relminmxid, newMinMulti))
                               1679                 :            105 :                 newMinMulti = classForm->relminmxid;
                               1680                 :                :         }
                               1681                 :                :     }
                               1682                 :                : 
                               1683                 :                :     /* we're done with pg_class */
 6488 alvherre@alvh.no-ip.     1684                 :           1496 :     systable_endscan(scan);
 1910 andres@anarazel.de       1685                 :           1496 :     table_close(relation, AccessShareLock);
                               1686                 :                : 
                               1687                 :                :     /* chicken out if bogus data found */
 3555 tgl@sss.pgh.pa.us        1688         [ -  + ]:           1496 :     if (bogus)
 3555 tgl@sss.pgh.pa.us        1689                 :UBC           0 :         return;
                               1690                 :                : 
 6370 tgl@sss.pgh.pa.us        1691         [ -  + ]:CBC        1496 :     Assert(TransactionIdIsNormal(newFrozenXid));
 3863 alvherre@alvh.no-ip.     1692         [ -  + ]:           1496 :     Assert(MultiXactIdIsValid(newMinMulti));
                               1693                 :                : 
                               1694                 :                :     /* Now fetch the pg_database tuple we need to update. */
 1910 andres@anarazel.de       1695                 :           1496 :     relation = table_open(DatabaseRelationId, RowExclusiveLock);
                               1696                 :                : 
                               1697                 :                :     /*
                               1698                 :                :      * Get the pg_database tuple to scribble on.  Note that this does not
                               1699                 :                :      * directly rely on the syscache to avoid issues with flattened toast
                               1700                 :                :      * values for the in-place update.
                               1701                 :                :      */
 1223 michael@paquier.xyz      1702                 :           1496 :     ScanKeyInit(&key[0],
                               1703                 :                :                 Anum_pg_database_oid,
                               1704                 :                :                 BTEqualStrategyNumber, F_OIDEQ,
                               1705                 :                :                 ObjectIdGetDatum(MyDatabaseId));
                               1706                 :                : 
                               1707                 :           1496 :     scan = systable_beginscan(relation, DatabaseOidIndexId, true,
                               1708                 :                :                               NULL, 1, key);
                               1709                 :           1496 :     tuple = systable_getnext(scan);
                               1710                 :           1496 :     tuple = heap_copytuple(tuple);
                               1711                 :           1496 :     systable_endscan(scan);
                               1712                 :                : 
 8267 tgl@sss.pgh.pa.us        1713         [ -  + ]:           1496 :     if (!HeapTupleIsValid(tuple))
 6370 tgl@sss.pgh.pa.us        1714         [ #  # ]:UBC           0 :         elog(ERROR, "could not find tuple for database %u", MyDatabaseId);
                               1715                 :                : 
 8267 tgl@sss.pgh.pa.us        1716                 :CBC        1496 :     dbform = (Form_pg_database) GETSTRUCT(tuple);
                               1717                 :                : 
                               1718                 :                :     /*
                               1719                 :                :      * As in vac_update_relstats(), we ordinarily don't want to let
                               1720                 :                :      * datfrozenxid go backward; but if it's "in the future" then it must be
                               1721                 :                :      * corrupt and it seems best to overwrite it.
                               1722                 :                :      */
 3555                          1723   [ +  +  -  + ]:           1720 :     if (dbform->datfrozenxid != newFrozenXid &&
                               1724         [ -  - ]:            224 :         (TransactionIdPrecedes(dbform->datfrozenxid, newFrozenXid) ||
 3555 tgl@sss.pgh.pa.us        1725                 :UBC           0 :          TransactionIdPrecedes(lastSaneFrozenXid, dbform->datfrozenxid)))
                               1726                 :                :     {
 6370 tgl@sss.pgh.pa.us        1727                 :CBC         224 :         dbform->datfrozenxid = newFrozenXid;
 6488 alvherre@alvh.no-ip.     1728                 :            224 :         dirty = true;
                               1729                 :                :     }
                               1730                 :                :     else
 3555 tgl@sss.pgh.pa.us        1731                 :           1272 :         newFrozenXid = dbform->datfrozenxid;
                               1732                 :                : 
                               1733                 :                :     /* Ditto for datminmxid */
                               1734   [ -  +  -  - ]:           1496 :     if (dbform->datminmxid != newMinMulti &&
 3555 tgl@sss.pgh.pa.us        1735         [ #  # ]:UBC           0 :         (MultiXactIdPrecedes(dbform->datminmxid, newMinMulti) ||
                               1736                 :              0 :          MultiXactIdPrecedes(lastSaneMinMulti, dbform->datminmxid)))
                               1737                 :                :     {
 3863 alvherre@alvh.no-ip.     1738                 :              0 :         dbform->datminmxid = newMinMulti;
 4099                          1739                 :              0 :         dirty = true;
                               1740                 :                :     }
                               1741                 :                :     else
 3555 tgl@sss.pgh.pa.us        1742                 :CBC        1496 :         newMinMulti = dbform->datminmxid;
                               1743                 :                : 
 6488 alvherre@alvh.no-ip.     1744         [ +  + ]:           1496 :     if (dirty)
                               1745                 :            224 :         heap_inplace_update(relation, tuple);
                               1746                 :                : 
                               1747                 :           1496 :     heap_freetuple(tuple);
 1910 andres@anarazel.de       1748                 :           1496 :     table_close(relation, RowExclusiveLock);
                               1749                 :                : 
                               1750                 :                :     /*
                               1751                 :                :      * If we were able to advance datfrozenxid or datminmxid, see if we can
                               1752                 :                :      * truncate pg_xact and/or pg_multixact.  Also do it if the shared
                               1753                 :                :      * XID-wrap-limit info is stale, since this action will update that too.
                               1754                 :                :      */
 5339 tgl@sss.pgh.pa.us        1755   [ +  +  +  + ]:           1496 :     if (dirty || ForceTransactionIdLimitUpdate())
 3555                          1756                 :            742 :         vac_truncate_clog(newFrozenXid, newMinMulti,
                               1757                 :                :                           lastSaneFrozenXid, lastSaneMinMulti);
                               1758                 :                : }
                               1759                 :                : 
                               1760                 :                : 
                               1761                 :                : /*
                               1762                 :                :  *  vac_truncate_clog() -- attempt to truncate the commit log
                               1763                 :                :  *
                               1764                 :                :  *      Scan pg_database to determine the system-wide oldest datfrozenxid,
                               1765                 :                :  *      and use it to truncate the transaction commit log (pg_xact).
                               1766                 :                :  *      Also update the XID wrap limit info maintained by varsup.c.
                               1767                 :                :  *      Likewise for datminmxid.
                               1768                 :                :  *
                               1769                 :                :  *      The passed frozenXID and minMulti are the updated values for my own
                               1770                 :                :  *      pg_database entry. They're used to initialize the "min" calculations.
                               1771                 :                :  *      The caller also passes the "last sane" XID and MXID, since it has
                               1772                 :                :  *      those at hand already.
                               1773                 :                :  *
                               1774                 :                :  *      This routine is only invoked when we've managed to change our
                               1775                 :                :  *      DB's datfrozenxid/datminmxid values, or we found that the shared
                               1776                 :                :  *      XID-wrap-limit info is stale.
                               1777                 :                :  */
                               1778                 :                : static void
                               1779                 :            742 : vac_truncate_clog(TransactionId frozenXID,
                               1780                 :                :                   MultiXactId minMulti,
                               1781                 :                :                   TransactionId lastSaneFrozenXid,
                               1782                 :                :                   MultiXactId lastSaneMinMulti)
                               1783                 :                : {
 1154 tmunro@postgresql.or     1784                 :            742 :     TransactionId nextXID = ReadNextTransactionId();
                               1785                 :                :     Relation    relation;
                               1786                 :                :     TableScanDesc scan;
                               1787                 :                :     HeapTuple   tuple;
                               1788                 :                :     Oid         oldestxid_datoid;
                               1789                 :                :     Oid         minmulti_datoid;
 3555 tgl@sss.pgh.pa.us        1790                 :            742 :     bool        bogus = false;
 6370                          1791                 :            742 :     bool        frozenAlreadyWrapped = false;
                               1792                 :                : 
                               1793                 :                :     /* Restrict task to one backend per cluster; see SimpleLruTruncate(). */
 1338 noah@leadboat.com        1794                 :            742 :     LWLockAcquire(WrapLimitsVacuumLock, LW_EXCLUSIVE);
                               1795                 :                : 
                               1796                 :                :     /* init oldest datoids to sync with my frozenXID/minMulti values */
 4099 alvherre@alvh.no-ip.     1797                 :            742 :     oldestxid_datoid = MyDatabaseId;
 3863                          1798                 :            742 :     minmulti_datoid = MyDatabaseId;
                               1799                 :                : 
                               1800                 :                :     /*
                               1801                 :                :      * Scan pg_database to compute the minimum datfrozenxid/datminmxid
                               1802                 :                :      *
                               1803                 :                :      * Since vac_update_datfrozenxid updates datfrozenxid/datminmxid in-place,
                               1804                 :                :      * the values could change while we look at them.  Fetch each one just
                               1805                 :                :      * once to ensure sane behavior of the comparison logic.  (Here, as in
                               1806                 :                :      * many other places, we assume that fetching or updating an XID in shared
                               1807                 :                :      * storage is atomic.)
                               1808                 :                :      *
                               1809                 :                :      * Note: we need not worry about a race condition with new entries being
                               1810                 :                :      * inserted by CREATE DATABASE.  Any such entry will have a copy of some
                               1811                 :                :      * existing DB's datfrozenxid, and that source DB cannot be ours because
                               1812                 :                :      * of the interlock against copying a DB containing an active backend.
                               1813                 :                :      * Hence the new entry will not reduce the minimum.  Also, if two VACUUMs
                               1814                 :                :      * concurrently modify the datfrozenxid's of different databases, the
                               1815                 :                :      * worst possible outcome is that pg_xact is not truncated as aggressively
                               1816                 :                :      * as it could be.
                               1817                 :                :      */
 1910 andres@anarazel.de       1818                 :            742 :     relation = table_open(DatabaseRelationId, AccessShareLock);
                               1819                 :                : 
 1861                          1820                 :            742 :     scan = table_beginscan_catalog(relation, 0, NULL);
                               1821                 :                : 
 8000 tgl@sss.pgh.pa.us        1822         [ +  + ]:           2905 :     while ((tuple = heap_getnext(scan, ForwardScanDirection)) != NULL)
                               1823                 :                :     {
 2882                          1824                 :           2163 :         volatile FormData_pg_database *dbform = (Form_pg_database) GETSTRUCT(tuple);
                               1825                 :           2163 :         TransactionId datfrozenxid = dbform->datfrozenxid;
                               1826                 :           2163 :         TransactionId datminmxid = dbform->datminmxid;
                               1827                 :                : 
                               1828         [ -  + ]:           2163 :         Assert(TransactionIdIsNormal(datfrozenxid));
                               1829         [ -  + ]:           2163 :         Assert(MultiXactIdIsValid(datminmxid));
                               1830                 :                : 
                               1831                 :                :         /*
                               1832                 :                :          * If database is in the process of getting dropped, or has been
                               1833                 :                :          * interrupted while doing so, no connections to it are possible
                               1834                 :                :          * anymore. Therefore we don't need to take it into account here.
                               1835                 :                :          * Which is good, because it can't be processed by autovacuum either.
                               1836                 :                :          */
  276 andres@anarazel.de       1837         [ +  + ]:           2163 :         if (database_is_invalid_form((Form_pg_database) dbform))
                               1838                 :                :         {
                               1839         [ -  + ]:              1 :             elog(DEBUG2,
                               1840                 :                :                  "skipping invalid database \"%s\" while computing relfrozenxid",
                               1841                 :                :                  NameStr(dbform->datname));
                               1842                 :              1 :             continue;
                               1843                 :                :         }
                               1844                 :                : 
                               1845                 :                :         /*
                               1846                 :                :          * If things are working properly, no database should have a
                               1847                 :                :          * datfrozenxid or datminmxid that is "in the future".  However, such
                               1848                 :                :          * cases have been known to arise due to bugs in pg_upgrade.  If we
                               1849                 :                :          * see any entries that are "in the future", chicken out and don't do
                               1850                 :                :          * anything.  This ensures we won't truncate clog before those
                               1851                 :                :          * databases have been scanned and cleaned up.  (We will issue the
                               1852                 :                :          * "already wrapped" warning if appropriate, though.)
                               1853                 :                :          */
 2882 tgl@sss.pgh.pa.us        1854   [ +  -  -  + ]:           4324 :         if (TransactionIdPrecedes(lastSaneFrozenXid, datfrozenxid) ||
                               1855                 :           2162 :             MultiXactIdPrecedes(lastSaneMinMulti, datminmxid))
 3555 tgl@sss.pgh.pa.us        1856                 :UBC           0 :             bogus = true;
                               1857                 :                : 
 2882 tgl@sss.pgh.pa.us        1858         [ -  + ]:CBC        2162 :         if (TransactionIdPrecedes(nextXID, datfrozenxid))
 6370 tgl@sss.pgh.pa.us        1859                 :UBC           0 :             frozenAlreadyWrapped = true;
 2882 tgl@sss.pgh.pa.us        1860         [ +  + ]:CBC        2162 :         else if (TransactionIdPrecedes(datfrozenxid, frozenXID))
                               1861                 :                :         {
                               1862                 :            143 :             frozenXID = datfrozenxid;
 1972 andres@anarazel.de       1863                 :            143 :             oldestxid_datoid = dbform->oid;
                               1864                 :                :         }
                               1865                 :                : 
 2882 tgl@sss.pgh.pa.us        1866         [ +  + ]:           2162 :         if (MultiXactIdPrecedes(datminmxid, minMulti))
                               1867                 :                :         {
 2882 tgl@sss.pgh.pa.us        1868                 :GBC           1 :             minMulti = datminmxid;
 1972 andres@anarazel.de       1869                 :              1 :             minmulti_datoid = dbform->oid;
                               1870                 :                :         }
                               1871                 :                :     }
                               1872                 :                : 
 1861 andres@anarazel.de       1873                 :CBC         742 :     table_endscan(scan);
                               1874                 :                : 
 1910                          1875                 :            742 :     table_close(relation, AccessShareLock);
                               1876                 :                : 
                               1877                 :                :     /*
                               1878                 :                :      * Do not truncate CLOG if we seem to have suffered wraparound already;
                               1879                 :                :      * the computed minimum XID might be bogus.  This case should now be
                               1880                 :                :      * impossible due to the defenses in GetNewTransactionId, but we keep the
                               1881                 :                :      * test anyway.
                               1882                 :                :      */
 6370 tgl@sss.pgh.pa.us        1883         [ -  + ]:            742 :     if (frozenAlreadyWrapped)
                               1884                 :                :     {
 7574 tgl@sss.pgh.pa.us        1885         [ #  # ]:UBC           0 :         ereport(WARNING,
                               1886                 :                :                 (errmsg("some databases have not been vacuumed in over 2 billion transactions"),
                               1887                 :                :                  errdetail("You might have already suffered transaction-wraparound data loss.")));
  276 andres@anarazel.de       1888                 :              0 :         LWLockRelease(WrapLimitsVacuumLock);
 8048 tgl@sss.pgh.pa.us        1889                 :              0 :         return;
                               1890                 :                :     }
                               1891                 :                : 
                               1892                 :                :     /* chicken out if data is bogus in any other way */
 3555 tgl@sss.pgh.pa.us        1893         [ -  + ]:CBC         742 :     if (bogus)
                               1894                 :                :     {
  276 andres@anarazel.de       1895                 :UBC           0 :         LWLockRelease(WrapLimitsVacuumLock);
 3555 tgl@sss.pgh.pa.us        1896                 :              0 :         return;
                               1897                 :                :     }
                               1898                 :                : 
                               1899                 :                :     /*
                               1900                 :                :      * Advance the oldest value for commit timestamps before truncating, so
                               1901                 :                :      * that if a user requests a timestamp for a transaction we're truncating
                               1902                 :                :      * away right after this point, they get NULL instead of an ugly "file not
                               1903                 :                :      * found" error from slru.c.  This doesn't matter for xact/multixact
                               1904                 :                :      * because they are not subject to arbitrary lookups from users.
                               1905                 :                :      */
 2642 alvherre@alvh.no-ip.     1906                 :CBC         742 :     AdvanceOldestCommitTsXid(frozenXID);
                               1907                 :                : 
                               1908                 :                :     /*
                               1909                 :                :      * Truncate CLOG, multixact and CommitTs to the oldest computed value.
                               1910                 :                :      */
 2579 rhaas@postgresql.org     1911                 :            742 :     TruncateCLOG(frozenXID, oldestxid_datoid);
 3092 alvherre@alvh.no-ip.     1912                 :            742 :     TruncateCommitTs(frozenXID);
 3123 andres@anarazel.de       1913                 :            742 :     TruncateMultiXact(minMulti, minmulti_datoid);
                               1914                 :                : 
                               1915                 :                :     /*
                               1916                 :                :      * Update the wrap limit for GetNewTransactionId and creation of new
                               1917                 :                :      * MultiXactIds.  Note: these functions will also signal the postmaster
                               1918                 :                :      * for an(other) autovac cycle if needed.   XXX should we avoid possibly
                               1919                 :                :      * signaling twice?
                               1920                 :                :      */
 4099 alvherre@alvh.no-ip.     1921                 :            742 :     SetTransactionIdLimit(frozenXID, oldestxid_datoid);
 2588 tgl@sss.pgh.pa.us        1922                 :            742 :     SetMultiXactIdLimit(minMulti, minmulti_datoid, false);
                               1923                 :                : 
 1338 noah@leadboat.com        1924                 :            742 :     LWLockRelease(WrapLimitsVacuumLock);
                               1925                 :                : }
                               1926                 :                : 
                               1927                 :                : 
                               1928                 :                : /*
                               1929                 :                :  *  vacuum_rel() -- vacuum one heap relation
                               1930                 :                :  *
                               1931                 :                :  *      relid identifies the relation to vacuum.  If relation is supplied,
                               1932                 :                :  *      use the name therein for reporting any failure to open/lock the rel;
                               1933                 :                :  *      do not use it once we've successfully opened the rel, since it might
                               1934                 :                :  *      be stale.
                               1935                 :                :  *
                               1936                 :                :  *      Returns true if it's okay to proceed with a requested ANALYZE
                               1937                 :                :  *      operation on this table.
                               1938                 :                :  *
                               1939                 :                :  *      Doing one heap at a time incurs extra overhead, since we need to
                               1940                 :                :  *      check that the heap exists again just before we vacuum it.  The
                               1941                 :                :  *      reason that we do this is so that vacuuming can be spread across
                               1942                 :                :  *      many small transactions.  Otherwise, two-phase locking would require
                               1943                 :                :  *      us to lock the entire database during one pass of the vacuum cleaner.
                               1944                 :                :  *
                               1945                 :                :  *      At entry and exit, we are not inside a transaction.
                               1946                 :                :  */
                               1947                 :                : static bool
  377 drowley@postgresql.o     1948                 :          84070 : vacuum_rel(Oid relid, RangeVar *relation, VacuumParams *params,
                               1949                 :                :            BufferAccessStrategy bstrategy)
                               1950                 :                : {
                               1951                 :                :     LOCKMODE    lmode;
                               1952                 :                :     Relation    rel;
                               1953                 :                :     LockRelId   lockrelid;
                               1954                 :                :     Oid         priv_relid;
                               1955                 :                :     Oid         toast_relid;
                               1956                 :                :     Oid         save_userid;
                               1957                 :                :     int         save_sec_context;
                               1958                 :                :     int         save_nestlevel;
                               1959                 :                : 
 3315 alvherre@alvh.no-ip.     1960         [ -  + ]:          84070 :     Assert(params != NULL);
                               1961                 :                : 
                               1962                 :                :     /* Begin a transaction for vacuuming this relation */
 7641 tgl@sss.pgh.pa.us        1963                 :          84070 :     StartTransactionCommand();
                               1964                 :                : 
 1854 rhaas@postgresql.org     1965         [ +  + ]:          84070 :     if (!(params->options & VACOPT_FULL))
                               1966                 :                :     {
                               1967                 :                :         /*
                               1968                 :                :          * In lazy vacuum, we can set the PROC_IN_VACUUM flag, which lets
                               1969                 :                :          * other concurrent VACUUMs know that they can ignore this one while
                               1970                 :                :          * determining their OldestXmin.  (The reason we don't set it during a
                               1971                 :                :          * full VACUUM is exactly that we may have to run user-defined
                               1972                 :                :          * functions for functional indexes, and we want to make sure that if
                               1973                 :                :          * they use the snapshot set above, any tuples it requires can't get
                               1974                 :                :          * removed from other tables.  An index function that depends on the
                               1975                 :                :          * contents of other tables is arguably broken, but we won't break it
                               1976                 :                :          * here by violating transaction semantics.)
                               1977                 :                :          *
                               1978                 :                :          * We also set the VACUUM_FOR_WRAPAROUND flag, which is passed down by
                               1979                 :                :          * autovacuum; it's used to avoid canceling a vacuum that was invoked
                               1980                 :                :          * in an emergency.
                               1981                 :                :          *
                               1982                 :                :          * Note: these flags remain set until CommitTransaction or
                               1983                 :                :          * AbortTransaction.  We don't want to clear them until we reset
                               1984                 :                :          * MyProc->xid/xmin, otherwise GetOldestNonRemovableTransactionId()
                               1985                 :                :          * might appear to go backwards, which is probably Not Good.  (We also
                               1986                 :                :          * set PROC_IN_VACUUM *before* taking our own snapshot, so that our
                               1987                 :                :          * xmin doesn't become visible ahead of setting the flag.)
                               1988                 :                :          */
 1235 alvherre@alvh.no-ip.     1989                 :          83884 :         LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
 1245                          1990                 :          83884 :         MyProc->statusFlags |= PROC_IN_VACUUM;
 3315                          1991         [ +  + ]:          83884 :         if (params->is_wraparound)
 1245 alvherre@alvh.no-ip.     1992                 :GBC       74126 :             MyProc->statusFlags |= PROC_VACUUM_FOR_WRAPAROUND;
 1245 alvherre@alvh.no-ip.     1993                 :CBC       83884 :         ProcGlobal->statusFlags[MyProc->pgxactoff] = MyProc->statusFlags;
 6017                          1994                 :          83884 :         LWLockRelease(ProcArrayLock);
                               1995                 :                :     }
                               1996                 :                : 
                               1997                 :                :     /*
                               1998                 :                :      * Need to acquire a snapshot to prevent pg_subtrans from being truncated,
                               1999                 :                :      * cutoff xids in local memory wrapping around, and to have updated xmin
                               2000                 :                :      * horizons.
                               2001                 :                :      */
 1235                          2002                 :          84070 :     PushActiveSnapshot(GetTransactionSnapshot());
                               2003                 :                : 
                               2004                 :                :     /*
                               2005                 :                :      * Check for user-requested abort.  Note we want this to be inside a
                               2006                 :                :      * transaction, so xact.c doesn't issue useless WARNING.
                               2007                 :                :      */
 8491 tgl@sss.pgh.pa.us        2008         [ +  + ]:          84070 :     CHECK_FOR_INTERRUPTS();
                               2009                 :                : 
                               2010                 :                :     /*
                               2011                 :                :      * Determine the type of lock we want --- hard exclusive lock for a FULL
                               2012                 :                :      * vacuum, but just ShareUpdateExclusiveLock for concurrent vacuum. Either
                               2013                 :                :      * way, we can be sure that no other backend is vacuuming the same table.
                               2014                 :                :      */
 1854 rhaas@postgresql.org     2015                 :         168140 :     lmode = (params->options & VACOPT_FULL) ?
                               2016         [ +  + ]:          84070 :         AccessExclusiveLock : ShareUpdateExclusiveLock;
                               2017                 :                : 
                               2018                 :                :     /* open the relation and get the appropriate lock on it */
 1105 pg@bowt.ie               2019                 :          84070 :     rel = vacuum_open_relation(relid, relation, params->options,
                               2020                 :          84070 :                                params->log_min_duration >= 0, lmode);
                               2021                 :                : 
                               2022                 :                :     /* leave if relation could not be opened or locked */
                               2023         [ +  + ]:          84070 :     if (!rel)
                               2024                 :                :     {
 5694 alvherre@alvh.no-ip.     2025                 :             12 :         PopActiveSnapshot();
 6449 tgl@sss.pgh.pa.us        2026                 :             12 :         CommitTransactionCommand();
 4815 rhaas@postgresql.org     2027                 :             12 :         return false;
                               2028                 :                :     }
                               2029                 :                : 
                               2030                 :                :     /*
                               2031                 :                :      * When recursing to a TOAST table, check privileges on the parent.  NB:
                               2032                 :                :      * This is only safe to do because we hold a session lock on the main
                               2033                 :                :      * relation that prevents concurrent deletion.
                               2034                 :                :      */
   32 nathan@postgresql.or     2035         [ +  + ]:GNC       84058 :     if (OidIsValid(params->toast_parent))
                               2036                 :           3370 :         priv_relid = params->toast_parent;
                               2037                 :                :     else
                               2038                 :          80688 :         priv_relid = RelationGetRelid(rel);
                               2039                 :                : 
                               2040                 :                :     /*
                               2041                 :                :      * Check if relation needs to be skipped based on privileges.  This check
                               2042                 :                :      * happens also when building the relation list to vacuum for a manual
                               2043                 :                :      * operation, and needs to be done additionally here as VACUUM could
                               2044                 :                :      * happen across multiple transactions where privileges could have changed
                               2045                 :                :      * in-between.  Make sure to only generate logs for VACUUM in this case.
                               2046                 :                :      */
                               2047         [ +  + ]:          84058 :     if (!vacuum_is_permitted_for_relation(priv_relid,
                               2048                 :                :                                           rel->rd_rel,
                               2049                 :          84058 :                                           params->options & ~VACOPT_ANALYZE))
                               2050                 :                :     {
 1105 pg@bowt.ie               2051                 :CBC          36 :         relation_close(rel, lmode);
 5694 alvherre@alvh.no-ip.     2052                 :             36 :         PopActiveSnapshot();
 7641 tgl@sss.pgh.pa.us        2053                 :             36 :         CommitTransactionCommand();
  488 jdavis@postgresql.or     2054                 :             36 :         return false;
                               2055                 :                :     }
                               2056                 :                : 
                               2057                 :                :     /*
                               2058                 :                :      * Check that it's of a vacuumable relkind.
                               2059                 :                :      */
 1105 pg@bowt.ie               2060         [ +  + ]:          84022 :     if (rel->rd_rel->relkind != RELKIND_RELATION &&
                               2061         [ +  + ]:          31053 :         rel->rd_rel->relkind != RELKIND_MATVIEW &&
                               2062         [ +  + ]:          31049 :         rel->rd_rel->relkind != RELKIND_TOASTVALUE &&
                               2063         [ +  + ]:             76 :         rel->rd_rel->relkind != RELKIND_PARTITIONED_TABLE)
                               2064                 :                :     {
 7574 tgl@sss.pgh.pa.us        2065         [ +  - ]:              1 :         ereport(WARNING,
                               2066                 :                :                 (errmsg("skipping \"%s\" --- cannot vacuum non-tables or special system tables",
                               2067                 :                :                         RelationGetRelationName(rel))));
 1105 pg@bowt.ie               2068                 :              1 :         relation_close(rel, lmode);
 5694 alvherre@alvh.no-ip.     2069                 :              1 :         PopActiveSnapshot();
 7641 tgl@sss.pgh.pa.us        2070                 :              1 :         CommitTransactionCommand();
 4815 rhaas@postgresql.org     2071                 :              1 :         return false;
                               2072                 :                :     }
                               2073                 :                : 
                               2074                 :                :     /*
                               2075                 :                :      * Silently ignore tables that are temp tables of other backends ---
                               2076                 :                :      * trying to vacuum these will lead to great unhappiness, since their
                               2077                 :                :      * contents are probably not up-to-date on disk.  (We don't throw a
                               2078                 :                :      * warning here; it would just lead to chatter during a database-wide
                               2079                 :                :      * VACUUM.)
                               2080                 :                :      */
 1105 pg@bowt.ie               2081   [ +  +  -  + ]:          84021 :     if (RELATION_IS_OTHER_TEMP(rel))
                               2082                 :                :     {
 1105 pg@bowt.ie               2083                 :UBC           0 :         relation_close(rel, lmode);
 5694 alvherre@alvh.no-ip.     2084                 :              0 :         PopActiveSnapshot();
 7641 tgl@sss.pgh.pa.us        2085                 :              0 :         CommitTransactionCommand();
 4815 rhaas@postgresql.org     2086                 :              0 :         return false;
                               2087                 :                :     }
                               2088                 :                : 
                               2089                 :                :     /*
                               2090                 :                :      * Silently ignore partitioned tables as there is no work to be done.  The
                               2091                 :                :      * useful work is on their child partitions, which have been queued up for
                               2092                 :                :      * us separately.
                               2093                 :                :      */
 1105 pg@bowt.ie               2094         [ +  + ]:CBC       84021 :     if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
                               2095                 :                :     {
                               2096                 :             75 :         relation_close(rel, lmode);
 2600 rhaas@postgresql.org     2097                 :             75 :         PopActiveSnapshot();
                               2098                 :             75 :         CommitTransactionCommand();
                               2099                 :                :         /* It's OK to proceed with ANALYZE on this table */
                               2100                 :             75 :         return true;
                               2101                 :                :     }
                               2102                 :                : 
                               2103                 :                :     /*
                               2104                 :                :      * Get a session-level lock too. This will protect our access to the
                               2105                 :                :      * relation across multiple transactions, so that we can vacuum the
                               2106                 :                :      * relation's TOAST table (if any) secure in the knowledge that no one is
                               2107                 :                :      * deleting the parent relation.
                               2108                 :                :      *
                               2109                 :                :      * NOTE: this cannot block, even if someone else is waiting for access,
                               2110                 :                :      * because the lock manager knows that both lock requests are from the
                               2111                 :                :      * same process.
                               2112                 :                :      */
 1105 pg@bowt.ie               2113                 :          83946 :     lockrelid = rel->rd_lockInfo.lockRelId;
                               2114                 :          83946 :     LockRelationIdForSession(&lockrelid, lmode);
                               2115                 :                : 
                               2116                 :                :     /*
                               2117                 :                :      * Set index_cleanup option based on index_cleanup reloption if it wasn't
                               2118                 :                :      * specified in VACUUM command, or when running in an autovacuum worker
                               2119                 :                :      */
 1031                          2120         [ +  + ]:          83946 :     if (params->index_cleanup == VACOPTVALUE_UNSPECIFIED)
                               2121                 :                :     {
                               2122                 :                :         StdRdOptIndexCleanup vacuum_index_cleanup;
                               2123                 :                : 
    3 akorotkov@postgresql     2124         [ +  + ]:          76950 :         if (rel->rd_options == NULL)
 1031 pg@bowt.ie               2125                 :          76750 :             vacuum_index_cleanup = STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO;
                               2126                 :                :         else
                               2127                 :            200 :             vacuum_index_cleanup =
    3 akorotkov@postgresql     2128                 :            200 :                 ((StdRdOptions *) rel->rd_options)->vacuum_index_cleanup;
                               2129                 :                : 
 1031 pg@bowt.ie               2130         [ +  + ]:          76950 :         if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_AUTO)
                               2131                 :          76938 :             params->index_cleanup = VACOPTVALUE_AUTO;
                               2132         [ +  + ]:             12 :         else if (vacuum_index_cleanup == STDRD_OPTION_VACUUM_INDEX_CLEANUP_ON)
                               2133                 :              6 :             params->index_cleanup = VACOPTVALUE_ENABLED;
                               2134                 :                :         else
                               2135                 :                :         {
                               2136         [ -  + ]:              6 :             Assert(vacuum_index_cleanup ==
                               2137                 :                :                    STDRD_OPTION_VACUUM_INDEX_CLEANUP_OFF);
                               2138                 :              6 :             params->index_cleanup = VACOPTVALUE_DISABLED;
                               2139                 :                :         }
                               2140                 :                :     }
                               2141                 :                : 
                               2142                 :                :     /*
                               2143                 :                :      * Set truncate option based on truncate reloption if it wasn't specified
                               2144                 :                :      * in VACUUM command, or when running in an autovacuum worker
                               2145                 :                :      */
                               2146         [ +  + ]:          83946 :     if (params->truncate == VACOPTVALUE_UNSPECIFIED)
                               2147                 :                :     {
    3 akorotkov@postgresql     2148         [ +  + ]:          76963 :         if (rel->rd_options == NULL ||
                               2149         [ +  + ]:            200 :             ((StdRdOptions *) rel->rd_options)->vacuum_truncate)
 1031 pg@bowt.ie               2150                 :          76960 :             params->truncate = VACOPTVALUE_ENABLED;
                               2151                 :                :         else
                               2152                 :              3 :             params->truncate = VACOPTVALUE_DISABLED;
                               2153                 :                :     }
                               2154                 :                : 
                               2155                 :                :     /*
                               2156                 :                :      * Remember the relation's TOAST relation for later, if the caller asked
                               2157                 :                :      * us to process it.  In VACUUM FULL, though, the toast table is
                               2158                 :                :      * automatically rebuilt by cluster_rel so we shouldn't recurse to it,
                               2159                 :                :      * unless PROCESS_MAIN is disabled.
                               2160                 :                :      */
 1160 michael@paquier.xyz      2161         [ +  + ]:          83946 :     if ((params->options & VACOPT_PROCESS_TOAST) != 0 &&
  405                          2162         [ +  + ]:           9592 :         ((params->options & VACOPT_FULL) == 0 ||
                               2163         [ +  + ]:            172 :          (params->options & VACOPT_PROCESS_MAIN) == 0))
 1105 pg@bowt.ie               2164                 :           9423 :         toast_relid = rel->rd_rel->reltoastrelid;
                               2165                 :                :     else
 5723 alvherre@alvh.no-ip.     2166                 :          74523 :         toast_relid = InvalidOid;
                               2167                 :                : 
                               2168                 :                :     /*
                               2169                 :                :      * Switch to the table owner's userid, so that any index functions are run
                               2170                 :                :      * as that user.  Also lock down security-restricted operations and
                               2171                 :                :      * arrange to make GUC variable changes local to this command. (This is
                               2172                 :                :      * unnecessary, but harmless, for lazy VACUUM.)
                               2173                 :                :      */
 5240 tgl@sss.pgh.pa.us        2174                 :          83946 :     GetUserIdAndSecContext(&save_userid, &save_sec_context);
 1105 pg@bowt.ie               2175                 :          83946 :     SetUserIdAndSecContext(rel->rd_rel->relowner,
                               2176                 :                :                            save_sec_context | SECURITY_RESTRICTED_OPERATION);
 5240 tgl@sss.pgh.pa.us        2177                 :          83946 :     save_nestlevel = NewGUCNestLevel();
   41 jdavis@postgresql.or     2178                 :GNC       83946 :     RestrictSearchPath();
                               2179                 :                : 
                               2180                 :                :     /*
                               2181                 :                :      * If PROCESS_MAIN is set (the default), it's time to vacuum the main
                               2182                 :                :      * relation.  Otherwise, we can skip this part.  If processing the TOAST
                               2183                 :                :      * table is required (e.g., PROCESS_TOAST is set), we force PROCESS_MAIN
                               2184                 :                :      * to be set when we recurse to the TOAST table.
                               2185                 :                :      */
  403 michael@paquier.xyz      2186         [ +  + ]:CBC       83946 :     if (params->options & VACOPT_PROCESS_MAIN)
                               2187                 :                :     {
                               2188                 :                :         /*
                               2189                 :                :          * Do the actual work --- either FULL or "lazy" vacuum
                               2190                 :                :          */
                               2191         [ +  + ]:          83869 :         if (params->options & VACOPT_FULL)
                               2192                 :                :         {
                               2193                 :            169 :             ClusterParams cluster_params = {0};
                               2194                 :                : 
                               2195                 :                :             /* close relation before vacuuming, but hold lock until commit */
                               2196                 :            169 :             relation_close(rel, NoLock);
                               2197                 :            169 :             rel = NULL;
                               2198                 :                : 
                               2199         [ +  + ]:            169 :             if ((params->options & VACOPT_VERBOSE) != 0)
                               2200                 :              1 :                 cluster_params.options |= CLUOPT_VERBOSE;
                               2201                 :                : 
                               2202                 :                :             /* VACUUM FULL is now a variant of CLUSTER; see cluster.c */
                               2203                 :            169 :             cluster_rel(relid, InvalidOid, &cluster_params);
                               2204                 :                :         }
                               2205                 :                :         else
  377 drowley@postgresql.o     2206                 :          83700 :             table_relation_vacuum(rel, params, bstrategy);
                               2207                 :                :     }
                               2208                 :                : 
                               2209                 :                :     /* Roll back any GUC changes executed by index functions */
 5240 tgl@sss.pgh.pa.us        2210                 :          83943 :     AtEOXact_GUC(false, save_nestlevel);
                               2211                 :                : 
                               2212                 :                :     /* Restore userid and security context */
                               2213                 :          83943 :     SetUserIdAndSecContext(save_userid, save_sec_context);
                               2214                 :                : 
                               2215                 :                :     /* all done with this class, but hold lock until commit */
 1105 pg@bowt.ie               2216         [ +  + ]:          83943 :     if (rel)
                               2217                 :          83777 :         relation_close(rel, NoLock);
                               2218                 :                : 
                               2219                 :                :     /*
                               2220                 :                :      * Complete the transaction and free all temporary memory used.
                               2221                 :                :      */
 5694 alvherre@alvh.no-ip.     2222                 :          83943 :     PopActiveSnapshot();
 7641 tgl@sss.pgh.pa.us        2223                 :          83943 :     CommitTransactionCommand();
                               2224                 :                : 
                               2225                 :                :     /*
                               2226                 :                :      * If the relation has a secondary toast rel, vacuum that too while we
                               2227                 :                :      * still hold the session lock on the main table.  Note however that
                               2228                 :                :      * "analyze" will not get done on the toast table.  This is good, because
                               2229                 :                :      * the toaster always uses hardcoded index access and statistics are
                               2230                 :                :      * totally unimportant for toast relations.
                               2231                 :                :      */
 8312                          2232         [ +  + ]:          83943 :     if (toast_relid != InvalidOid)
                               2233                 :                :     {
                               2234                 :                :         VacuumParams toast_vacuum_params;
                               2235                 :                : 
                               2236                 :                :         /*
                               2237                 :                :          * Force VACOPT_PROCESS_MAIN so vacuum_rel() processes it.  Likewise,
                               2238                 :                :          * set toast_parent so that the privilege checks are done on the main
                               2239                 :                :          * relation.  NB: This is only safe to do because we hold a session
                               2240                 :                :          * lock on the main relation that prevents concurrent deletion.
                               2241                 :                :          */
  405 michael@paquier.xyz      2242                 :           3370 :         memcpy(&toast_vacuum_params, params, sizeof(VacuumParams));
                               2243                 :           3370 :         toast_vacuum_params.options |= VACOPT_PROCESS_MAIN;
   32 nathan@postgresql.or     2244                 :GNC        3370 :         toast_vacuum_params.toast_parent = relid;
                               2245                 :                : 
  299 nathan@postgresql.or     2246                 :CBC        3370 :         vacuum_rel(toast_relid, NULL, &toast_vacuum_params, bstrategy);
                               2247                 :                :     }
                               2248                 :                : 
                               2249                 :                :     /*
                               2250                 :                :      * Now release the session-level lock on the main table.
                               2251                 :                :      */
 1105 pg@bowt.ie               2252                 :          83943 :     UnlockRelationIdForSession(&lockrelid, lmode);
                               2253                 :                : 
                               2254                 :                :     /* Report that we really did it. */
 4815 rhaas@postgresql.org     2255                 :          83943 :     return true;
                               2256                 :                : }
                               2257                 :                : 
                               2258                 :                : 
                               2259                 :                : /*
                               2260                 :                :  * Open all the vacuumable indexes of the given relation, obtaining the
                               2261                 :                :  * specified kind of lock on each.  Return an array of Relation pointers for
                               2262                 :                :  * the indexes into *Irel, and the number of indexes into *nindexes.
                               2263                 :                :  *
                               2264                 :                :  * We consider an index vacuumable if it is marked insertable (indisready).
                               2265                 :                :  * If it isn't, probably a CREATE INDEX CONCURRENTLY command failed early in
                               2266                 :                :  * execution, and what we have is too corrupt to be processable.  We will
                               2267                 :                :  * vacuum even if the index isn't indisvalid; this is important because in a
                               2268                 :                :  * unique index, uniqueness checks will be performed anyway and had better not
                               2269                 :                :  * hit dangling index pointers.
                               2270                 :                :  */
                               2271                 :                : void
 5179 tgl@sss.pgh.pa.us        2272                 :          89691 : vac_open_indexes(Relation relation, LOCKMODE lockmode,
                               2273                 :                :                  int *nindexes, Relation **Irel)
                               2274                 :                : {
                               2275                 :                :     List       *indexoidlist;
                               2276                 :                :     ListCell   *indexoidscan;
                               2277                 :                :     int         i;
                               2278                 :                : 
                               2279         [ -  + ]:          89691 :     Assert(lockmode != NoLock);
                               2280                 :                : 
                               2281                 :          89691 :     indexoidlist = RelationGetIndexList(relation);
                               2282                 :                : 
                               2283                 :                :     /* allocate enough memory for all indexes */
 4155                          2284                 :          89691 :     i = list_length(indexoidlist);
                               2285                 :                : 
                               2286         [ +  + ]:          89691 :     if (i > 0)
                               2287                 :          84327 :         *Irel = (Relation *) palloc(i * sizeof(Relation));
                               2288                 :                :     else
 5179                          2289                 :           5364 :         *Irel = NULL;
                               2290                 :                : 
                               2291                 :                :     /* collect just the ready indexes */
                               2292                 :          89691 :     i = 0;
                               2293   [ +  +  +  +  :         223283 :     foreach(indexoidscan, indexoidlist)
                                              +  + ]
                               2294                 :                :     {
                               2295                 :         133592 :         Oid         indexoid = lfirst_oid(indexoidscan);
                               2296                 :                :         Relation    indrel;
                               2297                 :                : 
 4155                          2298                 :         133592 :         indrel = index_open(indexoid, lockmode);
 1935 peter_e@gmx.net          2299         [ +  - ]:         133592 :         if (indrel->rd_index->indisready)
 4155 tgl@sss.pgh.pa.us        2300                 :         133592 :             (*Irel)[i++] = indrel;
                               2301                 :                :         else
 4155 tgl@sss.pgh.pa.us        2302                 :UBC           0 :             index_close(indrel, lockmode);
                               2303                 :                :     }
                               2304                 :                : 
 4155 tgl@sss.pgh.pa.us        2305                 :CBC       89691 :     *nindexes = i;
                               2306                 :                : 
 5179                          2307                 :          89691 :     list_free(indexoidlist);
 8721 bruce@momjian.us         2308                 :          89691 : }
                               2309                 :                : 
                               2310                 :                : /*
                               2311                 :                :  * Release the resources acquired by vac_open_indexes.  Optionally release
                               2312                 :                :  * the locks (say NoLock to keep 'em).
                               2313                 :                :  */
                               2314                 :                : void
 5179 tgl@sss.pgh.pa.us        2315                 :          90030 : vac_close_indexes(int nindexes, Relation *Irel, LOCKMODE lockmode)
                               2316                 :                : {
                               2317         [ +  + ]:          90030 :     if (Irel == NULL)
                               2318                 :           5706 :         return;
                               2319                 :                : 
                               2320         [ +  + ]:         217910 :     while (nindexes--)
                               2321                 :                :     {
                               2322                 :         133586 :         Relation    ind = Irel[nindexes];
                               2323                 :                : 
 6467                          2324                 :         133586 :         index_close(ind, lockmode);
                               2325                 :                :     }
 9716 bruce@momjian.us         2326                 :          84324 :     pfree(Irel);
                               2327                 :                : }
                               2328                 :                : 
                               2329                 :                : /*
                               2330                 :                :  * vacuum_delay_point --- check for interrupts and cost-based delay.
                               2331                 :                :  *
                               2332                 :                :  * This should be called in each major loop of VACUUM processing,
                               2333                 :                :  * typically once per page processed.
                               2334                 :                :  */
                               2335                 :                : void
 7369 tgl@sss.pgh.pa.us        2336                 :       34721397 : vacuum_delay_point(void)
                               2337                 :                : {
 1546 akapila@postgresql.o     2338                 :       34721397 :     double      msec = 0;
                               2339                 :                : 
                               2340                 :                :     /* Always check for interrupts */
 7369 tgl@sss.pgh.pa.us        2341         [ +  + ]:       34721397 :     CHECK_FOR_INTERRUPTS();
                               2342                 :                : 
  373 dgustafsson@postgres     2343         [ +  + ]:       34721397 :     if (InterruptPending ||
                               2344   [ +  +  +  - ]:       34721396 :         (!VacuumCostActive && !ConfigReloadPending))
                               2345                 :       29807789 :         return;
                               2346                 :                : 
                               2347                 :                :     /*
                               2348                 :                :      * Autovacuum workers should reload the configuration file if requested.
                               2349                 :                :      * This allows changes to [autovacuum_]vacuum_cost_limit and
                               2350                 :                :      * [autovacuum_]vacuum_cost_delay to take effect while a table is being
                               2351                 :                :      * vacuumed or analyzed.
                               2352                 :                :      */
   41 heikki.linnakangas@i     2353   [ -  +  -  - ]:GNC     4913608 :     if (ConfigReloadPending && AmAutoVacuumWorkerProcess())
                               2354                 :                :     {
  373 dgustafsson@postgres     2355                 :UBC           0 :         ConfigReloadPending = false;
                               2356                 :              0 :         ProcessConfigFile(PGC_SIGHUP);
                               2357                 :              0 :         VacuumUpdateCosts();
                               2358                 :                :     }
                               2359                 :                : 
                               2360                 :                :     /*
                               2361                 :                :      * If we disabled cost-based delays after reloading the config file,
                               2362                 :                :      * return.
                               2363                 :                :      */
  373 dgustafsson@postgres     2364         [ -  + ]:CBC     4913608 :     if (!VacuumCostActive)
 1546 akapila@postgresql.o     2365                 :UBC           0 :         return;
                               2366                 :                : 
                               2367                 :                :     /*
                               2368                 :                :      * For parallel vacuum, the delay is computed based on the shared cost
                               2369                 :                :      * balance.  See compute_parallel_delay.
                               2370                 :                :      */
 1546 akapila@postgresql.o     2371         [ -  + ]:CBC     4913608 :     if (VacuumSharedCostBalance != NULL)
 1546 akapila@postgresql.o     2372                 :UBC           0 :         msec = compute_parallel_delay();
  373 dgustafsson@postgres     2373         [ +  + ]:CBC     4913608 :     else if (VacuumCostBalance >= vacuum_cost_limit)
                               2374                 :           2061 :         msec = vacuum_cost_delay * VacuumCostBalance / vacuum_cost_limit;
                               2375                 :                : 
                               2376                 :                :     /* Nap if appropriate */
 1546 akapila@postgresql.o     2377         [ +  + ]:        4913608 :     if (msec > 0)
                               2378                 :                :     {
  373 dgustafsson@postgres     2379         [ +  + ]:           2061 :         if (msec > vacuum_cost_delay * 4)
                               2380                 :              8 :             msec = vacuum_cost_delay * 4;
                               2381                 :                : 
  396 tmunro@postgresql.or     2382                 :           2061 :         pgstat_report_wait_start(WAIT_EVENT_VACUUM_DELAY);
                               2383                 :           2061 :         pg_usleep(msec * 1000);
                               2384                 :           2061 :         pgstat_report_wait_end();
                               2385                 :                : 
                               2386                 :                :         /*
                               2387                 :                :          * We don't want to ignore postmaster death during very long vacuums
                               2388                 :                :          * with vacuum_cost_delay configured.  We can't use the usual
                               2389                 :                :          * WaitLatch() approach here because we want microsecond-based sleep
                               2390                 :                :          * durations above.
                               2391                 :                :          */
                               2392   [ +  -  -  + ]:           2061 :         if (IsUnderPostmaster && !PostmasterIsAlive())
  396 tmunro@postgresql.or     2393                 :UBC           0 :             exit(1);
                               2394                 :                : 
 7369 tgl@sss.pgh.pa.us        2395                 :CBC        2061 :         VacuumCostBalance = 0;
                               2396                 :                : 
                               2397                 :                :         /*
                               2398                 :                :          * Balance and update limit values for autovacuum workers. We must do
                               2399                 :                :          * this periodically, as the number of workers across which we are
                               2400                 :                :          * balancing the limit may have changed.
                               2401                 :                :          *
                               2402                 :                :          * TODO: There may be better criteria for determining when to do this
                               2403                 :                :          * besides "check after napping".
                               2404                 :                :          */
  373 dgustafsson@postgres     2405                 :           2061 :         AutoVacuumUpdateCostLimit();
                               2406                 :                : 
                               2407                 :                :         /* Might have gotten an interrupt while sleeping */
 7369 tgl@sss.pgh.pa.us        2408         [ -  + ]:           2061 :         CHECK_FOR_INTERRUPTS();
                               2409                 :                :     }
                               2410                 :                : }
                               2411                 :                : 
                               2412                 :                : /*
                               2413                 :                :  * Computes the vacuum delay for parallel workers.
                               2414                 :                :  *
                               2415                 :                :  * The basic idea of a cost-based delay for parallel vacuum is to allow each
                               2416                 :                :  * worker to sleep in proportion to the share of work it's done.  We achieve this
                               2417                 :                :  * by allowing all parallel vacuum workers including the leader process to
                               2418                 :                :  * have a shared view of cost related parameters (mainly VacuumCostBalance).
                               2419                 :                :  * We allow each worker to update it as and when it has incurred any cost and
                               2420                 :                :  * then based on that decide whether it needs to sleep.  We compute the time
                               2421                 :                :  * to sleep for a worker based on the cost it has incurred
                               2422                 :                :  * (VacuumCostBalanceLocal) and then reduce the VacuumSharedCostBalance by
                               2423                 :                :  * that amount.  This avoids putting to sleep those workers which have done less
                               2424                 :                :  * I/O than other workers and therefore ensure that workers
                               2425                 :                :  * which are doing more I/O got throttled more.
                               2426                 :                :  *
                               2427                 :                :  * We allow a worker to sleep only if it has performed I/O above a certain
                               2428                 :                :  * threshold, which is calculated based on the number of active workers
                               2429                 :                :  * (VacuumActiveNWorkers), and the overall cost balance is more than
                               2430                 :                :  * VacuumCostLimit set by the system.  Testing reveals that we achieve
                               2431                 :                :  * the required throttling if we force a worker that has done more than 50%
                               2432                 :                :  * of its share of work to sleep.
                               2433                 :                :  */
                               2434                 :                : static double
 1546 akapila@postgresql.o     2435                 :UBC           0 : compute_parallel_delay(void)
                               2436                 :                : {
                               2437                 :              0 :     double      msec = 0;
                               2438                 :                :     uint32      shared_balance;
                               2439                 :                :     int         nworkers;
                               2440                 :                : 
                               2441                 :                :     /* Parallel vacuum must be active */
                               2442         [ #  # ]:              0 :     Assert(VacuumSharedCostBalance);
                               2443                 :                : 
                               2444                 :              0 :     nworkers = pg_atomic_read_u32(VacuumActiveNWorkers);
                               2445                 :                : 
                               2446                 :                :     /* At least count itself */
                               2447         [ #  # ]:              0 :     Assert(nworkers >= 1);
                               2448                 :                : 
                               2449                 :                :     /* Update the shared cost balance value atomically */
                               2450                 :              0 :     shared_balance = pg_atomic_add_fetch_u32(VacuumSharedCostBalance, VacuumCostBalance);
                               2451                 :                : 
                               2452                 :                :     /* Compute the total local balance for the current worker */
                               2453                 :              0 :     VacuumCostBalanceLocal += VacuumCostBalance;
                               2454                 :                : 
  373 dgustafsson@postgres     2455         [ #  # ]:              0 :     if ((shared_balance >= vacuum_cost_limit) &&
                               2456         [ #  # ]:              0 :         (VacuumCostBalanceLocal > 0.5 * ((double) vacuum_cost_limit / nworkers)))
                               2457                 :                :     {
                               2458                 :                :         /* Compute sleep time based on the local cost balance */
                               2459                 :              0 :         msec = vacuum_cost_delay * VacuumCostBalanceLocal / vacuum_cost_limit;
 1546 akapila@postgresql.o     2460                 :              0 :         pg_atomic_sub_fetch_u32(VacuumSharedCostBalance, VacuumCostBalanceLocal);
                               2461                 :              0 :         VacuumCostBalanceLocal = 0;
                               2462                 :                :     }
                               2463                 :                : 
                               2464                 :                :     /*
                               2465                 :                :      * Reset the local balance as we accumulated it into the shared value.
                               2466                 :                :      */
                               2467                 :              0 :     VacuumCostBalance = 0;
                               2468                 :                : 
                               2469                 :              0 :     return msec;
                               2470                 :                : }
                               2471                 :                : 
                               2472                 :                : /*
                               2473                 :                :  * A wrapper function of defGetBoolean().
                               2474                 :                :  *
                               2475                 :                :  * This function returns VACOPTVALUE_ENABLED and VACOPTVALUE_DISABLED instead
                               2476                 :                :  * of true and false.
                               2477                 :                :  */
                               2478                 :                : static VacOptValue
 1031 pg@bowt.ie               2479                 :CBC         158 : get_vacoptval_from_boolean(DefElem *def)
                               2480                 :                : {
                               2481         [ +  + ]:            158 :     return defGetBoolean(def) ? VACOPTVALUE_ENABLED : VACOPTVALUE_DISABLED;
                               2482                 :                : }
                               2483                 :                : 
                               2484                 :                : /*
                               2485                 :                :  *  vac_bulkdel_one_index() -- bulk-deletion for index relation.
                               2486                 :                :  *
                               2487                 :                :  * Returns bulk delete stats derived from input stats
                               2488                 :                :  */
                               2489                 :                : IndexBulkDeleteResult *
  844 akapila@postgresql.o     2490                 :           1032 : vac_bulkdel_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat,
                               2491                 :                :                       TidStore *dead_items, VacDeadItemsInfo *dead_items_info)
                               2492                 :                : {
                               2493                 :                :     /* Do bulk deletion */
                               2494                 :           1032 :     istat = index_bulk_delete(ivinfo, istat, vac_tid_reaped,
                               2495                 :                :                               (void *) dead_items);
                               2496                 :                : 
                               2497         [ -  + ]:           1032 :     ereport(ivinfo->message_level,
                               2498                 :                :             (errmsg("scanned index \"%s\" to remove %lld row versions",
                               2499                 :                :                     RelationGetRelationName(ivinfo->index),
                               2500                 :                :                     (long long) dead_items_info->num_items)));
                               2501                 :                : 
                               2502                 :           1032 :     return istat;
                               2503                 :                : }
                               2504                 :                : 
                               2505                 :                : /*
                               2506                 :                :  *  vac_cleanup_one_index() -- do post-vacuum cleanup for index relation.
                               2507                 :                :  *
                               2508                 :                :  * Returns bulk delete stats derived from input stats
                               2509                 :                :  */
                               2510                 :                : IndexBulkDeleteResult *
                               2511                 :          93965 : vac_cleanup_one_index(IndexVacuumInfo *ivinfo, IndexBulkDeleteResult *istat)
                               2512                 :                : {
                               2513                 :          93965 :     istat = index_vacuum_cleanup(ivinfo, istat);
                               2514                 :                : 
                               2515         [ +  + ]:          93965 :     if (istat)
                               2516         [ -  + ]:           1181 :         ereport(ivinfo->message_level,
                               2517                 :                :                 (errmsg("index \"%s\" now contains %.0f row versions in %u pages",
                               2518                 :                :                         RelationGetRelationName(ivinfo->index),
                               2519                 :                :                         istat->num_index_tuples,
                               2520                 :                :                         istat->num_pages),
                               2521                 :                :                  errdetail("%.0f index row versions were removed.\n"
                               2522                 :                :                            "%u index pages were newly deleted.\n"
                               2523                 :                :                            "%u index pages are currently deleted, of which %u are currently reusable.",
                               2524                 :                :                            istat->tuples_removed,
                               2525                 :                :                            istat->pages_newly_deleted,
                               2526                 :                :                            istat->pages_deleted, istat->pages_free)));
                               2527                 :                : 
                               2528                 :          93965 :     return istat;
                               2529                 :                : }
                               2530                 :                : 
                               2531                 :                : /*
                               2532                 :                :  *  vac_tid_reaped() -- is a particular tid deletable?
                               2533                 :                :  *
                               2534                 :                :  *      This has the right signature to be an IndexBulkDeleteCallback.
                               2535                 :                :  */
                               2536                 :                : static bool
                               2537                 :        3057708 : vac_tid_reaped(ItemPointer itemptr, void *state)
                               2538                 :                : {
   12 msawada@postgresql.o     2539                 :GNC     3057708 :     TidStore   *dead_items = (TidStore *) state;
                               2540                 :                : 
                               2541                 :        3057708 :     return TidStoreIsMember(dead_items, itemptr);
                               2542                 :                : }
        

Generated by: LCOV version 2.1-beta2-3-g6141622