LCOV - differential code coverage report
Current view: top level - src/backend/commands - vacuum.c (source / functions) Coverage Total Hit UNC UBC GBC GNC CBC DUB DCB
Current: a2387c32f2f8a1643c7d71b951587e6bcb2d4744 vs 371a302eecdc82274b0ae2967d18fd726a0aa6a1 Lines: 88.8 % 779 692 1 86 52 640 1 56
Current Date: 2025-10-26 12:31:50 -0700 Functions: 95.5 % 22 21 1 4 17 3
Baseline: lcov-20251027-010456-baseline Branches: 75.5 % 678 512 4 162 1 44 467
Baseline Date: 2025-10-26 11:01:32 +1300 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 100.0 % 8 8 8
(30,360] days: 74.1 % 81 60 1 20 44 16
(360..) days: 90.4 % 690 624 66 624
Function coverage date bins:
(30,360] days: 100.0 % 4 4 3 1
(360..) days: 94.4 % 18 17 1 1 16
Branch coverage date bins:
(7,30] days: 83.3 % 6 5 1 5
(30,360] days: 79.4 % 68 54 3 11 39 15
(360..) days: 75.0 % 604 453 151 1 452

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

Generated by: LCOV version 2.4-beta