LCOV - differential code coverage report
Current view: top level - src/backend/postmaster - autovacuum.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GIC GNC CBC DUB DCB
Current: bed3ffbf9d952be6c7d739d068cdce44c046dfb7 vs 574581b50ac9c63dd9e4abebb731a3b67e5b50f6 Lines: 79.2 % 1070 847 47 176 17 1 117 712 3 90
Current Date: 2026-05-05 10:23:31 +0900 Functions: 94.1 % 34 32 1 1 12 20 3
Baseline: lcov-20260505-025707-baseline Branches: 62.8 % 670 421 47 1 201 15 71 335 16 42
Baseline Date: 2026-05-05 10:27:06 +0900 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(7,30] days: 46.3 % 67 31 36 31
(30,360] days: 89.2 % 120 107 11 2 2 86 19
(360..) days: 80.3 % 883 709 174 15 1 693
Function coverage date bins:
(7,30] days: 66.7 % 3 2 1 2
(30,360] days: 100.0 % 1 1 1
(360..) days: 96.7 % 30 29 1 9 20
Branch coverage date bins:
(7,30] days: 36.4 % 22 8 14 8
(30,360] days: 68.8 % 112 77 33 2 2 63 12
(360..) days: 62.7 % 536 336 1 199 13 323

 Age         Owner                    Branch data    TLA  Line data    Source code
                                  1                 :                : /*-------------------------------------------------------------------------
                                  2                 :                :  *
                                  3                 :                :  * autovacuum.c
                                  4                 :                :  *
                                  5                 :                :  * PostgreSQL Integrated Autovacuum Daemon
                                  6                 :                :  *
                                  7                 :                :  * The autovacuum system is structured in two different kinds of processes: the
                                  8                 :                :  * autovacuum launcher and the autovacuum worker.  The launcher is an
                                  9                 :                :  * always-running process, started by the postmaster when the autovacuum GUC
                                 10                 :                :  * parameter is set.  The launcher schedules autovacuum workers to be started
                                 11                 :                :  * when appropriate.  The workers are the processes which execute the actual
                                 12                 :                :  * vacuuming; they connect to a database as determined in the launcher, and
                                 13                 :                :  * once connected they examine the catalogs to select the tables to vacuum.
                                 14                 :                :  *
                                 15                 :                :  * The autovacuum launcher cannot start the worker processes by itself,
                                 16                 :                :  * because doing so would cause robustness issues (namely, failure to shut
                                 17                 :                :  * them down on exceptional conditions, and also, since the launcher is
                                 18                 :                :  * connected to shared memory and is thus subject to corruption there, it is
                                 19                 :                :  * not as robust as the postmaster).  So it leaves that task to the postmaster.
                                 20                 :                :  *
                                 21                 :                :  * There is an autovacuum shared memory area, where the launcher stores
                                 22                 :                :  * information about the database it wants vacuumed.  When it wants a new
                                 23                 :                :  * worker to start, it sets a flag in shared memory and sends a signal to the
                                 24                 :                :  * postmaster.  Then postmaster knows nothing more than it must start a worker;
                                 25                 :                :  * so it forks a new child, which turns into a worker.  This new process
                                 26                 :                :  * connects to shared memory, and there it can inspect the information that the
                                 27                 :                :  * launcher has set up.
                                 28                 :                :  *
                                 29                 :                :  * If the fork() call fails in the postmaster, it sets a flag in the shared
                                 30                 :                :  * memory area, and sends a signal to the launcher.  The launcher, upon
                                 31                 :                :  * noticing the flag, can try starting the worker again by resending the
                                 32                 :                :  * signal.  Note that the failure can only be transient (fork failure due to
                                 33                 :                :  * high load, memory pressure, too many processes, etc); more permanent
                                 34                 :                :  * problems, like failure to connect to a database, are detected later in the
                                 35                 :                :  * worker and dealt with just by having the worker exit normally.  The launcher
                                 36                 :                :  * will launch a new worker again later, per schedule.
                                 37                 :                :  *
                                 38                 :                :  * When the worker is done vacuuming it sends SIGUSR2 to the launcher.  The
                                 39                 :                :  * launcher then wakes up and is able to launch another worker, if the schedule
                                 40                 :                :  * is so tight that a new worker is needed immediately.  At this time the
                                 41                 :                :  * launcher can also balance the settings for the various remaining workers'
                                 42                 :                :  * cost-based vacuum delay feature.
                                 43                 :                :  *
                                 44                 :                :  * Note that there can be more than one worker in a database concurrently.
                                 45                 :                :  * They will store the table they are currently vacuuming in shared memory, so
                                 46                 :                :  * that other workers avoid being blocked waiting for the vacuum lock for that
                                 47                 :                :  * table.  They will also fetch the last time the table was vacuumed from
                                 48                 :                :  * pgstats just before vacuuming each table, to avoid vacuuming a table that
                                 49                 :                :  * was just finished being vacuumed by another worker and thus is no longer
                                 50                 :                :  * noted in shared memory.  However, there is a small window (due to not yet
                                 51                 :                :  * holding the relation lock) during which a worker may choose a table that was
                                 52                 :                :  * already vacuumed; this is a bug in the current design.
                                 53                 :                :  *
                                 54                 :                :  * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
                                 55                 :                :  * Portions Copyright (c) 1994, Regents of the University of California
                                 56                 :                :  *
                                 57                 :                :  *
                                 58                 :                :  * IDENTIFICATION
                                 59                 :                :  *    src/backend/postmaster/autovacuum.c
                                 60                 :                :  *
                                 61                 :                :  *-------------------------------------------------------------------------
                                 62                 :                :  */
                                 63                 :                : #include "postgres.h"
                                 64                 :                : 
                                 65                 :                : #include <math.h>
                                 66                 :                : #include <signal.h>
                                 67                 :                : #include <sys/time.h>
                                 68                 :                : #include <unistd.h>
                                 69                 :                : 
                                 70                 :                : #include "access/heapam.h"
                                 71                 :                : #include "access/htup_details.h"
                                 72                 :                : #include "access/multixact.h"
                                 73                 :                : #include "access/reloptions.h"
                                 74                 :                : #include "access/tableam.h"
                                 75                 :                : #include "access/transam.h"
                                 76                 :                : #include "access/xact.h"
                                 77                 :                : #include "catalog/dependency.h"
                                 78                 :                : #include "catalog/namespace.h"
                                 79                 :                : #include "catalog/pg_database.h"
                                 80                 :                : #include "catalog/pg_namespace.h"
                                 81                 :                : #include "commands/vacuum.h"
                                 82                 :                : #include "common/int.h"
                                 83                 :                : #include "funcapi.h"
                                 84                 :                : #include "lib/ilist.h"
                                 85                 :                : #include "libpq/pqsignal.h"
                                 86                 :                : #include "miscadmin.h"
                                 87                 :                : #include "nodes/makefuncs.h"
                                 88                 :                : #include "pgstat.h"
                                 89                 :                : #include "postmaster/autovacuum.h"
                                 90                 :                : #include "postmaster/interrupt.h"
                                 91                 :                : #include "postmaster/postmaster.h"
                                 92                 :                : #include "storage/aio_subsys.h"
                                 93                 :                : #include "storage/bufmgr.h"
                                 94                 :                : #include "storage/ipc.h"
                                 95                 :                : #include "storage/fd.h"
                                 96                 :                : #include "storage/latch.h"
                                 97                 :                : #include "storage/lmgr.h"
                                 98                 :                : #include "storage/pmsignal.h"
                                 99                 :                : #include "storage/proc.h"
                                100                 :                : #include "storage/procsignal.h"
                                101                 :                : #include "storage/smgr.h"
                                102                 :                : #include "storage/subsystems.h"
                                103                 :                : #include "tcop/tcopprot.h"
                                104                 :                : #include "utils/fmgroids.h"
                                105                 :                : #include "utils/fmgrprotos.h"
                                106                 :                : #include "utils/guc_hooks.h"
                                107                 :                : #include "utils/injection_point.h"
                                108                 :                : #include "utils/lsyscache.h"
                                109                 :                : #include "utils/memutils.h"
                                110                 :                : #include "utils/ps_status.h"
                                111                 :                : #include "utils/rel.h"
                                112                 :                : #include "utils/snapmgr.h"
                                113                 :                : #include "utils/syscache.h"
                                114                 :                : #include "utils/timeout.h"
                                115                 :                : #include "utils/timestamp.h"
                                116                 :                : #include "utils/tuplestore.h"
                                117                 :                : #include "utils/wait_event.h"
                                118                 :                : 
                                119                 :                : 
                                120                 :                : /*
                                121                 :                :  * GUC parameters
                                122                 :                :  */
                                123                 :                : bool        autovacuum_start_daemon = false;
                                124                 :                : int         autovacuum_worker_slots;
                                125                 :                : int         autovacuum_max_workers;
                                126                 :                : int         autovacuum_work_mem = -1;
                                127                 :                : int         autovacuum_naptime;
                                128                 :                : int         autovacuum_vac_thresh;
                                129                 :                : int         autovacuum_vac_max_thresh;
                                130                 :                : double      autovacuum_vac_scale;
                                131                 :                : int         autovacuum_vac_ins_thresh;
                                132                 :                : double      autovacuum_vac_ins_scale;
                                133                 :                : int         autovacuum_anl_thresh;
                                134                 :                : double      autovacuum_anl_scale;
                                135                 :                : int         autovacuum_freeze_max_age;
                                136                 :                : int         autovacuum_multixact_freeze_max_age;
                                137                 :                : double      autovacuum_freeze_score_weight = 1.0;
                                138                 :                : double      autovacuum_multixact_freeze_score_weight = 1.0;
                                139                 :                : double      autovacuum_vacuum_score_weight = 1.0;
                                140                 :                : double      autovacuum_vacuum_insert_score_weight = 1.0;
                                141                 :                : double      autovacuum_analyze_score_weight = 1.0;
                                142                 :                : double      autovacuum_vac_cost_delay;
                                143                 :                : int         autovacuum_vac_cost_limit;
                                144                 :                : 
                                145                 :                : int         Log_autovacuum_min_duration = 600000;
                                146                 :                : int         Log_autoanalyze_min_duration = 600000;
                                147                 :                : 
                                148                 :                : /* the minimum allowed time between two awakenings of the launcher */
                                149                 :                : #define MIN_AUTOVAC_SLEEPTIME 100.0 /* milliseconds */
                                150                 :                : #define MAX_AUTOVAC_SLEEPTIME 300   /* seconds */
                                151                 :                : 
                                152                 :                : /*
                                153                 :                :  * Variables to save the cost-related storage parameters for the current
                                154                 :                :  * relation being vacuumed by this autovacuum worker. Using these, we can
                                155                 :                :  * ensure we don't overwrite the values of vacuum_cost_delay and
                                156                 :                :  * vacuum_cost_limit after reloading the configuration file. They are
                                157                 :                :  * initialized to "invalid" values to indicate that no cost-related storage
                                158                 :                :  * parameters were specified and will be set in do_autovacuum() after checking
                                159                 :                :  * the storage parameters in table_recheck_autovac().
                                160                 :                :  */
                                161                 :                : static double av_storage_param_cost_delay = -1;
                                162                 :                : static int  av_storage_param_cost_limit = -1;
                                163                 :                : 
                                164                 :                : /* Flags set by signal handlers */
                                165                 :                : static volatile sig_atomic_t got_SIGUSR2 = false;
                                166                 :                : 
                                167                 :                : /* Comparison points for determining whether freeze_max_age is exceeded */
                                168                 :                : static TransactionId recentXid;
                                169                 :                : static MultiXactId recentMulti;
                                170                 :                : 
                                171                 :                : /* Default freeze ages to use for autovacuum (varies by database) */
                                172                 :                : static int  default_freeze_min_age;
                                173                 :                : static int  default_freeze_table_age;
                                174                 :                : static int  default_multixact_freeze_min_age;
                                175                 :                : static int  default_multixact_freeze_table_age;
                                176                 :                : 
                                177                 :                : /* Memory context for long-lived data */
                                178                 :                : static MemoryContext AutovacMemCxt;
                                179                 :                : 
                                180                 :                : /* struct to keep track of databases in launcher */
                                181                 :                : typedef struct avl_dbase
                                182                 :                : {
                                183                 :                :     Oid         adl_datid;      /* hash key -- must be first */
                                184                 :                :     TimestampTz adl_next_worker;
                                185                 :                :     int         adl_score;
                                186                 :                :     dlist_node  adl_node;
                                187                 :                : } avl_dbase;
                                188                 :                : 
                                189                 :                : /* struct to keep track of databases in worker */
                                190                 :                : typedef struct avw_dbase
                                191                 :                : {
                                192                 :                :     Oid         adw_datid;
                                193                 :                :     char       *adw_name;
                                194                 :                :     TransactionId adw_frozenxid;
                                195                 :                :     MultiXactId adw_minmulti;
                                196                 :                :     PgStat_StatDBEntry *adw_entry;
                                197                 :                : } avw_dbase;
                                198                 :                : 
                                199                 :                : /* struct to keep track of tables to vacuum and/or analyze, in 1st pass */
                                200                 :                : typedef struct av_relation
                                201                 :                : {
                                202                 :                :     Oid         ar_toastrelid;  /* hash key - must be first */
                                203                 :                :     Oid         ar_relid;
                                204                 :                :     bool        ar_hasrelopts;
                                205                 :                :     AutoVacOpts ar_reloptions;  /* copy of AutoVacOpts from the main table's
                                206                 :                :                                  * reloptions, or NULL if none */
                                207                 :                : } av_relation;
                                208                 :                : 
                                209                 :                : /* struct to keep track of tables to vacuum and/or analyze, after rechecking */
                                210                 :                : typedef struct autovac_table
                                211                 :                : {
                                212                 :                :     Oid         at_relid;
                                213                 :                :     VacuumParams at_params;
                                214                 :                :     double      at_storage_param_vac_cost_delay;
                                215                 :                :     int         at_storage_param_vac_cost_limit;
                                216                 :                :     bool        at_dobalance;
                                217                 :                :     char       *at_relname;
                                218                 :                :     char       *at_nspname;
                                219                 :                :     char       *at_datname;
                                220                 :                : } autovac_table;
                                221                 :                : 
                                222                 :                : /*-------------
                                223                 :                :  * This struct holds information about a single worker's whereabouts.  We keep
                                224                 :                :  * an array of these in shared memory, sized according to
                                225                 :                :  * autovacuum_worker_slots.
                                226                 :                :  *
                                227                 :                :  * wi_links     entry into free list or running list
                                228                 :                :  * wi_dboid     OID of the database this worker is supposed to work on
                                229                 :                :  * wi_tableoid  OID of the table currently being vacuumed, if any
                                230                 :                :  * wi_sharedrel flag indicating whether table is marked relisshared
                                231                 :                :  * wi_proc      pointer to PGPROC of the running worker, NULL if not started
                                232                 :                :  * wi_launchtime Time at which this worker was launched
                                233                 :                :  * wi_dobalance Whether this worker should be included in balance calculations
                                234                 :                :  *
                                235                 :                :  * All fields are protected by AutovacuumLock, except for wi_tableoid and
                                236                 :                :  * wi_sharedrel which are protected by AutovacuumScheduleLock (note these
                                237                 :                :  * two fields are read-only for everyone except that worker itself).
                                238                 :                :  *-------------
                                239                 :                :  */
                                240                 :                : typedef struct WorkerInfoData
                                241                 :                : {
                                242                 :                :     dlist_node  wi_links;
                                243                 :                :     Oid         wi_dboid;
                                244                 :                :     Oid         wi_tableoid;
                                245                 :                :     PGPROC     *wi_proc;
                                246                 :                :     TimestampTz wi_launchtime;
                                247                 :                :     pg_atomic_flag wi_dobalance;
                                248                 :                :     bool        wi_sharedrel;
                                249                 :                : } WorkerInfoData;
                                250                 :                : 
                                251                 :                : typedef struct WorkerInfoData *WorkerInfo;
                                252                 :                : 
                                253                 :                : /*
                                254                 :                :  * Possible signals received by the launcher from remote processes.  These are
                                255                 :                :  * stored atomically in shared memory so that other processes can set them
                                256                 :                :  * without locking.
                                257                 :                :  */
                                258                 :                : typedef enum
                                259                 :                : {
                                260                 :                :     AutoVacForkFailed,          /* failed trying to start a worker */
                                261                 :                :     AutoVacRebalance,           /* rebalance the cost limits */
                                262                 :                : }           AutoVacuumSignal;
                                263                 :                : 
                                264                 :                : #define AutoVacNumSignals (AutoVacRebalance + 1)
                                265                 :                : 
                                266                 :                : /*
                                267                 :                :  * Autovacuum workitem array, stored in AutoVacuumShmem->av_workItems.  This
                                268                 :                :  * list is mostly protected by AutovacuumLock, except that if an item is
                                269                 :                :  * marked 'active' other processes must not modify the work-identifying
                                270                 :                :  * members.
                                271                 :                :  */
                                272                 :                : typedef struct AutoVacuumWorkItem
                                273                 :                : {
                                274                 :                :     AutoVacuumWorkItemType avw_type;
                                275                 :                :     bool        avw_used;       /* below data is valid */
                                276                 :                :     bool        avw_active;     /* being processed */
                                277                 :                :     Oid         avw_database;
                                278                 :                :     Oid         avw_relation;
                                279                 :                :     BlockNumber avw_blockNumber;
                                280                 :                : } AutoVacuumWorkItem;
                                281                 :                : 
                                282                 :                : #define NUM_WORKITEMS   256
                                283                 :                : 
                                284                 :                : /*-------------
                                285                 :                :  * The main autovacuum shmem struct.  On shared memory we store this main
                                286                 :                :  * struct and the array of WorkerInfo structs.  This struct keeps:
                                287                 :                :  *
                                288                 :                :  * av_signal        set by other processes to indicate various conditions
                                289                 :                :  * av_launcherpid   the PID of the autovacuum launcher
                                290                 :                :  * av_freeWorkers   the WorkerInfo freelist
                                291                 :                :  * av_runningWorkers the WorkerInfo non-free queue
                                292                 :                :  * av_startingWorker pointer to WorkerInfo currently being started (cleared by
                                293                 :                :  *                  the worker itself as soon as it's up and running)
                                294                 :                :  * av_workItems     work item array
                                295                 :                :  * av_nworkersForBalance the number of autovacuum workers to use when
                                296                 :                :  *                  calculating the per worker cost limit
                                297                 :                :  *
                                298                 :                :  * This struct is protected by AutovacuumLock, except for av_signal and parts
                                299                 :                :  * of the worker list (see above).
                                300                 :                :  *-------------
                                301                 :                :  */
                                302                 :                : typedef struct
                                303                 :                : {
                                304                 :                :     sig_atomic_t av_signal[AutoVacNumSignals];
                                305                 :                :     pid_t       av_launcherpid;
                                306                 :                :     dclist_head av_freeWorkers;
                                307                 :                :     dlist_head  av_runningWorkers;
                                308                 :                :     WorkerInfo  av_startingWorker;
                                309                 :                :     AutoVacuumWorkItem av_workItems[NUM_WORKITEMS];
                                310                 :                :     pg_atomic_uint32 av_nworkersForBalance;
                                311                 :                : } AutoVacuumShmemStruct;
                                312                 :                : 
                                313                 :                : static AutoVacuumShmemStruct *AutoVacuumShmem;
                                314                 :                : 
                                315                 :                : static void AutoVacuumShmemRequest(void *arg);
                                316                 :                : static void AutoVacuumShmemInit(void *arg);
                                317                 :                : 
                                318                 :                : const ShmemCallbacks AutoVacuumShmemCallbacks = {
                                319                 :                :     .request_fn = AutoVacuumShmemRequest,
                                320                 :                :     .init_fn = AutoVacuumShmemInit,
                                321                 :                : };
                                322                 :                : 
                                323                 :                : /*
                                324                 :                :  * the database list (of avl_dbase elements) in the launcher, and the context
                                325                 :                :  * that contains it
                                326                 :                :  */
                                327                 :                : static dlist_head DatabaseList = DLIST_STATIC_INIT(DatabaseList);
                                328                 :                : static MemoryContext DatabaseListCxt = NULL;
                                329                 :                : 
                                330                 :                : /*
                                331                 :                :  * This struct is used by relation_needs_vacanalyze() to return the table's
                                332                 :                :  * score (i.e., the maximum of the component scores) as well as the component
                                333                 :                :  * scores themselves.
                                334                 :                :  */
                                335                 :                : typedef struct
                                336                 :                : {
                                337                 :                :     double      max;            /* maximum of all values below */
                                338                 :                :     double      xid;            /* transaction ID component */
                                339                 :                :     double      mxid;           /* multixact ID component */
                                340                 :                :     double      vac;            /* vacuum component */
                                341                 :                :     double      vac_ins;        /* vacuum insert component */
                                342                 :                :     double      anl;            /* analyze component */
                                343                 :                : } AutoVacuumScores;
                                344                 :                : 
                                345                 :                : /*
                                346                 :                :  * This struct is used to track and sort the list of tables to process.
                                347                 :                :  */
                                348                 :                : typedef struct
                                349                 :                : {
                                350                 :                :     Oid         oid;
                                351                 :                :     double      score;
                                352                 :                : } TableToProcess;
                                353                 :                : 
                                354                 :                : /*
                                355                 :                :  * Dummy pointer to persuade Valgrind that we've not leaked the array of
                                356                 :                :  * avl_dbase structs.  Make it global to ensure the compiler doesn't
                                357                 :                :  * optimize it away.
                                358                 :                :  */
                                359                 :                : #ifdef USE_VALGRIND
                                360                 :                : extern avl_dbase *avl_dbase_array;
                                361                 :                : avl_dbase  *avl_dbase_array;
                                362                 :                : #endif
                                363                 :                : 
                                364                 :                : /* Pointer to my own WorkerInfo, valid on each worker */
                                365                 :                : static WorkerInfo MyWorkerInfo = NULL;
                                366                 :                : 
                                367                 :                : static Oid  do_start_worker(void);
                                368                 :                : static void ProcessAutoVacLauncherInterrupts(void);
                                369                 :                : pg_noreturn static void AutoVacLauncherShutdown(void);
                                370                 :                : static void launcher_determine_sleep(bool canlaunch, bool recursing,
                                371                 :                :                                      struct timeval *nap);
                                372                 :                : static void launch_worker(TimestampTz now);
                                373                 :                : static List *get_database_list(void);
                                374                 :                : static void rebuild_database_list(Oid newdb);
                                375                 :                : static int  db_comparator(const void *a, const void *b);
                                376                 :                : static void autovac_recalculate_workers_for_balance(void);
                                377                 :                : 
                                378                 :                : static void do_autovacuum(void);
                                379                 :                : static void FreeWorkerInfo(int code, Datum arg);
                                380                 :                : 
                                381                 :                : static autovac_table *table_recheck_autovac(Oid relid, HTAB *table_toast_map,
                                382                 :                :                                             TupleDesc pg_class_desc,
                                383                 :                :                                             int effective_multixact_freeze_max_age);
                                384                 :                : static void relation_needs_vacanalyze(Oid relid, AutoVacOpts *relopts,
                                385                 :                :                                       Form_pg_class classForm,
                                386                 :                :                                       int effective_multixact_freeze_max_age,
                                387                 :                :                                       int elevel,
                                388                 :                :                                       bool *dovacuum, bool *doanalyze, bool *wraparound,
                                389                 :                :                                       AutoVacuumScores *scores);
                                390                 :                : 
                                391                 :                : static void autovacuum_do_vac_analyze(autovac_table *tab,
                                392                 :                :                                       BufferAccessStrategy bstrategy);
                                393                 :                : static AutoVacOpts *extract_autovac_opts(HeapTuple tup,
                                394                 :                :                                          TupleDesc pg_class_desc);
                                395                 :                : static void perform_work_item(AutoVacuumWorkItem *workitem);
                                396                 :                : static void autovac_report_activity(autovac_table *tab);
                                397                 :                : static void autovac_report_workitem(AutoVacuumWorkItem *workitem,
                                398                 :                :                                     const char *nspname, const char *relname);
                                399                 :                : static void avl_sigusr2_handler(SIGNAL_ARGS);
                                400                 :                : static bool av_worker_available(void);
                                401                 :                : static void check_av_worker_gucs(void);
                                402                 :                : 
                                403                 :                : 
                                404                 :                : 
                                405                 :                : /********************************************************************
                                406                 :                :  *                    AUTOVACUUM LAUNCHER CODE
                                407                 :                :  ********************************************************************/
                                408                 :                : 
                                409                 :                : /*
                                410                 :                :  * Main entry point for the autovacuum launcher process.
                                411                 :                :  */
                                412                 :                : void
  438 peter@eisentraut.org      413                 :CBC         477 : AutoVacLauncherMain(const void *startup_data, size_t startup_data_len)
                                414                 :                : {
                                415                 :                :     sigjmp_buf  local_sigjmp_buf;
                                416                 :                : 
  778 heikki.linnakangas@i      417         [ -  + ]:            477 :     Assert(startup_data_len == 0);
                                418                 :                : 
                                419                 :                :     /* Release postmaster's working memory context */
                                420         [ +  - ]:            477 :     if (PostmasterContext)
                                421                 :                :     {
                                422                 :            477 :         MemoryContextDelete(PostmasterContext);
                                423                 :            477 :         PostmasterContext = NULL;
                                424                 :                :     }
                                425                 :                : 
 2246 peter@eisentraut.org      426                 :            477 :     init_ps_display(NULL);
                                427                 :                : 
 3343 tgl@sss.pgh.pa.us         428         [ +  + ]:            477 :     ereport(DEBUG1,
                                429                 :                :             (errmsg_internal("autovacuum launcher started")));
                                430                 :                : 
 6768 alvherre@alvh.no-ip.      431         [ -  + ]:            477 :     if (PostAuthDelay)
 6768 alvherre@alvh.no-ip.      432                 :UBC           0 :         pg_usleep(PostAuthDelay * 1000000L);
                                433                 :                : 
  672 heikki.linnakangas@i      434         [ -  + ]:CBC         477 :     Assert(GetProcessingMode() == InitProcessing);
                                435                 :                : 
                                436                 :                :     /*
                                437                 :                :      * Set up signal handlers.  We operate on databases much like a regular
                                438                 :                :      * backend, so we use the same signal handling.  See equivalent code in
                                439                 :                :      * tcop/postgres.c.
                                440                 :                :      */
 2331 rhaas@postgresql.org      441                 :            477 :     pqsignal(SIGHUP, SignalHandlerForConfigReload);
 6091 tgl@sss.pgh.pa.us         442                 :            477 :     pqsignal(SIGINT, StatementCancelHandler);
 2331 rhaas@postgresql.org      443                 :            477 :     pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
                                444                 :                :     /* SIGQUIT handler was already set up by InitPostmasterChild */
                                445                 :                : 
 5041 alvherre@alvh.no-ip.      446                 :            477 :     InitializeTimeouts();       /* establishes SIGALRM handler */
                                447                 :                : 
   21 andrew@dunslane.net       448                 :GNC         477 :     pqsignal(SIGPIPE, PG_SIG_IGN);
 6091 tgl@sss.pgh.pa.us         449                 :CBC         477 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
                                450                 :            477 :     pqsignal(SIGUSR2, avl_sigusr2_handler);
 7019 alvherre@alvh.no-ip.      451                 :            477 :     pqsignal(SIGFPE, FloatExceptionHandler);
   21 andrew@dunslane.net       452                 :GNC         477 :     pqsignal(SIGCHLD, PG_SIG_DFL);
                                453                 :                : 
                                454                 :                :     /*
                                455                 :                :      * Create a per-backend PGPROC struct in shared memory.  We must do this
                                456                 :                :      * before we can use LWLocks or access any shared memory.
                                457                 :                :      */
 6091 tgl@sss.pgh.pa.us         458                 :CBC         477 :     InitProcess();
                                459                 :                : 
                                460                 :                :     /* Early initialization */
 1734 andres@anarazel.de        461                 :            477 :     BaseInit();
                                462                 :                : 
  937 michael@paquier.xyz       463                 :            477 :     InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
                                464                 :                : 
 6091 tgl@sss.pgh.pa.us         465                 :            477 :     SetProcessingMode(NormalProcessing);
                                466                 :                : 
                                467                 :                :     /*
                                468                 :                :      * Create a memory context that we will do all our work in.  We do this so
                                469                 :                :      * that we can reset the context during error recovery and thereby avoid
                                470                 :                :      * possible memory leaks.
                                471                 :                :      */
 6959 alvherre@alvh.no-ip.      472                 :            477 :     AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
                                473                 :                :                                           "Autovacuum Launcher",
                                474                 :                :                                           ALLOCSET_DEFAULT_SIZES);
                                475                 :            477 :     MemoryContextSwitchTo(AutovacMemCxt);
                                476                 :                : 
                                477                 :                :     /*
                                478                 :                :      * If an exception is encountered, processing resumes here.
                                479                 :                :      *
                                480                 :                :      * This code is a stripped down version of PostgresMain error recovery.
                                481                 :                :      *
                                482                 :                :      * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
                                483                 :                :      * (to wit, BlockSig) will be restored when longjmp'ing to here.  Thus,
                                484                 :                :      * signals other than SIGQUIT will be blocked until we complete error
                                485                 :                :      * recovery.  It might seem that this policy makes the HOLD_INTERRUPTS()
                                486                 :                :      * call redundant, but it is not since InterruptPending might be set
                                487                 :                :      * already.
                                488                 :                :      */
 7019                           489         [ -  + ]:            477 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
                                490                 :                :     {
                                491                 :                :         /* since not using PG_TRY, must reset error stack by hand */
 7019 alvherre@alvh.no-ip.      492                 :UBC           0 :         error_context_stack = NULL;
                                493                 :                : 
                                494                 :                :         /* Prevents interrupts while cleaning up */
                                495                 :              0 :         HOLD_INTERRUPTS();
                                496                 :                : 
                                497                 :                :         /* Forget any pending QueryCancel or timeout request */
 5041                           498                 :              0 :         disable_all_timeouts(false);
 3240 tgl@sss.pgh.pa.us         499                 :              0 :         QueryCancelPending = false; /* second to avoid race condition */
                                500                 :                : 
                                501                 :                :         /* Report the error to the server log */
 7019 alvherre@alvh.no-ip.      502                 :              0 :         EmitErrorReport();
                                503                 :                : 
                                504                 :                :         /* Abort the current transaction in order to recover */
 6091 tgl@sss.pgh.pa.us         505                 :              0 :         AbortCurrentTransaction();
                                506                 :                : 
                                507                 :                :         /*
                                508                 :                :          * Release any other resources, for the case where we were not in a
                                509                 :                :          * transaction.
                                510                 :                :          */
 3185 alvherre@alvh.no-ip.      511                 :              0 :         LWLockReleaseAll();
                                512                 :              0 :         pgstat_report_wait_end();
  414 andres@anarazel.de        513                 :              0 :         pgaio_error_cleanup();
 3185 alvherre@alvh.no-ip.      514                 :              0 :         UnlockBuffers();
                                515                 :                :         /* this is probably dead code, but let's be safe: */
 2848 tgl@sss.pgh.pa.us         516         [ #  # ]:              0 :         if (AuxProcessResourceOwner)
                                517                 :              0 :             ReleaseAuxProcessResources(false);
 3185 alvherre@alvh.no-ip.      518                 :              0 :         AtEOXact_Buffers(false);
                                519                 :              0 :         AtEOXact_SMgr();
 2929 tgl@sss.pgh.pa.us         520                 :              0 :         AtEOXact_Files(false);
 3185 alvherre@alvh.no-ip.      521                 :              0 :         AtEOXact_HashTables(false);
                                522                 :                : 
                                523                 :                :         /*
                                524                 :                :          * Now return to normal top-level context and clear ErrorContext for
                                525                 :                :          * next time.
                                526                 :                :          */
 6959                           527                 :              0 :         MemoryContextSwitchTo(AutovacMemCxt);
 7019                           528                 :              0 :         FlushErrorState();
                                529                 :                : 
                                530                 :                :         /* Flush any leaked data in the top-level context */
  902 nathan@postgresql.or      531                 :              0 :         MemoryContextReset(AutovacMemCxt);
                                532                 :                : 
                                533                 :                :         /* don't leave dangling pointers to freed memory */
 6959 alvherre@alvh.no-ip.      534                 :              0 :         DatabaseListCxt = NULL;
 4949                           535                 :              0 :         dlist_init(&DatabaseList);
                                536                 :                : 
                                537                 :                :         /* Now we can allow interrupts again */
 7019                           538         [ #  # ]:              0 :         RESUME_INTERRUPTS();
                                539                 :                : 
                                540                 :                :         /* if in shutdown mode, no need for anything further; just go away */
 2331 rhaas@postgresql.org      541         [ #  # ]:              0 :         if (ShutdownRequestPending)
                                542                 :              0 :             AutoVacLauncherShutdown();
                                543                 :                : 
                                544                 :                :         /*
                                545                 :                :          * Sleep at least 1 second after any error.  We don't want to be
                                546                 :                :          * filling the error logs as fast as we can.
                                547                 :                :          */
 7019 alvherre@alvh.no-ip.      548                 :              0 :         pg_usleep(1000000L);
                                549                 :                :     }
                                550                 :                : 
                                551                 :                :     /* We can now handle ereport(ERROR) */
 7019 alvherre@alvh.no-ip.      552                 :CBC         477 :     PG_exception_stack = &local_sigjmp_buf;
                                553                 :                : 
                                554                 :                :     /* must unblock signals before calling rebuild_database_list */
 1187 tmunro@postgresql.or      555                 :            477 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
                                556                 :                : 
                                557                 :                :     /*
                                558                 :                :      * Set always-secure search path.  Launcher doesn't connect to a database,
                                559                 :                :      * so this has no effect.
                                560                 :                :      */
 2990 noah@leadboat.com         561                 :            477 :     SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
                                562                 :                : 
                                563                 :                :     /*
                                564                 :                :      * Force zero_damaged_pages OFF in the autovac process, even if it is set
                                565                 :                :      * in postgresql.conf.  We don't really want such a dangerous option being
                                566                 :                :      * applied non-interactively.
                                567                 :                :      */
 5271 tgl@sss.pgh.pa.us         568                 :            477 :     SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
                                569                 :                : 
                                570                 :                :     /*
                                571                 :                :      * Force settable timeouts off to avoid letting these settings prevent
                                572                 :                :      * regular maintenance from being executed.
                                573                 :                :      */
                                574                 :            477 :     SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
  810 akorotkov@postgresql      575                 :            477 :     SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
 4798 tgl@sss.pgh.pa.us         576                 :            477 :     SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
 3611                           577                 :            477 :     SetConfigOption("idle_in_transaction_session_timeout", "0",
                                578                 :                :                     PGC_SUSET, PGC_S_OVERRIDE);
                                579                 :                : 
                                580                 :                :     /*
                                581                 :                :      * Force default_transaction_isolation to READ COMMITTED.  We don't want
                                582                 :                :      * to pay the overhead of serializable mode, nor add any risk of causing
                                583                 :                :      * deadlocks or delaying other transactions.
                                584                 :                :      */
 5271                           585                 :            477 :     SetConfigOption("default_transaction_isolation", "read committed",
                                586                 :                :                     PGC_SUSET, PGC_S_OVERRIDE);
                                587                 :                : 
                                588                 :                :     /*
                                589                 :                :      * Even when system is configured to use a different fetch consistency,
                                590                 :                :      * for autovac we always want fresh stats.
                                591                 :                :      */
 1490 andres@anarazel.de        592                 :            477 :     SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
                                593                 :                : 
                                594                 :                :     /*
                                595                 :                :      * In emergency mode, just start a worker (unless shutdown was requested)
                                596                 :                :      * and go away.
                                597                 :                :      */
 6798 tgl@sss.pgh.pa.us         598         [ -  + ]:            477 :     if (!AutoVacuumingActive())
                                599                 :                :     {
 2331 rhaas@postgresql.org      600         [ #  # ]:UBC           0 :         if (!ShutdownRequestPending)
 4045 alvherre@alvh.no-ip.      601                 :              0 :             do_start_worker();
 6746 bruce@momjian.us          602                 :              0 :         proc_exit(0);           /* done */
                                603                 :                :     }
                                604                 :                : 
 6959 alvherre@alvh.no-ip.      605                 :CBC         477 :     AutoVacuumShmem->av_launcherpid = MyProcPid;
                                606                 :                : 
                                607                 :                :     /*
                                608                 :                :      * Create the initial database list.  The invariant we want this list to
                                609                 :                :      * keep is that it's ordered by decreasing next_worker.  As soon as an
                                610                 :                :      * entry is updated to a higher time, it will be moved to the front (which
                                611                 :                :      * is correct because the only operation is to add autovacuum_naptime to
                                612                 :                :      * the entry, and time always increases).
                                613                 :                :      */
                                614                 :            477 :     rebuild_database_list(InvalidOid);
                                615                 :                : 
                                616                 :                :     /* loop until shutdown request */
 2331 rhaas@postgresql.org      617         [ +  + ]:           2077 :     while (!ShutdownRequestPending)
                                618                 :                :     {
                                619                 :                :         struct timeval nap;
 6959 alvherre@alvh.no-ip.      620                 :           2076 :         TimestampTz current_time = 0;
                                621                 :                :         bool        can_launch;
                                622                 :                : 
                                623                 :                :         /*
                                624                 :                :          * This loop is a bit different from the normal use of WaitLatch,
                                625                 :                :          * because we'd like to sleep before the first launch of a child
                                626                 :                :          * process.  So it's WaitLatch, then ResetLatch, then check for
                                627                 :                :          * wakening conditions.
                                628                 :                :          */
                                629                 :                : 
  484 nathan@postgresql.or      630                 :           2076 :         launcher_determine_sleep(av_worker_available(), false, &nap);
                                631                 :                : 
                                632                 :                :         /*
                                633                 :                :          * Wait until naptime expires or we get some type of signal (all the
                                634                 :                :          * signal handlers will wake us by calling SetLatch).
                                635                 :                :          */
 2720 tmunro@postgresql.or      636                 :           2076 :         (void) WaitLatch(MyLatch,
                                637                 :                :                          WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
                                638                 :           2076 :                          (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
                                639                 :                :                          WAIT_EVENT_AUTOVACUUM_MAIN);
                                640                 :                : 
 4129 andres@anarazel.de        641                 :           2073 :         ResetLatch(MyLatch);
                                642                 :                : 
  426 heikki.linnakangas@i      643                 :           2073 :         ProcessAutoVacLauncherInterrupts();
                                644                 :                : 
                                645                 :                :         /*
                                646                 :                :          * a worker finished, or postmaster signaled failure to start a worker
                                647                 :                :          */
 6091 tgl@sss.pgh.pa.us         648         [ +  + ]:           1600 :         if (got_SIGUSR2)
                                649                 :                :         {
                                650                 :             95 :             got_SIGUSR2 = false;
                                651                 :                : 
                                652                 :                :             /* rebalance cost limits, if needed */
 6889 alvherre@alvh.no-ip.      653         [ +  + ]:             95 :             if (AutoVacuumShmem->av_signal[AutoVacRebalance])
                                654                 :                :             {
 6959                           655                 :             47 :                 LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
 6889                           656                 :             47 :                 AutoVacuumShmem->av_signal[AutoVacRebalance] = false;
 1124 dgustafsson@postgres      657                 :             47 :                 autovac_recalculate_workers_for_balance();
 6959 alvherre@alvh.no-ip.      658                 :             47 :                 LWLockRelease(AutovacuumLock);
                                659                 :                :             }
                                660                 :                : 
 6889                           661         [ -  + ]:             95 :             if (AutoVacuumShmem->av_signal[AutoVacForkFailed])
                                662                 :                :             {
                                663                 :                :                 /*
                                664                 :                :                  * If the postmaster failed to start a new worker, we sleep
                                665                 :                :                  * for a little while and resend the signal.  The new worker's
                                666                 :                :                  * state is still in memory, so this is sufficient.  After
                                667                 :                :                  * that, we restart the main loop.
                                668                 :                :                  *
                                669                 :                :                  * XXX should we put a limit to the number of times we retry?
                                670                 :                :                  * I don't think it makes much sense, because a future start
                                671                 :                :                  * of a worker will continue to fail in the same way.
                                672                 :                :                  */
 6889 alvherre@alvh.no-ip.      673                 :UBC           0 :                 AutoVacuumShmem->av_signal[AutoVacForkFailed] = false;
 5912 bruce@momjian.us          674                 :              0 :                 pg_usleep(1000000L);    /* 1s */
 6889 alvherre@alvh.no-ip.      675                 :              0 :                 SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
 6889 alvherre@alvh.no-ip.      676                 :GBC           9 :                 continue;
                                677                 :                :             }
                                678                 :                :         }
                                679                 :                : 
                                680                 :                :         /*
                                681                 :                :          * There are some conditions that we need to check before trying to
                                682                 :                :          * start a worker.  First, we need to make sure that there is a worker
                                683                 :                :          * slot available.  Second, we need to make sure that no other worker
                                684                 :                :          * failed while starting up.
                                685                 :                :          */
                                686                 :                : 
 6889 alvherre@alvh.no-ip.      687                 :CBC        1600 :         current_time = GetCurrentTimestamp();
 7019                           688                 :           1600 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
                                689                 :                : 
  484 nathan@postgresql.or      690                 :           1600 :         can_launch = av_worker_available();
                                691                 :                : 
 6393 tgl@sss.pgh.pa.us         692         [ -  + ]:           1600 :         if (AutoVacuumShmem->av_startingWorker != NULL)
                                693                 :                :         {
                                694                 :                :             int         waittime;
 6393 tgl@sss.pgh.pa.us         695                 :UBC           0 :             WorkerInfo  worker = AutoVacuumShmem->av_startingWorker;
                                696                 :                : 
                                697                 :                :             /*
                                698                 :                :              * We can't launch another worker when another one is still
                                699                 :                :              * starting up (or failed while doing so), so just sleep for a bit
                                700                 :                :              * more; that worker will wake us up again as soon as it's ready.
                                701                 :                :              * We will only wait autovacuum_naptime seconds (up to a maximum
                                702                 :                :              * of 60 seconds) for this to happen however.  Note that failure
                                703                 :                :              * to connect to a particular database is not a problem here,
                                704                 :                :              * because the worker removes itself from the startingWorker
                                705                 :                :              * pointer before trying to connect.  Problems detected by the
                                706                 :                :              * postmaster (like fork() failure) are also reported and handled
                                707                 :                :              * differently.  The only problems that may cause this code to
                                708                 :                :              * fire are errors in the earlier sections of AutoVacWorkerMain,
                                709                 :                :              * before the worker removes the WorkerInfo from the
                                710                 :                :              * startingWorker pointer.
                                711                 :                :              */
 6889 alvherre@alvh.no-ip.      712                 :              0 :             waittime = Min(autovacuum_naptime, 60) * 1000;
 6943                           713         [ #  # ]:              0 :             if (TimestampDifferenceExceeds(worker->wi_launchtime, current_time,
                                714                 :                :                                            waittime))
                                715                 :                :             {
 6959                           716                 :              0 :                 LWLockRelease(AutovacuumLock);
                                717                 :              0 :                 LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
                                718                 :                : 
                                719                 :                :                 /*
                                720                 :                :                  * No other process can put a worker in starting mode, so if
                                721                 :                :                  * startingWorker is still INVALID after exchanging our lock,
                                722                 :                :                  * we assume it's the same one we saw above (so we don't
                                723                 :                :                  * recheck the launch time).
                                724                 :                :                  */
 6393 tgl@sss.pgh.pa.us         725         [ #  # ]:              0 :                 if (AutoVacuumShmem->av_startingWorker != NULL)
                                726                 :                :                 {
                                727                 :              0 :                     worker = AutoVacuumShmem->av_startingWorker;
 6959 alvherre@alvh.no-ip.      728                 :              0 :                     worker->wi_dboid = InvalidOid;
                                729                 :              0 :                     worker->wi_tableoid = InvalidOid;
 3647                           730                 :              0 :                     worker->wi_sharedrel = false;
 6768                           731                 :              0 :                     worker->wi_proc = NULL;
 6959                           732                 :              0 :                     worker->wi_launchtime = 0;
  484 nathan@postgresql.or      733                 :              0 :                     dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
                                734                 :                :                                      &worker->wi_links);
 6393 tgl@sss.pgh.pa.us         735                 :              0 :                     AutoVacuumShmem->av_startingWorker = NULL;
 1625 alvherre@alvh.no-ip.      736         [ #  # ]:              0 :                     ereport(WARNING,
                                737                 :                :                             errmsg("autovacuum worker took too long to start; canceled"));
                                738                 :                :                 }
                                739                 :                :             }
                                740                 :                :             else
 6959                           741                 :              0 :                 can_launch = false;
                                742                 :                :         }
 6746 bruce@momjian.us          743                 :CBC        1600 :         LWLockRelease(AutovacuumLock);  /* either shared or exclusive */
                                744                 :                : 
                                745                 :                :         /* if we can't do anything, just go back to sleep */
 6889 alvherre@alvh.no-ip.      746         [ +  + ]:           1600 :         if (!can_launch)
 6889 alvherre@alvh.no-ip.      747                 :GBC           9 :             continue;
                                748                 :                : 
                                749                 :                :         /* We're OK to start a new worker */
                                750                 :                : 
 4949 alvherre@alvh.no-ip.      751         [ +  + ]:CBC        1591 :         if (dlist_is_empty(&DatabaseList))
                                752                 :                :         {
                                753                 :                :             /*
                                754                 :                :              * Special case when the list is empty: start a worker right away.
                                755                 :                :              * This covers the initial case, when no database is in pgstats
                                756                 :                :              * (thus the list is empty).  Note that the constraints in
                                757                 :                :              * launcher_determine_sleep keep us from starting workers too
                                758                 :                :              * quickly (at most once every autovacuum_naptime when the list is
                                759                 :                :              * empty).
                                760                 :                :              */
 6889                           761                 :              7 :             launch_worker(current_time);
                                762                 :                :         }
                                763                 :                :         else
                                764                 :                :         {
                                765                 :                :             /*
                                766                 :                :              * because rebuild_database_list constructs a list with most
                                767                 :                :              * distant adl_next_worker first, we obtain our database from the
                                768                 :                :              * tail of the list.
                                769                 :                :              */
                                770                 :                :             avl_dbase  *avdb;
                                771                 :                : 
 4949                           772                 :           1584 :             avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
                                773                 :                : 
                                774                 :                :             /*
                                775                 :                :              * launch a worker if next_worker is right now or it is in the
                                776                 :                :              * past
                                777                 :                :              */
                                778         [ +  + ]:           1584 :             if (TimestampDifferenceExceeds(avdb->adl_next_worker,
                                779                 :                :                                            current_time, 0))
                                780                 :             48 :                 launch_worker(current_time);
                                781                 :                :         }
                                782                 :                :     }
                                783                 :                : 
 2331 rhaas@postgresql.org      784                 :              1 :     AutoVacLauncherShutdown();
                                785                 :                : }
                                786                 :                : 
                                787                 :                : /*
                                788                 :                :  * Process any new interrupts.
                                789                 :                :  */
                                790                 :                : static void
  426 heikki.linnakangas@i      791                 :           2073 : ProcessAutoVacLauncherInterrupts(void)
                                792                 :                : {
                                793                 :                :     /* the normal shutdown case */
 2331 rhaas@postgresql.org      794         [ +  + ]:           2073 :     if (ShutdownRequestPending)
                                795                 :            472 :         AutoVacLauncherShutdown();
                                796                 :                : 
                                797         [ +  + ]:           1601 :     if (ConfigReloadPending)
                                798                 :                :     {
  484 nathan@postgresql.or      799                 :             64 :         int         autovacuum_max_workers_prev = autovacuum_max_workers;
                                800                 :                : 
 2331 rhaas@postgresql.org      801                 :             64 :         ConfigReloadPending = false;
                                802                 :             64 :         ProcessConfigFile(PGC_SIGHUP);
                                803                 :                : 
                                804                 :                :         /* shutdown requested in config file? */
                                805         [ +  + ]:             64 :         if (!AutoVacuumingActive())
                                806                 :              1 :             AutoVacLauncherShutdown();
                                807                 :                : 
                                808                 :                :         /*
                                809                 :                :          * If autovacuum_max_workers changed, emit a WARNING if
                                810                 :                :          * autovacuum_worker_slots < autovacuum_max_workers.  If it didn't
                                811                 :                :          * change, skip this to avoid too many repeated log messages.
                                812                 :                :          */
  484 nathan@postgresql.or      813         [ -  + ]:             63 :         if (autovacuum_max_workers_prev != autovacuum_max_workers)
  484 nathan@postgresql.or      814                 :UBC           0 :             check_av_worker_gucs();
                                815                 :                : 
                                816                 :                :         /* rebuild the list in case the naptime changed */
 2331 rhaas@postgresql.org      817                 :CBC          63 :         rebuild_database_list(InvalidOid);
                                818                 :                :     }
                                819                 :                : 
                                820                 :                :     /* Process barrier events */
 2329                           821         [ +  + ]:           1600 :     if (ProcSignalBarrierPending)
                                822                 :             67 :         ProcessProcSignalBarrier();
                                823                 :                : 
                                824                 :                :     /* Perform logging of memory contexts of this process */
 1666 fujii@postgresql.org      825         [ -  + ]:           1600 :     if (LogMemoryContextPending)
 1666 fujii@postgresql.org      826                 :UBC           0 :         ProcessLogMemoryContextInterrupt();
                                827                 :                : 
                                828                 :                :     /* Process sinval catchup interrupts that happened while sleeping */
 2331 rhaas@postgresql.org      829                 :CBC        1600 :     ProcessCatchupInterrupt();
                                830                 :           1600 : }
                                831                 :                : 
                                832                 :                : /*
                                833                 :                :  * Perform a normal exit from the autovac launcher.
                                834                 :                :  */
                                835                 :                : static void
 2175 noah@leadboat.com         836                 :            474 : AutoVacLauncherShutdown(void)
                                837                 :                : {
 3343 tgl@sss.pgh.pa.us         838         [ +  + ]:            474 :     ereport(DEBUG1,
                                839                 :                :             (errmsg_internal("autovacuum launcher shutting down")));
 6959 alvherre@alvh.no-ip.      840                 :            474 :     AutoVacuumShmem->av_launcherpid = 0;
                                841                 :                : 
 6746 bruce@momjian.us          842                 :            474 :     proc_exit(0);               /* done */
                                843                 :                : }
                                844                 :                : 
                                845                 :                : /*
                                846                 :                :  * Determine the time to sleep, based on the database list.
                                847                 :                :  *
                                848                 :                :  * The "canlaunch" parameter indicates whether we can start a worker right now,
                                849                 :                :  * for example due to the workers being all busy.  If this is false, we will
                                850                 :                :  * cause a long sleep, which will be interrupted when a worker exits.
                                851                 :                :  */
                                852                 :                : static void
 3240 tgl@sss.pgh.pa.us         853                 :           2077 : launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval *nap)
                                854                 :                : {
                                855                 :                :     /*
                                856                 :                :      * We sleep until the next scheduled vacuum.  We trust that when the
                                857                 :                :      * database list was built, care was taken so that no entries have times
                                858                 :                :      * in the past; if the first entry has too close a next_worker value, or a
                                859                 :                :      * time in the past, we will sleep a small nominal time.
                                860                 :                :      */
 6959 alvherre@alvh.no-ip.      861         [ +  + ]:           2077 :     if (!canlaunch)
                                862                 :                :     {
 6901 alvherre@alvh.no-ip.      863                 :GBC          17 :         nap->tv_sec = autovacuum_naptime;
                                864                 :             17 :         nap->tv_usec = 0;
                                865                 :                :     }
 4949 alvherre@alvh.no-ip.      866         [ +  + ]:CBC        2060 :     else if (!dlist_is_empty(&DatabaseList))
                                867                 :                :     {
 6746 bruce@momjian.us          868                 :           2032 :         TimestampTz current_time = GetCurrentTimestamp();
                                869                 :                :         TimestampTz next_wakeup;
                                870                 :                :         avl_dbase  *avdb;
                                871                 :                :         long        secs;
                                872                 :                :         int         usecs;
                                873                 :                : 
 4949 alvherre@alvh.no-ip.      874                 :           2032 :         avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
                                875                 :                : 
 6959                           876                 :           2032 :         next_wakeup = avdb->adl_next_worker;
                                877                 :           2032 :         TimestampDifference(current_time, next_wakeup, &secs, &usecs);
                                878                 :                : 
 6901                           879                 :           2032 :         nap->tv_sec = secs;
                                880                 :           2032 :         nap->tv_usec = usecs;
                                881                 :                :     }
                                882                 :                :     else
                                883                 :                :     {
                                884                 :                :         /* list is empty, sleep for whole autovacuum_naptime seconds  */
                                885                 :             28 :         nap->tv_sec = autovacuum_naptime;
                                886                 :             28 :         nap->tv_usec = 0;
                                887                 :                :     }
                                888                 :                : 
                                889                 :                :     /*
                                890                 :                :      * If the result is exactly zero, it means a database had an entry with
                                891                 :                :      * time in the past.  Rebuild the list so that the databases are evenly
                                892                 :                :      * distributed again, and recalculate the time to sleep.  This can happen
                                893                 :                :      * if there are more tables needing vacuum than workers, and they all take
                                894                 :                :      * longer to vacuum than autovacuum_naptime.
                                895                 :                :      *
                                896                 :                :      * We only recurse once.  rebuild_database_list should always return times
                                897                 :                :      * in the future, but it seems best not to trust too much on that.
                                898                 :                :      */
 6883 tgl@sss.pgh.pa.us         899   [ +  +  +  +  :           2077 :     if (nap->tv_sec == 0 && nap->tv_usec == 0 && !recursing)
                                              +  - ]
                                900                 :                :     {
 6959 alvherre@alvh.no-ip.      901                 :GBC           1 :         rebuild_database_list(InvalidOid);
 6901                           902                 :              1 :         launcher_determine_sleep(canlaunch, true, nap);
                                903                 :              1 :         return;
                                904                 :                :     }
                                905                 :                : 
                                906                 :                :     /* The smallest time we'll allow the launcher to sleep. */
 6174 alvherre@alvh.no-ip.      907   [ +  +  +  + ]:CBC        2076 :     if (nap->tv_sec <= 0 && nap->tv_usec <= MIN_AUTOVAC_SLEEPTIME * 1000)
                                908                 :                :     {
 6883 tgl@sss.pgh.pa.us         909                 :             19 :         nap->tv_sec = 0;
 6174 alvherre@alvh.no-ip.      910                 :             19 :         nap->tv_usec = MIN_AUTOVAC_SLEEPTIME * 1000;
                                911                 :                :     }
                                912                 :                : 
                                913                 :                :     /*
                                914                 :                :      * If the sleep time is too large, clamp it to an arbitrary maximum (plus
                                915                 :                :      * any fractional seconds, for simplicity).  This avoids an essentially
                                916                 :                :      * infinite sleep in strange cases like the system clock going backwards a
                                917                 :                :      * few years.
                                918                 :                :      */
 3973                           919         [ +  + ]:           2076 :     if (nap->tv_sec > MAX_AUTOVAC_SLEEPTIME)
                                920                 :              9 :         nap->tv_sec = MAX_AUTOVAC_SLEEPTIME;
                                921                 :                : }
                                922                 :                : 
                                923                 :                : /*
                                924                 :                :  * Build an updated DatabaseList.  It must only contain databases that appear
                                925                 :                :  * in pgstats, and must be sorted by next_worker from highest to lowest,
                                926                 :                :  * distributed regularly across the next autovacuum_naptime interval.
                                927                 :                :  *
                                928                 :                :  * Receives the Oid of the database that made this list be generated (we call
                                929                 :                :  * this the "new" database, because when the database was already present on
                                930                 :                :  * the list, we expect that this function is not called at all).  The
                                931                 :                :  * preexisting list, if any, will be used to preserve the order of the
                                932                 :                :  * databases in the autovacuum_naptime period.  The new database is put at the
                                933                 :                :  * end of the interval.  The actual values are not saved, which should not be
                                934                 :                :  * much of a problem.
                                935                 :                :  */
                                936                 :                : static void
 6959                           937                 :            548 : rebuild_database_list(Oid newdb)
                                938                 :                : {
                                939                 :                :     List       *dblist;
                                940                 :                :     ListCell   *cell;
                                941                 :                :     MemoryContext newcxt;
                                942                 :                :     MemoryContext oldcxt;
                                943                 :                :     MemoryContext tmpcxt;
                                944                 :                :     HASHCTL     hctl;
                                945                 :                :     int         score;
                                946                 :                :     int         nelems;
                                947                 :                :     HTAB       *dbhash;
                                948                 :                :     dlist_iter  iter;
                                949                 :                : 
                                950                 :            548 :     newcxt = AllocSetContextCreate(AutovacMemCxt,
                                951                 :                :                                    "Autovacuum database list",
                                952                 :                :                                    ALLOCSET_DEFAULT_SIZES);
                                953                 :            548 :     tmpcxt = AllocSetContextCreate(newcxt,
                                954                 :                :                                    "Autovacuum database list (tmp)",
                                955                 :                :                                    ALLOCSET_DEFAULT_SIZES);
                                956                 :            548 :     oldcxt = MemoryContextSwitchTo(tmpcxt);
                                957                 :                : 
                                958                 :                :     /*
                                959                 :                :      * Implementing this is not as simple as it sounds, because we need to put
                                960                 :                :      * the new database at the end of the list; next the databases that were
                                961                 :                :      * already on the list, and finally (at the tail of the list) all the
                                962                 :                :      * other databases that are not on the existing list.
                                963                 :                :      *
                                964                 :                :      * To do this, we build an empty hash table of scored databases.  We will
                                965                 :                :      * start with the lowest score (zero) for the new database, then
                                966                 :                :      * increasing scores for the databases in the existing list, in order, and
                                967                 :                :      * lastly increasing scores for all databases gotten via
                                968                 :                :      * get_database_list() that are not already on the hash.
                                969                 :                :      *
                                970                 :                :      * Then we will put all the hash elements into an array, sort the array by
                                971                 :                :      * score, and finally put the array elements into the new doubly linked
                                972                 :                :      * list.
                                973                 :                :      */
                                974                 :            548 :     hctl.keysize = sizeof(Oid);
                                975                 :            548 :     hctl.entrysize = sizeof(avl_dbase);
                                976                 :            548 :     hctl.hcxt = tmpcxt;
 1454 tgl@sss.pgh.pa.us         977                 :            548 :     dbhash = hash_create("autovacuum db hash", 20, &hctl, /* magic number here
                                978                 :                :                                                              * FIXME */
                                979                 :                :                          HASH_ELEM | HASH_BLOBS | HASH_CONTEXT);
                                980                 :                : 
                                981                 :                :     /* start by inserting the new database */
 6959 alvherre@alvh.no-ip.      982                 :            548 :     score = 0;
                                983         [ +  + ]:            548 :     if (OidIsValid(newdb))
                                984                 :                :     {
                                985                 :                :         avl_dbase  *db;
                                986                 :                :         PgStat_StatDBEntry *entry;
                                987                 :                : 
                                988                 :                :         /* only consider this database if it has a pgstat entry */
                                989                 :              7 :         entry = pgstat_fetch_stat_dbentry(newdb);
                                990         [ +  - ]:              7 :         if (entry != NULL)
                                991                 :                :         {
                                992                 :                :             /* we assume it isn't found because the hash was just created */
                                993                 :              7 :             db = hash_search(dbhash, &newdb, HASH_ENTER, NULL);
                                994                 :                : 
                                995                 :                :             /* hash_search already filled in the key */
                                996                 :              7 :             db->adl_score = score++;
                                997                 :                :             /* next_worker is filled in later */
                                998                 :                :         }
                                999                 :                :     }
                               1000                 :                : 
                               1001                 :                :     /* Now insert the databases from the existing list */
 4949                          1002   [ +  -  +  + ]:            678 :     dlist_foreach(iter, &DatabaseList)
                               1003                 :                :     {
                               1004                 :            130 :         avl_dbase  *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
                               1005                 :                :         avl_dbase  *db;
                               1006                 :                :         bool        found;
                               1007                 :                :         PgStat_StatDBEntry *entry;
                               1008                 :                : 
                               1009                 :                :         /*
                               1010                 :                :          * skip databases with no stat entries -- in particular, this gets rid
                               1011                 :                :          * of dropped databases
                               1012                 :                :          */
                               1013                 :            130 :         entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
                               1014         [ -  + ]:            130 :         if (entry == NULL)
 4949 alvherre@alvh.no-ip.     1015                 :UBC           0 :             continue;
                               1016                 :                : 
 4949 alvherre@alvh.no-ip.     1017                 :CBC         130 :         db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
                               1018                 :                : 
                               1019         [ +  - ]:            130 :         if (!found)
                               1020                 :                :         {
                               1021                 :                :             /* hash_search already filled in the key */
                               1022                 :            130 :             db->adl_score = score++;
                               1023                 :                :             /* next_worker is filled in later */
                               1024                 :                :         }
                               1025                 :                :     }
                               1026                 :                : 
                               1027                 :                :     /* finally, insert all qualifying databases not previously inserted */
 6959                          1028                 :            548 :     dblist = get_database_list();
                               1029   [ +  -  +  +  :           2503 :     foreach(cell, dblist)
                                              +  + ]
                               1030                 :                :     {
                               1031                 :           1955 :         avw_dbase  *avdb = lfirst(cell);
                               1032                 :                :         avl_dbase  *db;
                               1033                 :                :         bool        found;
                               1034                 :                :         PgStat_StatDBEntry *entry;
                               1035                 :                : 
                               1036                 :                :         /* only consider databases with a pgstat entry */
                               1037                 :           1955 :         entry = pgstat_fetch_stat_dbentry(avdb->adw_datid);
                               1038         [ +  + ]:           1955 :         if (entry == NULL)
                               1039                 :           1114 :             continue;
                               1040                 :                : 
                               1041                 :            841 :         db = hash_search(dbhash, &(avdb->adw_datid), HASH_ENTER, &found);
                               1042                 :                :         /* only update the score if the database was not already on the hash */
                               1043         [ +  + ]:            841 :         if (!found)
                               1044                 :                :         {
                               1045                 :                :             /* hash_search already filled in the key */
                               1046                 :            704 :             db->adl_score = score++;
                               1047                 :                :             /* next_worker is filled in later */
                               1048                 :                :         }
                               1049                 :                :     }
                               1050                 :            548 :     nelems = score;
                               1051                 :                : 
                               1052                 :                :     /* from here on, the allocated memory belongs to the new list */
                               1053                 :            548 :     MemoryContextSwitchTo(newcxt);
 4949                          1054                 :            548 :     dlist_init(&DatabaseList);
                               1055                 :                : 
 6959                          1056         [ +  + ]:            548 :     if (nelems > 0)
                               1057                 :                :     {
                               1058                 :                :         TimestampTz current_time;
                               1059                 :                :         int         millis_increment;
                               1060                 :                :         avl_dbase  *dbary;
                               1061                 :                :         avl_dbase  *db;
                               1062                 :                :         HASH_SEQ_STATUS seq;
                               1063                 :                :         int         i;
                               1064                 :                : 
                               1065                 :                :         /* put all the hash elements into an array */
                               1066                 :            527 :         dbary = palloc(nelems * sizeof(avl_dbase));
                               1067                 :                :         /* keep Valgrind quiet */
                               1068                 :                : #ifdef USE_VALGRIND
                               1069                 :                :         avl_dbase_array = dbary;
                               1070                 :                : #endif
                               1071                 :                : 
                               1072                 :            527 :         i = 0;
                               1073                 :            527 :         hash_seq_init(&seq, dbhash);
                               1074         [ +  + ]:           1368 :         while ((db = hash_seq_search(&seq)) != NULL)
                               1075                 :            841 :             memcpy(&(dbary[i++]), db, sizeof(avl_dbase));
                               1076                 :                : 
                               1077                 :                :         /* sort the array */
                               1078                 :            527 :         qsort(dbary, nelems, sizeof(avl_dbase), db_comparator);
                               1079                 :                : 
                               1080                 :                :         /*
                               1081                 :                :          * Determine the time interval between databases in the schedule. If
                               1082                 :                :          * we see that the configured naptime would take us to sleep times
                               1083                 :                :          * lower than our min sleep time (which launcher_determine_sleep is
                               1084                 :                :          * coded not to allow), silently use a larger naptime (but don't touch
                               1085                 :                :          * the GUC variable).
                               1086                 :                :          */
                               1087                 :            527 :         millis_increment = 1000.0 * autovacuum_naptime / nelems;
 6174                          1088         [ -  + ]:            527 :         if (millis_increment <= MIN_AUTOVAC_SLEEPTIME)
 6174 alvherre@alvh.no-ip.     1089                 :UBC           0 :             millis_increment = MIN_AUTOVAC_SLEEPTIME * 1.1;
                               1090                 :                : 
 6959 alvherre@alvh.no-ip.     1091                 :CBC         527 :         current_time = GetCurrentTimestamp();
                               1092                 :                : 
                               1093                 :                :         /*
                               1094                 :                :          * move the elements from the array into the dlist, setting the
                               1095                 :                :          * next_worker while walking the array
                               1096                 :                :          */
                               1097         [ +  + ]:           1368 :         for (i = 0; i < nelems; i++)
                               1098                 :                :         {
 1348 drowley@postgresql.o     1099                 :            841 :             db = &(dbary[i]);
                               1100                 :                : 
 6959 alvherre@alvh.no-ip.     1101                 :            841 :             current_time = TimestampTzPlusMilliseconds(current_time,
                               1102                 :                :                                                        millis_increment);
                               1103                 :            841 :             db->adl_next_worker = current_time;
                               1104                 :                : 
                               1105                 :                :             /* later elements should go closer to the head of the list */
 4949                          1106                 :            841 :             dlist_push_head(&DatabaseList, &db->adl_node);
                               1107                 :                :         }
                               1108                 :                :     }
                               1109                 :                : 
                               1110                 :                :     /* all done, clean up memory */
 6959                          1111         [ +  + ]:            548 :     if (DatabaseListCxt != NULL)
                               1112                 :             71 :         MemoryContextDelete(DatabaseListCxt);
                               1113                 :            548 :     MemoryContextDelete(tmpcxt);
                               1114                 :            548 :     DatabaseListCxt = newcxt;
                               1115                 :            548 :     MemoryContextSwitchTo(oldcxt);
                               1116                 :            548 : }
                               1117                 :                : 
                               1118                 :                : /* qsort comparator for avl_dbase, using adl_score */
                               1119                 :                : static int
                               1120                 :            460 : db_comparator(const void *a, const void *b)
                               1121                 :                : {
  809 nathan@postgresql.or     1122                 :            920 :     return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
                               1123                 :            460 :                       ((const avl_dbase *) b)->adl_score);
                               1124                 :                : }
                               1125                 :                : 
                               1126                 :                : /*
                               1127                 :                :  * do_start_worker
                               1128                 :                :  *
                               1129                 :                :  * Bare-bones procedure for starting an autovacuum worker from the launcher.
                               1130                 :                :  * It determines what database to work on, sets up shared memory stuff and
                               1131                 :                :  * signals postmaster to start the worker.  It fails gracefully if invoked when
                               1132                 :                :  * autovacuum_workers are already active.
                               1133                 :                :  *
                               1134                 :                :  * Return value is the OID of the database that the worker is going to process,
                               1135                 :                :  * or InvalidOid if no worker was actually started.
                               1136                 :                :  */
                               1137                 :                : static Oid
 6983 alvherre@alvh.no-ip.     1138                 :             55 : do_start_worker(void)
                               1139                 :                : {
                               1140                 :                :     List       *dblist;
                               1141                 :                :     ListCell   *cell;
                               1142                 :                :     TransactionId xidForceLimit;
                               1143                 :                :     MultiXactId multiForceLimit;
                               1144                 :                :     bool        for_xid_wrap;
                               1145                 :                :     bool        for_multi_wrap;
                               1146                 :                :     avw_dbase  *avdb;
                               1147                 :                :     TimestampTz current_time;
 6959                          1148                 :             55 :     bool        skipit = false;
 6810                          1149                 :             55 :     Oid         retval = InvalidOid;
                               1150                 :                :     MemoryContext tmpcxt,
                               1151                 :                :                 oldcxt;
                               1152                 :                : 
                               1153                 :                :     /* return quickly when there are no free workers */
 6959                          1154                 :             55 :     LWLockAcquire(AutovacuumLock, LW_SHARED);
  484 nathan@postgresql.or     1155         [ -  + ]:             55 :     if (!av_worker_available())
                               1156                 :                :     {
 6959 alvherre@alvh.no-ip.     1157                 :UBC           0 :         LWLockRelease(AutovacuumLock);
                               1158                 :              0 :         return InvalidOid;
                               1159                 :                :     }
 6959 alvherre@alvh.no-ip.     1160                 :CBC          55 :     LWLockRelease(AutovacuumLock);
                               1161                 :                : 
                               1162                 :                :     /*
                               1163                 :                :      * Create and switch to a temporary context to avoid leaking the memory
                               1164                 :                :      * allocated for the database list.
                               1165                 :                :      */
 6810                          1166                 :             55 :     tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
                               1167                 :                :                                    "Autovacuum start worker (tmp)",
                               1168                 :                :                                    ALLOCSET_DEFAULT_SIZES);
                               1169                 :             55 :     oldcxt = MemoryContextSwitchTo(tmpcxt);
                               1170                 :                : 
                               1171                 :                :     /* Get a list of databases */
 6959                          1172                 :             55 :     dblist = get_database_list();
                               1173                 :                : 
                               1174                 :                :     /*
                               1175                 :                :      * Determine the oldest datfrozenxid/relfrozenxid that we will allow to
                               1176                 :                :      * pass without forcing a vacuum.  (This limit can be tightened for
                               1177                 :                :      * particular tables, but not loosened.)
                               1178                 :                :      */
 1905 tmunro@postgresql.or     1179                 :             55 :     recentXid = ReadNextTransactionId();
 6983 alvherre@alvh.no-ip.     1180                 :             55 :     xidForceLimit = recentXid - autovacuum_freeze_max_age;
                               1181                 :                :     /* ensure it's a "normal" XID, else TransactionIdPrecedes misbehaves */
                               1182                 :                :     /* this can cause the limit to go backwards by 3, but that's OK */
                               1183         [ -  + ]:             55 :     if (xidForceLimit < FirstNormalTransactionId)
 6983 alvherre@alvh.no-ip.     1184                 :UBC           0 :         xidForceLimit -= FirstNormalTransactionId;
                               1185                 :                : 
                               1186                 :                :     /* Also determine the oldest datminmxid we will consider. */
 4850 alvherre@alvh.no-ip.     1187                 :CBC          55 :     recentMulti = ReadNextMultiXactId();
 4015 rhaas@postgresql.org     1188                 :             55 :     multiForceLimit = recentMulti - MultiXactMemberFreezeThreshold();
 4850 alvherre@alvh.no-ip.     1189         [ -  + ]:             55 :     if (multiForceLimit < FirstMultiXactId)
 4850 alvherre@alvh.no-ip.     1190                 :UBC           0 :         multiForceLimit -= FirstMultiXactId;
                               1191                 :                : 
                               1192                 :                :     /*
                               1193                 :                :      * Choose a database to connect to.  We pick the database that was least
                               1194                 :                :      * recently auto-vacuumed, or one that needs vacuuming to prevent Xid
                               1195                 :                :      * wraparound-related data loss.  If any db at risk of Xid wraparound is
                               1196                 :                :      * found, we pick the one with oldest datfrozenxid, independently of
                               1197                 :                :      * autovacuum times; similarly we pick the one with the oldest datminmxid
                               1198                 :                :      * if any is in MultiXactId wraparound.  Note that those in Xid wraparound
                               1199                 :                :      * danger are given more priority than those in multi wraparound danger.
                               1200                 :                :      *
                               1201                 :                :      * Note that a database with no stats entry is not considered, except for
                               1202                 :                :      * Xid wraparound purposes.  The theory is that if no one has ever
                               1203                 :                :      * connected to it since the stats were last initialized, it doesn't need
                               1204                 :                :      * vacuuming.
                               1205                 :                :      *
                               1206                 :                :      * XXX This could be improved if we had more info about whether it needs
                               1207                 :                :      * vacuuming before connecting to it.  Perhaps look through the pgstats
                               1208                 :                :      * data for the database's tables?  One idea is to keep track of the
                               1209                 :                :      * number of new and dead tuples per database in pgstats.  However it
                               1210                 :                :      * isn't clear how to construct a metric that measures that and not cause
                               1211                 :                :      * starvation for less busy databases.
                               1212                 :                :      */
 6959 alvherre@alvh.no-ip.     1213                 :CBC          55 :     avdb = NULL;
 6983                          1214                 :             55 :     for_xid_wrap = false;
 4850                          1215                 :             55 :     for_multi_wrap = false;
 6959                          1216                 :             55 :     current_time = GetCurrentTimestamp();
 6983                          1217   [ +  -  +  +  :            296 :     foreach(cell, dblist)
                                              +  + ]
                               1218                 :                :     {
 6959                          1219                 :            241 :         avw_dbase  *tmp = lfirst(cell);
                               1220                 :                :         dlist_iter  iter;
                               1221                 :                : 
                               1222                 :                :         /* Check to see if this one is at risk of wraparound */
                               1223         [ -  + ]:            241 :         if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
                               1224                 :                :         {
 6959 alvherre@alvh.no-ip.     1225   [ #  #  #  # ]:UBC           0 :             if (avdb == NULL ||
 4850                          1226                 :              0 :                 TransactionIdPrecedes(tmp->adw_frozenxid,
                               1227                 :                :                                       avdb->adw_frozenxid))
 6959                          1228                 :              0 :                 avdb = tmp;
 6983                          1229                 :              0 :             for_xid_wrap = true;
 6983 alvherre@alvh.no-ip.     1230                 :CBC         173 :             continue;
                               1231                 :                :         }
                               1232         [ -  + ]:            241 :         else if (for_xid_wrap)
 6983 alvherre@alvh.no-ip.     1233                 :UBC           0 :             continue;           /* ignore not-at-risk DBs */
 4614 alvherre@alvh.no-ip.     1234         [ -  + ]:CBC         241 :         else if (MultiXactIdPrecedes(tmp->adw_minmulti, multiForceLimit))
                               1235                 :                :         {
 4850 alvherre@alvh.no-ip.     1236   [ #  #  #  # ]:UBC           0 :             if (avdb == NULL ||
 4614                          1237                 :              0 :                 MultiXactIdPrecedes(tmp->adw_minmulti, avdb->adw_minmulti))
 4850                          1238                 :              0 :                 avdb = tmp;
                               1239                 :              0 :             for_multi_wrap = true;
                               1240                 :              0 :             continue;
                               1241                 :                :         }
 4850 alvherre@alvh.no-ip.     1242         [ -  + ]:CBC         241 :         else if (for_multi_wrap)
 4850 alvherre@alvh.no-ip.     1243                 :UBC           0 :             continue;           /* ignore not-at-risk DBs */
                               1244                 :                : 
                               1245                 :                :         /* Find pgstat entry if any */
 6798 alvherre@alvh.no-ip.     1246                 :CBC         241 :         tmp->adw_entry = pgstat_fetch_stat_dbentry(tmp->adw_datid);
                               1247                 :                : 
                               1248                 :                :         /*
                               1249                 :                :          * Skip a database with no pgstat entry; it means it hasn't seen any
                               1250                 :                :          * activity.
                               1251                 :                :          */
 6959                          1252         [ +  + ]:            241 :         if (!tmp->adw_entry)
                               1253                 :             66 :             continue;
                               1254                 :                : 
                               1255                 :                :         /*
                               1256                 :                :          * Also, skip a database that appears on the database list as having
                               1257                 :                :          * been processed recently (less than autovacuum_naptime seconds ago).
                               1258                 :                :          * We do this so that we don't select a database which we just
                               1259                 :                :          * selected, but that pgstat hasn't gotten around to updating the last
                               1260                 :                :          * autovacuum time yet.
                               1261                 :                :          */
                               1262                 :            175 :         skipit = false;
                               1263                 :                : 
 4949                          1264   [ +  -  +  + ]:            401 :         dlist_reverse_foreach(iter, &DatabaseList)
                               1265                 :                :         {
                               1266                 :            381 :             avl_dbase  *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
                               1267                 :                : 
 6959                          1268         [ +  + ]:            381 :             if (dbp->adl_datid == tmp->adw_datid)
                               1269                 :                :             {
                               1270                 :                :                 /*
                               1271                 :                :                  * Skip this database if its next_worker value falls between
                               1272                 :                :                  * the current time and the current time plus naptime.
                               1273                 :                :                  */
 6938                          1274         [ +  + ]:            155 :                 if (!TimestampDifferenceExceeds(dbp->adl_next_worker,
 6746 bruce@momjian.us         1275                 :            107 :                                                 current_time, 0) &&
 6943 alvherre@alvh.no-ip.     1276         [ +  - ]:            107 :                     !TimestampDifferenceExceeds(current_time,
                               1277                 :                :                                                 dbp->adl_next_worker,
                               1278                 :                :                                                 autovacuum_naptime * 1000))
 6959                          1279                 :            107 :                     skipit = true;
                               1280                 :                : 
                               1281                 :            155 :                 break;
                               1282                 :                :             }
                               1283                 :                :         }
                               1284         [ +  + ]:            175 :         if (skipit)
 6983                          1285                 :            107 :             continue;
                               1286                 :                : 
                               1287                 :                :         /*
                               1288                 :                :          * Remember the db with oldest autovac time.  (If we are here, both
                               1289                 :                :          * tmp->entry and db->entry must be non-null.)
                               1290                 :                :          */
 6959                          1291         [ +  + ]:             68 :         if (avdb == NULL ||
                               1292         [ -  + ]:             20 :             tmp->adw_entry->last_autovac_time < avdb->adw_entry->last_autovac_time)
                               1293                 :             48 :             avdb = tmp;
                               1294                 :                :     }
                               1295                 :                : 
                               1296                 :                :     /* Found a database -- process it */
                               1297         [ +  + ]:             55 :     if (avdb != NULL)
                               1298                 :                :     {
                               1299                 :                :         WorkerInfo  worker;
                               1300                 :                :         dlist_node *wptr;
                               1301                 :                : 
 6983                          1302                 :             48 :         LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
                               1303                 :                : 
                               1304                 :                :         /*
                               1305                 :                :          * Get a worker entry from the freelist.  We checked above, so there
                               1306                 :                :          * really should be a free slot.
                               1307                 :                :          */
  484 nathan@postgresql.or     1308                 :             48 :         wptr = dclist_pop_head_node(&AutoVacuumShmem->av_freeWorkers);
                               1309                 :                : 
 4949 alvherre@alvh.no-ip.     1310                 :             48 :         worker = dlist_container(WorkerInfoData, wi_links, wptr);
 6959                          1311                 :             48 :         worker->wi_dboid = avdb->adw_datid;
 6768                          1312                 :             48 :         worker->wi_proc = NULL;
 6959                          1313                 :             48 :         worker->wi_launchtime = GetCurrentTimestamp();
                               1314                 :                : 
 6393 tgl@sss.pgh.pa.us        1315                 :             48 :         AutoVacuumShmem->av_startingWorker = worker;
                               1316                 :                : 
 6983 alvherre@alvh.no-ip.     1317                 :             48 :         LWLockRelease(AutovacuumLock);
                               1318                 :                : 
                               1319                 :             48 :         SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
                               1320                 :                : 
 6810                          1321                 :             48 :         retval = avdb->adw_datid;
                               1322                 :                :     }
 6959                          1323         [ -  + ]:              7 :     else if (skipit)
                               1324                 :                :     {
                               1325                 :                :         /*
                               1326                 :                :          * If we skipped all databases on the list, rebuild it, because it
                               1327                 :                :          * probably contains a dropped database.
                               1328                 :                :          */
 6959 alvherre@alvh.no-ip.     1329                 :UBC           0 :         rebuild_database_list(InvalidOid);
                               1330                 :                :     }
                               1331                 :                : 
 6810 alvherre@alvh.no-ip.     1332                 :CBC          55 :     MemoryContextSwitchTo(oldcxt);
                               1333                 :             55 :     MemoryContextDelete(tmpcxt);
                               1334                 :                : 
                               1335                 :             55 :     return retval;
                               1336                 :                : }
                               1337                 :                : 
                               1338                 :                : /*
                               1339                 :                :  * launch_worker
                               1340                 :                :  *
                               1341                 :                :  * Wrapper for starting a worker from the launcher.  Besides actually starting
                               1342                 :                :  * it, update the database list to reflect the next time that another one will
                               1343                 :                :  * need to be started on the selected database.  The actual database choice is
                               1344                 :                :  * left to do_start_worker.
                               1345                 :                :  *
                               1346                 :                :  * This routine is also expected to insert an entry into the database list if
                               1347                 :                :  * the selected database was previously absent from the list.
                               1348                 :                :  */
                               1349                 :                : static void
 6959                          1350                 :             55 : launch_worker(TimestampTz now)
                               1351                 :                : {
                               1352                 :                :     Oid         dbid;
                               1353                 :                :     dlist_iter  iter;
                               1354                 :                : 
                               1355                 :             55 :     dbid = do_start_worker();
                               1356         [ +  + ]:             55 :     if (OidIsValid(dbid))
                               1357                 :                :     {
 4724 bruce@momjian.us         1358                 :             48 :         bool        found = false;
                               1359                 :                : 
                               1360                 :                :         /*
                               1361                 :                :          * Walk the database list and update the corresponding entry.  If the
                               1362                 :                :          * database is not on the list, we'll recreate the list.
                               1363                 :                :          */
 4949 alvherre@alvh.no-ip.     1364   [ +  -  +  + ]:            162 :         dlist_foreach(iter, &DatabaseList)
                               1365                 :                :         {
                               1366                 :            155 :             avl_dbase  *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
                               1367                 :                : 
 6959                          1368         [ +  + ]:            155 :             if (avdb->adl_datid == dbid)
                               1369                 :                :             {
 4949                          1370                 :             41 :                 found = true;
                               1371                 :                : 
                               1372                 :                :                 /*
                               1373                 :                :                  * add autovacuum_naptime seconds to the current time, and use
                               1374                 :                :                  * that as the new "next_worker" field for this database.
                               1375                 :                :                  */
 6959                          1376                 :             41 :                 avdb->adl_next_worker =
                               1377                 :             41 :                     TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
                               1378                 :                : 
 4949                          1379                 :             41 :                 dlist_move_head(&DatabaseList, iter.cur);
 6959                          1380                 :             41 :                 break;
                               1381                 :                :             }
                               1382                 :                :         }
                               1383                 :                : 
                               1384                 :                :         /*
                               1385                 :                :          * If the database was not present in the database list, we rebuild
                               1386                 :                :          * the list.  It's possible that the database does not get into the
                               1387                 :                :          * list anyway, for example if it's a database that doesn't have a
                               1388                 :                :          * pgstat entry, but this is not a problem because we don't want to
                               1389                 :                :          * schedule workers regularly into those in any case.
                               1390                 :                :          */
 4949                          1391         [ +  + ]:             48 :         if (!found)
 6959                          1392                 :              7 :             rebuild_database_list(dbid);
                               1393                 :                :     }
 6983                          1394                 :             55 : }
                               1395                 :                : 
                               1396                 :                : /*
                               1397                 :                :  * Called from postmaster to signal a failure to fork a process to become
                               1398                 :                :  * worker.  The postmaster should kill(SIGUSR2) the launcher shortly
                               1399                 :                :  * after calling this function.
                               1400                 :                :  */
                               1401                 :                : void
 6889 alvherre@alvh.no-ip.     1402                 :UBC           0 : AutoVacWorkerFailed(void)
                               1403                 :                : {
                               1404                 :              0 :     AutoVacuumShmem->av_signal[AutoVacForkFailed] = true;
                               1405                 :              0 : }
                               1406                 :                : 
                               1407                 :                : /* SIGUSR2: a worker is up and running, or just finished, or failed to fork */
                               1408                 :                : static void
 6091 tgl@sss.pgh.pa.us        1409                 :CBC          95 : avl_sigusr2_handler(SIGNAL_ARGS)
                               1410                 :                : {
                               1411                 :             95 :     got_SIGUSR2 = true;
 4129 andres@anarazel.de       1412                 :             95 :     SetLatch(MyLatch);
 6959 alvherre@alvh.no-ip.     1413                 :             95 : }
                               1414                 :                : 
                               1415                 :                : 
                               1416                 :                : /********************************************************************
                               1417                 :                :  *                    AUTOVACUUM WORKER CODE
                               1418                 :                :  ********************************************************************/
                               1419                 :                : 
                               1420                 :                : /*
                               1421                 :                :  * Main entry point for autovacuum worker processes.
                               1422                 :                :  */
                               1423                 :                : void
  438 peter@eisentraut.org     1424                 :             60 : AutoVacWorkerMain(const void *startup_data, size_t startup_data_len)
                               1425                 :                : {
                               1426                 :                :     sigjmp_buf  local_sigjmp_buf;
                               1427                 :                :     Oid         dbid;
                               1428                 :                : 
  778 heikki.linnakangas@i     1429         [ -  + ]:             60 :     Assert(startup_data_len == 0);
                               1430                 :                : 
                               1431                 :                :     /* Release postmaster's working memory context */
                               1432         [ +  - ]:             60 :     if (PostmasterContext)
                               1433                 :                :     {
                               1434                 :             60 :         MemoryContextDelete(PostmasterContext);
                               1435                 :             60 :         PostmasterContext = NULL;
                               1436                 :                :     }
                               1437                 :                : 
 2246 peter@eisentraut.org     1438                 :             60 :     init_ps_display(NULL);
                               1439                 :                : 
  672 heikki.linnakangas@i     1440         [ -  + ]:             60 :     Assert(GetProcessingMode() == InitProcessing);
                               1441                 :                : 
                               1442                 :                :     /*
                               1443                 :                :      * Set up signal handlers.  We operate on databases much like a regular
                               1444                 :                :      * backend, so we use the same signal handling.  See equivalent code in
                               1445                 :                :      * tcop/postgres.c.
                               1446                 :                :      */
 2331 rhaas@postgresql.org     1447                 :             60 :     pqsignal(SIGHUP, SignalHandlerForConfigReload);
                               1448                 :                : 
                               1449                 :                :     /*
                               1450                 :                :      * SIGINT is used to signal canceling the current table's vacuum; SIGTERM
                               1451                 :                :      * means abort and exit cleanly, and SIGQUIT means abandon ship.
                               1452                 :                :      */
 7600 tgl@sss.pgh.pa.us        1453                 :             60 :     pqsignal(SIGINT, StatementCancelHandler);
                               1454                 :             60 :     pqsignal(SIGTERM, die);
                               1455                 :                :     /* SIGQUIT handler was already set up by InitPostmasterChild */
                               1456                 :                : 
 5041 alvherre@alvh.no-ip.     1457                 :             60 :     InitializeTimeouts();       /* establishes SIGALRM handler */
                               1458                 :                : 
   21 andrew@dunslane.net      1459                 :GNC          60 :     pqsignal(SIGPIPE, PG_SIG_IGN);
 6122 tgl@sss.pgh.pa.us        1460                 :CBC          60 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
   21 andrew@dunslane.net      1461                 :GNC          60 :     pqsignal(SIGUSR2, PG_SIG_IGN);
 7572 tgl@sss.pgh.pa.us        1462                 :CBC          60 :     pqsignal(SIGFPE, FloatExceptionHandler);
   21 andrew@dunslane.net      1463                 :GNC          60 :     pqsignal(SIGCHLD, PG_SIG_DFL);
                               1464                 :                : 
                               1465                 :                :     /*
                               1466                 :                :      * Create a per-backend PGPROC struct in shared memory.  We must do this
                               1467                 :                :      * before we can use LWLocks or access any shared memory.
                               1468                 :                :      */
 7426 tgl@sss.pgh.pa.us        1469                 :CBC          60 :     InitProcess();
                               1470                 :                : 
                               1471                 :                :     /* Early initialization */
 1734 andres@anarazel.de       1472                 :             60 :     BaseInit();
                               1473                 :                : 
                               1474                 :                :     /*
                               1475                 :                :      * If an exception is encountered, processing resumes here.
                               1476                 :                :      *
                               1477                 :                :      * Unlike most auxiliary processes, we don't attempt to continue
                               1478                 :                :      * processing after an error; we just clean up and exit.  The autovac
                               1479                 :                :      * launcher is responsible for spawning another worker later.
                               1480                 :                :      *
                               1481                 :                :      * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
                               1482                 :                :      * (to wit, BlockSig) will be restored when longjmp'ing to here.  Thus,
                               1483                 :                :      * signals other than SIGQUIT will be blocked until we exit.  It might
                               1484                 :                :      * seem that this policy makes the HOLD_INTERRUPTS() call redundant, but
                               1485                 :                :      * it is not since InterruptPending might be set already.
                               1486                 :                :      */
 7600 tgl@sss.pgh.pa.us        1487         [ -  + ]:             60 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
                               1488                 :                :     {
                               1489                 :                :         /* since not using PG_TRY, must reset error stack by hand */
 2386 michael@paquier.xyz      1490                 :UBC           0 :         error_context_stack = NULL;
                               1491                 :                : 
                               1492                 :                :         /* Prevents interrupts while cleaning up */
 7600 tgl@sss.pgh.pa.us        1493                 :              0 :         HOLD_INTERRUPTS();
                               1494                 :                : 
                               1495                 :                :         /* Report the error to the server log */
                               1496                 :              0 :         EmitErrorReport();
                               1497                 :                : 
                               1498                 :                :         /*
                               1499                 :                :          * We can now go away.  Note that because we called InitProcess, a
                               1500                 :                :          * callback was registered to do ProcKill, which will clean up
                               1501                 :                :          * necessary state.
                               1502                 :                :          */
                               1503                 :              0 :         proc_exit(0);
                               1504                 :                :     }
                               1505                 :                : 
                               1506                 :                :     /* We can now handle ereport(ERROR) */
 7600 tgl@sss.pgh.pa.us        1507                 :CBC          60 :     PG_exception_stack = &local_sigjmp_buf;
                               1508                 :                : 
 1187 tmunro@postgresql.or     1509                 :             60 :     sigprocmask(SIG_SETMASK, &UnBlockSig, NULL);
                               1510                 :                : 
                               1511                 :                :     /*
                               1512                 :                :      * Set always-secure search path, so malicious users can't redirect user
                               1513                 :                :      * code (e.g. pg_index.indexprs).  (That code runs in a
                               1514                 :                :      * SECURITY_RESTRICTED_OPERATION sandbox, so malicious users could not
                               1515                 :                :      * take control of the entire autovacuum worker in any case.)
                               1516                 :                :      */
 2990 noah@leadboat.com        1517                 :             60 :     SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
                               1518                 :                : 
                               1519                 :                :     /*
                               1520                 :                :      * Force zero_damaged_pages OFF in the autovac process, even if it is set
                               1521                 :                :      * in postgresql.conf.  We don't really want such a dangerous option being
                               1522                 :                :      * applied non-interactively.
                               1523                 :                :      */
 7364 tgl@sss.pgh.pa.us        1524                 :             60 :     SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
                               1525                 :                : 
                               1526                 :                :     /*
                               1527                 :                :      * Force settable timeouts off to avoid letting these settings prevent
                               1528                 :                :      * regular maintenance from being executed.
                               1529                 :                :      */
 6959 alvherre@alvh.no-ip.     1530                 :             60 :     SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
  810 akorotkov@postgresql     1531                 :             60 :     SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
 4798 tgl@sss.pgh.pa.us        1532                 :             60 :     SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
 3611                          1533                 :             60 :     SetConfigOption("idle_in_transaction_session_timeout", "0",
                               1534                 :                :                     PGC_SUSET, PGC_S_OVERRIDE);
                               1535                 :                : 
                               1536                 :                :     /*
                               1537                 :                :      * Force default_transaction_isolation to READ COMMITTED.  We don't want
                               1538                 :                :      * to pay the overhead of serializable mode, nor add any risk of causing
                               1539                 :                :      * deadlocks or delaying other transactions.
                               1540                 :                :      */
 5271                          1541                 :             60 :     SetConfigOption("default_transaction_isolation", "read committed",
                               1542                 :                :                     PGC_SUSET, PGC_S_OVERRIDE);
                               1543                 :                : 
                               1544                 :                :     /*
                               1545                 :                :      * Force synchronous replication off to allow regular maintenance even if
                               1546                 :                :      * we are waiting for standbys to connect. This is important to ensure we
                               1547                 :                :      * aren't blocked from performing anti-wraparound tasks.
                               1548                 :                :      */
 5510 simon@2ndQuadrant.co     1549         [ +  - ]:             60 :     if (synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)
 5271 tgl@sss.pgh.pa.us        1550                 :             60 :         SetConfigOption("synchronous_commit", "local",
                               1551                 :                :                         PGC_SUSET, PGC_S_OVERRIDE);
                               1552                 :                : 
                               1553                 :                :     /*
                               1554                 :                :      * Even when system is configured to use a different fetch consistency,
                               1555                 :                :      * for autovac we always want fresh stats.
                               1556                 :                :      */
 1490 andres@anarazel.de       1557                 :             60 :     SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
                               1558                 :                : 
                               1559                 :                :     /*
                               1560                 :                :      * Get the info about the database we're going to work on.
                               1561                 :                :      */
 6959 alvherre@alvh.no-ip.     1562                 :             60 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
                               1563                 :                : 
                               1564                 :                :     /*
                               1565                 :                :      * beware of startingWorker being INVALID; this should normally not
                               1566                 :                :      * happen, but if a worker fails after forking and before this, the
                               1567                 :                :      * launcher might have decided to remove it from the queue and start
                               1568                 :                :      * again.
                               1569                 :                :      */
 6393 tgl@sss.pgh.pa.us        1570         [ +  - ]:             60 :     if (AutoVacuumShmem->av_startingWorker != NULL)
                               1571                 :                :     {
                               1572                 :             60 :         MyWorkerInfo = AutoVacuumShmem->av_startingWorker;
 6943 alvherre@alvh.no-ip.     1573                 :             60 :         dbid = MyWorkerInfo->wi_dboid;
 6768                          1574                 :             60 :         MyWorkerInfo->wi_proc = MyProc;
                               1575                 :                : 
                               1576                 :                :         /* insert into the running list */
 4949                          1577                 :             60 :         dlist_push_head(&AutoVacuumShmem->av_runningWorkers,
                               1578                 :             60 :                         &MyWorkerInfo->wi_links);
                               1579                 :                : 
                               1580                 :                :         /*
                               1581                 :                :          * remove from the "starting" pointer, so that the launcher can start
                               1582                 :                :          * a new worker if required
                               1583                 :                :          */
 6393 tgl@sss.pgh.pa.us        1584                 :             60 :         AutoVacuumShmem->av_startingWorker = NULL;
 6943 alvherre@alvh.no-ip.     1585                 :             60 :         LWLockRelease(AutovacuumLock);
                               1586                 :                : 
                               1587                 :             60 :         on_shmem_exit(FreeWorkerInfo, 0);
                               1588                 :                : 
                               1589                 :                :         /* wake up the launcher */
                               1590         [ +  - ]:             60 :         if (AutoVacuumShmem->av_launcherpid != 0)
 6091 tgl@sss.pgh.pa.us        1591                 :             60 :             kill(AutoVacuumShmem->av_launcherpid, SIGUSR2);
                               1592                 :                :     }
                               1593                 :                :     else
                               1594                 :                :     {
                               1595                 :                :         /* no worker entry for me, go away */
 6889 alvherre@alvh.no-ip.     1596         [ #  # ]:UBC           0 :         elog(WARNING, "autovacuum worker started without a worker entry");
 6941 tgl@sss.pgh.pa.us        1597                 :              0 :         dbid = InvalidOid;
 6943 alvherre@alvh.no-ip.     1598                 :              0 :         LWLockRelease(AutovacuumLock);
                               1599                 :                :     }
                               1600                 :                : 
 7019 alvherre@alvh.no-ip.     1601         [ +  - ]:CBC          60 :     if (OidIsValid(dbid))
                               1602                 :                :     {
                               1603                 :                :         char        dbname[NAMEDATALEN];
                               1604                 :                : 
                               1605                 :                :         /*
                               1606                 :                :          * Report autovac startup to the cumulative stats system.  We
                               1607                 :                :          * deliberately do this before InitPostgres, so that the
                               1608                 :                :          * last_autovac_time will get updated even if the connection attempt
                               1609                 :                :          * fails.  This is to prevent autovac from getting "stuck" repeatedly
                               1610                 :                :          * selecting an unopenable database, rather than making any progress
                               1611                 :                :          * on stuff it can connect to.
                               1612                 :                :          */
                               1613                 :             60 :         pgstat_report_autovac(dbid);
                               1614                 :                : 
                               1615                 :                :         /*
                               1616                 :                :          * Connect to the selected database, specifying no particular user,
                               1617                 :                :          * and ignoring datallowconn.  Collect the database's name for
                               1618                 :                :          * display.
                               1619                 :                :          *
                               1620                 :                :          * Note: if we have selected a just-deleted database (due to using
                               1621                 :                :          * stale stats info), we'll fail and exit here.
                               1622                 :                :          */
  493 tgl@sss.pgh.pa.us        1623                 :             60 :         InitPostgres(NULL, dbid, NULL, InvalidOid,
                               1624                 :                :                      INIT_PG_OVERRIDE_ALLOW_CONNS,
                               1625                 :                :                      dbname);
 7600                          1626                 :             60 :         SetProcessingMode(NormalProcessing);
 2246 peter@eisentraut.org     1627                 :             60 :         set_ps_display(dbname);
 7313 bruce@momjian.us         1628         [ +  + ]:             60 :         ereport(DEBUG1,
                               1629                 :                :                 (errmsg_internal("autovacuum: processing database \"%s\"", dbname)));
                               1630                 :                : 
 6768 alvherre@alvh.no-ip.     1631         [ -  + ]:             60 :         if (PostAuthDelay)
 6768 alvherre@alvh.no-ip.     1632                 :UBC           0 :             pg_usleep(PostAuthDelay * 1000000L);
                               1633                 :                : 
                               1634                 :                :         /* And do an appropriate amount of work */
 1905 tmunro@postgresql.or     1635                 :CBC          60 :         recentXid = ReadNextTransactionId();
 4850 alvherre@alvh.no-ip.     1636                 :             60 :         recentMulti = ReadNextMultiXactId();
 6978                          1637                 :             60 :         do_autovacuum();
                               1638                 :                :     }
                               1639                 :                : 
                               1640                 :                :     /* All done, go away */
 7600 tgl@sss.pgh.pa.us        1641                 :             58 :     proc_exit(0);
                               1642                 :                : }
                               1643                 :                : 
                               1644                 :                : /*
                               1645                 :                :  * Return a WorkerInfo to the free list
                               1646                 :                :  */
                               1647                 :                : static void
 6959 alvherre@alvh.no-ip.     1648                 :             60 : FreeWorkerInfo(int code, Datum arg)
                               1649                 :                : {
                               1650         [ +  - ]:             60 :     if (MyWorkerInfo != NULL)
                               1651                 :                :     {
                               1652                 :             60 :         LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
                               1653                 :                : 
 4947 tgl@sss.pgh.pa.us        1654                 :             60 :         dlist_delete(&MyWorkerInfo->wi_links);
 6959 alvherre@alvh.no-ip.     1655                 :             60 :         MyWorkerInfo->wi_dboid = InvalidOid;
                               1656                 :             60 :         MyWorkerInfo->wi_tableoid = InvalidOid;
 3647                          1657                 :             60 :         MyWorkerInfo->wi_sharedrel = false;
 6768                          1658                 :             60 :         MyWorkerInfo->wi_proc = NULL;
 6959                          1659                 :             60 :         MyWorkerInfo->wi_launchtime = 0;
 1124 dgustafsson@postgres     1660                 :             60 :         pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
  484 nathan@postgresql.or     1661                 :             60 :         dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
                               1662                 :             60 :                          &MyWorkerInfo->wi_links);
                               1663                 :                :         /* not mine anymore */
 6959 alvherre@alvh.no-ip.     1664                 :             60 :         MyWorkerInfo = NULL;
                               1665                 :                : 
                               1666                 :                :         /*
                               1667                 :                :          * now that we're inactive, cause a rebalancing of the surviving
                               1668                 :                :          * workers
                               1669                 :                :          */
 6889                          1670                 :             60 :         AutoVacuumShmem->av_signal[AutoVacRebalance] = true;
 6959                          1671                 :             60 :         LWLockRelease(AutovacuumLock);
                               1672                 :                :     }
                               1673                 :             60 : }
                               1674                 :                : 
                               1675                 :                : /*
                               1676                 :                :  * Update vacuum cost-based delay-related parameters for autovacuum workers and
                               1677                 :                :  * backends executing VACUUM or ANALYZE using the value of relevant GUCs and
                               1678                 :                :  * global state. This must be called during setup for vacuum and after every
                               1679                 :                :  * config reload to ensure up-to-date values.
                               1680                 :                :  */
                               1681                 :                : void
 1124 dgustafsson@postgres     1682                 :           9550 : VacuumUpdateCosts(void)
                               1683                 :                : {
 6959 alvherre@alvh.no-ip.     1684         [ +  + ]:           9550 :     if (MyWorkerInfo)
                               1685                 :                :     {
 1124 dgustafsson@postgres     1686         [ -  + ]:           1169 :         if (av_storage_param_cost_delay >= 0)
 1124 dgustafsson@postgres     1687                 :UBC           0 :             vacuum_cost_delay = av_storage_param_cost_delay;
 1124 dgustafsson@postgres     1688         [ +  - ]:CBC        1169 :         else if (autovacuum_vac_cost_delay >= 0)
                               1689                 :           1169 :             vacuum_cost_delay = autovacuum_vac_cost_delay;
                               1690                 :                :         else
                               1691                 :                :             /* fall back to VacuumCostDelay */
 1124 dgustafsson@postgres     1692                 :UBC           0 :             vacuum_cost_delay = VacuumCostDelay;
                               1693                 :                : 
 1124 dgustafsson@postgres     1694                 :CBC        1169 :         AutoVacuumUpdateCostLimit();
                               1695                 :                :     }
                               1696                 :                :     else
                               1697                 :                :     {
                               1698                 :                :         /* Must be explicit VACUUM or ANALYZE or parallel autovacuum worker */
                               1699                 :           8381 :         vacuum_cost_delay = VacuumCostDelay;
                               1700                 :           8381 :         vacuum_cost_limit = VacuumCostLimit;
                               1701                 :                :     }
                               1702                 :                : 
                               1703                 :                :     /*
                               1704                 :                :      * If configuration changes are allowed to impact VacuumCostActive, make
                               1705                 :                :      * sure it is updated.
                               1706                 :                :      */
                               1707         [ -  + ]:           9550 :     if (VacuumFailsafeActive)
 1124 dgustafsson@postgres     1708         [ #  # ]:UBC           0 :         Assert(!VacuumCostActive);
 1124 dgustafsson@postgres     1709         [ +  + ]:CBC        9550 :     else if (vacuum_cost_delay > 0)
                               1710                 :           1172 :         VacuumCostActive = true;
                               1711                 :                :     else
                               1712                 :                :     {
                               1713                 :           8378 :         VacuumCostActive = false;
                               1714                 :           8378 :         VacuumCostBalance = 0;
                               1715                 :                :     }
                               1716                 :                : 
                               1717                 :                :     /*
                               1718                 :                :      * Since the cost logging requires a lock, avoid rendering the log message
                               1719                 :                :      * in case we are using a message level where the log wouldn't be emitted.
                               1720                 :                :      */
 1111                          1721   [ +  +  +  + ]:           9550 :     if (MyWorkerInfo && message_level_is_interesting(DEBUG2))
                               1722                 :                :     {
                               1723                 :                :         Oid         dboid,
                               1724                 :                :                     tableoid;
                               1725                 :                : 
 1124 dgustafsson@postgres     1726         [ -  + ]:GBC          13 :         Assert(!LWLockHeldByMe(AutovacuumLock));
                               1727                 :                : 
                               1728                 :             13 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
                               1729                 :             13 :         dboid = MyWorkerInfo->wi_dboid;
                               1730                 :             13 :         tableoid = MyWorkerInfo->wi_tableoid;
                               1731                 :             13 :         LWLockRelease(AutovacuumLock);
                               1732                 :                : 
                               1733   [ +  -  -  +  :             13 :         elog(DEBUG2,
                                        +  -  -  + ]
                               1734                 :                :              "Autovacuum VacuumUpdateCosts(db=%u, rel=%u, dobalance=%s, cost_limit=%d, cost_delay=%g active=%s failsafe=%s)",
                               1735                 :                :              dboid, tableoid, pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance) ? "no" : "yes",
                               1736                 :                :              vacuum_cost_limit, vacuum_cost_delay,
                               1737                 :                :              vacuum_cost_delay > 0 ? "yes" : "no",
                               1738                 :                :              VacuumFailsafeActive ? "yes" : "no");
                               1739                 :                :     }
 6959 alvherre@alvh.no-ip.     1740                 :CBC        9550 : }
                               1741                 :                : 
                               1742                 :                : /*
                               1743                 :                :  * Update vacuum_cost_limit with the correct value for an autovacuum worker,
                               1744                 :                :  * given the value of other relevant cost limit parameters and the number of
                               1745                 :                :  * workers across which the limit must be balanced. Autovacuum workers must
                               1746                 :                :  * call this regularly in case av_nworkersForBalance has been updated by
                               1747                 :                :  * another worker or by the autovacuum launcher. They must also call it after a
                               1748                 :                :  * config reload.
                               1749                 :                :  */
                               1750                 :                : void
 1124 dgustafsson@postgres     1751                 :           2916 : AutoVacuumUpdateCostLimit(void)
                               1752                 :                : {
                               1753         [ +  + ]:           2916 :     if (!MyWorkerInfo)
 1124 dgustafsson@postgres     1754                 :GBC          18 :         return;
                               1755                 :                : 
                               1756                 :                :     /*
                               1757                 :                :      * note: in cost_limit, zero also means use value from elsewhere, because
                               1758                 :                :      * zero is not a valid value.
                               1759                 :                :      */
                               1760                 :                : 
 1124 dgustafsson@postgres     1761         [ -  + ]:CBC        2898 :     if (av_storage_param_cost_limit > 0)
 1124 dgustafsson@postgres     1762                 :UBC           0 :         vacuum_cost_limit = av_storage_param_cost_limit;
                               1763                 :                :     else
                               1764                 :                :     {
                               1765                 :                :         int         nworkers_for_balance;
                               1766                 :                : 
 1124 dgustafsson@postgres     1767         [ +  + ]:CBC        2898 :         if (autovacuum_vac_cost_limit > 0)
 1124 dgustafsson@postgres     1768                 :GBC           9 :             vacuum_cost_limit = autovacuum_vac_cost_limit;
                               1769                 :                :         else
 1124 dgustafsson@postgres     1770                 :CBC        2889 :             vacuum_cost_limit = VacuumCostLimit;
                               1771                 :                : 
                               1772                 :                :         /* Only balance limit if no cost-related storage parameters specified */
                               1773         [ -  + ]:           2898 :         if (pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance))
 1124 dgustafsson@postgres     1774                 :UBC           0 :             return;
                               1775                 :                : 
 1124 dgustafsson@postgres     1776         [ -  + ]:CBC        2898 :         Assert(vacuum_cost_limit > 0);
                               1777                 :                : 
                               1778                 :           2898 :         nworkers_for_balance = pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
                               1779                 :                : 
                               1780                 :                :         /* There is at least 1 autovac worker (this worker) */
                               1781         [ -  + ]:           2898 :         if (nworkers_for_balance <= 0)
 1124 dgustafsson@postgres     1782         [ #  # ]:UBC           0 :             elog(ERROR, "nworkers_for_balance must be > 0");
                               1783                 :                : 
 1124 dgustafsson@postgres     1784                 :CBC        2898 :         vacuum_cost_limit = Max(vacuum_cost_limit / nworkers_for_balance, 1);
                               1785                 :                :     }
                               1786                 :                : }
                               1787                 :                : 
                               1788                 :                : /*
                               1789                 :                :  * autovac_recalculate_workers_for_balance
                               1790                 :                :  *      Recalculate the number of workers to consider, given cost-related
                               1791                 :                :  *      storage parameters and the current number of active workers.
                               1792                 :                :  *
                               1793                 :                :  * Caller must hold the AutovacuumLock in at least shared mode to access
                               1794                 :                :  * worker->wi_proc.
                               1795                 :                :  */
                               1796                 :                : static void
                               1797                 :            631 : autovac_recalculate_workers_for_balance(void)
                               1798                 :                : {
                               1799                 :                :     dlist_iter  iter;
                               1800                 :                :     int         orig_nworkers_for_balance;
                               1801                 :            631 :     int         nworkers_for_balance = 0;
                               1802                 :                : 
                               1803         [ -  + ]:            631 :     Assert(LWLockHeldByMe(AutovacuumLock));
                               1804                 :                : 
                               1805                 :            631 :     orig_nworkers_for_balance =
                               1806                 :            631 :         pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
                               1807                 :                : 
 4949 alvherre@alvh.no-ip.     1808   [ +  -  +  + ]:           1215 :     dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
                               1809                 :                :     {
 4724 bruce@momjian.us         1810                 :            584 :         WorkerInfo  worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
                               1811                 :                : 
 1124 dgustafsson@postgres     1812   [ +  -  -  + ]:           1168 :         if (worker->wi_proc == NULL ||
                               1813                 :            584 :             pg_atomic_unlocked_test_flag(&worker->wi_dobalance))
 1124 dgustafsson@postgres     1814                 :UBC           0 :             continue;
                               1815                 :                : 
 1124 dgustafsson@postgres     1816                 :CBC         584 :         nworkers_for_balance++;
                               1817                 :                :     }
                               1818                 :                : 
                               1819         [ +  + ]:            631 :     if (nworkers_for_balance != orig_nworkers_for_balance)
                               1820                 :             79 :         pg_atomic_write_u32(&AutoVacuumShmem->av_nworkersForBalance,
                               1821                 :                :                             nworkers_for_balance);
 6959 alvherre@alvh.no-ip.     1822                 :            631 : }
                               1823                 :                : 
                               1824                 :                : /*
                               1825                 :                :  * get_database_list
                               1826                 :                :  *      Return a list of all databases found in pg_database.
                               1827                 :                :  *
                               1828                 :                :  * The list and associated data is allocated in the caller's memory context,
                               1829                 :                :  * which is in charge of ensuring that it's properly cleaned up afterwards.
                               1830                 :                :  *
                               1831                 :                :  * Note: this is the only function in which the autovacuum launcher uses a
                               1832                 :                :  * transaction.  Although we aren't attached to any particular database and
                               1833                 :                :  * therefore can't access most catalogs, we do have enough infrastructure
                               1834                 :                :  * to do a seqscan on pg_database.
                               1835                 :                :  */
                               1836                 :                : static List *
                               1837                 :            603 : get_database_list(void)
                               1838                 :                : {
 7507 bruce@momjian.us         1839                 :            603 :     List       *dblist = NIL;
                               1840                 :                :     Relation    rel;
                               1841                 :                :     TableScanDesc scan;
                               1842                 :                :     HeapTuple   tup;
                               1843                 :                :     MemoryContext resultcxt;
                               1844                 :                : 
                               1845                 :                :     /* This is the context that we will allocate our output data in */
 5657 alvherre@alvh.no-ip.     1846                 :            603 :     resultcxt = CurrentMemoryContext;
                               1847                 :                : 
                               1848                 :                :     /*
                               1849                 :                :      * Start a transaction so we can access pg_database.
                               1850                 :                :      */
 6091 tgl@sss.pgh.pa.us        1851                 :            603 :     StartTransactionCommand();
                               1852                 :                : 
 2661 andres@anarazel.de       1853                 :            603 :     rel = table_open(DatabaseRelationId, AccessShareLock);
 2612                          1854                 :            603 :     scan = table_beginscan_catalog(rel, 0, NULL);
                               1855                 :                : 
 6091 tgl@sss.pgh.pa.us        1856         [ +  + ]:           2801 :     while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
                               1857                 :                :     {
                               1858                 :           2198 :         Form_pg_database pgdatabase = (Form_pg_database) GETSTRUCT(tup);
                               1859                 :                :         avw_dbase  *avdb;
                               1860                 :                :         MemoryContext oldcxt;
                               1861                 :                : 
                               1862                 :                :         /*
                               1863                 :                :          * If database has partially been dropped, we can't, nor need to,
                               1864                 :                :          * vacuum it.
                               1865                 :                :          */
 1027 andres@anarazel.de       1866         [ +  + ]:           2198 :         if (database_is_invalid_form(pgdatabase))
                               1867                 :                :         {
                               1868         [ -  + ]:              2 :             elog(DEBUG2,
                               1869                 :                :                  "autovacuum: skipping invalid database \"%s\"",
                               1870                 :                :                  NameStr(pgdatabase->datname));
                               1871                 :              2 :             continue;
                               1872                 :                :         }
                               1873                 :                : 
                               1874                 :                :         /*
                               1875                 :                :          * Allocate our results in the caller's context, not the
                               1876                 :                :          * transaction's. We do this inside the loop, and restore the original
                               1877                 :                :          * context at the end, so that leaky things like heap_getnext() are
                               1878                 :                :          * not called in a potentially long-lived context.
                               1879                 :                :          */
 5657 alvherre@alvh.no-ip.     1880                 :           2196 :         oldcxt = MemoryContextSwitchTo(resultcxt);
                               1881                 :                : 
  146 michael@paquier.xyz      1882                 :GNC        2196 :         avdb = palloc_object(avw_dbase);
                               1883                 :                : 
 2723 andres@anarazel.de       1884                 :CBC        2196 :         avdb->adw_datid = pgdatabase->oid;
 6091 tgl@sss.pgh.pa.us        1885                 :           2196 :         avdb->adw_name = pstrdup(NameStr(pgdatabase->datname));
                               1886                 :           2196 :         avdb->adw_frozenxid = pgdatabase->datfrozenxid;
 4614 alvherre@alvh.no-ip.     1887                 :           2196 :         avdb->adw_minmulti = pgdatabase->datminmxid;
                               1888                 :                :         /* this gets set later: */
 6959                          1889                 :           2196 :         avdb->adw_entry = NULL;
                               1890                 :                : 
 6979                          1891                 :           2196 :         dblist = lappend(dblist, avdb);
 5657                          1892                 :           2196 :         MemoryContextSwitchTo(oldcxt);
                               1893                 :                :     }
                               1894                 :                : 
 2612 andres@anarazel.de       1895                 :            603 :     table_endscan(scan);
 2661                          1896                 :            603 :     table_close(rel, AccessShareLock);
                               1897                 :                : 
 6091 tgl@sss.pgh.pa.us        1898                 :            603 :     CommitTransactionCommand();
                               1899                 :                : 
                               1900                 :                :     /* Be sure to restore caller's memory context */
 1343                          1901                 :            603 :     MemoryContextSwitchTo(resultcxt);
                               1902                 :                : 
 7600                          1903                 :            603 :     return dblist;
                               1904                 :                : }
                               1905                 :                : 
                               1906                 :                : /*
                               1907                 :                :  * List comparator for TableToProcess.  Note that this sorts the tables based
                               1908                 :                :  * on their scores in descending order.
                               1909                 :                :  */
                               1910                 :                : static int
   39 nathan@postgresql.or     1911                 :GNC        2747 : TableToProcessComparator(const ListCell *a, const ListCell *b)
                               1912                 :                : {
                               1913                 :           2747 :     TableToProcess *t1 = (TableToProcess *) lfirst(a);
                               1914                 :           2747 :     TableToProcess *t2 = (TableToProcess *) lfirst(b);
                               1915                 :                : 
                               1916         [ +  + ]:           2747 :     return (t2->score < t1->score) ? -1 : (t2->score > t1->score) ? 1 : 0;
                               1917                 :                : }
                               1918                 :                : 
                               1919                 :                : /*
                               1920                 :                :  * Process a database table-by-table
                               1921                 :                :  *
                               1922                 :                :  * Note that CHECK_FOR_INTERRUPTS is supposed to be used in certain spots in
                               1923                 :                :  * order not to ignore shutdown commands for too long.
                               1924                 :                :  */
                               1925                 :                : static void
 6978 alvherre@alvh.no-ip.     1926                 :CBC          60 : do_autovacuum(void)
                               1927                 :                : {
                               1928                 :                :     Relation    classRel;
                               1929                 :                :     HeapTuple   tuple;
                               1930                 :                :     TableScanDesc relScan;
                               1931                 :                :     Form_pg_database dbForm;
   39 nathan@postgresql.or     1932                 :GNC          60 :     List       *tables_to_process = NIL;
 3452 rhaas@postgresql.org     1933                 :CBC          60 :     List       *orphan_oids = NIL;
                               1934                 :                :     HASHCTL     ctl;
                               1935                 :                :     HTAB       *table_toast_map;
                               1936                 :                :     ListCell   *volatile cell;
                               1937                 :                :     BufferAccessStrategy bstrategy;
                               1938                 :                :     ScanKeyData key;
                               1939                 :                :     TupleDesc   pg_class_desc;
                               1940                 :                :     int         effective_multixact_freeze_max_age;
 3392                          1941                 :             60 :     bool        did_vacuum = false;
                               1942                 :             60 :     bool        found_concurrent_worker = false;
                               1943                 :                :     int         i;
                               1944                 :                : 
                               1945                 :                :     /*
                               1946                 :                :      * StartTransactionCommand and CommitTransactionCommand will automatically
                               1947                 :                :      * switch to other contexts.  We need this one to keep the list of
                               1948                 :                :      * relations to vacuum/analyze across transactions.
                               1949                 :                :      */
 6884 alvherre@alvh.no-ip.     1950                 :             60 :     AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
                               1951                 :                :                                           "Autovacuum worker",
                               1952                 :                :                                           ALLOCSET_DEFAULT_SIZES);
                               1953                 :             60 :     MemoryContextSwitchTo(AutovacMemCxt);
                               1954                 :                : 
                               1955                 :                :     /* Start a transaction so our commands have one to play into. */
 7600 tgl@sss.pgh.pa.us        1956                 :             60 :     StartTransactionCommand();
                               1957                 :                : 
                               1958                 :                :     /*
                               1959                 :                :      * This injection point is put in a transaction block to work with a wait
                               1960                 :                :      * that uses a condition variable.
                               1961                 :                :      */
  360 michael@paquier.xyz      1962                 :             60 :     INJECTION_POINT("autovacuum-worker-start", NULL);
                               1963                 :                : 
                               1964                 :                :     /*
                               1965                 :                :      * Compute the multixact age for which freezing is urgent.  This is
                               1966                 :                :      * normally autovacuum_multixact_freeze_max_age, but may be less if
                               1967                 :                :      * multixact members are bloated.
                               1968                 :                :      */
 4015 rhaas@postgresql.org     1969                 :             59 :     effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
                               1970                 :                : 
                               1971                 :                :     /*
                               1972                 :                :      * Find the pg_database entry and select the default freeze ages. We use
                               1973                 :                :      * zero in template and nonconnectable databases, else the system-wide
                               1974                 :                :      * default.
                               1975                 :                :      */
 5924                          1976                 :             59 :     tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
 7121 tgl@sss.pgh.pa.us        1977         [ -  + ]:             59 :     if (!HeapTupleIsValid(tuple))
 7121 tgl@sss.pgh.pa.us        1978         [ #  # ]:UBC           0 :         elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
 7121 tgl@sss.pgh.pa.us        1979                 :CBC          59 :     dbForm = (Form_pg_database) GETSTRUCT(tuple);
                               1980                 :                : 
                               1981   [ +  +  -  + ]:             59 :     if (dbForm->datistemplate || !dbForm->datallowconn)
                               1982                 :                :     {
                               1983                 :             24 :         default_freeze_min_age = 0;
 6318 heikki.linnakangas@i     1984                 :             24 :         default_freeze_table_age = 0;
 4464 alvherre@alvh.no-ip.     1985                 :             24 :         default_multixact_freeze_min_age = 0;
                               1986                 :             24 :         default_multixact_freeze_table_age = 0;
                               1987                 :                :     }
                               1988                 :                :     else
                               1989                 :                :     {
 7121 tgl@sss.pgh.pa.us        1990                 :             35 :         default_freeze_min_age = vacuum_freeze_min_age;
 6318 heikki.linnakangas@i     1991                 :             35 :         default_freeze_table_age = vacuum_freeze_table_age;
 4464 alvherre@alvh.no-ip.     1992                 :             35 :         default_multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
                               1993                 :             35 :         default_multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
                               1994                 :                :     }
                               1995                 :                : 
 7121 tgl@sss.pgh.pa.us        1996                 :             59 :     ReleaseSysCache(tuple);
                               1997                 :                : 
                               1998                 :                :     /* StartTransactionCommand changed elsewhere */
 7600                          1999                 :             59 :     MemoryContextSwitchTo(AutovacMemCxt);
                               2000                 :                : 
 2661 andres@anarazel.de       2001                 :             59 :     classRel = table_open(RelationRelationId, AccessShareLock);
                               2002                 :                : 
                               2003                 :                :     /* create a copy so we can use it after closing pg_class */
 6294 alvherre@alvh.no-ip.     2004                 :             59 :     pg_class_desc = CreateTupleDescCopy(RelationGetDescr(classRel));
                               2005                 :                : 
                               2006                 :                :     /* create hash table for toast <-> main relid mapping */
 6474                          2007                 :             59 :     ctl.keysize = sizeof(Oid);
 6294                          2008                 :             59 :     ctl.entrysize = sizeof(av_relation);
                               2009                 :                : 
 6474                          2010                 :             59 :     table_toast_map = hash_create("TOAST to main relid map",
                               2011                 :                :                                   100,
                               2012                 :                :                                   &ctl,
                               2013                 :                :                                   HASH_ELEM | HASH_BLOBS);
                               2014                 :                : 
                               2015                 :                :     /*
                               2016                 :                :      * Scan pg_class to determine which tables to vacuum.
                               2017                 :                :      *
                               2018                 :                :      * We do this in two passes: on the first one we collect the list of plain
                               2019                 :                :      * relations and materialized views, and on the second one we collect
                               2020                 :                :      * TOAST tables. The reason for doing the second pass is that during it we
                               2021                 :                :      * want to use the main relation's pg_class.reloptions entry if the TOAST
                               2022                 :                :      * table does not have any, and we cannot obtain it unless we know
                               2023                 :                :      * beforehand what's the main table OID.
                               2024                 :                :      *
                               2025                 :                :      * We need to check TOAST tables separately because in cases with short,
                               2026                 :                :      * wide tables there might be proportionally much more activity in the
                               2027                 :                :      * TOAST table than in its parent.
                               2028                 :                :      */
 2612 andres@anarazel.de       2029                 :             59 :     relScan = table_beginscan_catalog(classRel, 0, NULL);
                               2030                 :                : 
                               2031                 :                :     /*
                               2032                 :                :      * On the first pass, we collect main tables to vacuum, and also the main
                               2033                 :                :      * table relid to TOAST relid mapping.
                               2034                 :                :      */
 7572 tgl@sss.pgh.pa.us        2035         [ +  + ]:          38476 :     while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
                               2036                 :                :     {
                               2037                 :          38417 :         Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
                               2038                 :                :         AutoVacOpts *relopts;
                               2039                 :                :         Oid         relid;
                               2040                 :                :         bool        dovacuum;
                               2041                 :                :         bool        doanalyze;
                               2042                 :                :         bool        wraparound;
                               2043                 :                :         AutoVacuumScores scores;
                               2044                 :                : 
 4811 kgrittn@postgresql.o     2045         [ +  + ]:          38417 :         if (classForm->relkind != RELKIND_RELATION &&
 1723 alvherre@alvh.no-ip.     2046         [ +  + ]:          29482 :             classForm->relkind != RELKIND_MATVIEW)
 4811 kgrittn@postgresql.o     2047                 :          29443 :             continue;
                               2048                 :                : 
 2723 andres@anarazel.de       2049                 :           9015 :         relid = classForm->oid;
                               2050                 :                : 
                               2051                 :                :         /*
                               2052                 :                :          * Check if it is a temp table (presumably, of some other backend's).
                               2053                 :                :          * We cannot safely process other backends' temp tables.
                               2054                 :                :          */
 5622 rhaas@postgresql.org     2055         [ +  + ]:           9015 :         if (classForm->relpersistence == RELPERSISTENCE_TEMP)
                               2056                 :                :         {
                               2057                 :                :             /*
                               2058                 :                :              * We just ignore it if the owning backend is still active and
                               2059                 :                :              * using the temporary schema.  Also, for safety, ignore it if the
                               2060                 :                :              * namespace doesn't exist or isn't a temp namespace after all.
                               2061                 :                :              */
 2258 tgl@sss.pgh.pa.us        2062         [ -  + ]:             41 :             if (checkTempNamespaceStatus(classForm->relnamespace) == TEMP_NAMESPACE_IDLE)
                               2063                 :                :             {
                               2064                 :                :                 /*
                               2065                 :                :                  * The table seems to be orphaned -- although it might be that
                               2066                 :                :                  * the owning backend has already deleted it and exited; our
                               2067                 :                :                  * pg_class scan snapshot is not necessarily up-to-date
                               2068                 :                :                  * anymore, so we could be looking at a committed-dead entry.
                               2069                 :                :                  * Remember it so we can try to delete it later.
                               2070                 :                :                  */
 3452 rhaas@postgresql.org     2071                 :UBC           0 :                 orphan_oids = lappend_oid(orphan_oids, relid);
                               2072                 :                :             }
 3446 tgl@sss.pgh.pa.us        2073                 :CBC          41 :             continue;
                               2074                 :                :         }
                               2075                 :                : 
                               2076                 :                :         /* Fetch reloptions and the pgstat entry for this table */
                               2077                 :           8974 :         relopts = extract_autovac_opts(tuple, pg_class_desc);
                               2078                 :                : 
                               2079                 :                :         /* Check if it needs vacuum or analyze */
   29 nathan@postgresql.or     2080                 :GNC        8974 :         relation_needs_vacanalyze(relid, relopts, classForm,
                               2081                 :                :                                   effective_multixact_freeze_max_age,
                               2082                 :                :                                   DEBUG3,
                               2083                 :                :                                   &dovacuum, &doanalyze, &wraparound,
                               2084                 :                :                                   &scores);
                               2085                 :                : 
                               2086                 :                :         /* Relations that need work are added to tables_to_process */
 3446 tgl@sss.pgh.pa.us        2087   [ +  +  +  + ]:CBC        8974 :         if (dovacuum || doanalyze)
                               2088                 :                :         {
   39 nathan@postgresql.or     2089                 :GNC         580 :             TableToProcess *table = palloc_object(TableToProcess);
                               2090                 :                : 
                               2091                 :            580 :             table->oid = relid;
                               2092                 :            580 :             table->score = scores.max;
                               2093                 :            580 :             tables_to_process = lappend(tables_to_process, table);
                               2094                 :                :         }
                               2095                 :                : 
                               2096                 :                :         /*
                               2097                 :                :          * Remember TOAST associations for the second pass.  Note: we must do
                               2098                 :                :          * this whether or not the table is going to be vacuumed, because we
                               2099                 :                :          * don't automatically vacuum toast tables along the parent table.
                               2100                 :                :          */
 3446 tgl@sss.pgh.pa.us        2101         [ +  + ]:CBC        8974 :         if (OidIsValid(classForm->reltoastrelid))
                               2102                 :                :         {
                               2103                 :                :             av_relation *hentry;
                               2104                 :                :             bool        found;
                               2105                 :                : 
                               2106                 :           8364 :             hentry = hash_search(table_toast_map,
                               2107                 :           4182 :                                  &classForm->reltoastrelid,
                               2108                 :                :                                  HASH_ENTER, &found);
                               2109                 :                : 
                               2110         [ +  - ]:           4182 :             if (!found)
                               2111                 :                :             {
                               2112                 :                :                 /* hash_search already filled in the key */
                               2113                 :           4182 :                 hentry->ar_relid = relid;
                               2114                 :           4182 :                 hentry->ar_hasrelopts = false;
                               2115         [ +  + ]:           4182 :                 if (relopts != NULL)
                               2116                 :                :                 {
                               2117                 :             88 :                     hentry->ar_hasrelopts = true;
                               2118                 :             88 :                     memcpy(&hentry->ar_reloptions, relopts,
                               2119                 :                :                            sizeof(AutoVacOpts));
                               2120                 :                :                 }
                               2121                 :                :             }
                               2122                 :                :         }
                               2123                 :                : 
                               2124                 :                :         /* Release stuff to avoid per-relation leakage */
  347                          2125         [ +  + ]:           8974 :         if (relopts)
                               2126                 :            179 :             pfree(relopts);
                               2127                 :                :     }
                               2128                 :                : 
 2612 andres@anarazel.de       2129                 :             59 :     table_endscan(relScan);
                               2130                 :                : 
                               2131                 :                :     /* second pass: check TOAST tables */
 6474 alvherre@alvh.no-ip.     2132                 :             59 :     ScanKeyInit(&key,
                               2133                 :                :                 Anum_pg_class_relkind,
                               2134                 :                :                 BTEqualStrategyNumber, F_CHAREQ,
                               2135                 :                :                 CharGetDatum(RELKIND_TOASTVALUE));
                               2136                 :                : 
 2612 andres@anarazel.de       2137                 :             59 :     relScan = table_beginscan_catalog(classRel, 1, &key);
 6474 alvherre@alvh.no-ip.     2138         [ +  + ]:           4249 :     while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
                               2139                 :                :     {
                               2140                 :           4190 :         Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
                               2141                 :                :         Oid         relid;
                               2142                 :                :         AutoVacOpts *relopts;
  347 tgl@sss.pgh.pa.us        2143                 :           4190 :         bool        free_relopts = false;
                               2144                 :                :         bool        dovacuum;
                               2145                 :                :         bool        doanalyze;
                               2146                 :                :         bool        wraparound;
                               2147                 :                :         AutoVacuumScores scores;
                               2148                 :                : 
                               2149                 :                :         /*
                               2150                 :                :          * We cannot safely process other backends' temp tables, so skip 'em.
                               2151                 :                :          */
 5622 rhaas@postgresql.org     2152         [ +  + ]:           4190 :         if (classForm->relpersistence == RELPERSISTENCE_TEMP)
 6474 alvherre@alvh.no-ip.     2153                 :              8 :             continue;
                               2154                 :                : 
 2723 andres@anarazel.de       2155                 :           4182 :         relid = classForm->oid;
                               2156                 :                : 
                               2157                 :                :         /*
                               2158                 :                :          * fetch reloptions -- if this toast table does not have them, try the
                               2159                 :                :          * main rel
                               2160                 :                :          */
 6294 alvherre@alvh.no-ip.     2161                 :           4182 :         relopts = extract_autovac_opts(tuple, pg_class_desc);
  347 tgl@sss.pgh.pa.us        2162         [ +  + ]:           4182 :         if (relopts)
  347 tgl@sss.pgh.pa.us        2163                 :GBC           2 :             free_relopts = true;
                               2164                 :                :         else
                               2165                 :                :         {
                               2166                 :                :             av_relation *hentry;
                               2167                 :                :             bool        found;
                               2168                 :                : 
 6294 alvherre@alvh.no-ip.     2169                 :CBC        4180 :             hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
                               2170   [ +  -  +  + ]:           4180 :             if (found && hentry->ar_hasrelopts)
                               2171                 :             86 :                 relopts = &hentry->ar_reloptions;
                               2172                 :                :         }
                               2173                 :                : 
   29 nathan@postgresql.or     2174                 :GNC        4182 :         relation_needs_vacanalyze(relid, relopts, classForm,
                               2175                 :                :                                   effective_multixact_freeze_max_age,
                               2176                 :                :                                   DEBUG3,
                               2177                 :                :                                   &dovacuum, &doanalyze, &wraparound,
                               2178                 :                :                                   &scores);
                               2179                 :                : 
                               2180                 :                :         /* ignore analyze for toast tables */
 6474 alvherre@alvh.no-ip.     2181         [ +  + ]:CBC        4182 :         if (dovacuum)
                               2182                 :                :         {
   39 nathan@postgresql.or     2183                 :GNC           5 :             TableToProcess *table = palloc_object(TableToProcess);
                               2184                 :                : 
                               2185                 :              5 :             table->oid = relid;
                               2186                 :              5 :             table->score = scores.max;
                               2187                 :              5 :             tables_to_process = lappend(tables_to_process, table);
                               2188                 :                :         }
                               2189                 :                : 
                               2190                 :                :         /* Release stuff to avoid leakage */
  347 tgl@sss.pgh.pa.us        2191         [ +  + ]:CBC        4182 :         if (free_relopts)
  347 tgl@sss.pgh.pa.us        2192                 :GBC           2 :             pfree(relopts);
                               2193                 :                :     }
                               2194                 :                : 
 2612 andres@anarazel.de       2195                 :CBC          59 :     table_endscan(relScan);
 2661                          2196                 :             59 :     table_close(classRel, AccessShareLock);
                               2197                 :                : 
                               2198                 :                :     /*
                               2199                 :                :      * Recheck orphan temporary tables, and if they still seem orphaned, drop
                               2200                 :                :      * them.  We'll eat a transaction per dropped table, which might seem
                               2201                 :                :      * excessive, but we should only need to do anything as a result of a
                               2202                 :                :      * previous backend crash, so this should not happen often enough to
                               2203                 :                :      * justify "optimizing".  Using separate transactions ensures that we
                               2204                 :                :      * don't bloat the lock table if there are many temp tables to be dropped,
                               2205                 :                :      * and it ensures that we don't lose work if a deletion attempt fails.
                               2206                 :                :      */
 3446 tgl@sss.pgh.pa.us        2207   [ -  +  -  -  :             59 :     foreach(cell, orphan_oids)
                                              -  + ]
                               2208                 :                :     {
 3446 tgl@sss.pgh.pa.us        2209                 :UBC           0 :         Oid         relid = lfirst_oid(cell);
                               2210                 :                :         Form_pg_class classForm;
                               2211                 :                :         ObjectAddress object;
                               2212                 :                : 
                               2213                 :                :         /*
                               2214                 :                :          * Check for user-requested abort.
                               2215                 :                :          */
                               2216         [ #  # ]:              0 :         CHECK_FOR_INTERRUPTS();
                               2217                 :                : 
                               2218                 :                :         /*
                               2219                 :                :          * Try to lock the table.  If we can't get the lock immediately,
                               2220                 :                :          * somebody else is using (or dropping) the table, so it's not our
                               2221                 :                :          * concern anymore.  Having the lock prevents race conditions below.
                               2222                 :                :          */
                               2223         [ #  # ]:              0 :         if (!ConditionalLockRelationOid(relid, AccessExclusiveLock))
                               2224                 :              0 :             continue;
                               2225                 :                : 
                               2226                 :                :         /*
                               2227                 :                :          * Re-fetch the pg_class tuple and re-check whether it still seems to
                               2228                 :                :          * be an orphaned temp table.  If it's not there or no longer the same
                               2229                 :                :          * relation, ignore it.
                               2230                 :                :          */
                               2231                 :              0 :         tuple = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
                               2232         [ #  # ]:              0 :         if (!HeapTupleIsValid(tuple))
                               2233                 :                :         {
                               2234                 :                :             /* be sure to drop useless lock so we don't bloat lock table */
                               2235                 :              0 :             UnlockRelationOid(relid, AccessExclusiveLock);
                               2236                 :              0 :             continue;
                               2237                 :                :         }
                               2238                 :              0 :         classForm = (Form_pg_class) GETSTRUCT(tuple);
                               2239                 :                : 
                               2240                 :                :         /*
                               2241                 :                :          * Make all the same tests made in the loop above.  In event of OID
                               2242                 :                :          * counter wraparound, the pg_class entry we have now might be
                               2243                 :                :          * completely unrelated to the one we saw before.
                               2244                 :                :          */
                               2245         [ #  # ]:              0 :         if (!((classForm->relkind == RELKIND_RELATION ||
                               2246         [ #  # ]:              0 :                classForm->relkind == RELKIND_MATVIEW) &&
                               2247         [ #  # ]:              0 :               classForm->relpersistence == RELPERSISTENCE_TEMP))
                               2248                 :                :         {
                               2249                 :              0 :             UnlockRelationOid(relid, AccessExclusiveLock);
                               2250                 :              0 :             continue;
                               2251                 :                :         }
                               2252                 :                : 
 2258                          2253         [ #  # ]:              0 :         if (checkTempNamespaceStatus(classForm->relnamespace) != TEMP_NAMESPACE_IDLE)
                               2254                 :                :         {
 3446                          2255                 :              0 :             UnlockRelationOid(relid, AccessExclusiveLock);
                               2256                 :              0 :             continue;
                               2257                 :                :         }
                               2258                 :                : 
                               2259                 :                :         /*
                               2260                 :                :          * Try to lock the temp namespace, too.  Even though we have lock on
                               2261                 :                :          * the table itself, there's a risk of deadlock against an incoming
                               2262                 :                :          * backend trying to clean out the temp namespace, in case this table
                               2263                 :                :          * has dependencies (such as sequences) that the backend's
                               2264                 :                :          * performDeletion call might visit in a different order.  If we can
                               2265                 :                :          * get AccessShareLock on the namespace, that's sufficient to ensure
                               2266                 :                :          * we're not running concurrently with RemoveTempRelations.  If we
                               2267                 :                :          * can't, back off and let RemoveTempRelations do its thing.
                               2268                 :                :          */
  763                          2269         [ #  # ]:              0 :         if (!ConditionalLockDatabaseObject(NamespaceRelationId,
                               2270                 :                :                                            classForm->relnamespace, 0,
                               2271                 :                :                                            AccessShareLock))
                               2272                 :                :         {
                               2273                 :              0 :             UnlockRelationOid(relid, AccessExclusiveLock);
                               2274                 :              0 :             continue;
                               2275                 :                :         }
                               2276                 :                : 
                               2277                 :                :         /* OK, let's delete it */
 3446                          2278         [ #  # ]:              0 :         ereport(LOG,
                               2279                 :                :                 (errmsg("autovacuum: dropping orphan temp table \"%s.%s.%s\"",
                               2280                 :                :                         get_database_name(MyDatabaseId),
                               2281                 :                :                         get_namespace_name(classForm->relnamespace),
                               2282                 :                :                         NameStr(classForm->relname))));
                               2283                 :                : 
                               2284                 :                :         /*
                               2285                 :                :          * Deletion might involve TOAST table access, so ensure we have a
                               2286                 :                :          * valid snapshot.
                               2287                 :                :          */
  340 nathan@postgresql.or     2288                 :              0 :         PushActiveSnapshot(GetTransactionSnapshot());
                               2289                 :                : 
 3446 tgl@sss.pgh.pa.us        2290                 :              0 :         object.classId = RelationRelationId;
                               2291                 :              0 :         object.objectId = relid;
                               2292                 :              0 :         object.objectSubId = 0;
 3441                          2293                 :              0 :         performDeletion(&object, DROP_CASCADE,
                               2294                 :                :                         PERFORM_DELETION_INTERNAL |
                               2295                 :                :                         PERFORM_DELETION_QUIETLY |
                               2296                 :                :                         PERFORM_DELETION_SKIP_EXTENSIONS);
                               2297                 :                : 
                               2298                 :                :         /*
                               2299                 :                :          * To commit the deletion, end current transaction and start a new
                               2300                 :                :          * one.  Note this also releases the locks we took.
                               2301                 :                :          */
  340 nathan@postgresql.or     2302                 :              0 :         PopActiveSnapshot();
 3452 rhaas@postgresql.org     2303                 :              0 :         CommitTransactionCommand();
                               2304                 :              0 :         StartTransactionCommand();
                               2305                 :                : 
                               2306                 :                :         /* StartTransactionCommand changed current memory context */
                               2307                 :              0 :         MemoryContextSwitchTo(AutovacMemCxt);
                               2308                 :                :     }
                               2309                 :                : 
                               2310                 :                :     /*
                               2311                 :                :      * In case list_sort() would modify the list even when all the scores are
                               2312                 :                :      * 0.0, skip sorting if all the weight parameters are set to 0.0.  This is
                               2313                 :                :      * probably not necessary, but we want to ensure folks have a guaranteed
                               2314                 :                :      * escape hatch from the scoring system.
                               2315                 :                :      */
   39 nathan@postgresql.or     2316         [ -  + ]:GNC          59 :     if (autovacuum_freeze_score_weight != 0.0 ||
   39 nathan@postgresql.or     2317         [ #  # ]:UNC           0 :         autovacuum_multixact_freeze_score_weight != 0.0 ||
                               2318         [ #  # ]:              0 :         autovacuum_vacuum_score_weight != 0.0 ||
                               2319         [ #  # ]:              0 :         autovacuum_vacuum_insert_score_weight != 0.0 ||
                               2320         [ #  # ]:              0 :         autovacuum_analyze_score_weight != 0.0)
   39 nathan@postgresql.or     2321                 :GNC          59 :         list_sort(tables_to_process, TableToProcessComparator);
                               2322                 :                : 
                               2323                 :                :     /*
                               2324                 :                :      * Optionally, create a buffer access strategy object for VACUUM to use.
                               2325                 :                :      * We use the same BufferAccessStrategy object for all tables VACUUMed by
                               2326                 :                :      * this worker to prevent autovacuum from blowing out shared buffers.
                               2327                 :                :      *
                               2328                 :                :      * VacuumBufferUsageLimit being set to 0 results in
                               2329                 :                :      * GetAccessStrategyWithSize returning NULL, effectively meaning we can
                               2330                 :                :      * use up to all of shared buffers.
                               2331                 :                :      *
                               2332                 :                :      * If we later enter failsafe mode on any of the tables being vacuumed, we
                               2333                 :                :      * will cease use of the BufferAccessStrategy only for that table.
                               2334                 :                :      *
                               2335                 :                :      * XXX should we consider adding code to adjust the size of this if
                               2336                 :                :      * VacuumBufferUsageLimit changes?
                               2337                 :                :      */
 1124 drowley@postgresql.o     2338                 :CBC          59 :     bstrategy = GetAccessStrategyWithSize(BAS_VACUUM, VacuumBufferUsageLimit);
                               2339                 :                : 
                               2340                 :                :     /*
                               2341                 :                :      * create a memory context to act as fake PortalContext, so that the
                               2342                 :                :      * contexts created in the vacuum code are cleaned up for each table.
                               2343                 :                :      */
 6884 alvherre@alvh.no-ip.     2344                 :             59 :     PortalContext = AllocSetContextCreate(AutovacMemCxt,
                               2345                 :                :                                           "Autovacuum Portal",
                               2346                 :                :                                           ALLOCSET_DEFAULT_SIZES);
                               2347                 :                : 
                               2348                 :                :     /*
                               2349                 :                :      * Perform operations on collected tables.
                               2350                 :                :      */
   39 nathan@postgresql.or     2351   [ +  +  +  +  :GNC         701 :     foreach_ptr(TableToProcess, table, tables_to_process)
                                              +  + ]
                               2352                 :                :     {
                               2353                 :            585 :         Oid         relid = table->oid;
                               2354                 :                :         HeapTuple   classTup;
                               2355                 :                :         autovac_table *tab;
                               2356                 :                :         bool        isshared;
                               2357                 :                :         bool        skipit;
                               2358                 :                :         dlist_iter  iter;
                               2359                 :                : 
 7585 tgl@sss.pgh.pa.us        2360         [ -  + ]:CBC         585 :         CHECK_FOR_INTERRUPTS();
                               2361                 :                : 
                               2362                 :                :         /*
                               2363                 :                :          * Check for config changes before processing each collected table.
                               2364                 :                :          */
 2331 rhaas@postgresql.org     2365         [ -  + ]:            585 :         if (ConfigReloadPending)
                               2366                 :                :         {
 2331 rhaas@postgresql.org     2367                 :UBC           0 :             ConfigReloadPending = false;
 4050 alvherre@alvh.no-ip.     2368                 :              0 :             ProcessConfigFile(PGC_SIGHUP);
                               2369                 :                : 
                               2370                 :                :             /*
                               2371                 :                :              * You might be tempted to bail out if we see autovacuum is now
                               2372                 :                :              * disabled.  Must resist that temptation -- this might be a
                               2373                 :                :              * for-wraparound emergency worker, in which case that would be
                               2374                 :                :              * entirely inappropriate.
                               2375                 :                :              */
                               2376                 :                :         }
                               2377                 :                : 
                               2378                 :                :         /*
                               2379                 :                :          * Find out whether the table is shared or not.  (It's slightly
                               2380                 :                :          * annoying to fetch the syscache entry just for this, but in typical
                               2381                 :                :          * cases it adds little cost because table_recheck_autovac would
                               2382                 :                :          * refetch the entry anyway.  We could buy that back by copying the
                               2383                 :                :          * tuple here and passing it to table_recheck_autovac, but that
                               2384                 :                :          * increases the odds of that function working with stale data.)
                               2385                 :                :          */
 2975 tgl@sss.pgh.pa.us        2386                 :CBC         585 :         classTup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
                               2387         [ +  + ]:            585 :         if (!HeapTupleIsValid(classTup))
                               2388                 :              1 :             continue;           /* somebody deleted the rel, forget it */
                               2389                 :            584 :         isshared = ((Form_pg_class) GETSTRUCT(classTup))->relisshared;
                               2390                 :            584 :         ReleaseSysCache(classTup);
                               2391                 :                : 
                               2392                 :                :         /*
                               2393                 :                :          * Hold schedule lock from here until we've claimed the table.  We
                               2394                 :                :          * also need the AutovacuumLock to walk the worker array, but that one
                               2395                 :                :          * can just be a shared lock.
                               2396                 :                :          */
 6959 alvherre@alvh.no-ip.     2397                 :            584 :         LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
                               2398                 :            584 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
                               2399                 :                : 
                               2400                 :                :         /*
                               2401                 :                :          * Check whether the table is being vacuumed concurrently by another
                               2402                 :                :          * worker.
                               2403                 :                :          */
                               2404                 :            584 :         skipit = false;
 4949                          2405   [ +  -  +  + ]:           1168 :         dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
                               2406                 :                :         {
                               2407                 :            584 :             WorkerInfo  worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
                               2408                 :                : 
                               2409                 :                :             /* ignore myself */
 6959                          2410         [ +  - ]:            584 :             if (worker == MyWorkerInfo)
 4949                          2411                 :            584 :                 continue;
                               2412                 :                : 
                               2413                 :                :             /* ignore workers in other databases (unless table is shared) */
 3647 alvherre@alvh.no-ip.     2414   [ #  #  #  # ]:UBC           0 :             if (!worker->wi_sharedrel && worker->wi_dboid != MyDatabaseId)
 4949                          2415                 :              0 :                 continue;
                               2416                 :                : 
 6959                          2417         [ #  # ]:              0 :             if (worker->wi_tableoid == relid)
                               2418                 :                :             {
                               2419                 :              0 :                 skipit = true;
 3392 rhaas@postgresql.org     2420                 :              0 :                 found_concurrent_worker = true;
 6959 alvherre@alvh.no-ip.     2421                 :              0 :                 break;
                               2422                 :                :             }
                               2423                 :                :         }
 6959 alvherre@alvh.no-ip.     2424                 :CBC         584 :         LWLockRelease(AutovacuumLock);
                               2425         [ -  + ]:            584 :         if (skipit)
                               2426                 :                :         {
 6959 alvherre@alvh.no-ip.     2427                 :UBC           0 :             LWLockRelease(AutovacuumScheduleLock);
                               2428                 :              0 :             continue;
                               2429                 :                :         }
                               2430                 :                : 
                               2431                 :                :         /*
                               2432                 :                :          * Store the table's OID in shared memory before releasing the
                               2433                 :                :          * schedule lock, so that other workers don't try to vacuum it
                               2434                 :                :          * concurrently.  (We claim it here so as not to hold
                               2435                 :                :          * AutovacuumScheduleLock while rechecking the stats.)
                               2436                 :                :          */
 2975 tgl@sss.pgh.pa.us        2437                 :CBC         584 :         MyWorkerInfo->wi_tableoid = relid;
                               2438                 :            584 :         MyWorkerInfo->wi_sharedrel = isshared;
                               2439                 :            584 :         LWLockRelease(AutovacuumScheduleLock);
                               2440                 :                : 
                               2441                 :                :         /*
                               2442                 :                :          * Check whether pgstat data still says we need to vacuum this table.
                               2443                 :                :          * It could have changed if something else processed the table while
                               2444                 :                :          * we weren't looking. This doesn't entirely close the race condition,
                               2445                 :                :          * but it is very small.
                               2446                 :                :          */
 6884 alvherre@alvh.no-ip.     2447                 :            584 :         MemoryContextSwitchTo(AutovacMemCxt);
 4015 rhaas@postgresql.org     2448                 :            584 :         tab = table_recheck_autovac(relid, table_toast_map, pg_class_desc,
                               2449                 :                :                                     effective_multixact_freeze_max_age);
 6978 alvherre@alvh.no-ip.     2450         [ -  + ]:            584 :         if (tab == NULL)
                               2451                 :                :         {
                               2452                 :                :             /* someone else vacuumed the table, or it went away */
 2975 tgl@sss.pgh.pa.us        2453                 :UBC           0 :             LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
                               2454                 :              0 :             MyWorkerInfo->wi_tableoid = InvalidOid;
                               2455                 :              0 :             MyWorkerInfo->wi_sharedrel = false;
 6959 alvherre@alvh.no-ip.     2456                 :              0 :             LWLockRelease(AutovacuumScheduleLock);
 7568 tgl@sss.pgh.pa.us        2457                 :              0 :             continue;
                               2458                 :                :         }
                               2459                 :                : 
                               2460                 :                :         /*
                               2461                 :                :          * Save the cost-related storage parameter values in global variables
                               2462                 :                :          * for reference when updating vacuum_cost_delay and vacuum_cost_limit
                               2463                 :                :          * during vacuuming this table.
                               2464                 :                :          */
 1124 dgustafsson@postgres     2465                 :CBC         584 :         av_storage_param_cost_delay = tab->at_storage_param_vac_cost_delay;
                               2466                 :            584 :         av_storage_param_cost_limit = tab->at_storage_param_vac_cost_limit;
                               2467                 :                : 
                               2468                 :                :         /*
                               2469                 :                :          * We only expect this worker to ever set the flag, so don't bother
                               2470                 :                :          * checking the return value. We shouldn't have to retry.
                               2471                 :                :          */
                               2472         [ +  - ]:            584 :         if (tab->at_dobalance)
                               2473                 :            584 :             pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
                               2474                 :                :         else
 1124 dgustafsson@postgres     2475                 :UBC           0 :             pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
                               2476                 :                : 
 1124 dgustafsson@postgres     2477                 :CBC         584 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
                               2478                 :            584 :         autovac_recalculate_workers_for_balance();
                               2479                 :            584 :         LWLockRelease(AutovacuumLock);
                               2480                 :                : 
                               2481                 :                :         /*
                               2482                 :                :          * We wait until this point to update cost delay and cost limit
                               2483                 :                :          * values, even though we reloaded the configuration file above, so
                               2484                 :                :          * that we can take into account the cost-related storage parameters.
                               2485                 :                :          */
                               2486                 :            584 :         VacuumUpdateCosts();
                               2487                 :                : 
                               2488                 :                : 
                               2489                 :                :         /* clean up memory before each iteration */
  902 nathan@postgresql.or     2490                 :            584 :         MemoryContextReset(PortalContext);
                               2491                 :                : 
                               2492                 :                :         /*
                               2493                 :                :          * Save the relation name for a possible error message, to avoid a
                               2494                 :                :          * catalog lookup in case of an error.  If any of these return NULL,
                               2495                 :                :          * then the relation has been dropped since last we checked; skip it.
                               2496                 :                :          * Note: they must live in a long-lived memory context because we call
                               2497                 :                :          * vacuum and analyze in different transactions.
                               2498                 :                :          */
                               2499                 :                : 
 6501 alvherre@alvh.no-ip.     2500                 :            584 :         tab->at_relname = get_rel_name(tab->at_relid);
                               2501                 :            584 :         tab->at_nspname = get_namespace_name(get_rel_namespace(tab->at_relid));
                               2502                 :            584 :         tab->at_datname = get_database_name(MyDatabaseId);
                               2503   [ +  -  +  -  :            584 :         if (!tab->at_relname || !tab->at_nspname || !tab->at_datname)
                                              -  + ]
 6501 alvherre@alvh.no-ip.     2504                 :UBC           0 :             goto deleted;
                               2505                 :                : 
                               2506                 :                :         /*
                               2507                 :                :          * We will abort vacuuming the current table if something errors out,
                               2508                 :                :          * and continue with the next one in schedule; in particular, this
                               2509                 :                :          * happens if we are interrupted with SIGINT.
                               2510                 :                :          */
 6885 alvherre@alvh.no-ip.     2511         [ +  - ]:CBC         584 :         PG_TRY();
                               2512                 :                :         {
                               2513                 :                :             /* Use PortalContext for any per-table allocations */
 3146 tgl@sss.pgh.pa.us        2514                 :            584 :             MemoryContextSwitchTo(PortalContext);
                               2515                 :                : 
                               2516                 :                :             /* have at it */
 6501 alvherre@alvh.no-ip.     2517                 :            584 :             autovacuum_do_vac_analyze(tab, bstrategy);
                               2518                 :                : 
                               2519                 :                :             /*
                               2520                 :                :              * Clear a possible query-cancel signal, to avoid a late reaction
                               2521                 :                :              * to an automatically-sent signal because of vacuuming the
                               2522                 :                :              * current table (we're done with it, so it would make no sense to
                               2523                 :                :              * cancel at this point.)
                               2524                 :                :              */
 6766                          2525                 :            583 :             QueryCancelPending = false;
                               2526                 :                :         }
 6885 alvherre@alvh.no-ip.     2527                 :UBC           0 :         PG_CATCH();
                               2528                 :                :         {
                               2529                 :                :             /*
                               2530                 :                :              * Abort the transaction, start a new one, and proceed with the
                               2531                 :                :              * next table in our list.
                               2532                 :                :              */
 6768                          2533                 :              0 :             HOLD_INTERRUPTS();
 2605 rhaas@postgresql.org     2534         [ #  # ]:              0 :             if (tab->at_params.options & VACOPT_VACUUM)
 6768 alvherre@alvh.no-ip.     2535                 :              0 :                 errcontext("automatic vacuum of table \"%s.%s.%s\"",
                               2536                 :                :                            tab->at_datname, tab->at_nspname, tab->at_relname);
                               2537                 :                :             else
                               2538                 :              0 :                 errcontext("automatic analyze of table \"%s.%s.%s\"",
                               2539                 :                :                            tab->at_datname, tab->at_nspname, tab->at_relname);
                               2540                 :              0 :             EmitErrorReport();
                               2541                 :                : 
                               2542                 :                :             /* this resets ProcGlobal->statusFlags[i] too */
                               2543                 :              0 :             AbortOutOfAnyTransaction();
                               2544                 :              0 :             FlushErrorState();
  902 nathan@postgresql.or     2545                 :              0 :             MemoryContextReset(PortalContext);
                               2546                 :                : 
                               2547                 :                :             /* restart our transaction for the following operations */
 6768 alvherre@alvh.no-ip.     2548                 :              0 :             StartTransactionCommand();
                               2549         [ #  # ]:              0 :             RESUME_INTERRUPTS();
                               2550                 :                :         }
 6885 alvherre@alvh.no-ip.     2551         [ -  + ]:CBC         583 :         PG_END_TRY();
                               2552                 :                : 
                               2553                 :                :         /* Make sure we're back in AutovacMemCxt */
 3146 tgl@sss.pgh.pa.us        2554                 :            583 :         MemoryContextSwitchTo(AutovacMemCxt);
                               2555                 :                : 
 3392 rhaas@postgresql.org     2556                 :            583 :         did_vacuum = true;
                               2557                 :                : 
                               2558                 :                :         /* ProcGlobal->statusFlags[i] are reset at the next end of xact */
                               2559                 :                : 
                               2560                 :                :         /* be tidy */
 6501 alvherre@alvh.no-ip.     2561                 :            583 : deleted:
                               2562         [ +  - ]:            583 :         if (tab->at_datname != NULL)
                               2563                 :            583 :             pfree(tab->at_datname);
                               2564         [ +  - ]:            583 :         if (tab->at_nspname != NULL)
                               2565                 :            583 :             pfree(tab->at_nspname);
                               2566         [ +  - ]:            583 :         if (tab->at_relname != NULL)
                               2567                 :            583 :             pfree(tab->at_relname);
 6978                          2568                 :            583 :         pfree(tab);
                               2569                 :                : 
                               2570                 :                :         /*
                               2571                 :                :          * Remove my info from shared memory.  We set wi_dobalance on the
                               2572                 :                :          * assumption that we are more likely than not to vacuum a table with
                               2573                 :                :          * no cost-related storage parameters next, so we want to claim our
                               2574                 :                :          * share of I/O as soon as possible to avoid thrashing the global
                               2575                 :                :          * balance.
                               2576                 :                :          */
 2975 tgl@sss.pgh.pa.us        2577                 :            583 :         LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
 6768 alvherre@alvh.no-ip.     2578                 :            583 :         MyWorkerInfo->wi_tableoid = InvalidOid;
 3647                          2579                 :            583 :         MyWorkerInfo->wi_sharedrel = false;
 2975 tgl@sss.pgh.pa.us        2580                 :            583 :         LWLockRelease(AutovacuumScheduleLock);
 1124 dgustafsson@postgres     2581                 :            583 :         pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
                               2582                 :                :     }
                               2583                 :                : 
   39 nathan@postgresql.or     2584                 :GNC          58 :     list_free_deep(tables_to_process);
                               2585                 :                : 
                               2586                 :                :     /*
                               2587                 :                :      * Perform additional work items, as requested by backends.
                               2588                 :                :      */
 3185 alvherre@alvh.no-ip.     2589                 :CBC          58 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
                               2590         [ +  + ]:          14906 :     for (i = 0; i < NUM_WORKITEMS; i++)
                               2591                 :                :     {
                               2592                 :          14848 :         AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
                               2593                 :                : 
                               2594         [ +  + ]:          14848 :         if (!workitem->avw_used)
                               2595                 :          14842 :             continue;
                               2596         [ -  + ]:              6 :         if (workitem->avw_active)
 3185 alvherre@alvh.no-ip.     2597                 :UBC           0 :             continue;
 3109 alvherre@alvh.no-ip.     2598         [ -  + ]:CBC           6 :         if (workitem->avw_database != MyDatabaseId)
 3109 alvherre@alvh.no-ip.     2599                 :UBC           0 :             continue;
                               2600                 :                : 
                               2601                 :                :         /* claim this one, and release lock while performing it */
 3185 alvherre@alvh.no-ip.     2602                 :CBC           6 :         workitem->avw_active = true;
                               2603                 :              6 :         LWLockRelease(AutovacuumLock);
                               2604                 :                : 
  182 alvherre@kurilemu.de     2605                 :              6 :         PushActiveSnapshot(GetTransactionSnapshot());
 3185 alvherre@alvh.no-ip.     2606                 :              6 :         perform_work_item(workitem);
  182 alvherre@kurilemu.de     2607         [ +  - ]:              6 :         if (ActiveSnapshotSet())    /* transaction could have aborted */
                               2608                 :              6 :             PopActiveSnapshot();
                               2609                 :                : 
                               2610                 :                :         /*
                               2611                 :                :          * Check for config changes before acquiring lock for further jobs.
                               2612                 :                :          */
 3185 alvherre@alvh.no-ip.     2613         [ -  + ]:              6 :         CHECK_FOR_INTERRUPTS();
 2331 rhaas@postgresql.org     2614         [ -  + ]:              6 :         if (ConfigReloadPending)
                               2615                 :                :         {
 2331 rhaas@postgresql.org     2616                 :UBC           0 :             ConfigReloadPending = false;
 3185 alvherre@alvh.no-ip.     2617                 :              0 :             ProcessConfigFile(PGC_SIGHUP);
 1124 dgustafsson@postgres     2618                 :              0 :             VacuumUpdateCosts();
                               2619                 :                :         }
                               2620                 :                : 
 3185 alvherre@alvh.no-ip.     2621                 :CBC           6 :         LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
                               2622                 :                : 
                               2623                 :                :         /* and mark it done */
                               2624                 :              6 :         workitem->avw_active = false;
                               2625                 :              6 :         workitem->avw_used = false;
                               2626                 :                :     }
                               2627                 :             58 :     LWLockRelease(AutovacuumLock);
                               2628                 :                : 
                               2629                 :                :     /*
                               2630                 :                :      * We leak table_toast_map here (among other things), but since we're
                               2631                 :                :      * going away soon, it's not a problem normally.  But when using Valgrind,
                               2632                 :                :      * release some stuff to reduce complaints about leaked storage.
                               2633                 :                :      */
                               2634                 :                : #ifdef USE_VALGRIND
                               2635                 :                :     hash_destroy(table_toast_map);
                               2636                 :                :     FreeTupleDesc(pg_class_desc);
                               2637                 :                :     if (bstrategy)
                               2638                 :                :         pfree(bstrategy);
                               2639                 :                : #endif
                               2640                 :                : 
                               2641                 :                :     /* Run the rest in xact context, mainly to avoid Valgrind leak warnings */
  276 tgl@sss.pgh.pa.us        2642                 :GNC          58 :     MemoryContextSwitchTo(TopTransactionContext);
                               2643                 :                : 
                               2644                 :                :     /*
                               2645                 :                :      * Update pg_database.datfrozenxid, and truncate pg_xact if possible. We
                               2646                 :                :      * only need to do this once, not after each table.
                               2647                 :                :      *
                               2648                 :                :      * Even if we didn't vacuum anything, it may still be important to do
                               2649                 :                :      * this, because one indirect effect of vac_update_datfrozenxid() is to
                               2650                 :                :      * update TransamVariables->xidVacLimit.  That might need to be done even
                               2651                 :                :      * if we haven't vacuumed anything, because relations with older
                               2652                 :                :      * relfrozenxid values or other databases with older datfrozenxid values
                               2653                 :                :      * might have been dropped, allowing xidVacLimit to advance.
                               2654                 :                :      *
                               2655                 :                :      * However, it's also important not to do this blindly in all cases,
                               2656                 :                :      * because when autovacuum=off this will restart the autovacuum launcher.
                               2657                 :                :      * If we're not careful, an infinite loop can result, where workers find
                               2658                 :                :      * no work to do and restart the launcher, which starts another worker in
                               2659                 :                :      * the same database that finds no work to do.  To prevent that, we skip
                               2660                 :                :      * this if (1) we found no work to do and (2) we skipped at least one
                               2661                 :                :      * table due to concurrent autovacuum activity.  In that case, the other
                               2662                 :                :      * worker has already done it, or will do so when it finishes.
                               2663                 :                :      */
 3392 rhaas@postgresql.org     2664   [ +  +  +  - ]:CBC          58 :     if (did_vacuum || !found_concurrent_worker)
                               2665                 :             58 :         vac_update_datfrozenxid();
                               2666                 :                : 
                               2667                 :                :     /* Finally close out the last transaction. */
 7600 tgl@sss.pgh.pa.us        2668                 :             58 :     CommitTransactionCommand();
                               2669                 :             58 : }
                               2670                 :                : 
                               2671                 :                : /*
                               2672                 :                :  * Execute a previously registered work item.
                               2673                 :                :  */
                               2674                 :                : static void
 3321 alvherre@alvh.no-ip.     2675                 :              6 : perform_work_item(AutoVacuumWorkItem *workitem)
                               2676                 :                : {
                               2677                 :              6 :     char       *cur_datname = NULL;
                               2678                 :              6 :     char       *cur_nspname = NULL;
                               2679                 :              6 :     char       *cur_relname = NULL;
                               2680                 :                : 
                               2681                 :                :     /*
                               2682                 :                :      * Note we do not store table info in MyWorkerInfo, since this is not
                               2683                 :                :      * vacuuming proper.
                               2684                 :                :      */
                               2685                 :                : 
                               2686                 :                :     /*
                               2687                 :                :      * Save the relation name for a possible error message, to avoid a catalog
                               2688                 :                :      * lookup in case of an error.  If any of these return NULL, then the
                               2689                 :                :      * relation has been dropped since last we checked; skip it.
                               2690                 :                :      */
 3146 tgl@sss.pgh.pa.us        2691         [ -  + ]:              6 :     Assert(CurrentMemoryContext == AutovacMemCxt);
                               2692                 :                : 
 3321 alvherre@alvh.no-ip.     2693                 :              6 :     cur_relname = get_rel_name(workitem->avw_relation);
                               2694                 :              6 :     cur_nspname = get_namespace_name(get_rel_namespace(workitem->avw_relation));
                               2695                 :              6 :     cur_datname = get_database_name(MyDatabaseId);
                               2696   [ +  -  +  -  :              6 :     if (!cur_relname || !cur_nspname || !cur_datname)
                                              -  + ]
 3321 alvherre@alvh.no-ip.     2697                 :UBC           0 :         goto deleted2;
                               2698                 :                : 
 2629 alvherre@alvh.no-ip.     2699                 :CBC           6 :     autovac_report_workitem(workitem, cur_nspname, cur_relname);
                               2700                 :                : 
                               2701                 :                :     /* clean up memory before each work item */
  902 nathan@postgresql.or     2702                 :              6 :     MemoryContextReset(PortalContext);
                               2703                 :                : 
                               2704                 :                :     /*
                               2705                 :                :      * We will abort the current work item if something errors out, and
                               2706                 :                :      * continue with the next one; in particular, this happens if we are
                               2707                 :                :      * interrupted with SIGINT.  Note that this means that the work item list
                               2708                 :                :      * can be lossy.
                               2709                 :                :      */
 3321 alvherre@alvh.no-ip.     2710         [ +  - ]:              6 :     PG_TRY();
                               2711                 :                :     {
                               2712                 :                :         /* Use PortalContext for any per-work-item allocations */
 3146 tgl@sss.pgh.pa.us        2713                 :              6 :         MemoryContextSwitchTo(PortalContext);
                               2714                 :                : 
                               2715                 :                :         /*
                               2716                 :                :          * Have at it.  Functions called here are responsible for any required
                               2717                 :                :          * user switch and sandbox.
                               2718                 :                :          */
 3321 alvherre@alvh.no-ip.     2719         [ +  - ]:              6 :         switch (workitem->avw_type)
                               2720                 :                :         {
                               2721                 :              6 :             case AVW_BRINSummarizeRange:
                               2722                 :              6 :                 DirectFunctionCall2(brin_summarize_range,
                               2723                 :                :                                     ObjectIdGetDatum(workitem->avw_relation),
                               2724                 :                :                                     Int64GetDatum((int64) workitem->avw_blockNumber));
                               2725                 :              6 :                 break;
 3321 alvherre@alvh.no-ip.     2726                 :UBC           0 :             default:
                               2727         [ #  # ]:              0 :                 elog(WARNING, "unrecognized work item found: type %d",
                               2728                 :                :                      workitem->avw_type);
                               2729                 :              0 :                 break;
                               2730                 :                :         }
                               2731                 :                : 
                               2732                 :                :         /*
                               2733                 :                :          * Clear a possible query-cancel signal, to avoid a late reaction to
                               2734                 :                :          * an automatically-sent signal because of vacuuming the current table
                               2735                 :                :          * (we're done with it, so it would make no sense to cancel at this
                               2736                 :                :          * point.)
                               2737                 :                :          */
 3321 alvherre@alvh.no-ip.     2738                 :CBC           6 :         QueryCancelPending = false;
                               2739                 :                :     }
 3321 alvherre@alvh.no-ip.     2740                 :UBC           0 :     PG_CATCH();
                               2741                 :                :     {
                               2742                 :                :         /*
                               2743                 :                :          * Abort the transaction, start a new one, and proceed with the next
                               2744                 :                :          * table in our list.
                               2745                 :                :          */
                               2746                 :              0 :         HOLD_INTERRUPTS();
                               2747                 :              0 :         errcontext("processing work entry for relation \"%s.%s.%s\"",
                               2748                 :                :                    cur_datname, cur_nspname, cur_relname);
                               2749                 :              0 :         EmitErrorReport();
                               2750                 :                : 
                               2751                 :                :         /* this resets ProcGlobal->statusFlags[i] too */
                               2752                 :              0 :         AbortOutOfAnyTransaction();
                               2753                 :              0 :         FlushErrorState();
  902 nathan@postgresql.or     2754                 :              0 :         MemoryContextReset(PortalContext);
                               2755                 :                : 
                               2756                 :                :         /* restart our transaction for the following operations */
 3321 alvherre@alvh.no-ip.     2757                 :              0 :         StartTransactionCommand();
                               2758         [ #  # ]:              0 :         RESUME_INTERRUPTS();
                               2759                 :                :     }
 3321 alvherre@alvh.no-ip.     2760         [ -  + ]:CBC           6 :     PG_END_TRY();
                               2761                 :                : 
                               2762                 :                :     /* Make sure we're back in AutovacMemCxt */
 3146 tgl@sss.pgh.pa.us        2763                 :              6 :     MemoryContextSwitchTo(AutovacMemCxt);
                               2764                 :                : 
                               2765                 :                :     /* We intentionally do not set did_vacuum here */
                               2766                 :                : 
                               2767                 :                :     /* be tidy */
 3321 alvherre@alvh.no-ip.     2768                 :              6 : deleted2:
                               2769         [ +  - ]:              6 :     if (cur_datname)
                               2770                 :              6 :         pfree(cur_datname);
                               2771         [ +  - ]:              6 :     if (cur_nspname)
                               2772                 :              6 :         pfree(cur_nspname);
                               2773         [ +  - ]:              6 :     if (cur_relname)
                               2774                 :              6 :         pfree(cur_relname);
                               2775                 :              6 : }
                               2776                 :                : 
                               2777                 :                : /*
                               2778                 :                :  * extract_autovac_opts
                               2779                 :                :  *
                               2780                 :                :  * Given a relation's pg_class tuple, return a palloc'd copy of the
                               2781                 :                :  * AutoVacOpts portion of reloptions, if set; otherwise, return NULL.
                               2782                 :                :  *
                               2783                 :                :  * Note: callers do not have a relation lock on the table at this point,
                               2784                 :                :  * so the table could have been dropped, and its catalog rows gone, after
                               2785                 :                :  * we acquired the pg_class row.  If pg_class had a TOAST table, this would
                               2786                 :                :  * be a risk; fortunately, it doesn't.
                               2787                 :                :  */
                               2788                 :                : static AutoVacOpts *
 6294                          2789                 :          13740 : extract_autovac_opts(HeapTuple tup, TupleDesc pg_class_desc)
                               2790                 :                : {
                               2791                 :                :     bytea      *relopts;
                               2792                 :                :     AutoVacOpts *av;
                               2793                 :                : 
                               2794   [ +  +  +  +  :          13740 :     Assert(((Form_pg_class) GETSTRUCT(tup))->relkind == RELKIND_RELATION ||
                                              -  + ]
                               2795                 :                :            ((Form_pg_class) GETSTRUCT(tup))->relkind == RELKIND_MATVIEW ||
                               2796                 :                :            ((Form_pg_class) GETSTRUCT(tup))->relkind == RELKIND_TOASTVALUE);
                               2797                 :                : 
  754 akorotkov@postgresql     2798                 :          13740 :     relopts = extractRelOptions(tup, pg_class_desc, NULL);
                               2799         [ +  + ]:          13740 :     if (relopts == NULL)
                               2800                 :          13539 :         return NULL;
                               2801                 :                : 
  146 michael@paquier.xyz      2802                 :GNC         201 :     av = palloc_object(AutoVacOpts);
  754 akorotkov@postgresql     2803                 :CBC         201 :     memcpy(av, &(((StdRdOptions *) relopts)->autovacuum), sizeof(AutoVacOpts));
                               2804                 :            201 :     pfree(relopts);
                               2805                 :                : 
 6294 alvherre@alvh.no-ip.     2806                 :            201 :     return av;
                               2807                 :                : }
                               2808                 :                : 
                               2809                 :                : 
                               2810                 :                : /*
                               2811                 :                :  * table_recheck_autovac
                               2812                 :                :  *
                               2813                 :                :  * Recheck whether a table still needs vacuum or analyze.  Return value is a
                               2814                 :                :  * valid autovac_table pointer if it does, NULL otherwise.
                               2815                 :                :  *
                               2816                 :                :  * Note that the returned autovac_table does not have the name fields set.
                               2817                 :                :  */
                               2818                 :                : static autovac_table *
                               2819                 :            584 : table_recheck_autovac(Oid relid, HTAB *table_toast_map,
                               2820                 :                :                       TupleDesc pg_class_desc,
                               2821                 :                :                       int effective_multixact_freeze_max_age)
                               2822                 :                : {
                               2823                 :                :     Form_pg_class classForm;
                               2824                 :                :     HeapTuple   classTup;
                               2825                 :                :     bool        dovacuum;
                               2826                 :                :     bool        doanalyze;
 6978                          2827                 :            584 :     autovac_table *tab = NULL;
                               2828                 :                :     bool        wraparound;
                               2829                 :                :     AutoVacOpts *avopts;
  347 tgl@sss.pgh.pa.us        2830                 :            584 :     bool        free_avopts = false;
                               2831                 :                :     AutoVacuumScores scores;
                               2832                 :                : 
                               2833                 :                :     /* fetch the relation's relcache entry */
 5924 rhaas@postgresql.org     2834                 :            584 :     classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
 6978 alvherre@alvh.no-ip.     2835         [ -  + ]:            584 :     if (!HeapTupleIsValid(classTup))
 6978 alvherre@alvh.no-ip.     2836                 :UBC           0 :         return NULL;
 6978 alvherre@alvh.no-ip.     2837                 :CBC         584 :     classForm = (Form_pg_class) GETSTRUCT(classTup);
                               2838                 :                : 
                               2839                 :                :     /*
                               2840                 :                :      * Get the applicable reloptions.  If it is a TOAST table, try to get the
                               2841                 :                :      * main table reloptions if the toast table itself doesn't have.
                               2842                 :                :      */
 6294                          2843                 :            584 :     avopts = extract_autovac_opts(classTup, pg_class_desc);
  347 tgl@sss.pgh.pa.us        2844         [ +  + ]:            584 :     if (avopts)
                               2845                 :             20 :         free_avopts = true;
                               2846   [ +  +  +  - ]:            564 :     else if (classForm->relkind == RELKIND_TOASTVALUE &&
                               2847                 :                :              table_toast_map != NULL)
                               2848                 :                :     {
                               2849                 :                :         av_relation *hentry;
                               2850                 :                :         bool        found;
                               2851                 :                : 
 6294 alvherre@alvh.no-ip.     2852                 :              5 :         hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
                               2853   [ +  -  -  + ]:              5 :         if (found && hentry->ar_hasrelopts)
 6294 alvherre@alvh.no-ip.     2854                 :UBC           0 :             avopts = &hentry->ar_reloptions;
                               2855                 :                :     }
                               2856                 :                : 
   29 nathan@postgresql.or     2857                 :GNC         584 :     relation_needs_vacanalyze(relid, avopts, classForm,
                               2858                 :                :                               effective_multixact_freeze_max_age,
                               2859                 :                :                               DEBUG3,
                               2860                 :                :                               &dovacuum, &doanalyze, &wraparound,
                               2861                 :                :                               &scores);
                               2862                 :                : 
                               2863                 :                :     /* OK, it needs something done */
 6474 alvherre@alvh.no-ip.     2864   [ +  +  +  - ]:CBC         584 :     if (doanalyze || dovacuum)
                               2865                 :                :     {
                               2866                 :                :         int         freeze_min_age;
                               2867                 :                :         int         freeze_table_age;
                               2868                 :                :         int         multixact_freeze_min_age;
                               2869                 :                :         int         multixact_freeze_table_age;
                               2870                 :                :         int         log_vacuum_min_duration;
                               2871                 :                :         int         log_analyze_min_duration;
                               2872                 :                : 
                               2873                 :                :         /*
                               2874                 :                :          * Calculate the vacuum cost parameters and the freeze ages.  If there
                               2875                 :                :          * are options set in pg_class.reloptions, use them; in the case of a
                               2876                 :                :          * toast table, try the main table too.  Otherwise use the GUC
                               2877                 :                :          * defaults, autovacuum's own first and plain vacuum second.
                               2878                 :                :          */
                               2879                 :                : 
                               2880                 :                :         /* -1 in autovac setting means use log_autovacuum_min_duration */
  202 peter@eisentraut.org     2881         [ +  + ]:GNC          20 :         log_vacuum_min_duration = (avopts && avopts->log_vacuum_min_duration >= 0)
                               2882                 :                :             ? avopts->log_vacuum_min_duration
 4050 alvherre@alvh.no-ip.     2883         [ +  + ]:CBC         604 :             : Log_autovacuum_min_duration;
                               2884                 :                : 
                               2885                 :                :         /* -1 in autovac setting means use log_autoanalyze_min_duration */
  202 peter@eisentraut.org     2886         [ -  + ]:GNC          20 :         log_analyze_min_duration = (avopts && avopts->log_analyze_min_duration >= 0)
                               2887                 :                :             ? avopts->log_analyze_min_duration
                               2888         [ +  + ]:            604 :             : Log_autoanalyze_min_duration;
                               2889                 :                : 
                               2890                 :                :         /* these do not have autovacuum-specific settings */
 6095 alvherre@alvh.no-ip.     2891         [ -  + ]:CBC          20 :         freeze_min_age = (avopts && avopts->freeze_min_age >= 0)
                               2892                 :                :             ? avopts->freeze_min_age
                               2893         [ +  + ]:            604 :             : default_freeze_min_age;
                               2894                 :                : 
                               2895         [ -  + ]:             20 :         freeze_table_age = (avopts && avopts->freeze_table_age >= 0)
                               2896                 :                :             ? avopts->freeze_table_age
                               2897         [ +  + ]:            604 :             : default_freeze_table_age;
                               2898                 :                : 
 4464                          2899                 :            604 :         multixact_freeze_min_age = (avopts &&
                               2900         [ -  + ]:             20 :                                     avopts->multixact_freeze_min_age >= 0)
                               2901                 :                :             ? avopts->multixact_freeze_min_age
                               2902         [ +  + ]:            604 :             : default_multixact_freeze_min_age;
                               2903                 :                : 
                               2904                 :            604 :         multixact_freeze_table_age = (avopts &&
                               2905         [ -  + ]:             20 :                                       avopts->multixact_freeze_table_age >= 0)
                               2906                 :                :             ? avopts->multixact_freeze_table_age
                               2907         [ +  + ]:            604 :             : default_multixact_freeze_table_age;
                               2908                 :                : 
  146 michael@paquier.xyz      2909                 :GNC         584 :         tab = palloc_object(autovac_table);
 6978 alvherre@alvh.no-ip.     2910                 :CBC         584 :         tab->at_relid = relid;
                               2911                 :                : 
                               2912                 :                :         /*
                               2913                 :                :          * Select VACUUM options.  Note we don't say VACOPT_PROCESS_TOAST, so
                               2914                 :                :          * that vacuum() skips toast relations.  Also note we tell vacuum() to
                               2915                 :                :          * skip vac_update_datfrozenxid(); we'll do that separately.
                               2916                 :                :          */
 1215 tgl@sss.pgh.pa.us        2917                 :            584 :         tab->at_params.options =
 1156 michael@paquier.xyz      2918                 :            584 :             (dovacuum ? (VACOPT_VACUUM |
                               2919                 :                :                          VACOPT_PROCESS_MAIN |
                               2920         [ +  + ]:            584 :                          VACOPT_SKIP_DATABASE_STATS) : 0) |
 4066 alvherre@alvh.no-ip.     2921         [ +  + ]:            584 :             (doanalyze ? VACOPT_ANALYZE : 0) |
 2854 michael@paquier.xyz      2922         [ +  - ]:            584 :             (!wraparound ? VACOPT_SKIP_LOCKED : 0);
                               2923                 :                : 
                               2924                 :                :         /*
                               2925                 :                :          * index_cleanup and truncate are unspecified at first in autovacuum.
                               2926                 :                :          * They will be filled in with usable values using their reloptions
                               2927                 :                :          * (or reloption defaults) later.
                               2928                 :                :          */
 1782 pg@bowt.ie               2929                 :            584 :         tab->at_params.index_cleanup = VACOPTVALUE_UNSPECIFIED;
                               2930                 :            584 :         tab->at_params.truncate = VACOPTVALUE_UNSPECIFIED;
 4066 alvherre@alvh.no-ip.     2931                 :            584 :         tab->at_params.freeze_min_age = freeze_min_age;
                               2932                 :            584 :         tab->at_params.freeze_table_age = freeze_table_age;
                               2933                 :            584 :         tab->at_params.multixact_freeze_min_age = multixact_freeze_min_age;
                               2934                 :            584 :         tab->at_params.multixact_freeze_table_age = multixact_freeze_table_age;
                               2935                 :            584 :         tab->at_params.is_wraparound = wraparound;
  202 peter@eisentraut.org     2936                 :GNC         584 :         tab->at_params.log_vacuum_min_duration = log_vacuum_min_duration;
                               2937                 :            584 :         tab->at_params.log_analyze_min_duration = log_analyze_min_duration;
  783 nathan@postgresql.or     2938                 :CBC         584 :         tab->at_params.toast_parent = InvalidOid;
                               2939                 :                : 
                               2940                 :                :         /* Determine the number of parallel vacuum workers to use */
   29 msawada@postgresql.o     2941                 :GNC         584 :         tab->at_params.nworkers = 0;
                               2942         [ +  + ]:            584 :         if (avopts)
                               2943                 :                :         {
                               2944         [ -  + ]:             20 :             if (avopts->autovacuum_parallel_workers == 0)
                               2945                 :                :             {
                               2946                 :                :                 /*
                               2947                 :                :                  * Disable parallel vacuum, if the reloption sets the parallel
                               2948                 :                :                  * degree as zero.
                               2949                 :                :                  */
   29 msawada@postgresql.o     2950                 :UNC           0 :                 tab->at_params.nworkers = -1;
                               2951                 :                :             }
   29 msawada@postgresql.o     2952         [ +  + ]:GNC          20 :             else if (avopts->autovacuum_parallel_workers > 0)
                               2953                 :              2 :                 tab->at_params.nworkers = avopts->autovacuum_parallel_workers;
                               2954                 :                : 
                               2955                 :                :             /*
                               2956                 :                :              * autovacuum_parallel_workers == -1 falls through, keep
                               2957                 :                :              * nworkers=0
                               2958                 :                :              */
                               2959                 :                :         }
                               2960                 :                : 
                               2961                 :                :         /*
                               2962                 :                :          * Later, in vacuum_rel(), we check reloptions for any
                               2963                 :                :          * vacuum_max_eager_freeze_failure_rate override.
                               2964                 :                :          */
  448 melanieplageman@gmai     2965                 :CBC         584 :         tab->at_params.max_eager_freeze_failure_rate = vacuum_max_eager_freeze_failure_rate;
 1124 dgustafsson@postgres     2966                 :            584 :         tab->at_storage_param_vac_cost_limit = avopts ?
                               2967         [ +  + ]:            584 :             avopts->vacuum_cost_limit : 0;
                               2968                 :            584 :         tab->at_storage_param_vac_cost_delay = avopts ?
                               2969         [ +  + ]:            584 :             avopts->vacuum_cost_delay : -1;
 6501 alvherre@alvh.no-ip.     2970                 :            584 :         tab->at_relname = NULL;
                               2971                 :            584 :         tab->at_nspname = NULL;
                               2972                 :            584 :         tab->at_datname = NULL;
                               2973                 :                : 
                               2974                 :                :         /*
                               2975                 :                :          * If any of the cost delay parameters has been set individually for
                               2976                 :                :          * this table, disable the balancing algorithm.
                               2977                 :                :          */
 4232                          2978                 :            584 :         tab->at_dobalance =
                               2979   [ +  +  +  - ]:            604 :             !(avopts && (avopts->vacuum_cost_limit > 0 ||
 1106 dgustafsson@postgres     2980         [ +  - ]:             20 :                          avopts->vacuum_cost_delay >= 0));
                               2981                 :                :     }
                               2982                 :                : 
  347 tgl@sss.pgh.pa.us        2983         [ +  + ]:            584 :     if (free_avopts)
                               2984                 :             20 :         pfree(avopts);
 6978 alvherre@alvh.no-ip.     2985                 :            584 :     heap_freetuple(classTup);
                               2986                 :            584 :     return tab;
                               2987                 :                : }
                               2988                 :                : 
                               2989                 :                : /*
                               2990                 :                :  * relation_needs_vacanalyze
                               2991                 :                :  *
                               2992                 :                :  * Check whether a relation needs to be vacuumed or analyzed; return each into
                               2993                 :                :  * "dovacuum" and "doanalyze", respectively.  Also return whether the vacuum is
                               2994                 :                :  * being forced because of Xid or multixact wraparound.
                               2995                 :                :  *
                               2996                 :                :  * relopts is a pointer to the AutoVacOpts options (either for itself in the
                               2997                 :                :  * case of a plain table, or for either itself or its parent table in the case
                               2998                 :                :  * of a TOAST table), NULL if none.
                               2999                 :                :  *
                               3000                 :                :  * A table needs to be vacuumed if the number of dead tuples exceeds a
                               3001                 :                :  * threshold.  This threshold is calculated as
                               3002                 :                :  *
                               3003                 :                :  * threshold = vac_base_thresh + vac_scale_factor * reltuples
                               3004                 :                :  * if (threshold > vac_max_thresh)
                               3005                 :                :  *     threshold = vac_max_thresh;
                               3006                 :                :  *
                               3007                 :                :  * For analyze, the analysis done is that the number of tuples inserted,
                               3008                 :                :  * deleted and updated since the last analyze exceeds a threshold calculated
                               3009                 :                :  * in the same fashion as above.  Note that the cumulative stats system stores
                               3010                 :                :  * the number of tuples (both live and dead) that there were as of the last
                               3011                 :                :  * analyze.  This is asymmetric to the VACUUM case.
                               3012                 :                :  *
                               3013                 :                :  * We also force vacuum if the table's relfrozenxid is more than freeze_max_age
                               3014                 :                :  * transactions back, and if its relminmxid is more than
                               3015                 :                :  * multixact_freeze_max_age multixacts back.
                               3016                 :                :  *
                               3017                 :                :  * A table whose autovacuum_enabled option is false is
                               3018                 :                :  * automatically skipped (unless we have to vacuum it due to freeze_max_age).
                               3019                 :                :  * Thus autovacuum can be disabled for specific tables. Also, when the cumulative
                               3020                 :                :  * stats system does not have data about a table, it will be skipped.
                               3021                 :                :  *
                               3022                 :                :  * A table whose vac_base_thresh value is < 0 takes the base value from the
                               3023                 :                :  * autovacuum_vacuum_threshold GUC variable.  Similarly, a vac_scale_factor
                               3024                 :                :  * value < 0 is substituted with the value of
                               3025                 :                :  * autovacuum_vacuum_scale_factor GUC variable.  Ditto for analyze.
                               3026                 :                :  *
                               3027                 :                :  * This function also returns scores that can be used to sort the list of
                               3028                 :                :  * tables to process.  The idea is to have autovacuum prioritize tables that
                               3029                 :                :  * are furthest beyond their thresholds (e.g., a table nearing transaction ID
                               3030                 :                :  * wraparound should be vacuumed first).  This prioritization scheme is
                               3031                 :                :  * certainly far from perfect; there are simply too many possibilities for any
                               3032                 :                :  * scoring technique to work across all workloads, and the situation might
                               3033                 :                :  * change significantly between the time we calculate the score and the time
                               3034                 :                :  * that autovacuum processes it.  However, we have attempted to develop
                               3035                 :                :  * something that is expected to work for a large portion of workloads with
                               3036                 :                :  * reasonable parameter settings.
                               3037                 :                :  *
                               3038                 :                :  * The autovacuum table score is calculated as the maximum of the ratios of
                               3039                 :                :  * each of the table's relevant values to its threshold.  For example, if the
                               3040                 :                :  * number of inserted tuples is 100, and the insert threshold for the table is
                               3041                 :                :  * 80, the insert score is 1.25.  If all other scores are below that value, the
                               3042                 :                :  * returned score will be 1.25.  The other criteria considered for the score
                               3043                 :                :  * are the table ages (both relfrozenxid and relminmxid) compared to the
                               3044                 :                :  * corresponding freeze-max-age setting, the number of updated/deleted tuples
                               3045                 :                :  * compared to the vacuum threshold, and the number of inserted/updated/deleted
                               3046                 :                :  * tuples compared to the analyze threshold.
                               3047                 :                :  *
                               3048                 :                :  * One exception to the previous paragraph is for tables nearing wraparound,
                               3049                 :                :  * i.e., those that have surpassed the effective failsafe ages.  In that case,
                               3050                 :                :  * the relfrozenxid/relminmxid-based score is scaled aggressively so that the
                               3051                 :                :  * table has a decent chance of sorting to the front of the list.
                               3052                 :                :  *
                               3053                 :                :  * To adjust how strongly each component contributes to the score, the
                               3054                 :                :  * following parameters can be adjusted from their default of 1.0 to anywhere
                               3055                 :                :  * between 0.0 and 10.0 (inclusive).  Setting all of these to 0.0 restores
                               3056                 :                :  * pre-v19 prioritization behavior:
                               3057                 :                :  *
                               3058                 :                :  *     autovacuum_freeze_score_weight
                               3059                 :                :  *     autovacuum_multixact_freeze_score_weight
                               3060                 :                :  *     autovacuum_vacuum_score_weight
                               3061                 :                :  *     autovacuum_vacuum_insert_score_weight
                               3062                 :                :  *     autovacuum_analyze_score_weight
                               3063                 :                :  *
                               3064                 :                :  * The autovacuum table score is returned in scores->max.  The component scores
                               3065                 :                :  * are also returned in the "scores" argument via the other members of the
                               3066                 :                :  * AutoVacuumScores struct.
                               3067                 :                :  */
                               3068                 :                : static void
                               3069                 :          13740 : relation_needs_vacanalyze(Oid relid,
                               3070                 :                :                           AutoVacOpts *relopts,
                               3071                 :                :                           Form_pg_class classForm,
                               3072                 :                :                           int effective_multixact_freeze_max_age,
                               3073                 :                :                           int elevel,
                               3074                 :                :  /* output params below */
                               3075                 :                :                           bool *dovacuum,
                               3076                 :                :                           bool *doanalyze,
                               3077                 :                :                           bool *wraparound,
                               3078                 :                :                           AutoVacuumScores *scores)
                               3079                 :                : {
                               3080                 :                :     PgStat_StatTabEntry *tabentry;
                               3081                 :                :     bool        force_vacuum;
                               3082                 :                :     bool        av_enabled;
   26 nathan@postgresql.or     3083                 :GNC       13740 :     bool        may_free = false;
                               3084                 :                : 
                               3085                 :                :     /* constants from reloptions or GUC variables */
                               3086                 :                :     int         vac_base_thresh,
                               3087                 :                :                 vac_max_thresh,
                               3088                 :                :                 vac_ins_base_thresh,
                               3089                 :                :                 anl_base_thresh;
                               3090                 :                :     float4      vac_scale_factor,
                               3091                 :                :                 vac_ins_scale_factor,
                               3092                 :                :                 anl_scale_factor;
                               3093                 :                : 
                               3094                 :                :     /* thresholds calculated from above constants */
                               3095                 :                :     float4      vacthresh,
                               3096                 :                :                 vacinsthresh,
                               3097                 :                :                 anlthresh;
                               3098                 :                : 
                               3099                 :                :     /* number of vacuum (resp. analyze) tuples at this time */
                               3100                 :                :     float4      vactuples,
                               3101                 :                :                 instuples,
                               3102                 :                :                 anltuples;
                               3103                 :                : 
                               3104                 :                :     /* freeze parameters */
                               3105                 :                :     int         freeze_max_age;
                               3106                 :                :     int         multixact_freeze_max_age;
                               3107                 :                :     TransactionId xidForceLimit;
                               3108                 :                :     TransactionId relfrozenxid;
                               3109                 :                :     MultiXactId relminmxid;
                               3110                 :                :     MultiXactId multiForceLimit;
                               3111                 :                :     uint32      xid_age;
                               3112                 :                :     uint32      mxid_age;
                               3113                 :                :     int         effective_xid_failsafe_age;
                               3114                 :                :     int         effective_mxid_failsafe_age;
                               3115                 :                : 
   32                          3116                 :          13740 :     float4      pcnt_unfrozen = 1;
                               3117                 :          13740 :     float4      reltuples = classForm->reltuples;
                               3118                 :          13740 :     int32       relpages = classForm->relpages;
                               3119                 :          13740 :     int32       relallfrozen = classForm->relallfrozen;
                               3120                 :                : 
 1285 peter@eisentraut.org     3121         [ -  + ]:CBC       13740 :     Assert(classForm != NULL);
                               3122         [ -  + ]:          13740 :     Assert(OidIsValid(relid));
                               3123                 :                : 
   39 nathan@postgresql.or     3124                 :GNC       13740 :     memset(scores, 0, sizeof(AutoVacuumScores));
                               3125                 :          13740 :     *dovacuum = false;
                               3126                 :          13740 :     *doanalyze = false;
                               3127                 :                : 
                               3128                 :                :     /*
                               3129                 :                :      * Determine vacuum/analyze equation parameters.  We have two possible
                               3130                 :                :      * sources: the passed reloptions (which could be a main table or a toast
                               3131                 :                :      * table), or the autovacuum GUC variables.
                               3132                 :                :      */
                               3133                 :                : 
                               3134                 :                :     /* -1 in autovac setting means use plain vacuum_scale_factor */
 6095 alvherre@alvh.no-ip.     3135         [ -  + ]:CBC         287 :     vac_scale_factor = (relopts && relopts->vacuum_scale_factor >= 0)
 6095 alvherre@alvh.no-ip.     3136                 :UBC           0 :         ? relopts->vacuum_scale_factor
 6095 alvherre@alvh.no-ip.     3137         [ +  + ]:CBC       14027 :         : autovacuum_vac_scale;
                               3138                 :                : 
                               3139         [ -  + ]:            287 :     vac_base_thresh = (relopts && relopts->vacuum_threshold >= 0)
                               3140                 :                :         ? relopts->vacuum_threshold
                               3141         [ +  + ]:          14027 :         : autovacuum_vac_thresh;
                               3142                 :                : 
                               3143                 :                :     /* -1 is used to disable max threshold */
  454 nathan@postgresql.or     3144         [ -  + ]:            287 :     vac_max_thresh = (relopts && relopts->vacuum_max_threshold >= -1)
                               3145                 :                :         ? relopts->vacuum_max_threshold
                               3146         [ +  + ]:          14027 :         : autovacuum_vac_max_thresh;
                               3147                 :                : 
 2229 drowley@postgresql.o     3148         [ -  + ]:            287 :     vac_ins_scale_factor = (relopts && relopts->vacuum_ins_scale_factor >= 0)
 2229 drowley@postgresql.o     3149                 :UBC           0 :         ? relopts->vacuum_ins_scale_factor
 2229 drowley@postgresql.o     3150         [ +  + ]:CBC       14027 :         : autovacuum_vac_ins_scale;
                               3151                 :                : 
                               3152                 :                :     /* -1 is used to disable insert vacuums */
                               3153         [ -  + ]:            287 :     vac_ins_base_thresh = (relopts && relopts->vacuum_ins_threshold >= -1)
                               3154                 :                :         ? relopts->vacuum_ins_threshold
                               3155         [ +  + ]:          14027 :         : autovacuum_vac_ins_thresh;
                               3156                 :                : 
 6095 alvherre@alvh.no-ip.     3157         [ -  + ]:            287 :     anl_scale_factor = (relopts && relopts->analyze_scale_factor >= 0)
 6095 alvherre@alvh.no-ip.     3158                 :UBC           0 :         ? relopts->analyze_scale_factor
 6095 alvherre@alvh.no-ip.     3159         [ +  + ]:CBC       14027 :         : autovacuum_anl_scale;
                               3160                 :                : 
                               3161         [ -  + ]:            287 :     anl_base_thresh = (relopts && relopts->analyze_threshold >= 0)
                               3162                 :                :         ? relopts->analyze_threshold
                               3163         [ +  + ]:          14027 :         : autovacuum_anl_thresh;
                               3164                 :                : 
                               3165         [ -  + ]:            287 :     freeze_max_age = (relopts && relopts->freeze_max_age >= 0)
 6095 alvherre@alvh.no-ip.     3166                 :UBC           0 :         ? Min(relopts->freeze_max_age, autovacuum_freeze_max_age)
 6095 alvherre@alvh.no-ip.     3167         [ +  + ]:CBC       14027 :         : autovacuum_freeze_max_age;
                               3168                 :                : 
 4464                          3169         [ -  + ]:            287 :     multixact_freeze_max_age = (relopts && relopts->multixact_freeze_max_age >= 0)
 4015 rhaas@postgresql.org     3170                 :UBC           0 :         ? Min(relopts->multixact_freeze_max_age, effective_multixact_freeze_max_age)
 4015 rhaas@postgresql.org     3171         [ +  + ]:CBC       14027 :         : effective_multixact_freeze_max_age;
                               3172                 :                : 
 6095 alvherre@alvh.no-ip.     3173   [ +  +  +  + ]:          13740 :     av_enabled = (relopts ? relopts->enabled : true);
   32 nathan@postgresql.or     3174                 :GNC       13740 :     av_enabled &= AutoVacuumingActive();
                               3175                 :                : 
   39                          3176                 :          13740 :     relfrozenxid = classForm->relfrozenxid;
                               3177                 :          13740 :     relminmxid = classForm->relminmxid;
                               3178                 :                : 
                               3179                 :                :     /* Force vacuum if table is at risk of wraparound */
 7121 tgl@sss.pgh.pa.us        3180                 :CBC       13740 :     xidForceLimit = recentXid - freeze_max_age;
                               3181         [ -  + ]:          13740 :     if (xidForceLimit < FirstNormalTransactionId)
 7121 tgl@sss.pgh.pa.us        3182                 :UBC           0 :         xidForceLimit -= FirstNormalTransactionId;
  736 noah@leadboat.com        3183   [ +  -  -  + ]:CBC       27480 :     force_vacuum = (TransactionIdIsNormal(relfrozenxid) &&
                               3184                 :          13740 :                     TransactionIdPrecedes(relfrozenxid, xidForceLimit));
 4850 alvherre@alvh.no-ip.     3185         [ +  - ]:          13740 :     if (!force_vacuum)
                               3186                 :                :     {
 4464                          3187                 :          13740 :         multiForceLimit = recentMulti - multixact_freeze_max_age;
 4850                          3188         [ -  + ]:          13740 :         if (multiForceLimit < FirstMultiXactId)
 4850 alvherre@alvh.no-ip.     3189                 :UBC           0 :             multiForceLimit -= FirstMultiXactId;
  736 noah@leadboat.com        3190   [ +  -  -  + ]:CBC       27480 :         force_vacuum = MultiXactIdIsValid(relminmxid) &&
                               3191                 :          13740 :             MultiXactIdPrecedes(relminmxid, multiForceLimit);
                               3192                 :                :     }
 6768 alvherre@alvh.no-ip.     3193                 :          13740 :     *wraparound = force_vacuum;
                               3194                 :                : 
                               3195                 :                :     /*
                               3196                 :                :      * To calculate the (M)XID age portion of the score, divide the age by its
                               3197                 :                :      * respective *_freeze_max_age parameter.
                               3198                 :                :      */
   32 nathan@postgresql.or     3199         [ +  - ]:GNC       13740 :     xid_age = TransactionIdIsNormal(relfrozenxid) ? recentXid - relfrozenxid : 0;
                               3200         [ +  - ]:          13740 :     mxid_age = MultiXactIdIsValid(relminmxid) ? recentMulti - relminmxid : 0;
                               3201                 :                : 
                               3202                 :          13740 :     scores->xid = (double) xid_age / freeze_max_age;
                               3203                 :          13740 :     scores->mxid = (double) mxid_age / multixact_freeze_max_age;
                               3204                 :                : 
                               3205                 :                :     /*
                               3206                 :                :      * To ensure tables are given increased priority once they begin
                               3207                 :                :      * approaching wraparound, we scale the score aggressively if the ages
                               3208                 :                :      * surpass vacuum_failsafe_age or vacuum_multixact_failsafe_age.
                               3209                 :                :      *
                               3210                 :                :      * As in vacuum_xid_failsafe_check(), the effective failsafe age is no
                               3211                 :                :      * less than 105% the value of the respective *_freeze_max_age parameter.
                               3212                 :                :      * Note that per-table settings could result in a low score even if the
                               3213                 :                :      * table surpasses the failsafe settings.  However, this is a strange
                               3214                 :                :      * enough corner case that we don't bother trying to handle it.
                               3215                 :                :      *
                               3216                 :                :      * We further adjust the effective failsafe ages with the weight
                               3217                 :                :      * parameters so that increasing them lowers the ages at which we begin
                               3218                 :                :      * scaling aggressively.
                               3219                 :                :      */
                               3220         [ +  - ]:          13740 :     effective_xid_failsafe_age = Max(vacuum_failsafe_age,
                               3221                 :                :                                      autovacuum_freeze_max_age * 1.05);
                               3222         [ +  - ]:          13740 :     effective_mxid_failsafe_age = Max(vacuum_multixact_failsafe_age,
                               3223                 :                :                                       autovacuum_multixact_freeze_max_age * 1.05);
                               3224                 :                : 
                               3225         [ -  + ]:          13740 :     if (autovacuum_freeze_score_weight > 1.0)
   32 nathan@postgresql.or     3226                 :UNC           0 :         effective_xid_failsafe_age /= autovacuum_freeze_score_weight;
   32 nathan@postgresql.or     3227         [ -  + ]:GNC       13740 :     if (autovacuum_multixact_freeze_score_weight > 1.0)
   32 nathan@postgresql.or     3228                 :UNC           0 :         effective_mxid_failsafe_age /= autovacuum_multixact_freeze_score_weight;
                               3229                 :                : 
   32 nathan@postgresql.or     3230         [ -  + ]:GNC       13740 :     if (xid_age >= effective_xid_failsafe_age)
   32 nathan@postgresql.or     3231         [ #  # ]:UNC           0 :         scores->xid = pow(scores->xid, Max(1.0, (double) xid_age / 100000000));
   32 nathan@postgresql.or     3232         [ -  + ]:GNC       13740 :     if (mxid_age >= effective_mxid_failsafe_age)
   32 nathan@postgresql.or     3233         [ #  # ]:UNC           0 :         scores->mxid = pow(scores->mxid, Max(1.0, (double) mxid_age / 100000000));
                               3234                 :                : 
   32 nathan@postgresql.or     3235                 :GNC       13740 :     scores->xid *= autovacuum_freeze_score_weight;
                               3236                 :          13740 :     scores->mxid *= autovacuum_multixact_freeze_score_weight;
                               3237                 :                : 
                               3238         [ +  + ]:          13740 :     scores->max = Max(scores->xid, scores->mxid);
                               3239         [ -  + ]:          13740 :     if (force_vacuum)
   39 nathan@postgresql.or     3240                 :UNC           0 :         *dovacuum = true;
                               3241                 :                : 
                               3242                 :                :     /*
                               3243                 :                :      * If we found stats for the table, and autovacuum is currently enabled,
                               3244                 :                :      * make a threshold-based decision whether to vacuum and/or analyze.  If
                               3245                 :                :      * autovacuum is currently disabled, we must be here for anti-wraparound
                               3246                 :                :      * vacuuming only, so don't vacuum (or analyze) anything that's not being
                               3247                 :                :      * forced.
                               3248                 :                :      */
   29 nathan@postgresql.or     3249                 :GNC       13740 :     tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
                               3250                 :                :                                               relid, &may_free);
   32                          3251         [ +  + ]:          13740 :     if (!tabentry)
                               3252                 :           2987 :         return;
                               3253                 :                : 
                               3254                 :          10753 :     vactuples = tabentry->dead_tuples;
                               3255                 :          10753 :     instuples = tabentry->ins_since_vacuum;
                               3256                 :          10753 :     anltuples = tabentry->mod_since_analyze;
                               3257                 :                : 
                               3258                 :                :     /* If the table hasn't yet been vacuumed, take reltuples as zero */
                               3259         [ +  + ]:          10753 :     if (reltuples < 0)
                               3260                 :           3060 :         reltuples = 0;
                               3261                 :                : 
                               3262                 :                :     /*
                               3263                 :                :      * If we have data for relallfrozen, calculate the unfrozen percentage of
                               3264                 :                :      * the table to modify insert scale factor. This helps us decide whether
                               3265                 :                :      * or not to vacuum an insert-heavy table based on the number of inserts
                               3266                 :                :      * to the more "active" part of the table.
                               3267                 :                :      */
                               3268   [ +  +  +  + ]:          10753 :     if (relpages > 0 && relallfrozen > 0)
                               3269                 :                :     {
                               3270                 :                :         /*
                               3271                 :                :          * It could be the stats were updated manually and relallfrozen >
                               3272                 :                :          * relpages. Clamp relallfrozen to relpages to avoid nonsensical
                               3273                 :                :          * calculations.
                               3274                 :                :          */
                               3275                 :           2558 :         relallfrozen = Min(relallfrozen, relpages);
                               3276                 :           2558 :         pcnt_unfrozen = 1 - ((float4) relallfrozen / relpages);
                               3277                 :                :     }
                               3278                 :                : 
                               3279                 :          10753 :     vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples;
                               3280   [ +  -  -  + ]:          10753 :     if (vac_max_thresh >= 0 && vacthresh > (float4) vac_max_thresh)
   32 nathan@postgresql.or     3281                 :UNC           0 :         vacthresh = (float4) vac_max_thresh;
                               3282                 :                : 
   32 nathan@postgresql.or     3283                 :GNC       10753 :     vacinsthresh = (float4) vac_ins_base_thresh +
                               3284                 :          10753 :         vac_ins_scale_factor * reltuples * pcnt_unfrozen;
                               3285                 :          10753 :     anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples;
                               3286                 :                : 
                               3287                 :                :     /* Determine if this table needs vacuum, and update the score. */
                               3288         [ +  - ]:          10753 :     scores->vac = (double) vactuples / Max(vacthresh, 1);
                               3289                 :          10753 :     scores->vac *= autovacuum_vacuum_score_weight;
                               3290         [ +  + ]:          10753 :     scores->max = Max(scores->max, scores->vac);
                               3291   [ +  +  +  + ]:          10753 :     if (av_enabled && vactuples > vacthresh)
                               3292                 :            408 :         *dovacuum = true;
                               3293                 :                : 
                               3294         [ +  - ]:          10753 :     if (vac_ins_base_thresh >= 0)
                               3295                 :                :     {
                               3296         [ +  - ]:          10753 :         scores->vac_ins = (double) instuples / Max(vacinsthresh, 1);
                               3297                 :          10753 :         scores->vac_ins *= autovacuum_vacuum_insert_score_weight;
                               3298         [ +  + ]:          10753 :         scores->max = Max(scores->max, scores->vac_ins);
                               3299   [ +  +  +  + ]:          10753 :         if (av_enabled && instuples > vacinsthresh)
                               3300                 :            232 :             *dovacuum = true;
                               3301                 :                :     }
                               3302                 :                : 
                               3303                 :                :     /*
                               3304                 :                :      * Determine if this table needs analyze, and update the score.  Note that
                               3305                 :                :      * we don't analyze TOAST tables and pg_statistic.
                               3306                 :                :      */
                               3307         [ +  + ]:          10753 :     if (relid != StatisticRelationId &&
                               3308         [ +  + ]:          10701 :         classForm->relkind != RELKIND_TOASTVALUE)
                               3309                 :                :     {
                               3310         [ +  - ]:           7626 :         scores->anl = (double) anltuples / Max(anlthresh, 1);
                               3311                 :           7626 :         scores->anl *= autovacuum_analyze_score_weight;
                               3312         [ +  + ]:           7626 :         scores->max = Max(scores->max, scores->anl);
                               3313   [ +  +  +  + ]:           7626 :         if (av_enabled && anltuples > anlthresh)
                               3314                 :           1057 :             *doanalyze = true;
                               3315                 :                :     }
                               3316                 :                : 
                               3317         [ +  - ]:          10753 :     if (vac_ins_base_thresh >= 0)
                               3318         [ -  + ]:          10753 :         elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: %.0f (thresh %.0f, score %.2f), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
                               3319                 :                :              NameStr(classForm->relname),
                               3320                 :                :              vactuples, vacthresh, scores->vac,
                               3321                 :                :              instuples, vacinsthresh, scores->vac_ins,
                               3322                 :                :              anltuples, anlthresh, scores->anl,
                               3323                 :                :              scores->xid, scores->mxid);
                               3324                 :                :     else
   32 nathan@postgresql.or     3325         [ #  # ]:UNC           0 :         elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: (disabled), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
                               3326                 :                :              NameStr(classForm->relname),
                               3327                 :                :              vactuples, vacthresh, scores->vac,
                               3328                 :                :              anltuples, anlthresh, scores->anl,
                               3329                 :                :              scores->xid, scores->mxid);
                               3330                 :                : 
                               3331                 :                :     /* Avoid leaking pgstat entries until the end of autovacuum. */
   26 nathan@postgresql.or     3332         [ +  - ]:GNC       10753 :     if (may_free)
                               3333                 :          10753 :         pfree(tabentry);
                               3334                 :                : }
                               3335                 :                : 
                               3336                 :                : /*
                               3337                 :                :  * autovacuum_do_vac_analyze
                               3338                 :                :  *      Vacuum and/or analyze the specified table
                               3339                 :                :  *
                               3340                 :                :  * We expect the caller to have switched into a memory context that won't
                               3341                 :                :  * disappear at transaction commit.
                               3342                 :                :  */
                               3343                 :                : static void
 4066 alvherre@alvh.no-ip.     3344                 :CBC         584 : autovacuum_do_vac_analyze(autovac_table *tab, BufferAccessStrategy bstrategy)
                               3345                 :                : {
                               3346                 :                :     RangeVar   *rangevar;
                               3347                 :                :     VacuumRelation *rel;
                               3348                 :                :     List       *rel_list;
                               3349                 :                :     MemoryContext vac_context;
                               3350                 :                :     MemoryContext old_context;
                               3351                 :                : 
                               3352                 :                :     /* Let pgstat know what we're doing */
 6501                          3353                 :            584 :     autovac_report_activity(tab);
                               3354                 :                : 
                               3355                 :                :     /* Create a context that vacuum() can use as cross-transaction storage */
  347 tgl@sss.pgh.pa.us        3356                 :            584 :     vac_context = AllocSetContextCreate(CurrentMemoryContext,
                               3357                 :                :                                         "Vacuum",
                               3358                 :                :                                         ALLOCSET_DEFAULT_SIZES);
                               3359                 :                : 
                               3360                 :                :     /* Set up one VacuumRelation target, identified by OID, for vacuum() */
                               3361                 :            584 :     old_context = MemoryContextSwitchTo(vac_context);
 3136                          3362                 :            584 :     rangevar = makeRangeVar(tab->at_nspname, tab->at_relname, -1);
                               3363                 :            584 :     rel = makeVacuumRelation(rangevar, tab->at_relid, NIL);
                               3364                 :            584 :     rel_list = list_make1(rel);
  347                          3365                 :            584 :     MemoryContextSwitchTo(old_context);
                               3366                 :                : 
   35 nathan@postgresql.or     3367                 :            584 :     vacuum(rel_list, &tab->at_params, bstrategy, vac_context, true);
                               3368                 :                : 
 1125 drowley@postgresql.o     3369                 :            583 :     MemoryContextDelete(vac_context);
 7600 tgl@sss.pgh.pa.us        3370                 :            583 : }
                               3371                 :                : 
                               3372                 :                : /*
                               3373                 :                :  * autovac_report_activity
                               3374                 :                :  *      Report to pgstat what autovacuum is doing
                               3375                 :                :  *
                               3376                 :                :  * We send a SQL string corresponding to what the user would see if the
                               3377                 :                :  * equivalent command was to be issued manually.
                               3378                 :                :  *
                               3379                 :                :  * Note we assume that we are going to report the next command as soon as we're
                               3380                 :                :  * done with the current one, and exit right after the last one, so we don't
                               3381                 :                :  * bother to report "<IDLE>" or some such.
                               3382                 :                :  */
                               3383                 :                : static void
 6501 alvherre@alvh.no-ip.     3384                 :            584 : autovac_report_activity(autovac_table *tab)
                               3385                 :                : {
                               3386                 :                : #define MAX_AUTOVAC_ACTIV_LEN (NAMEDATALEN * 2 + 56)
                               3387                 :                :     char        activity[MAX_AUTOVAC_ACTIV_LEN];
                               3388                 :                :     int         len;
                               3389                 :                : 
                               3390                 :                :     /* Report the command and possible options */
 2605 rhaas@postgresql.org     3391         [ +  + ]:            584 :     if (tab->at_params.options & VACOPT_VACUUM)
 7291 alvherre@alvh.no-ip.     3392                 :            276 :         snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
                               3393                 :                :                  "autovacuum: VACUUM%s",
 2605 rhaas@postgresql.org     3394         [ +  + ]:            276 :                  tab->at_params.options & VACOPT_ANALYZE ? " ANALYZE" : "");
                               3395                 :                :     else
 7291 alvherre@alvh.no-ip.     3396                 :            308 :         snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
                               3397                 :                :                  "autovacuum: ANALYZE");
                               3398                 :                : 
                               3399                 :                :     /*
                               3400                 :                :      * Report the qualified name of the relation.
                               3401                 :                :      */
 6501                          3402                 :            584 :     len = strlen(activity);
                               3403                 :                : 
                               3404                 :            584 :     snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
                               3405                 :                :              " %s.%s%s", tab->at_nspname, tab->at_relname,
 4066                          3406         [ -  + ]:            584 :              tab->at_params.is_wraparound ? " (to prevent wraparound)" : "");
                               3407                 :                : 
                               3408                 :                :     /* Set statement_timestamp() to current time for pg_stat_activity */
 6799 tgl@sss.pgh.pa.us        3409                 :            584 :     SetCurrentStatementStartTimestamp();
                               3410                 :                : 
 5220 magnus@hagander.net      3411                 :            584 :     pgstat_report_activity(STATE_RUNNING, activity);
 7291 alvherre@alvh.no-ip.     3412                 :            584 : }
                               3413                 :                : 
                               3414                 :                : /*
                               3415                 :                :  * autovac_report_workitem
                               3416                 :                :  *      Report to pgstat that autovacuum is processing a work item
                               3417                 :                :  */
                               3418                 :                : static void
 3321                          3419                 :              6 : autovac_report_workitem(AutoVacuumWorkItem *workitem,
                               3420                 :                :                         const char *nspname, const char *relname)
                               3421                 :                : {
                               3422                 :                :     char        activity[MAX_AUTOVAC_ACTIV_LEN + 12 + 2];
                               3423                 :                :     char        blk[12 + 2];
                               3424                 :                :     int         len;
                               3425                 :                : 
                               3426         [ +  - ]:              6 :     switch (workitem->avw_type)
                               3427                 :                :     {
                               3428                 :              6 :         case AVW_BRINSummarizeRange:
                               3429                 :              6 :             snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
                               3430                 :                :                      "autovacuum: BRIN summarize");
                               3431                 :              6 :             break;
                               3432                 :                :     }
                               3433                 :                : 
                               3434                 :                :     /*
                               3435                 :                :      * Report the qualified name of the relation, and the block number if any
                               3436                 :                :      */
                               3437                 :              6 :     len = strlen(activity);
                               3438                 :                : 
                               3439         [ +  - ]:              6 :     if (BlockNumberIsValid(workitem->avw_blockNumber))
                               3440                 :              6 :         snprintf(blk, sizeof(blk), " %u", workitem->avw_blockNumber);
                               3441                 :                :     else
 3321 alvherre@alvh.no-ip.     3442                 :UBC           0 :         blk[0] = '\0';
                               3443                 :                : 
 3321 alvherre@alvh.no-ip.     3444                 :CBC           6 :     snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
                               3445                 :                :              " %s.%s%s", nspname, relname, blk);
                               3446                 :                : 
                               3447                 :                :     /* Set statement_timestamp() to current time for pg_stat_activity */
                               3448                 :              6 :     SetCurrentStatementStartTimestamp();
                               3449                 :                : 
                               3450                 :              6 :     pgstat_report_activity(STATE_RUNNING, activity);
                               3451                 :              6 : }
                               3452                 :                : 
                               3453                 :                : /*
                               3454                 :                :  * AutoVacuumingActive
                               3455                 :                :  *      Check GUC vars and report whether the autovacuum process should be
                               3456                 :                :  *      running.
                               3457                 :                :  */
                               3458                 :                : bool
 7600 tgl@sss.pgh.pa.us        3459                 :          60030 : AutoVacuumingActive(void)
                               3460                 :                : {
 6798                          3461   [ +  +  -  + ]:          60030 :     if (!autovacuum_start_daemon || !pgstat_track_counts)
 7600                          3462                 :           5318 :         return false;
                               3463                 :          54712 :     return true;
                               3464                 :                : }
                               3465                 :                : 
                               3466                 :                : /*
                               3467                 :                :  * Request one work item to the next autovacuum run processing our database.
                               3468                 :                :  * Return false if the request can't be recorded.
                               3469                 :                :  */
                               3470                 :                : bool
 3321 alvherre@alvh.no-ip.     3471                 :              6 : AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId,
                               3472                 :                :                       BlockNumber blkno)
                               3473                 :                : {
                               3474                 :                :     int         i;
 2974                          3475                 :              6 :     bool        result = false;
                               3476                 :                : 
 3321                          3477                 :              6 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
                               3478                 :                : 
                               3479                 :                :     /*
                               3480                 :                :      * Locate an unused work item and fill it with the given data.
                               3481                 :                :      */
 3185                          3482         [ +  - ]:             21 :     for (i = 0; i < NUM_WORKITEMS; i++)
                               3483                 :                :     {
                               3484                 :             21 :         AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
                               3485                 :                : 
                               3486         [ +  + ]:             21 :         if (workitem->avw_used)
                               3487                 :             15 :             continue;
                               3488                 :                : 
                               3489                 :              6 :         workitem->avw_used = true;
                               3490                 :              6 :         workitem->avw_active = false;
                               3491                 :              6 :         workitem->avw_type = type;
                               3492                 :              6 :         workitem->avw_database = MyDatabaseId;
                               3493                 :              6 :         workitem->avw_relation = relationId;
                               3494                 :              6 :         workitem->avw_blockNumber = blkno;
 2974                          3495                 :              6 :         result = true;
                               3496                 :                : 
                               3497                 :                :         /* done */
 3185                          3498                 :              6 :         break;
                               3499                 :                :     }
                               3500                 :                : 
 3321                          3501                 :              6 :     LWLockRelease(AutovacuumLock);
                               3502                 :                : 
 2974                          3503                 :              6 :     return result;
                               3504                 :                : }
                               3505                 :                : 
                               3506                 :                : /*
                               3507                 :                :  * autovac_init
                               3508                 :                :  *      This is called at postmaster initialization.
                               3509                 :                :  *
                               3510                 :                :  * All we do here is annoy the user if he got it wrong.
                               3511                 :                :  */
                               3512                 :                : void
 7600 tgl@sss.pgh.pa.us        3513                 :            989 : autovac_init(void)
                               3514                 :                : {
  484 nathan@postgresql.or     3515         [ +  + ]:            989 :     if (!autovacuum_start_daemon)
                               3516                 :            131 :         return;
                               3517         [ -  + ]:            858 :     else if (!pgstat_track_counts)
 7600 tgl@sss.pgh.pa.us        3518         [ #  # ]:UBC           0 :         ereport(WARNING,
                               3519                 :                :                 (errmsg("autovacuum not started because of misconfiguration"),
                               3520                 :                :                  errhint("Enable the \"track_counts\" option.")));
                               3521                 :                :     else
  484 nathan@postgresql.or     3522                 :CBC         858 :         check_av_worker_gucs();
                               3523                 :                : }
                               3524                 :                : 
                               3525                 :                : /*
                               3526                 :                :  * AutoVacuumShmemRequest
                               3527                 :                :  *      Register shared memory space needed for autovacuum
                               3528                 :                :  */
                               3529                 :                : static void
   29 heikki.linnakangas@i     3530                 :GNC        1244 : AutoVacuumShmemRequest(void *arg)
                               3531                 :                : {
                               3532                 :                :     Size        size;
                               3533                 :                : 
                               3534                 :                :     /*
                               3535                 :                :      * Need the fixed struct and the array of WorkerInfoData.
                               3536                 :                :      */
 6959 alvherre@alvh.no-ip.     3537                 :CBC        1244 :     size = sizeof(AutoVacuumShmemStruct);
                               3538                 :           1244 :     size = MAXALIGN(size);
  484 nathan@postgresql.or     3539                 :           1244 :     size = add_size(size, mul_size(autovacuum_worker_slots,
                               3540                 :                :                                    sizeof(WorkerInfoData)));
                               3541                 :                : 
   29 heikki.linnakangas@i     3542                 :GNC        1244 :     ShmemRequestStruct(.name = "AutoVacuum Data",
                               3543                 :                :                        .size = size,
                               3544                 :                :                        .ptr = (void **) &AutoVacuumShmem,
                               3545                 :                :         );
 7019 alvherre@alvh.no-ip.     3546                 :GIC        1244 : }
                               3547                 :                : 
                               3548                 :                : /*
                               3549                 :                :  * AutoVacuumShmemInit
                               3550                 :                :  *      Initialize autovacuum-related shared memory
                               3551                 :                :  */
                               3552                 :                : static void
   29 heikki.linnakangas@i     3553                 :GNC        1241 : AutoVacuumShmemInit(void *arg)
                               3554                 :                : {
                               3555                 :                :     WorkerInfo  worker;
                               3556                 :                : 
                               3557                 :           1241 :     AutoVacuumShmem->av_launcherpid = 0;
                               3558                 :           1241 :     dclist_init(&AutoVacuumShmem->av_freeWorkers);
                               3559                 :           1241 :     dlist_init(&AutoVacuumShmem->av_runningWorkers);
                               3560                 :           1241 :     AutoVacuumShmem->av_startingWorker = NULL;
                               3561                 :           1241 :     memset(AutoVacuumShmem->av_workItems, 0,
                               3562                 :                :            sizeof(AutoVacuumWorkItem) * NUM_WORKITEMS);
                               3563                 :                : 
                               3564                 :           1241 :     worker = (WorkerInfo) ((char *) AutoVacuumShmem +
                               3565                 :                :                            MAXALIGN(sizeof(AutoVacuumShmemStruct)));
                               3566                 :                : 
                               3567                 :                :     /* initialize the WorkerInfo free list */
                               3568         [ +  + ]:          15011 :     for (int i = 0; i < autovacuum_worker_slots; i++)
                               3569                 :                :     {
                               3570                 :          13770 :         dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
                               3571                 :          13770 :                          &worker[i].wi_links);
                               3572                 :          13770 :         pg_atomic_init_flag(&worker[i].wi_dobalance);
                               3573                 :                :     }
                               3574                 :                : 
                               3575                 :           1241 :     pg_atomic_init_u32(&AutoVacuumShmem->av_nworkersForBalance, 0);
 7600 tgl@sss.pgh.pa.us        3576                 :CBC        1241 : }
                               3577                 :                : 
                               3578                 :                : /*
                               3579                 :                :  * GUC check_hook for autovacuum_work_mem
                               3580                 :                :  */
                               3581                 :                : bool
 1330                          3582                 :           1288 : check_autovacuum_work_mem(int *newval, void **extra, GucSource source)
                               3583                 :                : {
                               3584                 :                :     /*
                               3585                 :                :      * -1 indicates fallback.
                               3586                 :                :      *
                               3587                 :                :      * If we haven't yet changed the boot_val default of -1, just let it be.
                               3588                 :                :      * Autovacuum will look to maintenance_work_mem instead.
                               3589                 :                :      */
                               3590         [ +  + ]:           1288 :     if (*newval == -1)
                               3591                 :           1286 :         return true;
                               3592                 :                : 
                               3593                 :                :     /*
                               3594                 :                :      * We clamp manually-set values to at least 64kB.  Since
                               3595                 :                :      * maintenance_work_mem is always set to at least this value, do the same
                               3596                 :                :      * here.
                               3597                 :                :      */
  637 john.naylor@postgres     3598         [ +  - ]:              2 :     if (*newval < 64)
                               3599                 :              2 :         *newval = 64;
                               3600                 :                : 
 1330 tgl@sss.pgh.pa.us        3601                 :              2 :     return true;
                               3602                 :                : }
                               3603                 :                : 
                               3604                 :                : /*
                               3605                 :                :  * Returns whether there is a free autovacuum worker slot available.
                               3606                 :                :  */
                               3607                 :                : static bool
  484 nathan@postgresql.or     3608                 :           3731 : av_worker_available(void)
                               3609                 :                : {
                               3610                 :                :     int         free_slots;
                               3611                 :                :     int         reserved_slots;
                               3612                 :                : 
                               3613                 :           3731 :     free_slots = dclist_count(&AutoVacuumShmem->av_freeWorkers);
                               3614                 :                : 
                               3615                 :           3731 :     reserved_slots = autovacuum_worker_slots - autovacuum_max_workers;
                               3616                 :           3731 :     reserved_slots = Max(0, reserved_slots);
                               3617                 :                : 
                               3618                 :           3731 :     return free_slots > reserved_slots;
                               3619                 :                : }
                               3620                 :                : 
                               3621                 :                : /*
                               3622                 :                :  * Emits a WARNING if autovacuum_worker_slots < autovacuum_max_workers.
                               3623                 :                :  */
                               3624                 :                : static void
                               3625                 :            858 : check_av_worker_gucs(void)
                               3626                 :                : {
                               3627         [ -  + ]:            858 :     if (autovacuum_worker_slots < autovacuum_max_workers)
  484 nathan@postgresql.or     3628         [ #  # ]:UBC           0 :         ereport(WARNING,
                               3629                 :                :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                               3630                 :                :                  errmsg("\"autovacuum_max_workers\" (%d) should be less than or equal to \"autovacuum_worker_slots\" (%d)",
                               3631                 :                :                         autovacuum_max_workers, autovacuum_worker_slots),
                               3632                 :                :                  errdetail("The server will only start up to \"autovacuum_worker_slots\" (%d) autovacuum workers at a given time.",
                               3633                 :                :                            autovacuum_worker_slots)));
  484 nathan@postgresql.or     3634                 :CBC         858 : }
                               3635                 :                : 
                               3636                 :                : /*
                               3637                 :                :  * pg_stat_get_autovacuum_scores
                               3638                 :                :  *
                               3639                 :                :  * Returns current autovacuum scores for all relevant tables in the current
                               3640                 :                :  * database.
                               3641                 :                :  */
                               3642                 :                : Datum
   29 nathan@postgresql.or     3643                 :UNC           0 : pg_stat_get_autovacuum_scores(PG_FUNCTION_ARGS)
                               3644                 :                : {
                               3645                 :                :     int         effective_multixact_freeze_max_age;
                               3646                 :                :     Relation    rel;
                               3647                 :                :     TableScanDesc scan;
                               3648                 :                :     HeapTuple   tup;
                               3649                 :              0 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
                               3650                 :                : 
                               3651                 :              0 :     InitMaterializedSRF(fcinfo, 0);
                               3652                 :                : 
                               3653                 :                :     /* some prerequisite initialization */
                               3654                 :              0 :     effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
                               3655                 :              0 :     recentXid = ReadNextTransactionId();
                               3656                 :              0 :     recentMulti = ReadNextMultiXactId();
                               3657                 :                : 
                               3658                 :                :     /* scan pg_class */
                               3659                 :              0 :     rel = table_open(RelationRelationId, AccessShareLock);
                               3660                 :              0 :     scan = table_beginscan_catalog(rel, 0, NULL);
                               3661         [ #  # ]:              0 :     while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
                               3662                 :                :     {
                               3663                 :              0 :         Form_pg_class form = (Form_pg_class) GETSTRUCT(tup);
                               3664                 :                :         AutoVacOpts *avopts;
                               3665                 :                :         bool        dovacuum;
                               3666                 :                :         bool        doanalyze;
                               3667                 :                :         bool        wraparound;
                               3668                 :                :         AutoVacuumScores scores;
                               3669                 :                :         Datum       vals[10];
                               3670                 :              0 :         bool        nulls[10] = {false};
                               3671                 :                : 
                               3672                 :                :         /* skip ineligible entries */
                               3673         [ #  # ]:              0 :         if (form->relkind != RELKIND_RELATION &&
                               3674         [ #  # ]:              0 :             form->relkind != RELKIND_MATVIEW &&
                               3675         [ #  # ]:              0 :             form->relkind != RELKIND_TOASTVALUE)
                               3676                 :              0 :             continue;
                               3677         [ #  # ]:              0 :         if (form->relpersistence == RELPERSISTENCE_TEMP)
                               3678                 :              0 :             continue;
                               3679                 :                : 
                               3680                 :              0 :         avopts = extract_autovac_opts(tup, RelationGetDescr(rel));
                               3681                 :              0 :         relation_needs_vacanalyze(form->oid, avopts, form,
                               3682                 :                :                                   effective_multixact_freeze_max_age,
                               3683                 :                :                                   LOG_NEVER,
                               3684                 :                :                                   &dovacuum, &doanalyze, &wraparound,
                               3685                 :                :                                   &scores);
                               3686         [ #  # ]:              0 :         if (avopts)
                               3687                 :              0 :             pfree(avopts);
                               3688                 :                : 
                               3689                 :              0 :         vals[0] = ObjectIdGetDatum(form->oid);
                               3690                 :              0 :         vals[1] = Float8GetDatum(scores.max);
                               3691                 :              0 :         vals[2] = Float8GetDatum(scores.xid);
                               3692                 :              0 :         vals[3] = Float8GetDatum(scores.mxid);
                               3693                 :              0 :         vals[4] = Float8GetDatum(scores.vac);
                               3694                 :              0 :         vals[5] = Float8GetDatum(scores.vac_ins);
                               3695                 :              0 :         vals[6] = Float8GetDatum(scores.anl);
                               3696                 :              0 :         vals[7] = BoolGetDatum(dovacuum);
                               3697                 :              0 :         vals[8] = BoolGetDatum(doanalyze);
                               3698                 :              0 :         vals[9] = BoolGetDatum(wraparound);
                               3699                 :                : 
                               3700                 :              0 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, vals, nulls);
                               3701                 :                :     }
                               3702                 :              0 :     table_endscan(scan);
                               3703                 :              0 :     table_close(rel, AccessShareLock);
                               3704                 :                : 
                               3705                 :              0 :     return (Datum) 0;
                               3706                 :                : }
        

Generated by: LCOV version 2.5.0-beta