LCOV - code coverage report
Current view: top level - src/backend/postmaster - autovacuum.c (source / functions) Coverage Total Hit
Test: PostgreSQL 20devel Lines: 81.3 % 1058 860
Test Date: 2026-07-03 19:57:34 Functions: 94.1 % 34 32
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
Branches: 67.8 % 636 431

             Branch data     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
     413                 :         471 : AutoVacLauncherMain(const void *startup_data, size_t startup_data_len)
     414                 :             : {
     415                 :             :     sigjmp_buf  local_sigjmp_buf;
     416                 :             : 
     417                 :             :     Assert(startup_data_len == 0);
     418                 :             : 
     419                 :             :     /* Release postmaster's working memory context */
     420         [ +  - ]:         471 :     if (PostmasterContext)
     421                 :             :     {
     422                 :         471 :         MemoryContextDelete(PostmasterContext);
     423                 :         471 :         PostmasterContext = NULL;
     424                 :             :     }
     425                 :             : 
     426                 :         471 :     init_ps_display(NULL);
     427                 :             : 
     428         [ +  + ]:         471 :     ereport(DEBUG1,
     429                 :             :             (errmsg_internal("autovacuum launcher started")));
     430                 :             : 
     431         [ -  + ]:         471 :     if (PostAuthDelay)
     432                 :           0 :         pg_usleep(PostAuthDelay * 1000000L);
     433                 :             : 
     434                 :             :     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                 :             :      */
     441                 :         471 :     pqsignal(SIGHUP, SignalHandlerForConfigReload);
     442                 :         471 :     pqsignal(SIGINT, StatementCancelHandler);
     443                 :         471 :     pqsignal(SIGTERM, SignalHandlerForShutdownRequest);
     444                 :             :     /* SIGQUIT handler was already set up by InitPostmasterChild */
     445                 :             : 
     446                 :         471 :     InitializeTimeouts();       /* establishes SIGALRM handler */
     447                 :             : 
     448                 :         471 :     pqsignal(SIGPIPE, PG_SIG_IGN);
     449                 :         471 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
     450                 :         471 :     pqsignal(SIGUSR2, avl_sigusr2_handler);
     451                 :         471 :     pqsignal(SIGFPE, FloatExceptionHandler);
     452                 :         471 :     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                 :             :      */
     458                 :         471 :     InitProcess();
     459                 :             : 
     460                 :             :     /* Early initialization */
     461                 :         471 :     BaseInit();
     462                 :             : 
     463                 :         471 :     InitPostgres(NULL, InvalidOid, NULL, InvalidOid, 0, NULL);
     464                 :             : 
     465                 :         471 :     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                 :             :      */
     472                 :         471 :     AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
     473                 :             :                                           "Autovacuum Launcher",
     474                 :             :                                           ALLOCSET_DEFAULT_SIZES);
     475                 :         471 :     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                 :             :      */
     489         [ -  + ]:         471 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
     490                 :             :     {
     491                 :             :         /* since not using PG_TRY, must reset error stack by hand */
     492                 :           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 */
     498                 :           0 :         disable_all_timeouts(false);
     499                 :           0 :         QueryCancelPending = false; /* second to avoid race condition */
     500                 :             : 
     501                 :             :         /* Report the error to the server log */
     502                 :           0 :         EmitErrorReport();
     503                 :             : 
     504                 :             :         /* Abort the current transaction in order to recover */
     505                 :           0 :         AbortCurrentTransaction();
     506                 :             : 
     507                 :             :         /*
     508                 :             :          * Release any other resources, for the case where we were not in a
     509                 :             :          * transaction.
     510                 :             :          */
     511                 :           0 :         LWLockReleaseAll();
     512                 :           0 :         pgstat_report_wait_end();
     513                 :           0 :         pgaio_error_cleanup();
     514                 :           0 :         UnlockBuffers();
     515                 :             :         /* this is probably dead code, but let's be safe: */
     516         [ #  # ]:           0 :         if (AuxProcessResourceOwner)
     517                 :           0 :             ReleaseAuxProcessResources(false);
     518                 :           0 :         AtEOXact_Buffers(false);
     519                 :           0 :         AtEOXact_SMgr();
     520                 :           0 :         AtEOXact_Files(false);
     521                 :           0 :         AtEOXact_HashTables(false);
     522                 :             : 
     523                 :             :         /*
     524                 :             :          * Now return to normal top-level context and clear ErrorContext for
     525                 :             :          * next time.
     526                 :             :          */
     527                 :           0 :         MemoryContextSwitchTo(AutovacMemCxt);
     528                 :           0 :         FlushErrorState();
     529                 :             : 
     530                 :             :         /* Flush any leaked data in the top-level context */
     531                 :           0 :         MemoryContextReset(AutovacMemCxt);
     532                 :             : 
     533                 :             :         /* don't leave dangling pointers to freed memory */
     534                 :           0 :         DatabaseListCxt = NULL;
     535                 :           0 :         dlist_init(&DatabaseList);
     536                 :             : 
     537                 :             :         /* Now we can allow interrupts again */
     538                 :           0 :         RESUME_INTERRUPTS();
     539                 :             : 
     540                 :             :         /* if in shutdown mode, no need for anything further; just go away */
     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                 :             :          */
     548                 :           0 :         pg_usleep(1000000L);
     549                 :             :     }
     550                 :             : 
     551                 :             :     /* We can now handle ereport(ERROR) */
     552                 :         471 :     PG_exception_stack = &local_sigjmp_buf;
     553                 :             : 
     554                 :             :     /* must unblock signals before calling rebuild_database_list */
     555                 :         471 :     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                 :             :      */
     561                 :         471 :     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                 :             :      */
     568                 :         471 :     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                 :         471 :     SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
     575                 :         471 :     SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
     576                 :         471 :     SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
     577                 :         471 :     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                 :             :      */
     585                 :         471 :     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                 :             :      */
     592                 :         471 :     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                 :             :      */
     598         [ -  + ]:         471 :     if (!AutoVacuumingActive())
     599                 :             :     {
     600         [ #  # ]:           0 :         if (!ShutdownRequestPending)
     601                 :           0 :             do_start_worker();
     602                 :           0 :         proc_exit(0);           /* done */
     603                 :             :     }
     604                 :             : 
     605                 :         471 :     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                 :         471 :     rebuild_database_list(InvalidOid);
     615                 :             : 
     616                 :             :     /* loop until shutdown request */
     617         [ +  - ]:        5770 :     while (!ShutdownRequestPending)
     618                 :             :     {
     619                 :             :         struct timeval nap;
     620                 :        5770 :         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                 :             : 
     630                 :        5770 :         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                 :             :          */
     636                 :        5770 :         (void) WaitLatch(MyLatch,
     637                 :             :                          WL_LATCH_SET | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH,
     638                 :        5770 :                          (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
     639                 :             :                          WAIT_EVENT_AUTOVACUUM_MAIN);
     640                 :             : 
     641                 :        5768 :         ResetLatch(MyLatch);
     642                 :             : 
     643                 :        5768 :         ProcessAutoVacLauncherInterrupts();
     644                 :             : 
     645                 :             :         /*
     646                 :             :          * a worker finished, or postmaster signaled failure to start a worker
     647                 :             :          */
     648         [ +  + ]:        5300 :         if (got_SIGUSR2)
     649                 :             :         {
     650                 :        3126 :             got_SIGUSR2 = false;
     651                 :             : 
     652                 :             :             /* rebalance cost limits, if needed */
     653         [ +  + ]:        3126 :             if (AutoVacuumShmem->av_signal[AutoVacRebalance])
     654                 :             :             {
     655                 :        1525 :                 LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
     656                 :        1525 :                 AutoVacuumShmem->av_signal[AutoVacRebalance] = false;
     657                 :        1525 :                 autovac_recalculate_workers_for_balance();
     658                 :        1525 :                 LWLockRelease(AutovacuumLock);
     659                 :             :             }
     660                 :             : 
     661         [ -  + ]:        3126 :             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                 :             :                  */
     673                 :           0 :                 AutoVacuumShmem->av_signal[AutoVacForkFailed] = false;
     674                 :           0 :                 pg_usleep(1000000L);    /* 1s */
     675                 :           0 :                 SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
     676                 :        1528 :                 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                 :             : 
     687                 :        5300 :         current_time = GetCurrentTimestamp();
     688                 :        5300 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
     689                 :             : 
     690                 :        5300 :         can_launch = av_worker_available();
     691                 :             : 
     692         [ +  + ]:        5300 :         if (AutoVacuumShmem->av_startingWorker != NULL)
     693                 :             :         {
     694                 :             :             int         waittime;
     695                 :          29 :             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                 :             :              */
     712                 :          29 :             waittime = Min(autovacuum_naptime, 60) * 1000;
     713         [ -  + ]:          29 :             if (TimestampDifferenceExceeds(worker->wi_launchtime, current_time,
     714                 :             :                                            waittime))
     715                 :             :             {
     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                 :             :                  */
     725         [ #  # ]:           0 :                 if (AutoVacuumShmem->av_startingWorker != NULL)
     726                 :             :                 {
     727                 :           0 :                     worker = AutoVacuumShmem->av_startingWorker;
     728                 :           0 :                     worker->wi_dboid = InvalidOid;
     729                 :           0 :                     worker->wi_tableoid = InvalidOid;
     730                 :           0 :                     worker->wi_sharedrel = false;
     731                 :           0 :                     worker->wi_proc = NULL;
     732                 :           0 :                     worker->wi_launchtime = 0;
     733                 :           0 :                     dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
     734                 :             :                                      &worker->wi_links);
     735                 :           0 :                     AutoVacuumShmem->av_startingWorker = NULL;
     736         [ #  # ]:           0 :                     ereport(WARNING,
     737                 :             :                             errmsg("autovacuum worker took too long to start; canceled"));
     738                 :             :                 }
     739                 :             :             }
     740                 :             :             else
     741                 :          29 :                 can_launch = false;
     742                 :             :         }
     743                 :        5300 :         LWLockRelease(AutovacuumLock);  /* either shared or exclusive */
     744                 :             : 
     745                 :             :         /* if we can't do anything, just go back to sleep */
     746         [ +  + ]:        5300 :         if (!can_launch)
     747                 :        1528 :             continue;
     748                 :             : 
     749                 :             :         /* We're OK to start a new worker */
     750                 :             : 
     751         [ +  + ]:        3772 :         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                 :             :              */
     761                 :           4 :             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                 :             : 
     772                 :        3768 :             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         [ +  + ]:        3768 :             if (TimestampDifferenceExceeds(avdb->adl_next_worker,
     779                 :             :                                            current_time, 0))
     780                 :        1568 :                 launch_worker(current_time);
     781                 :             :         }
     782                 :             :     }
     783                 :             : 
     784                 :           0 :     AutoVacLauncherShutdown();
     785                 :             : }
     786                 :             : 
     787                 :             : /*
     788                 :             :  * Process any new interrupts.
     789                 :             :  */
     790                 :             : static void
     791                 :        5768 : ProcessAutoVacLauncherInterrupts(void)
     792                 :             : {
     793                 :             :     /* the normal shutdown case */
     794         [ +  + ]:        5768 :     if (ShutdownRequestPending)
     795                 :         467 :         AutoVacLauncherShutdown();
     796                 :             : 
     797         [ +  + ]:        5301 :     if (ConfigReloadPending)
     798                 :             :     {
     799                 :          52 :         int         autovacuum_max_workers_prev = autovacuum_max_workers;
     800                 :             : 
     801                 :          52 :         ConfigReloadPending = false;
     802                 :          52 :         ProcessConfigFile(PGC_SIGHUP);
     803                 :             : 
     804                 :             :         /* shutdown requested in config file? */
     805         [ +  + ]:          52 :         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                 :             :          */
     813         [ -  + ]:          51 :         if (autovacuum_max_workers_prev != autovacuum_max_workers)
     814                 :           0 :             check_av_worker_gucs();
     815                 :             : 
     816                 :             :         /* rebuild the list in case the naptime changed */
     817                 :          51 :         rebuild_database_list(InvalidOid);
     818                 :             :     }
     819                 :             : 
     820                 :             :     /* Process barrier events */
     821         [ +  + ]:        5300 :     if (ProcSignalBarrierPending)
     822                 :          80 :         ProcessProcSignalBarrier();
     823                 :             : 
     824                 :             :     /* Perform logging of memory contexts of this process */
     825         [ -  + ]:        5300 :     if (LogMemoryContextPending)
     826                 :           0 :         ProcessLogMemoryContextInterrupt();
     827                 :             : 
     828                 :             :     /* Process sinval catchup interrupts that happened while sleeping */
     829                 :        5300 :     ProcessCatchupInterrupt();
     830                 :        5300 : }
     831                 :             : 
     832                 :             : /*
     833                 :             :  * Perform a normal exit from the autovac launcher.
     834                 :             :  */
     835                 :             : static void
     836                 :         468 : AutoVacLauncherShutdown(void)
     837                 :             : {
     838         [ +  + ]:         468 :     ereport(DEBUG1,
     839                 :             :             (errmsg_internal("autovacuum launcher shutting down")));
     840                 :         468 :     AutoVacuumShmem->av_launcherpid = 0;
     841                 :             : 
     842                 :         468 :     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
     853                 :        5886 : 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                 :             :      */
     861         [ +  + ]:        5886 :     if (!canlaunch)
     862                 :             :     {
     863                 :        2832 :         nap->tv_sec = autovacuum_naptime;
     864                 :        2832 :         nap->tv_usec = 0;
     865                 :             :     }
     866         [ +  + ]:        3054 :     else if (!dlist_is_empty(&DatabaseList))
     867                 :             :     {
     868                 :        3031 :         TimestampTz current_time = GetCurrentTimestamp();
     869                 :             :         TimestampTz next_wakeup;
     870                 :             :         avl_dbase  *avdb;
     871                 :             :         long        secs;
     872                 :             :         int         usecs;
     873                 :             : 
     874                 :        3031 :         avdb = dlist_tail_element(avl_dbase, adl_node, &DatabaseList);
     875                 :             : 
     876                 :        3031 :         next_wakeup = avdb->adl_next_worker;
     877                 :        3031 :         TimestampDifference(current_time, next_wakeup, &secs, &usecs);
     878                 :             : 
     879                 :        3031 :         nap->tv_sec = secs;
     880                 :        3031 :         nap->tv_usec = usecs;
     881                 :             :     }
     882                 :             :     else
     883                 :             :     {
     884                 :             :         /* list is empty, sleep for whole autovacuum_naptime seconds  */
     885                 :          23 :         nap->tv_sec = autovacuum_naptime;
     886                 :          23 :         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                 :             :      */
     899   [ +  +  +  +  :        5886 :     if (nap->tv_sec == 0 && nap->tv_usec == 0 && !recursing)
                   +  - ]
     900                 :             :     {
     901                 :         116 :         rebuild_database_list(InvalidOid);
     902                 :         116 :         launcher_determine_sleep(canlaunch, true, nap);
     903                 :         116 :         return;
     904                 :             :     }
     905                 :             : 
     906                 :             :     /* The smallest time we'll allow the launcher to sleep. */
     907   [ +  +  +  + ]:        5770 :     if (nap->tv_sec <= 0 && nap->tv_usec <= MIN_AUTOVAC_SLEEPTIME * 1000)
     908                 :             :     {
     909                 :         302 :         nap->tv_sec = 0;
     910                 :         302 :         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                 :             :      */
     919         [ +  + ]:        5770 :     if (nap->tv_sec > MAX_AUTOVAC_SLEEPTIME)
     920                 :          10 :         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
     937                 :         653 : 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                 :         653 :     newcxt = AllocSetContextCreate(AutovacMemCxt,
     951                 :             :                                    "Autovacuum database list",
     952                 :             :                                    ALLOCSET_DEFAULT_SIZES);
     953                 :         653 :     tmpcxt = AllocSetContextCreate(newcxt,
     954                 :             :                                    "Autovacuum database list (tmp)",
     955                 :             :                                    ALLOCSET_DEFAULT_SIZES);
     956                 :         653 :     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                 :         653 :     hctl.keysize = sizeof(Oid);
     975                 :         653 :     hctl.entrysize = sizeof(avl_dbase);
     976                 :         653 :     hctl.hcxt = tmpcxt;
     977                 :         653 :     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 */
     982                 :         653 :     score = 0;
     983         [ +  + ]:         653 :     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                 :          15 :         entry = pgstat_fetch_stat_dbentry(newdb);
     990         [ +  + ]:          15 :         if (entry != NULL)
     991                 :             :         {
     992                 :             :             /* we assume it isn't found because the hash was just created */
     993                 :          12 :             db = hash_search(dbhash, &newdb, HASH_ENTER, NULL);
     994                 :             : 
     995                 :             :             /* hash_search already filled in the key */
     996                 :          12 :             db->adl_score = score++;
     997                 :             :             /* next_worker is filled in later */
     998                 :             :         }
     999                 :             :     }
    1000                 :             : 
    1001                 :             :     /* Now insert the databases from the existing list */
    1002   [ +  -  +  + ]:        1116 :     dlist_foreach(iter, &DatabaseList)
    1003                 :             :     {
    1004                 :         463 :         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                 :         463 :         entry = pgstat_fetch_stat_dbentry(avdb->adl_datid);
    1014         [ -  + ]:         463 :         if (entry == NULL)
    1015                 :           0 :             continue;
    1016                 :             : 
    1017                 :         463 :         db = hash_search(dbhash, &(avdb->adl_datid), HASH_ENTER, &found);
    1018                 :             : 
    1019         [ +  - ]:         463 :         if (!found)
    1020                 :             :         {
    1021                 :             :             /* hash_search already filled in the key */
    1022                 :         463 :             db->adl_score = score++;
    1023                 :             :             /* next_worker is filled in later */
    1024                 :             :         }
    1025                 :             :     }
    1026                 :             : 
    1027                 :             :     /* finally, insert all qualifying databases not previously inserted */
    1028                 :         653 :     dblist = get_database_list();
    1029   [ +  -  +  +  :        2918 :     foreach(cell, dblist)
                   +  + ]
    1030                 :             :     {
    1031                 :        2266 :         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                 :        2266 :         entry = pgstat_fetch_stat_dbentry(avdb->adw_datid);
    1038         [ +  + ]:        2266 :         if (entry == NULL)
    1039                 :        1088 :             continue;
    1040                 :             : 
    1041                 :        1178 :         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         [ +  + ]:        1178 :         if (!found)
    1044                 :             :         {
    1045                 :             :             /* hash_search already filled in the key */
    1046                 :         703 :             db->adl_score = score++;
    1047                 :             :             /* next_worker is filled in later */
    1048                 :             :         }
    1049                 :             :     }
    1050                 :         652 :     nelems = score;
    1051                 :             : 
    1052                 :             :     /* from here on, the allocated memory belongs to the new list */
    1053                 :         652 :     MemoryContextSwitchTo(newcxt);
    1054                 :         652 :     dlist_init(&DatabaseList);
    1055                 :             : 
    1056         [ +  + ]:         652 :     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                 :         633 :         dbary = palloc(nelems * sizeof(avl_dbase));
    1067                 :             :         /* keep Valgrind quiet */
    1068                 :             : #ifdef USE_VALGRIND
    1069                 :             :         avl_dbase_array = dbary;
    1070                 :             : #endif
    1071                 :             : 
    1072                 :         633 :         i = 0;
    1073                 :         633 :         hash_seq_init(&seq, dbhash);
    1074         [ +  + ]:        1811 :         while ((db = hash_seq_search(&seq)) != NULL)
    1075                 :        1178 :             memcpy(&(dbary[i++]), db, sizeof(avl_dbase));
    1076                 :             : 
    1077                 :             :         /* sort the array */
    1078                 :         633 :         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                 :         633 :         millis_increment = 1000.0 * autovacuum_naptime / nelems;
    1088         [ -  + ]:         633 :         if (millis_increment <= MIN_AUTOVAC_SLEEPTIME)
    1089                 :           0 :             millis_increment = MIN_AUTOVAC_SLEEPTIME * 1.1;
    1090                 :             : 
    1091                 :         633 :         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         [ +  + ]:        1811 :         for (i = 0; i < nelems; i++)
    1098                 :             :         {
    1099                 :        1178 :             db = &(dbary[i]);
    1100                 :             : 
    1101                 :        1178 :             current_time = TimestampTzPlusMilliseconds(current_time,
    1102                 :             :                                                        millis_increment);
    1103                 :        1178 :             db->adl_next_worker = current_time;
    1104                 :             : 
    1105                 :             :             /* later elements should go closer to the head of the list */
    1106                 :        1178 :             dlist_push_head(&DatabaseList, &db->adl_node);
    1107                 :             :         }
    1108                 :             :     }
    1109                 :             : 
    1110                 :             :     /* all done, clean up memory */
    1111         [ +  + ]:         652 :     if (DatabaseListCxt != NULL)
    1112                 :         182 :         MemoryContextDelete(DatabaseListCxt);
    1113                 :         652 :     MemoryContextDelete(tmpcxt);
    1114                 :         652 :     DatabaseListCxt = newcxt;
    1115                 :         652 :     MemoryContextSwitchTo(oldcxt);
    1116                 :         652 : }
    1117                 :             : 
    1118                 :             : /* qsort comparator for avl_dbase, using adl_score */
    1119                 :             : static int
    1120                 :         804 : db_comparator(const void *a, const void *b)
    1121                 :             : {
    1122                 :        1608 :     return pg_cmp_s32(((const avl_dbase *) a)->adl_score,
    1123                 :         804 :                       ((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
    1138                 :        1572 : 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;
    1148                 :        1572 :     bool        skipit = false;
    1149                 :        1572 :     Oid         retval = InvalidOid;
    1150                 :             :     MemoryContext tmpcxt,
    1151                 :             :                 oldcxt;
    1152                 :             : 
    1153                 :             :     /* return quickly when there are no free workers */
    1154                 :        1572 :     LWLockAcquire(AutovacuumLock, LW_SHARED);
    1155         [ -  + ]:        1572 :     if (!av_worker_available())
    1156                 :             :     {
    1157                 :           0 :         LWLockRelease(AutovacuumLock);
    1158                 :           0 :         return InvalidOid;
    1159                 :             :     }
    1160                 :        1572 :     LWLockRelease(AutovacuumLock);
    1161                 :             : 
    1162                 :             :     /*
    1163                 :             :      * Create and switch to a temporary context to avoid leaking the memory
    1164                 :             :      * allocated for the database list.
    1165                 :             :      */
    1166                 :        1572 :     tmpcxt = AllocSetContextCreate(CurrentMemoryContext,
    1167                 :             :                                    "Autovacuum start worker (tmp)",
    1168                 :             :                                    ALLOCSET_DEFAULT_SIZES);
    1169                 :        1572 :     oldcxt = MemoryContextSwitchTo(tmpcxt);
    1170                 :             : 
    1171                 :             :     /* Get a list of databases */
    1172                 :        1572 :     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                 :             :      */
    1179                 :        1572 :     recentXid = ReadNextTransactionId();
    1180                 :        1572 :     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         [ -  + ]:        1572 :     if (xidForceLimit < FirstNormalTransactionId)
    1184                 :           0 :         xidForceLimit -= FirstNormalTransactionId;
    1185                 :             : 
    1186                 :             :     /* Also determine the oldest datminmxid we will consider. */
    1187                 :        1572 :     recentMulti = ReadNextMultiXactId();
    1188                 :        1572 :     multiForceLimit = recentMulti - MultiXactMemberFreezeThreshold();
    1189         [ -  + ]:        1572 :     if (multiForceLimit < FirstMultiXactId)
    1190                 :           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                 :             :      */
    1213                 :        1572 :     avdb = NULL;
    1214                 :        1572 :     for_xid_wrap = false;
    1215                 :        1572 :     for_multi_wrap = false;
    1216                 :        1572 :     current_time = GetCurrentTimestamp();
    1217   [ +  -  +  +  :        6351 :     foreach(cell, dblist)
                   +  + ]
    1218                 :             :     {
    1219                 :        4779 :         avw_dbase  *tmp = lfirst(cell);
    1220                 :             :         dlist_iter  iter;
    1221                 :             : 
    1222                 :             :         /* Check to see if this one is at risk of wraparound */
    1223         [ +  + ]:        4779 :         if (TransactionIdPrecedes(tmp->adw_frozenxid, xidForceLimit))
    1224                 :             :         {
    1225   [ +  +  +  + ]:        4383 :             if (avdb == NULL ||
    1226                 :        1734 :                 TransactionIdPrecedes(tmp->adw_frozenxid,
    1227                 :             :                                       avdb->adw_frozenxid))
    1228                 :         985 :                 avdb = tmp;
    1229                 :        2649 :             for_xid_wrap = true;
    1230                 :        3702 :             continue;
    1231                 :             :         }
    1232         [ +  + ]:        2130 :         else if (for_xid_wrap)
    1233                 :          88 :             continue;           /* ignore not-at-risk DBs */
    1234         [ -  + ]:        2042 :         else if (MultiXactIdPrecedes(tmp->adw_minmulti, multiForceLimit))
    1235                 :             :         {
    1236   [ #  #  #  # ]:           0 :             if (avdb == NULL ||
    1237                 :           0 :                 MultiXactIdPrecedes(tmp->adw_minmulti, avdb->adw_minmulti))
    1238                 :           0 :                 avdb = tmp;
    1239                 :           0 :             for_multi_wrap = true;
    1240                 :           0 :             continue;
    1241                 :             :         }
    1242         [ -  + ]:        2042 :         else if (for_multi_wrap)
    1243                 :           0 :             continue;           /* ignore not-at-risk DBs */
    1244                 :             : 
    1245                 :             :         /* Find pgstat entry if any */
    1246                 :        2042 :         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                 :             :          */
    1252         [ +  + ]:        2042 :         if (!tmp->adw_entry)
    1253                 :         100 :             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                 :        1942 :         skipit = false;
    1263                 :             : 
    1264   [ +  -  +  + ]:        3974 :         dlist_reverse_foreach(iter, &DatabaseList)
    1265                 :             :         {
    1266                 :        3948 :             avl_dbase  *dbp = dlist_container(avl_dbase, adl_node, iter.cur);
    1267                 :             : 
    1268         [ +  + ]:        3948 :             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                 :             :                  */
    1274         [ +  + ]:        1916 :                 if (!TimestampDifferenceExceeds(dbp->adl_next_worker,
    1275                 :         865 :                                                 current_time, 0) &&
    1276         [ +  - ]:         865 :                     !TimestampDifferenceExceeds(current_time,
    1277                 :             :                                                 dbp->adl_next_worker,
    1278                 :             :                                                 autovacuum_naptime * 1000))
    1279                 :         865 :                     skipit = true;
    1280                 :             : 
    1281                 :        1916 :                 break;
    1282                 :             :             }
    1283                 :             :         }
    1284         [ +  + ]:        1942 :         if (skipit)
    1285                 :         865 :             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                 :             :          */
    1291         [ +  + ]:        1077 :         if (avdb == NULL ||
    1292         [ +  + ]:         424 :             tmp->adw_entry->last_autovac_time < avdb->adw_entry->last_autovac_time)
    1293                 :         794 :             avdb = tmp;
    1294                 :             :     }
    1295                 :             : 
    1296                 :             :     /* Found a database -- process it */
    1297         [ +  + ]:        1572 :     if (avdb != NULL)
    1298                 :             :     {
    1299                 :             :         WorkerInfo  worker;
    1300                 :             :         dlist_node *wptr;
    1301                 :             : 
    1302                 :        1568 :         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                 :             :          */
    1308                 :        1568 :         wptr = dclist_pop_head_node(&AutoVacuumShmem->av_freeWorkers);
    1309                 :             : 
    1310                 :        1568 :         worker = dlist_container(WorkerInfoData, wi_links, wptr);
    1311                 :        1568 :         worker->wi_dboid = avdb->adw_datid;
    1312                 :        1568 :         worker->wi_proc = NULL;
    1313                 :        1568 :         worker->wi_launchtime = GetCurrentTimestamp();
    1314                 :             : 
    1315                 :        1568 :         AutoVacuumShmem->av_startingWorker = worker;
    1316                 :             : 
    1317                 :        1568 :         LWLockRelease(AutovacuumLock);
    1318                 :             : 
    1319                 :        1568 :         SendPostmasterSignal(PMSIGNAL_START_AUTOVAC_WORKER);
    1320                 :             : 
    1321                 :        1568 :         retval = avdb->adw_datid;
    1322                 :             :     }
    1323         [ -  + ]:           4 :     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                 :             :          */
    1329                 :           0 :         rebuild_database_list(InvalidOid);
    1330                 :             :     }
    1331                 :             : 
    1332                 :        1572 :     MemoryContextSwitchTo(oldcxt);
    1333                 :        1572 :     MemoryContextDelete(tmpcxt);
    1334                 :             : 
    1335                 :        1572 :     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
    1350                 :        1572 : launch_worker(TimestampTz now)
    1351                 :             : {
    1352                 :             :     Oid         dbid;
    1353                 :             :     dlist_iter  iter;
    1354                 :             : 
    1355                 :        1572 :     dbid = do_start_worker();
    1356         [ +  + ]:        1572 :     if (OidIsValid(dbid))
    1357                 :             :     {
    1358                 :        1568 :         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                 :             :          */
    1364   [ +  -  +  + ]:        3221 :         dlist_foreach(iter, &DatabaseList)
    1365                 :             :         {
    1366                 :        3206 :             avl_dbase  *avdb = dlist_container(avl_dbase, adl_node, iter.cur);
    1367                 :             : 
    1368         [ +  + ]:        3206 :             if (avdb->adl_datid == dbid)
    1369                 :             :             {
    1370                 :        1553 :                 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                 :             :                  */
    1376                 :        1553 :                 avdb->adl_next_worker =
    1377                 :        1553 :                     TimestampTzPlusMilliseconds(now, autovacuum_naptime * 1000);
    1378                 :             : 
    1379                 :        1553 :                 dlist_move_head(&DatabaseList, iter.cur);
    1380                 :        1553 :                 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                 :             :          */
    1391         [ +  + ]:        1568 :         if (!found)
    1392                 :          15 :             rebuild_database_list(dbid);
    1393                 :             :     }
    1394                 :        1572 : }
    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
    1402                 :           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
    1409                 :        3130 : avl_sigusr2_handler(SIGNAL_ARGS)
    1410                 :             : {
    1411                 :        3130 :     got_SIGUSR2 = true;
    1412                 :        3130 :     SetLatch(MyLatch);
    1413                 :        3130 : }
    1414                 :             : 
    1415                 :             : 
    1416                 :             : /********************************************************************
    1417                 :             :  *                    AUTOVACUUM WORKER CODE
    1418                 :             :  ********************************************************************/
    1419                 :             : 
    1420                 :             : /*
    1421                 :             :  * Main entry point for autovacuum worker processes.
    1422                 :             :  */
    1423                 :             : void
    1424                 :        1573 : AutoVacWorkerMain(const void *startup_data, size_t startup_data_len)
    1425                 :             : {
    1426                 :             :     sigjmp_buf  local_sigjmp_buf;
    1427                 :             :     Oid         dbid;
    1428                 :             : 
    1429                 :             :     Assert(startup_data_len == 0);
    1430                 :             : 
    1431                 :             :     /* Release postmaster's working memory context */
    1432         [ +  - ]:        1573 :     if (PostmasterContext)
    1433                 :             :     {
    1434                 :        1573 :         MemoryContextDelete(PostmasterContext);
    1435                 :        1573 :         PostmasterContext = NULL;
    1436                 :             :     }
    1437                 :             : 
    1438                 :        1573 :     init_ps_display(NULL);
    1439                 :             : 
    1440                 :             :     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                 :             :      */
    1447                 :        1573 :     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                 :             :      */
    1453                 :        1573 :     pqsignal(SIGINT, StatementCancelHandler);
    1454                 :        1573 :     pqsignal(SIGTERM, die);
    1455                 :             :     /* SIGQUIT handler was already set up by InitPostmasterChild */
    1456                 :             : 
    1457                 :        1573 :     InitializeTimeouts();       /* establishes SIGALRM handler */
    1458                 :             : 
    1459                 :        1573 :     pqsignal(SIGPIPE, PG_SIG_IGN);
    1460                 :        1573 :     pqsignal(SIGUSR1, procsignal_sigusr1_handler);
    1461                 :        1573 :     pqsignal(SIGUSR2, PG_SIG_IGN);
    1462                 :        1573 :     pqsignal(SIGFPE, FloatExceptionHandler);
    1463                 :        1573 :     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                 :             :      */
    1469                 :        1573 :     InitProcess();
    1470                 :             : 
    1471                 :             :     /* Early initialization */
    1472                 :        1573 :     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                 :             :      */
    1487         [ -  + ]:        1573 :     if (sigsetjmp(local_sigjmp_buf, 1) != 0)
    1488                 :             :     {
    1489                 :             :         /* since not using PG_TRY, must reset error stack by hand */
    1490                 :           0 :         error_context_stack = NULL;
    1491                 :             : 
    1492                 :             :         /* Prevents interrupts while cleaning up */
    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) */
    1507                 :        1573 :     PG_exception_stack = &local_sigjmp_buf;
    1508                 :             : 
    1509                 :        1573 :     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                 :             :      */
    1517                 :        1573 :     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                 :             :      */
    1524                 :        1573 :     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                 :             :      */
    1530                 :        1573 :     SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
    1531                 :        1573 :     SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
    1532                 :        1573 :     SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
    1533                 :        1573 :     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                 :             :      */
    1541                 :        1573 :     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                 :             :      */
    1549         [ +  - ]:        1573 :     if (synchronous_commit > SYNCHRONOUS_COMMIT_LOCAL_FLUSH)
    1550                 :        1573 :         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                 :             :      */
    1557                 :        1573 :     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                 :             :      */
    1562                 :        1573 :     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                 :             :      */
    1570         [ +  - ]:        1573 :     if (AutoVacuumShmem->av_startingWorker != NULL)
    1571                 :             :     {
    1572                 :        1573 :         MyWorkerInfo = AutoVacuumShmem->av_startingWorker;
    1573                 :        1573 :         dbid = MyWorkerInfo->wi_dboid;
    1574                 :        1573 :         MyWorkerInfo->wi_proc = MyProc;
    1575                 :             : 
    1576                 :             :         /* insert into the running list */
    1577                 :        1573 :         dlist_push_head(&AutoVacuumShmem->av_runningWorkers,
    1578                 :        1573 :                         &MyWorkerInfo->wi_links);
    1579                 :             : 
    1580                 :             :         /*
    1581                 :             :          * remove from the "starting" pointer, so that the launcher can start
    1582                 :             :          * a new worker if required
    1583                 :             :          */
    1584                 :        1573 :         AutoVacuumShmem->av_startingWorker = NULL;
    1585                 :        1573 :         LWLockRelease(AutovacuumLock);
    1586                 :             : 
    1587                 :        1573 :         on_shmem_exit(FreeWorkerInfo, 0);
    1588                 :             : 
    1589                 :             :         /* wake up the launcher */
    1590         [ +  - ]:        1573 :         if (AutoVacuumShmem->av_launcherpid != 0)
    1591                 :        1573 :             kill(AutoVacuumShmem->av_launcherpid, SIGUSR2);
    1592                 :             :     }
    1593                 :             :     else
    1594                 :             :     {
    1595                 :             :         /* no worker entry for me, go away */
    1596         [ #  # ]:           0 :         elog(WARNING, "autovacuum worker started without a worker entry");
    1597                 :           0 :         dbid = InvalidOid;
    1598                 :           0 :         LWLockRelease(AutovacuumLock);
    1599                 :             :     }
    1600                 :             : 
    1601         [ +  - ]:        1573 :     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                 :        1573 :         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                 :             :          */
    1623                 :        1573 :         InitPostgres(NULL, dbid, NULL, InvalidOid,
    1624                 :             :                      INIT_PG_OVERRIDE_ALLOW_CONNS,
    1625                 :             :                      dbname);
    1626                 :        1572 :         SetProcessingMode(NormalProcessing);
    1627                 :        1572 :         set_ps_display(dbname);
    1628         [ +  + ]:        1572 :         ereport(DEBUG1,
    1629                 :             :                 (errmsg_internal("autovacuum: processing database \"%s\"", dbname)));
    1630                 :             : 
    1631         [ -  + ]:        1572 :         if (PostAuthDelay)
    1632                 :           0 :             pg_usleep(PostAuthDelay * 1000000L);
    1633                 :             : 
    1634                 :             :         /* And do an appropriate amount of work */
    1635                 :        1572 :         recentXid = ReadNextTransactionId();
    1636                 :        1572 :         recentMulti = ReadNextMultiXactId();
    1637                 :        1572 :         do_autovacuum();
    1638                 :             :     }
    1639                 :             : 
    1640                 :             :     /* All done, go away */
    1641                 :        1569 :     proc_exit(0);
    1642                 :             : }
    1643                 :             : 
    1644                 :             : /*
    1645                 :             :  * Return a WorkerInfo to the free list
    1646                 :             :  */
    1647                 :             : static void
    1648                 :        1573 : FreeWorkerInfo(int code, Datum arg)
    1649                 :             : {
    1650         [ +  - ]:        1573 :     if (MyWorkerInfo != NULL)
    1651                 :             :     {
    1652                 :        1573 :         LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    1653                 :             : 
    1654                 :        1573 :         dlist_delete(&MyWorkerInfo->wi_links);
    1655                 :        1573 :         MyWorkerInfo->wi_dboid = InvalidOid;
    1656                 :        1573 :         MyWorkerInfo->wi_tableoid = InvalidOid;
    1657                 :        1573 :         MyWorkerInfo->wi_sharedrel = false;
    1658                 :        1573 :         MyWorkerInfo->wi_proc = NULL;
    1659                 :        1573 :         MyWorkerInfo->wi_launchtime = 0;
    1660                 :        1573 :         pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
    1661                 :        1573 :         dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
    1662                 :        1573 :                          &MyWorkerInfo->wi_links);
    1663                 :             :         /* not mine anymore */
    1664                 :        1573 :         MyWorkerInfo = NULL;
    1665                 :             : 
    1666                 :             :         /*
    1667                 :             :          * now that we're inactive, cause a rebalancing of the surviving
    1668                 :             :          * workers
    1669                 :             :          */
    1670                 :        1573 :         AutoVacuumShmem->av_signal[AutoVacRebalance] = true;
    1671                 :        1573 :         LWLockRelease(AutovacuumLock);
    1672                 :             :     }
    1673                 :        1573 : }
    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
    1682                 :      228497 : VacuumUpdateCosts(void)
    1683                 :             : {
    1684         [ +  + ]:      228497 :     if (MyWorkerInfo)
    1685                 :             :     {
    1686         [ -  + ]:      219529 :         if (av_storage_param_cost_delay >= 0)
    1687                 :           0 :             vacuum_cost_delay = av_storage_param_cost_delay;
    1688         [ +  - ]:      219529 :         else if (autovacuum_vac_cost_delay >= 0)
    1689                 :      219529 :             vacuum_cost_delay = autovacuum_vac_cost_delay;
    1690                 :             :         else
    1691                 :             :             /* fall back to VacuumCostDelay */
    1692                 :           0 :             vacuum_cost_delay = VacuumCostDelay;
    1693                 :             : 
    1694                 :      219529 :         AutoVacuumUpdateCostLimit();
    1695                 :             :     }
    1696                 :             :     else
    1697                 :             :     {
    1698                 :             :         /* Must be explicit VACUUM or ANALYZE or parallel autovacuum worker */
    1699                 :        8968 :         vacuum_cost_delay = VacuumCostDelay;
    1700                 :        8968 :         vacuum_cost_limit = VacuumCostLimit;
    1701                 :             :     }
    1702                 :             : 
    1703                 :             :     /*
    1704                 :             :      * If configuration changes are allowed to impact VacuumCostActive, make
    1705                 :             :      * sure it is updated.
    1706                 :             :      */
    1707         [ +  - ]:      228497 :     if (VacuumFailsafeActive)
    1708                 :             :         Assert(!VacuumCostActive);
    1709         [ +  + ]:      228497 :     else if (vacuum_cost_delay > 0)
    1710                 :      219534 :         VacuumCostActive = true;
    1711                 :             :     else
    1712                 :             :     {
    1713                 :        8963 :         VacuumCostActive = false;
    1714                 :        8963 :         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                 :             :      */
    1721   [ +  +  +  + ]:      228497 :     if (MyWorkerInfo && message_level_is_interesting(DEBUG2))
    1722                 :             :     {
    1723                 :             :         Oid         dboid,
    1724                 :             :                     tableoid;
    1725                 :             : 
    1726                 :             :         Assert(!LWLockHeldByMe(AutovacuumLock));
    1727                 :             : 
    1728                 :          29 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
    1729                 :          29 :         dboid = MyWorkerInfo->wi_dboid;
    1730                 :          29 :         tableoid = MyWorkerInfo->wi_tableoid;
    1731                 :          29 :         LWLockRelease(AutovacuumLock);
    1732                 :             : 
    1733   [ +  -  -  +  :          29 :         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                 :             :     }
    1740                 :      228497 : }
    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
    1751                 :      224182 : AutoVacuumUpdateCostLimit(void)
    1752                 :             : {
    1753         [ +  + ]:      224182 :     if (!MyWorkerInfo)
    1754                 :          16 :         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                 :             : 
    1761         [ -  + ]:      224166 :     if (av_storage_param_cost_limit > 0)
    1762                 :           0 :         vacuum_cost_limit = av_storage_param_cost_limit;
    1763                 :             :     else
    1764                 :             :     {
    1765                 :             :         int         nworkers_for_balance;
    1766                 :             : 
    1767         [ +  + ]:      224166 :         if (autovacuum_vac_cost_limit > 0)
    1768                 :          12 :             vacuum_cost_limit = autovacuum_vac_cost_limit;
    1769                 :             :         else
    1770                 :      224154 :             vacuum_cost_limit = VacuumCostLimit;
    1771                 :             : 
    1772                 :             :         /* Only balance limit if no cost-related storage parameters specified */
    1773         [ -  + ]:      224166 :         if (pg_atomic_unlocked_test_flag(&MyWorkerInfo->wi_dobalance))
    1774                 :           0 :             return;
    1775                 :             : 
    1776                 :             :         Assert(vacuum_cost_limit > 0);
    1777                 :             : 
    1778                 :      224166 :         nworkers_for_balance = pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
    1779                 :             : 
    1780                 :             :         /* There is at least 1 autovac worker (this worker) */
    1781         [ -  + ]:      224166 :         if (nworkers_for_balance <= 0)
    1782         [ #  # ]:           0 :             elog(ERROR, "nworkers_for_balance must be > 0");
    1783                 :             : 
    1784                 :      224166 :         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                 :      111289 : autovac_recalculate_workers_for_balance(void)
    1798                 :             : {
    1799                 :             :     dlist_iter  iter;
    1800                 :             :     int         orig_nworkers_for_balance;
    1801                 :      111289 :     int         nworkers_for_balance = 0;
    1802                 :             : 
    1803                 :             :     Assert(LWLockHeldByMe(AutovacuumLock));
    1804                 :             : 
    1805                 :      111289 :     orig_nworkers_for_balance =
    1806                 :      111289 :         pg_atomic_read_u32(&AutoVacuumShmem->av_nworkersForBalance);
    1807                 :             : 
    1808   [ +  -  +  + ]:      337837 :     dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
    1809                 :             :     {
    1810                 :      226548 :         WorkerInfo  worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
    1811                 :             : 
    1812   [ +  -  +  + ]:      453096 :         if (worker->wi_proc == NULL ||
    1813                 :      226548 :             pg_atomic_unlocked_test_flag(&worker->wi_dobalance))
    1814                 :        4463 :             continue;
    1815                 :             : 
    1816                 :      222085 :         nworkers_for_balance++;
    1817                 :             :     }
    1818                 :             : 
    1819         [ +  + ]:      111289 :     if (nworkers_for_balance != orig_nworkers_for_balance)
    1820                 :        2022 :         pg_atomic_write_u32(&AutoVacuumShmem->av_nworkersForBalance,
    1821                 :             :                             nworkers_for_balance);
    1822                 :      111289 : }
    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                 :        2225 : get_database_list(void)
    1838                 :             : {
    1839                 :        2225 :     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 */
    1846                 :        2225 :     resultcxt = CurrentMemoryContext;
    1847                 :             : 
    1848                 :             :     /*
    1849                 :             :      * Start a transaction so we can access pg_database.
    1850                 :             :      */
    1851                 :        2225 :     StartTransactionCommand();
    1852                 :             : 
    1853                 :        2225 :     rel = table_open(DatabaseRelationId, AccessShareLock);
    1854                 :        2225 :     scan = table_beginscan_catalog(rel, 0, NULL);
    1855                 :             : 
    1856         [ +  + ]:        9275 :     while (HeapTupleIsValid(tup = heap_getnext(scan, ForwardScanDirection)))
    1857                 :             :     {
    1858                 :        7050 :         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                 :             :          */
    1866         [ +  + ]:        7050 :         if (database_is_invalid_form(pgdatabase))
    1867                 :             :         {
    1868         [ -  + ]:           5 :             elog(DEBUG2,
    1869                 :             :                  "autovacuum: skipping invalid database \"%s\"",
    1870                 :             :                  NameStr(pgdatabase->datname));
    1871                 :           5 :             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                 :             :          */
    1880                 :        7045 :         oldcxt = MemoryContextSwitchTo(resultcxt);
    1881                 :             : 
    1882                 :        7045 :         avdb = palloc_object(avw_dbase);
    1883                 :             : 
    1884                 :        7045 :         avdb->adw_datid = pgdatabase->oid;
    1885                 :        7045 :         avdb->adw_name = pstrdup(NameStr(pgdatabase->datname));
    1886                 :        7045 :         avdb->adw_frozenxid = pgdatabase->datfrozenxid;
    1887                 :        7045 :         avdb->adw_minmulti = pgdatabase->datminmxid;
    1888                 :             :         /* this gets set later: */
    1889                 :        7045 :         avdb->adw_entry = NULL;
    1890                 :             : 
    1891                 :        7045 :         dblist = lappend(dblist, avdb);
    1892                 :        7045 :         MemoryContextSwitchTo(oldcxt);
    1893                 :             :     }
    1894                 :             : 
    1895                 :        2224 :     table_endscan(scan);
    1896                 :        2224 :     table_close(rel, AccessShareLock);
    1897                 :             : 
    1898                 :        2224 :     CommitTransactionCommand();
    1899                 :             : 
    1900                 :             :     /* Be sure to restore caller's memory context */
    1901                 :        2224 :     MemoryContextSwitchTo(resultcxt);
    1902                 :             : 
    1903                 :        2224 :     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
    1911                 :      112520 : TableToProcessComparator(const ListCell *a, const ListCell *b)
    1912                 :             : {
    1913                 :      112520 :     TableToProcess *t1 = (TableToProcess *) lfirst(a);
    1914                 :      112520 :     TableToProcess *t2 = (TableToProcess *) lfirst(b);
    1915                 :             : 
    1916         [ +  + ]:      112520 :     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
    1926                 :        1572 : do_autovacuum(void)
    1927                 :             : {
    1928                 :             :     Relation    classRel;
    1929                 :             :     HeapTuple   tuple;
    1930                 :             :     TableScanDesc relScan;
    1931                 :             :     Form_pg_database dbForm;
    1932                 :        1572 :     List       *tables_to_process = NIL;
    1933                 :        1572 :     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;
    1941                 :        1572 :     bool        did_vacuum = false;
    1942                 :        1572 :     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                 :             :      */
    1950                 :        1572 :     AutovacMemCxt = AllocSetContextCreate(TopMemoryContext,
    1951                 :             :                                           "Autovacuum worker",
    1952                 :             :                                           ALLOCSET_DEFAULT_SIZES);
    1953                 :        1572 :     MemoryContextSwitchTo(AutovacMemCxt);
    1954                 :             : 
    1955                 :             :     /* Start a transaction so our commands have one to play into. */
    1956                 :        1572 :     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                 :             :      */
    1962                 :        1572 :     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                 :             :      */
    1969                 :        1571 :     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                 :             :      */
    1976                 :        1571 :     tuple = SearchSysCache1(DATABASEOID, ObjectIdGetDatum(MyDatabaseId));
    1977         [ -  + ]:        1571 :     if (!HeapTupleIsValid(tuple))
    1978         [ #  # ]:           0 :         elog(ERROR, "cache lookup failed for database %u", MyDatabaseId);
    1979                 :        1571 :     dbForm = (Form_pg_database) GETSTRUCT(tuple);
    1980                 :             : 
    1981   [ +  +  -  + ]:        1571 :     if (dbForm->datistemplate || !dbForm->datallowconn)
    1982                 :             :     {
    1983                 :         497 :         default_freeze_min_age = 0;
    1984                 :         497 :         default_freeze_table_age = 0;
    1985                 :         497 :         default_multixact_freeze_min_age = 0;
    1986                 :         497 :         default_multixact_freeze_table_age = 0;
    1987                 :             :     }
    1988                 :             :     else
    1989                 :             :     {
    1990                 :        1074 :         default_freeze_min_age = vacuum_freeze_min_age;
    1991                 :        1074 :         default_freeze_table_age = vacuum_freeze_table_age;
    1992                 :        1074 :         default_multixact_freeze_min_age = vacuum_multixact_freeze_min_age;
    1993                 :        1074 :         default_multixact_freeze_table_age = vacuum_multixact_freeze_table_age;
    1994                 :             :     }
    1995                 :             : 
    1996                 :        1571 :     ReleaseSysCache(tuple);
    1997                 :             : 
    1998                 :             :     /* StartTransactionCommand changed elsewhere */
    1999                 :        1571 :     MemoryContextSwitchTo(AutovacMemCxt);
    2000                 :             : 
    2001                 :        1571 :     classRel = table_open(RelationRelationId, AccessShareLock);
    2002                 :             : 
    2003                 :             :     /* create a copy so we can use it after closing pg_class */
    2004                 :        1571 :     pg_class_desc = CreateTupleDescCopy(RelationGetDescr(classRel));
    2005                 :             : 
    2006                 :             :     /* create hash table for toast <-> main relid mapping */
    2007                 :        1571 :     ctl.keysize = sizeof(Oid);
    2008                 :        1571 :     ctl.entrysize = sizeof(av_relation);
    2009                 :             : 
    2010                 :        1571 :     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                 :             :      */
    2029                 :        1571 :     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                 :             :      */
    2035         [ +  + ]:      724891 :     while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
    2036                 :             :     {
    2037                 :      723320 :         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                 :             : 
    2045         [ +  + ]:      723320 :         if (classForm->relkind != RELKIND_RELATION &&
    2046         [ +  + ]:      604692 :             classForm->relkind != RELKIND_MATVIEW)
    2047                 :      604646 :             continue;
    2048                 :             : 
    2049                 :      118681 :         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                 :             :          */
    2055         [ +  + ]:      118681 :         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                 :             :              */
    2062         [ -  + ]:           7 :             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                 :             :                  */
    2071                 :           0 :                 orphan_oids = lappend_oid(orphan_oids, relid);
    2072                 :             :             }
    2073                 :           7 :             continue;
    2074                 :             :         }
    2075                 :             : 
    2076                 :             :         /* Fetch reloptions and the pgstat entry for this table */
    2077                 :      118674 :         relopts = extract_autovac_opts(tuple, pg_class_desc);
    2078                 :             : 
    2079                 :             :         /* Check if it needs vacuum or analyze */
    2080                 :      118674 :         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 */
    2087   [ +  +  +  + ]:      118674 :         if (dovacuum || doanalyze)
    2088                 :             :         {
    2089                 :       70420 :             TableToProcess *table = palloc_object(TableToProcess);
    2090                 :             : 
    2091                 :       70420 :             table->oid = relid;
    2092                 :       70420 :             table->score = scores.max;
    2093                 :       70420 :             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                 :             :          */
    2101         [ +  + ]:      118674 :         if (OidIsValid(classForm->reltoastrelid))
    2102                 :             :         {
    2103                 :             :             av_relation *hentry;
    2104                 :             :             bool        found;
    2105                 :             : 
    2106                 :      133556 :             hentry = hash_search(table_toast_map,
    2107                 :       66778 :                                  &classForm->reltoastrelid,
    2108                 :             :                                  HASH_ENTER, &found);
    2109                 :             : 
    2110         [ +  - ]:       66778 :             if (!found)
    2111                 :             :             {
    2112                 :             :                 /* hash_search already filled in the key */
    2113                 :       66778 :                 hentry->ar_relid = relid;
    2114                 :       66778 :                 hentry->ar_hasrelopts = false;
    2115         [ +  + ]:       66778 :                 if (relopts != NULL)
    2116                 :             :                 {
    2117                 :        1451 :                     hentry->ar_hasrelopts = true;
    2118                 :        1451 :                     memcpy(&hentry->ar_reloptions, relopts,
    2119                 :             :                            sizeof(AutoVacOpts));
    2120                 :             :                 }
    2121                 :             :             }
    2122                 :             :         }
    2123                 :             : 
    2124                 :             :         /* Release stuff to avoid per-relation leakage */
    2125         [ +  + ]:      118674 :         if (relopts)
    2126                 :        1498 :             pfree(relopts);
    2127                 :             :     }
    2128                 :             : 
    2129                 :        1571 :     table_endscan(relScan);
    2130                 :             : 
    2131                 :             :     /* second pass: check TOAST tables */
    2132                 :        1571 :     ScanKeyInit(&key,
    2133                 :             :                 Anum_pg_class_relkind,
    2134                 :             :                 BTEqualStrategyNumber, F_CHAREQ,
    2135                 :             :                 CharGetDatum(RELKIND_TOASTVALUE));
    2136                 :             : 
    2137                 :        1571 :     relScan = table_beginscan_catalog(classRel, 1, &key);
    2138         [ +  + ]:       68350 :     while ((tuple = heap_getnext(relScan, ForwardScanDirection)) != NULL)
    2139                 :             :     {
    2140                 :       66779 :         Form_pg_class classForm = (Form_pg_class) GETSTRUCT(tuple);
    2141                 :             :         Oid         relid;
    2142                 :             :         AutoVacOpts *relopts;
    2143                 :       66779 :         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                 :             :          */
    2152         [ +  + ]:       66779 :         if (classForm->relpersistence == RELPERSISTENCE_TEMP)
    2153                 :           4 :             continue;
    2154                 :             : 
    2155                 :       66775 :         relid = classForm->oid;
    2156                 :             : 
    2157                 :             :         /*
    2158                 :             :          * fetch reloptions -- if this toast table does not have them, try the
    2159                 :             :          * main rel
    2160                 :             :          */
    2161                 :       66775 :         relopts = extract_autovac_opts(tuple, pg_class_desc);
    2162         [ +  + ]:       66775 :         if (relopts)
    2163                 :           2 :             free_relopts = true;
    2164                 :             :         else
    2165                 :             :         {
    2166                 :             :             av_relation *hentry;
    2167                 :             :             bool        found;
    2168                 :             : 
    2169                 :       66773 :             hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
    2170   [ +  -  +  + ]:       66773 :             if (found && hentry->ar_hasrelopts)
    2171                 :        1449 :                 relopts = &hentry->ar_reloptions;
    2172                 :             :         }
    2173                 :             : 
    2174                 :       66775 :         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 */
    2181         [ +  + ]:       66775 :         if (dovacuum)
    2182                 :             :         {
    2183                 :       39744 :             TableToProcess *table = palloc_object(TableToProcess);
    2184                 :             : 
    2185                 :       39744 :             table->oid = relid;
    2186                 :       39744 :             table->score = scores.max;
    2187                 :       39744 :             tables_to_process = lappend(tables_to_process, table);
    2188                 :             :         }
    2189                 :             : 
    2190                 :             :         /* Release stuff to avoid leakage */
    2191         [ +  + ]:       66775 :         if (free_relopts)
    2192                 :           2 :             pfree(relopts);
    2193                 :             :     }
    2194                 :             : 
    2195                 :        1571 :     table_endscan(relScan);
    2196                 :        1571 :     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                 :             :      */
    2207   [ -  +  -  -  :        1571 :     foreach(cell, orphan_oids)
                   -  + ]
    2208                 :             :     {
    2209                 :           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                 :             : 
    2253         [ #  # ]:           0 :         if (checkTempNamespaceStatus(classForm->relnamespace) != TEMP_NAMESPACE_IDLE)
    2254                 :             :         {
    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                 :             :          */
    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 */
    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                 :             :          */
    2288                 :           0 :         PushActiveSnapshot(GetTransactionSnapshot());
    2289                 :             : 
    2290                 :           0 :         object.classId = RelationRelationId;
    2291                 :           0 :         object.objectId = relid;
    2292                 :           0 :         object.objectSubId = 0;
    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                 :             :          */
    2302                 :           0 :         PopActiveSnapshot();
    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                 :             :      */
    2316         [ -  + ]:        1571 :     if (autovacuum_freeze_score_weight != 0.0 ||
    2317         [ #  # ]:           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)
    2321                 :        1571 :         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                 :             :      */
    2338                 :        1571 :     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                 :             :      */
    2344                 :        1571 :     PortalContext = AllocSetContextCreate(AutovacMemCxt,
    2345                 :             :                                           "Autovacuum Portal",
    2346                 :             :                                           ALLOCSET_DEFAULT_SIZES);
    2347                 :             : 
    2348                 :             :     /*
    2349                 :             :      * Perform operations on collected tables.
    2350                 :             :      */
    2351   [ +  +  +  +  :      113302 :     foreach_ptr(TableToProcess, table, tables_to_process)
                   +  + ]
    2352                 :             :     {
    2353                 :      110164 :         Oid         relid = table->oid;
    2354                 :             :         HeapTuple   classTup;
    2355                 :             :         autovac_table *tab;
    2356                 :             :         bool        isshared;
    2357                 :             :         bool        skipit;
    2358                 :             :         dlist_iter  iter;
    2359                 :             : 
    2360         [ -  + ]:      110164 :         CHECK_FOR_INTERRUPTS();
    2361                 :             : 
    2362                 :             :         /*
    2363                 :             :          * Check for config changes before processing each collected table.
    2364                 :             :          */
    2365         [ -  + ]:      110164 :         if (ConfigReloadPending)
    2366                 :             :         {
    2367                 :           0 :             ConfigReloadPending = false;
    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                 :             :          */
    2386                 :      110164 :         classTup = SearchSysCache1(RELOID, ObjectIdGetDatum(relid));
    2387         [ -  + ]:      110164 :         if (!HeapTupleIsValid(classTup))
    2388                 :         400 :             continue;           /* somebody deleted the rel, forget it */
    2389                 :      110164 :         isshared = ((Form_pg_class) GETSTRUCT(classTup))->relisshared;
    2390                 :      110164 :         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                 :             :          */
    2397                 :      110164 :         LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
    2398                 :      110164 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
    2399                 :             : 
    2400                 :             :         /*
    2401                 :             :          * Check whether the table is being vacuumed concurrently by another
    2402                 :             :          * worker.
    2403                 :             :          */
    2404                 :      110164 :         skipit = false;
    2405   [ +  -  +  + ]:      336007 :         dlist_foreach(iter, &AutoVacuumShmem->av_runningWorkers)
    2406                 :             :         {
    2407                 :      226225 :             WorkerInfo  worker = dlist_container(WorkerInfoData, wi_links, iter.cur);
    2408                 :             : 
    2409                 :             :             /* ignore myself */
    2410         [ +  + ]:      226225 :             if (worker == MyWorkerInfo)
    2411                 :      109989 :                 continue;
    2412                 :             : 
    2413                 :             :             /* ignore workers in other databases (unless table is shared) */
    2414   [ +  +  -  + ]:      116236 :             if (!worker->wi_sharedrel && worker->wi_dboid != MyDatabaseId)
    2415                 :           0 :                 continue;
    2416                 :             : 
    2417         [ +  + ]:      116236 :             if (worker->wi_tableoid == relid)
    2418                 :             :             {
    2419                 :         382 :                 skipit = true;
    2420                 :         382 :                 found_concurrent_worker = true;
    2421                 :         382 :                 break;
    2422                 :             :             }
    2423                 :             :         }
    2424                 :      110164 :         LWLockRelease(AutovacuumLock);
    2425         [ +  + ]:      110164 :         if (skipit)
    2426                 :             :         {
    2427                 :         382 :             LWLockRelease(AutovacuumScheduleLock);
    2428                 :         382 :             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                 :             :          */
    2437                 :      109782 :         MyWorkerInfo->wi_tableoid = relid;
    2438                 :      109782 :         MyWorkerInfo->wi_sharedrel = isshared;
    2439                 :      109782 :         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                 :             :          */
    2447                 :      109782 :         MemoryContextSwitchTo(AutovacMemCxt);
    2448                 :      109782 :         tab = table_recheck_autovac(relid, table_toast_map, pg_class_desc,
    2449                 :             :                                     effective_multixact_freeze_max_age);
    2450         [ +  + ]:      109782 :         if (tab == NULL)
    2451                 :             :         {
    2452                 :             :             /* someone else vacuumed the table, or it went away */
    2453                 :          18 :             LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
    2454                 :          18 :             MyWorkerInfo->wi_tableoid = InvalidOid;
    2455                 :          18 :             MyWorkerInfo->wi_sharedrel = false;
    2456                 :          18 :             LWLockRelease(AutovacuumScheduleLock);
    2457                 :          18 :             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                 :             :          */
    2465                 :      109764 :         av_storage_param_cost_delay = tab->at_storage_param_vac_cost_delay;
    2466                 :      109764 :         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         [ +  - ]:      109764 :         if (tab->at_dobalance)
    2473                 :      109764 :             pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
    2474                 :             :         else
    2475                 :           0 :             pg_atomic_clear_flag(&MyWorkerInfo->wi_dobalance);
    2476                 :             : 
    2477                 :      109764 :         LWLockAcquire(AutovacuumLock, LW_SHARED);
    2478                 :      109764 :         autovac_recalculate_workers_for_balance();
    2479                 :      109764 :         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                 :      109764 :         VacuumUpdateCosts();
    2487                 :             : 
    2488                 :             : 
    2489                 :             :         /* clean up memory before each iteration */
    2490                 :      109764 :         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                 :             : 
    2500                 :      109764 :         tab->at_relname = get_rel_name(tab->at_relid);
    2501                 :      109764 :         tab->at_nspname = get_namespace_name(get_rel_namespace(tab->at_relid));
    2502                 :      109764 :         tab->at_datname = get_database_name(MyDatabaseId);
    2503   [ +  -  +  -  :      109764 :         if (!tab->at_relname || !tab->at_nspname || !tab->at_datname)
                   -  + ]
    2504                 :           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                 :             :          */
    2511         [ +  - ]:      109764 :         PG_TRY();
    2512                 :             :         {
    2513                 :             :             /* Use PortalContext for any per-table allocations */
    2514                 :      109764 :             MemoryContextSwitchTo(PortalContext);
    2515                 :             : 
    2516                 :             :             /* have at it */
    2517                 :      109764 :             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                 :             :              */
    2525                 :      109762 :             QueryCancelPending = false;
    2526                 :             :         }
    2527                 :           0 :         PG_CATCH();
    2528                 :             :         {
    2529                 :             :             /*
    2530                 :             :              * Abort the transaction, start a new one, and proceed with the
    2531                 :             :              * next table in our list.
    2532                 :             :              */
    2533                 :           0 :             HOLD_INTERRUPTS();
    2534         [ #  # ]:           0 :             if (tab->at_params.options & VACOPT_VACUUM)
    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();
    2545                 :           0 :             MemoryContextReset(PortalContext);
    2546                 :             : 
    2547                 :             :             /* restart our transaction for the following operations */
    2548                 :           0 :             StartTransactionCommand();
    2549                 :           0 :             RESUME_INTERRUPTS();
    2550                 :             :         }
    2551         [ -  + ]:      109762 :         PG_END_TRY();
    2552                 :             : 
    2553                 :             :         /* Make sure we're back in AutovacMemCxt */
    2554                 :      109762 :         MemoryContextSwitchTo(AutovacMemCxt);
    2555                 :             : 
    2556                 :      109762 :         did_vacuum = true;
    2557                 :             : 
    2558                 :             :         /* ProcGlobal->statusFlags[i] are reset at the next end of xact */
    2559                 :             : 
    2560                 :             :         /* be tidy */
    2561                 :      109762 : deleted:
    2562         [ +  - ]:      109762 :         if (tab->at_datname != NULL)
    2563                 :      109762 :             pfree(tab->at_datname);
    2564         [ +  - ]:      109762 :         if (tab->at_nspname != NULL)
    2565                 :      109762 :             pfree(tab->at_nspname);
    2566         [ +  - ]:      109762 :         if (tab->at_relname != NULL)
    2567                 :      109762 :             pfree(tab->at_relname);
    2568                 :      109762 :         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                 :             :          */
    2577                 :      109762 :         LWLockAcquire(AutovacuumScheduleLock, LW_EXCLUSIVE);
    2578                 :      109762 :         MyWorkerInfo->wi_tableoid = InvalidOid;
    2579                 :      109762 :         MyWorkerInfo->wi_sharedrel = false;
    2580                 :      109762 :         LWLockRelease(AutovacuumScheduleLock);
    2581                 :      109762 :         pg_atomic_test_set_flag(&MyWorkerInfo->wi_dobalance);
    2582                 :             :     }
    2583                 :             : 
    2584                 :        1569 :     list_free_deep(tables_to_process);
    2585                 :             : 
    2586                 :             :     /*
    2587                 :             :      * Perform additional work items, as requested by backends.
    2588                 :             :      */
    2589                 :        1569 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    2590         [ +  + ]:      403233 :     for (i = 0; i < NUM_WORKITEMS; i++)
    2591                 :             :     {
    2592                 :      401664 :         AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
    2593                 :             : 
    2594         [ +  + ]:      401664 :         if (!workitem->avw_used)
    2595                 :      401658 :             continue;
    2596         [ -  + ]:           6 :         if (workitem->avw_active)
    2597                 :           0 :             continue;
    2598         [ -  + ]:           6 :         if (workitem->avw_database != MyDatabaseId)
    2599                 :           0 :             continue;
    2600                 :             : 
    2601                 :             :         /* claim this one, and release lock while performing it */
    2602                 :           6 :         workitem->avw_active = true;
    2603                 :           6 :         LWLockRelease(AutovacuumLock);
    2604                 :             : 
    2605                 :           6 :         PushActiveSnapshot(GetTransactionSnapshot());
    2606                 :           6 :         perform_work_item(workitem);
    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                 :             :          */
    2613         [ -  + ]:           6 :         CHECK_FOR_INTERRUPTS();
    2614         [ -  + ]:           6 :         if (ConfigReloadPending)
    2615                 :             :         {
    2616                 :           0 :             ConfigReloadPending = false;
    2617                 :           0 :             ProcessConfigFile(PGC_SIGHUP);
    2618                 :           0 :             VacuumUpdateCosts();
    2619                 :             :         }
    2620                 :             : 
    2621                 :           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                 :        1569 :     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 */
    2642                 :        1569 :     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                 :             :      */
    2664   [ +  +  +  - ]:        1569 :     if (did_vacuum || !found_concurrent_worker)
    2665                 :        1569 :         vac_update_datfrozenxid();
    2666                 :             : 
    2667                 :             :     /* Finally close out the last transaction. */
    2668                 :        1569 :     CommitTransactionCommand();
    2669                 :        1569 : }
    2670                 :             : 
    2671                 :             : /*
    2672                 :             :  * Execute a previously registered work item.
    2673                 :             :  */
    2674                 :             : static void
    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                 :             :      */
    2691                 :             :     Assert(CurrentMemoryContext == AutovacMemCxt);
    2692                 :             : 
    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)
                   -  + ]
    2697                 :           0 :         goto deleted2;
    2698                 :             : 
    2699                 :           6 :     autovac_report_workitem(workitem, cur_nspname, cur_relname);
    2700                 :             : 
    2701                 :             :     /* clean up memory before each work item */
    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                 :             :      */
    2710         [ +  - ]:           6 :     PG_TRY();
    2711                 :             :     {
    2712                 :             :         /* Use PortalContext for any per-work-item allocations */
    2713                 :           6 :         MemoryContextSwitchTo(PortalContext);
    2714                 :             : 
    2715                 :             :         /*
    2716                 :             :          * Have at it.  Functions called here are responsible for any required
    2717                 :             :          * user switch and sandbox.
    2718                 :             :          */
    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;
    2726                 :           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                 :             :          */
    2738                 :           6 :         QueryCancelPending = false;
    2739                 :             :     }
    2740                 :           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();
    2754                 :           0 :         MemoryContextReset(PortalContext);
    2755                 :             : 
    2756                 :             :         /* restart our transaction for the following operations */
    2757                 :           0 :         StartTransactionCommand();
    2758                 :           0 :         RESUME_INTERRUPTS();
    2759                 :             :     }
    2760         [ -  + ]:           6 :     PG_END_TRY();
    2761                 :             : 
    2762                 :             :     /* Make sure we're back in AutovacMemCxt */
    2763                 :           6 :     MemoryContextSwitchTo(AutovacMemCxt);
    2764                 :             : 
    2765                 :             :     /* We intentionally do not set did_vacuum here */
    2766                 :             : 
    2767                 :             :     /* be tidy */
    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 *
    2789                 :      295231 : extract_autovac_opts(HeapTuple tup, TupleDesc pg_class_desc)
    2790                 :             : {
    2791                 :             :     bytea      *relopts;
    2792                 :             :     AutoVacOpts *av;
    2793                 :             : 
    2794                 :             :     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                 :             : 
    2798                 :      295231 :     relopts = extractRelOptions(tup, pg_class_desc, NULL);
    2799         [ +  + ]:      295231 :     if (relopts == NULL)
    2800                 :      292606 :         return NULL;
    2801                 :             : 
    2802                 :        2625 :     av = palloc_object(AutoVacOpts);
    2803                 :        2625 :     memcpy(av, &(((StdRdOptions *) relopts)->autovacuum), sizeof(AutoVacOpts));
    2804                 :        2625 :     pfree(relopts);
    2805                 :             : 
    2806                 :        2625 :     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                 :      109782 : 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;
    2827                 :      109782 :     autovac_table *tab = NULL;
    2828                 :             :     bool        wraparound;
    2829                 :             :     AutoVacOpts *avopts;
    2830                 :      109782 :     bool        free_avopts = false;
    2831                 :             :     AutoVacuumScores scores;
    2832                 :             : 
    2833                 :             :     /* fetch the relation's relcache entry */
    2834                 :      109782 :     classTup = SearchSysCacheCopy1(RELOID, ObjectIdGetDatum(relid));
    2835         [ -  + ]:      109782 :     if (!HeapTupleIsValid(classTup))
    2836                 :           0 :         return NULL;
    2837                 :      109782 :     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                 :             :      */
    2843                 :      109782 :     avopts = extract_autovac_opts(classTup, pg_class_desc);
    2844         [ +  + ]:      109782 :     if (avopts)
    2845                 :        1125 :         free_avopts = true;
    2846   [ +  +  +  - ]:      108657 :     else if (classForm->relkind == RELKIND_TOASTVALUE &&
    2847                 :             :              table_toast_map != NULL)
    2848                 :             :     {
    2849                 :             :         av_relation *hentry;
    2850                 :             :         bool        found;
    2851                 :             : 
    2852                 :       39567 :         hentry = hash_search(table_toast_map, &relid, HASH_FIND, &found);
    2853   [ +  -  +  + ]:       39567 :         if (found && hentry->ar_hasrelopts)
    2854                 :        1109 :             avopts = &hentry->ar_reloptions;
    2855                 :             :     }
    2856                 :             : 
    2857                 :      109782 :     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 */
    2864   [ +  +  +  + ]:      109782 :     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 */
    2881         [ +  + ]:        2234 :         log_vacuum_min_duration = (avopts && avopts->log_vacuum_min_duration >= 0)
    2882                 :             :             ? avopts->log_vacuum_min_duration
    2883         [ +  + ]:      111998 :             : Log_autovacuum_min_duration;
    2884                 :             : 
    2885                 :             :         /* -1 in autovac setting means use log_autoanalyze_min_duration */
    2886         [ -  + ]:        2234 :         log_analyze_min_duration = (avopts && avopts->log_analyze_min_duration >= 0)
    2887                 :             :             ? avopts->log_analyze_min_duration
    2888         [ +  + ]:      111998 :             : Log_autoanalyze_min_duration;
    2889                 :             : 
    2890                 :             :         /* these do not have autovacuum-specific settings */
    2891         [ -  + ]:        2234 :         freeze_min_age = (avopts && avopts->freeze_min_age >= 0)
    2892                 :             :             ? avopts->freeze_min_age
    2893         [ +  + ]:      111998 :             : default_freeze_min_age;
    2894                 :             : 
    2895         [ -  + ]:        2234 :         freeze_table_age = (avopts && avopts->freeze_table_age >= 0)
    2896                 :             :             ? avopts->freeze_table_age
    2897         [ +  + ]:      111998 :             : default_freeze_table_age;
    2898                 :             : 
    2899                 :      111998 :         multixact_freeze_min_age = (avopts &&
    2900         [ -  + ]:        2234 :                                     avopts->multixact_freeze_min_age >= 0)
    2901                 :             :             ? avopts->multixact_freeze_min_age
    2902         [ +  + ]:      111998 :             : default_multixact_freeze_min_age;
    2903                 :             : 
    2904                 :      111998 :         multixact_freeze_table_age = (avopts &&
    2905         [ -  + ]:        2234 :                                       avopts->multixact_freeze_table_age >= 0)
    2906                 :             :             ? avopts->multixact_freeze_table_age
    2907         [ +  + ]:      111998 :             : default_multixact_freeze_table_age;
    2908                 :             : 
    2909                 :      109764 :         tab = palloc_object(autovac_table);
    2910                 :      109764 :         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                 :             :          */
    2917                 :      109764 :         tab->at_params.options =
    2918                 :      109764 :             (dovacuum ? (VACOPT_VACUUM |
    2919                 :             :                          VACOPT_PROCESS_MAIN |
    2920         [ +  + ]:      109764 :                          VACOPT_SKIP_DATABASE_STATS) : 0) |
    2921         [ +  + ]:      109764 :             (doanalyze ? VACOPT_ANALYZE : 0) |
    2922         [ +  + ]:      109764 :             (!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                 :             :          */
    2929                 :      109764 :         tab->at_params.index_cleanup = VACOPTVALUE_UNSPECIFIED;
    2930                 :      109764 :         tab->at_params.truncate = VACOPTVALUE_UNSPECIFIED;
    2931                 :      109764 :         tab->at_params.freeze_min_age = freeze_min_age;
    2932                 :      109764 :         tab->at_params.freeze_table_age = freeze_table_age;
    2933                 :      109764 :         tab->at_params.multixact_freeze_min_age = multixact_freeze_min_age;
    2934                 :      109764 :         tab->at_params.multixact_freeze_table_age = multixact_freeze_table_age;
    2935                 :      109764 :         tab->at_params.is_wraparound = wraparound;
    2936                 :      109764 :         tab->at_params.log_vacuum_min_duration = log_vacuum_min_duration;
    2937                 :      109764 :         tab->at_params.log_analyze_min_duration = log_analyze_min_duration;
    2938                 :      109764 :         tab->at_params.toast_parent = InvalidOid;
    2939                 :             : 
    2940                 :             :         /* Determine the number of parallel vacuum workers to use */
    2941                 :      109764 :         tab->at_params.nworkers = 0;
    2942         [ +  + ]:      109764 :         if (avopts)
    2943                 :             :         {
    2944         [ -  + ]:        2234 :             if (avopts->autovacuum_parallel_workers == 0)
    2945                 :             :             {
    2946                 :             :                 /*
    2947                 :             :                  * Disable parallel vacuum, if the reloption sets the parallel
    2948                 :             :                  * degree as zero.
    2949                 :             :                  */
    2950                 :           0 :                 tab->at_params.nworkers = -1;
    2951                 :             :             }
    2952         [ +  + ]:        2234 :             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                 :             :          */
    2965                 :      109764 :         tab->at_params.max_eager_freeze_failure_rate = vacuum_max_eager_freeze_failure_rate;
    2966                 :      109764 :         tab->at_storage_param_vac_cost_limit = avopts ?
    2967         [ +  + ]:      109764 :             avopts->vacuum_cost_limit : 0;
    2968                 :      109764 :         tab->at_storage_param_vac_cost_delay = avopts ?
    2969         [ +  + ]:      109764 :             avopts->vacuum_cost_delay : -1;
    2970                 :      109764 :         tab->at_relname = NULL;
    2971                 :      109764 :         tab->at_nspname = NULL;
    2972                 :      109764 :         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                 :             :          */
    2978                 :      109764 :         tab->at_dobalance =
    2979   [ +  +  +  - ]:      111998 :             !(avopts && (avopts->vacuum_cost_limit > 0 ||
    2980         [ +  - ]:      111998 :                          avopts->vacuum_cost_delay >= 0));
    2981                 :             :     }
    2982                 :             : 
    2983         [ +  + ]:      109782 :     if (free_avopts)
    2984                 :        1125 :         pfree(avopts);
    2985                 :      109782 :     heap_freetuple(classTup);
    2986                 :      109782 :     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.  Furthermore,
    3052                 :             :  * the relminmxid-based score is scaled aggressively as
    3053                 :             :  * effective_multixact_freeze_max_age is lowered due to high multixact member
    3054                 :             :  * space usage.
    3055                 :             :  *
    3056                 :             :  * To adjust how strongly each component contributes to the score, the
    3057                 :             :  * following parameters can be adjusted from their default of 1.0 to anywhere
    3058                 :             :  * between 0.0 and 10.0 (inclusive).  Setting all of these to 0.0 restores
    3059                 :             :  * pre-v19 prioritization behavior:
    3060                 :             :  *
    3061                 :             :  *     autovacuum_freeze_score_weight
    3062                 :             :  *     autovacuum_multixact_freeze_score_weight
    3063                 :             :  *     autovacuum_vacuum_score_weight
    3064                 :             :  *     autovacuum_vacuum_insert_score_weight
    3065                 :             :  *     autovacuum_analyze_score_weight
    3066                 :             :  *
    3067                 :             :  * The autovacuum table score is returned in scores->max.  The component scores
    3068                 :             :  * are also returned in the "scores" argument via the other members of the
    3069                 :             :  * AutoVacuumScores struct.
    3070                 :             :  */
    3071                 :             : static void
    3072                 :      295231 : relation_needs_vacanalyze(Oid relid,
    3073                 :             :                           AutoVacOpts *relopts,
    3074                 :             :                           Form_pg_class classForm,
    3075                 :             :                           int effective_multixact_freeze_max_age,
    3076                 :             :                           int elevel,
    3077                 :             :  /* output params below */
    3078                 :             :                           bool *dovacuum,
    3079                 :             :                           bool *doanalyze,
    3080                 :             :                           bool *wraparound,
    3081                 :             :                           AutoVacuumScores *scores)
    3082                 :             : {
    3083                 :             :     PgStat_StatTabEntry *tabentry;
    3084                 :             :     bool        force_vacuum;
    3085                 :             :     bool        av_enabled;
    3086                 :      295231 :     bool        may_free = false;
    3087                 :             : 
    3088                 :             :     /* constants from reloptions or GUC variables */
    3089                 :             :     int         vac_base_thresh,
    3090                 :             :                 vac_max_thresh,
    3091                 :             :                 vac_ins_base_thresh,
    3092                 :             :                 anl_base_thresh;
    3093                 :             :     float4      vac_scale_factor,
    3094                 :             :                 vac_ins_scale_factor,
    3095                 :             :                 anl_scale_factor;
    3096                 :             : 
    3097                 :             :     /* thresholds calculated from above constants */
    3098                 :             :     float4      vacthresh,
    3099                 :             :                 vacinsthresh,
    3100                 :             :                 anlthresh;
    3101                 :             : 
    3102                 :             :     /* number of vacuum (resp. analyze) tuples at this time */
    3103                 :             :     float4      vactuples,
    3104                 :             :                 instuples,
    3105                 :             :                 anltuples;
    3106                 :             : 
    3107                 :             :     /* freeze parameters */
    3108                 :             :     int         freeze_max_age;
    3109                 :             :     int         multixact_freeze_max_age;
    3110                 :             :     TransactionId xidForceLimit;
    3111                 :             :     TransactionId relfrozenxid;
    3112                 :             :     MultiXactId relminmxid;
    3113                 :             :     MultiXactId multiForceLimit;
    3114                 :             :     uint32      xid_age;
    3115                 :             :     uint32      mxid_age;
    3116                 :             :     int         effective_xid_failsafe_age;
    3117                 :             :     int         effective_mxid_failsafe_age;
    3118                 :             : 
    3119                 :      295231 :     float4      pcnt_unfrozen = 1;
    3120                 :      295231 :     float4      reltuples = classForm->reltuples;
    3121                 :      295231 :     int32       relpages = classForm->relpages;
    3122                 :      295231 :     int32       relallfrozen = classForm->relallfrozen;
    3123                 :             : 
    3124                 :             :     Assert(classForm != NULL);
    3125                 :             :     Assert(OidIsValid(relid));
    3126                 :             : 
    3127                 :      295231 :     memset(scores, 0, sizeof(AutoVacuumScores));
    3128                 :      295231 :     *dovacuum = false;
    3129                 :      295231 :     *doanalyze = false;
    3130                 :             : 
    3131                 :             :     /*
    3132                 :             :      * Determine vacuum/analyze equation parameters.  We have two possible
    3133                 :             :      * sources: the passed reloptions (which could be a main table or a toast
    3134                 :             :      * table), or the autovacuum GUC variables.
    3135                 :             :      */
    3136                 :             : 
    3137                 :             :     /* -1 in autovac setting means use plain vacuum_scale_factor */
    3138         [ -  + ]:        5183 :     vac_scale_factor = (relopts && relopts->vacuum_scale_factor >= 0)
    3139                 :           0 :         ? relopts->vacuum_scale_factor
    3140         [ +  + ]:      300414 :         : autovacuum_vac_scale;
    3141                 :             : 
    3142         [ -  + ]:        5183 :     vac_base_thresh = (relopts && relopts->vacuum_threshold >= 0)
    3143                 :             :         ? relopts->vacuum_threshold
    3144         [ +  + ]:      300414 :         : autovacuum_vac_thresh;
    3145                 :             : 
    3146                 :             :     /* -1 is used to disable max threshold */
    3147         [ -  + ]:        5183 :     vac_max_thresh = (relopts && relopts->vacuum_max_threshold >= -1)
    3148                 :             :         ? relopts->vacuum_max_threshold
    3149         [ +  + ]:      300414 :         : autovacuum_vac_max_thresh;
    3150                 :             : 
    3151         [ -  + ]:        5183 :     vac_ins_scale_factor = (relopts && relopts->vacuum_ins_scale_factor >= 0)
    3152                 :           0 :         ? relopts->vacuum_ins_scale_factor
    3153         [ +  + ]:      300414 :         : autovacuum_vac_ins_scale;
    3154                 :             : 
    3155                 :             :     /* -1 is used to disable insert vacuums */
    3156         [ -  + ]:        5183 :     vac_ins_base_thresh = (relopts && relopts->vacuum_ins_threshold >= -1)
    3157                 :             :         ? relopts->vacuum_ins_threshold
    3158         [ +  + ]:      300414 :         : autovacuum_vac_ins_thresh;
    3159                 :             : 
    3160         [ -  + ]:        5183 :     anl_scale_factor = (relopts && relopts->analyze_scale_factor >= 0)
    3161                 :           0 :         ? relopts->analyze_scale_factor
    3162         [ +  + ]:      300414 :         : autovacuum_anl_scale;
    3163                 :             : 
    3164         [ -  + ]:        5183 :     anl_base_thresh = (relopts && relopts->analyze_threshold >= 0)
    3165                 :             :         ? relopts->analyze_threshold
    3166         [ +  + ]:      300414 :         : autovacuum_anl_thresh;
    3167                 :             : 
    3168         [ -  + ]:        5183 :     freeze_max_age = (relopts && relopts->freeze_max_age >= 0)
    3169                 :           0 :         ? Min(relopts->freeze_max_age, autovacuum_freeze_max_age)
    3170         [ +  + ]:      300414 :         : autovacuum_freeze_max_age;
    3171                 :             : 
    3172         [ -  + ]:        5183 :     multixact_freeze_max_age = (relopts && relopts->multixact_freeze_max_age >= 0)
    3173                 :           0 :         ? Min(relopts->multixact_freeze_max_age, effective_multixact_freeze_max_age)
    3174         [ +  + ]:      300414 :         : effective_multixact_freeze_max_age;
    3175                 :             : 
    3176         [ +  + ]:      295231 :     av_enabled = (relopts ? relopts->enabled : true);
    3177                 :      295231 :     av_enabled &= AutoVacuumingActive();
    3178                 :             : 
    3179                 :      295231 :     relfrozenxid = classForm->relfrozenxid;
    3180                 :      295231 :     relminmxid = classForm->relminmxid;
    3181                 :             : 
    3182                 :             :     /* Force vacuum if table is at risk of wraparound */
    3183                 :      295231 :     xidForceLimit = recentXid - freeze_max_age;
    3184         [ -  + ]:      295231 :     if (xidForceLimit < FirstNormalTransactionId)
    3185                 :           0 :         xidForceLimit -= FirstNormalTransactionId;
    3186   [ +  -  +  + ]:      590462 :     force_vacuum = (TransactionIdIsNormal(relfrozenxid) &&
    3187                 :      295231 :                     TransactionIdPrecedes(relfrozenxid, xidForceLimit));
    3188         [ +  + ]:      295231 :     if (!force_vacuum)
    3189                 :             :     {
    3190                 :       76119 :         multiForceLimit = recentMulti - multixact_freeze_max_age;
    3191         [ -  + ]:       76119 :         if (multiForceLimit < FirstMultiXactId)
    3192                 :           0 :             multiForceLimit -= FirstMultiXactId;
    3193   [ +  -  -  + ]:      152238 :         force_vacuum = MultiXactIdIsValid(relminmxid) &&
    3194                 :       76119 :             MultiXactIdPrecedes(relminmxid, multiForceLimit);
    3195                 :             :     }
    3196                 :      295231 :     *wraparound = force_vacuum;
    3197                 :             : 
    3198                 :             :     /*
    3199                 :             :      * To calculate the (M)XID age portion of the score, divide the age by its
    3200                 :             :      * respective *_freeze_max_age parameter.  The multixact_freeze_max_age
    3201                 :             :      * variable might be 0 here (i.e., a division-by-zero hazard), so in that
    3202                 :             :      * case we use the mxid_age as the MXID score.
    3203                 :             :      */
    3204         [ +  - ]:      295231 :     xid_age = TransactionIdIsNormal(relfrozenxid) ? recentXid - relfrozenxid : 0;
    3205         [ +  - ]:      295231 :     mxid_age = MultiXactIdIsValid(relminmxid) ? recentMulti - relminmxid : 0;
    3206                 :             : 
    3207                 :      295231 :     scores->xid = (double) xid_age / freeze_max_age;
    3208         [ +  - ]:      295231 :     scores->mxid = (double) mxid_age / Max(1, multixact_freeze_max_age);
    3209                 :             : 
    3210                 :             :     /*
    3211                 :             :      * To ensure tables are given increased priority once they begin
    3212                 :             :      * approaching wraparound, we scale the score aggressively if the ages
    3213                 :             :      * surpass vacuum_failsafe_age or vacuum_multixact_failsafe_age.
    3214                 :             :      *
    3215                 :             :      * As in vacuum_xid_failsafe_check(), the effective failsafe age is no
    3216                 :             :      * less than 105% the value of the respective *_freeze_max_age parameter.
    3217                 :             :      * Note that per-table settings could result in a low score even if the
    3218                 :             :      * table surpasses the failsafe settings.  However, this is a strange
    3219                 :             :      * enough corner case that we don't bother trying to handle it.
    3220                 :             :      *
    3221                 :             :      * We further adjust the effective failsafe ages with the weight
    3222                 :             :      * parameters so that increasing them lowers the ages at which we begin
    3223                 :             :      * scaling aggressively.
    3224                 :             :      */
    3225         [ +  - ]:      295231 :     effective_xid_failsafe_age = Max(vacuum_failsafe_age,
    3226                 :             :                                      autovacuum_freeze_max_age * 1.05);
    3227         [ +  - ]:      295231 :     effective_mxid_failsafe_age = Max(vacuum_multixact_failsafe_age,
    3228                 :             :                                       autovacuum_multixact_freeze_max_age * 1.05);
    3229                 :             : 
    3230         [ -  + ]:      295231 :     if (autovacuum_freeze_score_weight > 1.0)
    3231                 :           0 :         effective_xid_failsafe_age /= autovacuum_freeze_score_weight;
    3232         [ -  + ]:      295231 :     if (autovacuum_multixact_freeze_score_weight > 1.0)
    3233                 :           0 :         effective_mxid_failsafe_age /= autovacuum_multixact_freeze_score_weight;
    3234                 :             : 
    3235         [ +  + ]:      295231 :     if (xid_age >= effective_xid_failsafe_age)
    3236         [ -  + ]:       50492 :         scores->xid = pow(scores->xid, Max(1.0, (double) xid_age / 100000000));
    3237         [ -  + ]:      295231 :     if (mxid_age >= effective_mxid_failsafe_age)
    3238         [ #  # ]:           0 :         scores->mxid = pow(scores->mxid, Max(1.0, (double) mxid_age / 100000000));
    3239                 :             : 
    3240                 :      295231 :     scores->xid *= autovacuum_freeze_score_weight;
    3241                 :      295231 :     scores->mxid *= autovacuum_multixact_freeze_score_weight;
    3242                 :             : 
    3243         [ +  + ]:      295231 :     scores->max = Max(scores->xid, scores->mxid);
    3244         [ +  + ]:      295231 :     if (force_vacuum)
    3245                 :      219112 :         *dovacuum = true;
    3246                 :             : 
    3247                 :             :     /*
    3248                 :             :      * If we found stats for the table, and autovacuum is currently enabled,
    3249                 :             :      * make a threshold-based decision whether to vacuum and/or analyze.  If
    3250                 :             :      * autovacuum is currently disabled, we must be here for anti-wraparound
    3251                 :             :      * vacuuming only, so don't vacuum (or analyze) anything that's not being
    3252                 :             :      * forced.
    3253                 :             :      */
    3254                 :      295231 :     tabentry = pgstat_fetch_stat_tabentry_ext(classForm->relisshared,
    3255                 :             :                                               relid, &may_free);
    3256         [ +  + ]:      295231 :     if (!tabentry)
    3257                 :        6073 :         return;
    3258                 :             : 
    3259                 :      289158 :     vactuples = tabentry->dead_tuples;
    3260                 :      289158 :     instuples = tabentry->ins_since_vacuum;
    3261                 :      289158 :     anltuples = tabentry->mod_since_analyze;
    3262                 :             : 
    3263                 :             :     /* If the table hasn't yet been vacuumed, take reltuples as zero */
    3264         [ +  + ]:      289158 :     if (reltuples < 0)
    3265                 :        1988 :         reltuples = 0;
    3266                 :             : 
    3267                 :             :     /*
    3268                 :             :      * If we have data for relallfrozen, calculate the unfrozen percentage of
    3269                 :             :      * the table to modify insert scale factor. This helps us decide whether
    3270                 :             :      * or not to vacuum an insert-heavy table based on the number of inserts
    3271                 :             :      * to the more "active" part of the table.
    3272                 :             :      */
    3273   [ +  +  +  + ]:      289158 :     if (relpages > 0 && relallfrozen > 0)
    3274                 :             :     {
    3275                 :             :         /*
    3276                 :             :          * It could be the stats were updated manually and relallfrozen >
    3277                 :             :          * relpages. Clamp relallfrozen to relpages to avoid nonsensical
    3278                 :             :          * calculations.
    3279                 :             :          */
    3280                 :      105428 :         relallfrozen = Min(relallfrozen, relpages);
    3281                 :      105428 :         pcnt_unfrozen = 1 - ((float4) relallfrozen / relpages);
    3282                 :             :     }
    3283                 :             : 
    3284                 :      289158 :     vacthresh = (float4) vac_base_thresh + vac_scale_factor * reltuples;
    3285   [ +  -  -  + ]:      289158 :     if (vac_max_thresh >= 0 && vacthresh > (float4) vac_max_thresh)
    3286                 :           0 :         vacthresh = (float4) vac_max_thresh;
    3287                 :             : 
    3288                 :      289158 :     vacinsthresh = (float4) vac_ins_base_thresh +
    3289                 :      289158 :         vac_ins_scale_factor * reltuples * pcnt_unfrozen;
    3290                 :      289158 :     anlthresh = (float4) anl_base_thresh + anl_scale_factor * reltuples;
    3291                 :             : 
    3292                 :             :     /* Determine if this table needs vacuum, and update the score. */
    3293         [ +  - ]:      289158 :     scores->vac = (double) vactuples / Max(vacthresh, 1);
    3294                 :      289158 :     scores->vac *= autovacuum_vacuum_score_weight;
    3295         [ +  + ]:      289158 :     scores->max = Max(scores->max, scores->vac);
    3296   [ +  +  +  + ]:      289158 :     if (av_enabled && vactuples > vacthresh)
    3297                 :         235 :         *dovacuum = true;
    3298                 :             : 
    3299         [ +  - ]:      289158 :     if (vac_ins_base_thresh >= 0)
    3300                 :             :     {
    3301         [ +  - ]:      289158 :         scores->vac_ins = (double) instuples / Max(vacinsthresh, 1);
    3302                 :      289158 :         scores->vac_ins *= autovacuum_vacuum_insert_score_weight;
    3303         [ +  + ]:      289158 :         scores->max = Max(scores->max, scores->vac_ins);
    3304   [ +  +  +  + ]:      289158 :         if (av_enabled && instuples > vacinsthresh)
    3305                 :         164 :             *dovacuum = true;
    3306                 :             :     }
    3307                 :             : 
    3308                 :             :     /*
    3309                 :             :      * Determine if this table needs analyze, and update the score.  Note that
    3310                 :             :      * we don't analyze TOAST tables and pg_statistic.
    3311                 :             :      */
    3312         [ +  + ]:      289158 :     if (relid != StatisticRelationId &&
    3313         [ +  + ]:      286695 :         classForm->relkind != RELKIND_TOASTVALUE)
    3314                 :             :     {
    3315         [ +  - ]:      182881 :         scores->anl = (double) anltuples / Max(anlthresh, 1);
    3316                 :      182881 :         scores->anl *= autovacuum_analyze_score_weight;
    3317         [ +  + ]:      182881 :         scores->max = Max(scores->max, scores->anl);
    3318   [ +  +  +  + ]:      182881 :         if (av_enabled && anltuples > anlthresh)
    3319                 :         762 :             *doanalyze = true;
    3320                 :             :     }
    3321                 :             : 
    3322         [ +  - ]:      289158 :     if (vac_ins_base_thresh >= 0)
    3323         [ -  + ]:      289158 :         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",
    3324                 :             :              NameStr(classForm->relname),
    3325                 :             :              vactuples, vacthresh, scores->vac,
    3326                 :             :              instuples, vacinsthresh, scores->vac_ins,
    3327                 :             :              anltuples, anlthresh, scores->anl,
    3328                 :             :              scores->xid, scores->mxid);
    3329                 :             :     else
    3330         [ #  # ]:           0 :         elog(elevel, "%s: vac: %.0f (thresh %.0f, score %.2f), ins: (disabled), anl: %.0f (thresh %.0f, score %.2f), xid score: %.2f, mxid score: %.2f",
    3331                 :             :              NameStr(classForm->relname),
    3332                 :             :              vactuples, vacthresh, scores->vac,
    3333                 :             :              anltuples, anlthresh, scores->anl,
    3334                 :             :              scores->xid, scores->mxid);
    3335                 :             : 
    3336                 :             :     /* Avoid leaking pgstat entries until the end of autovacuum. */
    3337         [ +  - ]:      289158 :     if (may_free)
    3338                 :      289158 :         pfree(tabentry);
    3339                 :             : }
    3340                 :             : 
    3341                 :             : /*
    3342                 :             :  * autovacuum_do_vac_analyze
    3343                 :             :  *      Vacuum and/or analyze the specified table
    3344                 :             :  *
    3345                 :             :  * We expect the caller to have switched into a memory context that won't
    3346                 :             :  * disappear at transaction commit.
    3347                 :             :  */
    3348                 :             : static void
    3349                 :      109764 : autovacuum_do_vac_analyze(autovac_table *tab, BufferAccessStrategy bstrategy)
    3350                 :             : {
    3351                 :             :     RangeVar   *rangevar;
    3352                 :             :     VacuumRelation *rel;
    3353                 :             :     List       *rel_list;
    3354                 :             :     MemoryContext vac_context;
    3355                 :             :     MemoryContext old_context;
    3356                 :             : 
    3357                 :             :     /* Let pgstat know what we're doing */
    3358                 :      109764 :     autovac_report_activity(tab);
    3359                 :             : 
    3360                 :             :     /* Create a context that vacuum() can use as cross-transaction storage */
    3361                 :      109764 :     vac_context = AllocSetContextCreate(CurrentMemoryContext,
    3362                 :             :                                         "Vacuum",
    3363                 :             :                                         ALLOCSET_DEFAULT_SIZES);
    3364                 :             : 
    3365                 :             :     /* Set up one VacuumRelation target, identified by OID, for vacuum() */
    3366                 :      109764 :     old_context = MemoryContextSwitchTo(vac_context);
    3367                 :      109764 :     rangevar = makeRangeVar(tab->at_nspname, tab->at_relname, -1);
    3368                 :      109764 :     rel = makeVacuumRelation(rangevar, tab->at_relid, NIL);
    3369                 :      109764 :     rel_list = list_make1(rel);
    3370                 :      109764 :     MemoryContextSwitchTo(old_context);
    3371                 :             : 
    3372                 :      109764 :     vacuum(rel_list, &tab->at_params, bstrategy, vac_context, true);
    3373                 :             : 
    3374                 :      109762 :     MemoryContextDelete(vac_context);
    3375                 :      109762 : }
    3376                 :             : 
    3377                 :             : /*
    3378                 :             :  * autovac_report_activity
    3379                 :             :  *      Report to pgstat what autovacuum is doing
    3380                 :             :  *
    3381                 :             :  * We send a SQL string corresponding to what the user would see if the
    3382                 :             :  * equivalent command was to be issued manually.
    3383                 :             :  *
    3384                 :             :  * Note we assume that we are going to report the next command as soon as we're
    3385                 :             :  * done with the current one, and exit right after the last one, so we don't
    3386                 :             :  * bother to report "<IDLE>" or some such.
    3387                 :             :  */
    3388                 :             : static void
    3389                 :      109764 : autovac_report_activity(autovac_table *tab)
    3390                 :             : {
    3391                 :             : #define MAX_AUTOVAC_ACTIV_LEN (NAMEDATALEN * 2 + 56)
    3392                 :             :     char        activity[MAX_AUTOVAC_ACTIV_LEN];
    3393                 :             :     int         len;
    3394                 :             : 
    3395                 :             :     /* Report the command and possible options */
    3396         [ +  + ]:      109764 :     if (tab->at_params.options & VACOPT_VACUUM)
    3397                 :      109532 :         snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
    3398                 :             :                  "autovacuum: VACUUM%s",
    3399         [ +  + ]:      109532 :                  tab->at_params.options & VACOPT_ANALYZE ? " ANALYZE" : "");
    3400                 :             :     else
    3401                 :         232 :         snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
    3402                 :             :                  "autovacuum: ANALYZE");
    3403                 :             : 
    3404                 :             :     /*
    3405                 :             :      * Report the qualified name of the relation.
    3406                 :             :      */
    3407                 :      109764 :     len = strlen(activity);
    3408                 :             : 
    3409                 :      109764 :     snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
    3410                 :             :              " %s.%s%s", tab->at_nspname, tab->at_relname,
    3411         [ +  + ]:      109764 :              tab->at_params.is_wraparound ? " (to prevent wraparound)" : "");
    3412                 :             : 
    3413                 :             :     /* Set statement_timestamp() to current time for pg_stat_activity */
    3414                 :      109764 :     SetCurrentStatementStartTimestamp();
    3415                 :             : 
    3416                 :      109764 :     pgstat_report_activity(STATE_RUNNING, activity);
    3417                 :      109764 : }
    3418                 :             : 
    3419                 :             : /*
    3420                 :             :  * autovac_report_workitem
    3421                 :             :  *      Report to pgstat that autovacuum is processing a work item
    3422                 :             :  */
    3423                 :             : static void
    3424                 :           6 : autovac_report_workitem(AutoVacuumWorkItem *workitem,
    3425                 :             :                         const char *nspname, const char *relname)
    3426                 :             : {
    3427                 :             :     char        activity[MAX_AUTOVAC_ACTIV_LEN + 12 + 2];
    3428                 :             :     char        blk[12 + 2];
    3429                 :             :     int         len;
    3430                 :             : 
    3431         [ +  - ]:           6 :     switch (workitem->avw_type)
    3432                 :             :     {
    3433                 :           6 :         case AVW_BRINSummarizeRange:
    3434                 :           6 :             snprintf(activity, MAX_AUTOVAC_ACTIV_LEN,
    3435                 :             :                      "autovacuum: BRIN summarize");
    3436                 :           6 :             break;
    3437                 :             :     }
    3438                 :             : 
    3439                 :             :     /*
    3440                 :             :      * Report the qualified name of the relation, and the block number if any
    3441                 :             :      */
    3442                 :           6 :     len = strlen(activity);
    3443                 :             : 
    3444         [ +  - ]:           6 :     if (BlockNumberIsValid(workitem->avw_blockNumber))
    3445                 :           6 :         snprintf(blk, sizeof(blk), " %u", workitem->avw_blockNumber);
    3446                 :             :     else
    3447                 :           0 :         blk[0] = '\0';
    3448                 :             : 
    3449                 :           6 :     snprintf(activity + len, MAX_AUTOVAC_ACTIV_LEN - len,
    3450                 :             :              " %s.%s%s", nspname, relname, blk);
    3451                 :             : 
    3452                 :             :     /* Set statement_timestamp() to current time for pg_stat_activity */
    3453                 :           6 :     SetCurrentStatementStartTimestamp();
    3454                 :             : 
    3455                 :           6 :     pgstat_report_activity(STATE_RUNNING, activity);
    3456                 :           6 : }
    3457                 :             : 
    3458                 :             : /*
    3459                 :             :  * AutoVacuumingActive
    3460                 :             :  *      Check GUC vars and report whether the autovacuum process should be
    3461                 :             :  *      running.
    3462                 :             :  */
    3463                 :             : bool
    3464                 :      342366 : AutoVacuumingActive(void)
    3465                 :             : {
    3466   [ +  +  -  + ]:      342366 :     if (!autovacuum_start_daemon || !pgstat_track_counts)
    3467                 :        5364 :         return false;
    3468                 :      337002 :     return true;
    3469                 :             : }
    3470                 :             : 
    3471                 :             : /*
    3472                 :             :  * Request one work item to the next autovacuum run processing our database.
    3473                 :             :  * Return false if the request can't be recorded.
    3474                 :             :  */
    3475                 :             : bool
    3476                 :           6 : AutoVacuumRequestWork(AutoVacuumWorkItemType type, Oid relationId,
    3477                 :             :                       BlockNumber blkno)
    3478                 :             : {
    3479                 :             :     int         i;
    3480                 :           6 :     bool        result = false;
    3481                 :             : 
    3482                 :           6 :     LWLockAcquire(AutovacuumLock, LW_EXCLUSIVE);
    3483                 :             : 
    3484                 :             :     /*
    3485                 :             :      * Locate an unused work item and fill it with the given data.
    3486                 :             :      */
    3487         [ +  - ]:          21 :     for (i = 0; i < NUM_WORKITEMS; i++)
    3488                 :             :     {
    3489                 :          21 :         AutoVacuumWorkItem *workitem = &AutoVacuumShmem->av_workItems[i];
    3490                 :             : 
    3491         [ +  + ]:          21 :         if (workitem->avw_used)
    3492                 :          15 :             continue;
    3493                 :             : 
    3494                 :           6 :         workitem->avw_used = true;
    3495                 :           6 :         workitem->avw_active = false;
    3496                 :           6 :         workitem->avw_type = type;
    3497                 :           6 :         workitem->avw_database = MyDatabaseId;
    3498                 :           6 :         workitem->avw_relation = relationId;
    3499                 :           6 :         workitem->avw_blockNumber = blkno;
    3500                 :           6 :         result = true;
    3501                 :             : 
    3502                 :             :         /* done */
    3503                 :           6 :         break;
    3504                 :             :     }
    3505                 :             : 
    3506                 :           6 :     LWLockRelease(AutovacuumLock);
    3507                 :             : 
    3508                 :           6 :     return result;
    3509                 :             : }
    3510                 :             : 
    3511                 :             : /*
    3512                 :             :  * autovac_init
    3513                 :             :  *      This is called at postmaster initialization.
    3514                 :             :  *
    3515                 :             :  * All we do here is annoy the user if he got it wrong.
    3516                 :             :  */
    3517                 :             : void
    3518                 :         994 : autovac_init(void)
    3519                 :             : {
    3520         [ +  + ]:         994 :     if (!autovacuum_start_daemon)
    3521                 :         131 :         return;
    3522         [ -  + ]:         863 :     else if (!pgstat_track_counts)
    3523         [ #  # ]:           0 :         ereport(WARNING,
    3524                 :             :                 (errmsg("autovacuum not started because of misconfiguration"),
    3525                 :             :                  errhint("Enable the \"track_counts\" option.")));
    3526                 :             :     else
    3527                 :         863 :         check_av_worker_gucs();
    3528                 :             : }
    3529                 :             : 
    3530                 :             : /*
    3531                 :             :  * AutoVacuumShmemRequest
    3532                 :             :  *      Register shared memory space needed for autovacuum
    3533                 :             :  */
    3534                 :             : static void
    3535                 :        1249 : AutoVacuumShmemRequest(void *arg)
    3536                 :             : {
    3537                 :             :     Size        size;
    3538                 :             : 
    3539                 :             :     /*
    3540                 :             :      * Need the fixed struct and the array of WorkerInfoData.
    3541                 :             :      */
    3542                 :        1249 :     size = sizeof(AutoVacuumShmemStruct);
    3543                 :        1249 :     size = MAXALIGN(size);
    3544                 :        1249 :     size = add_size(size, mul_size(autovacuum_worker_slots,
    3545                 :             :                                    sizeof(WorkerInfoData)));
    3546                 :             : 
    3547                 :        1249 :     ShmemRequestStruct(.name = "AutoVacuum Data",
    3548                 :             :                        .size = size,
    3549                 :             :                        .ptr = (void **) &AutoVacuumShmem,
    3550                 :             :         );
    3551                 :        1249 : }
    3552                 :             : 
    3553                 :             : /*
    3554                 :             :  * AutoVacuumShmemInit
    3555                 :             :  *      Initialize autovacuum-related shared memory
    3556                 :             :  */
    3557                 :             : static void
    3558                 :        1246 : AutoVacuumShmemInit(void *arg)
    3559                 :             : {
    3560                 :             :     WorkerInfo  worker;
    3561                 :             : 
    3562                 :        1246 :     AutoVacuumShmem->av_launcherpid = 0;
    3563                 :        1246 :     dclist_init(&AutoVacuumShmem->av_freeWorkers);
    3564                 :        1246 :     dlist_init(&AutoVacuumShmem->av_runningWorkers);
    3565                 :        1246 :     AutoVacuumShmem->av_startingWorker = NULL;
    3566                 :        1246 :     memset(AutoVacuumShmem->av_workItems, 0,
    3567                 :             :            sizeof(AutoVacuumWorkItem) * NUM_WORKITEMS);
    3568                 :             : 
    3569                 :        1246 :     worker = (WorkerInfo) ((char *) AutoVacuumShmem +
    3570                 :             :                            MAXALIGN(sizeof(AutoVacuumShmemStruct)));
    3571                 :             : 
    3572                 :             :     /* initialize the WorkerInfo free list */
    3573         [ +  + ]:       14992 :     for (int i = 0; i < autovacuum_worker_slots; i++)
    3574                 :             :     {
    3575                 :       13746 :         dclist_push_head(&AutoVacuumShmem->av_freeWorkers,
    3576                 :       13746 :                          &worker[i].wi_links);
    3577                 :       13746 :         pg_atomic_init_flag(&worker[i].wi_dobalance);
    3578                 :             :     }
    3579                 :             : 
    3580                 :        1246 :     pg_atomic_init_u32(&AutoVacuumShmem->av_nworkersForBalance, 0);
    3581                 :        1246 : }
    3582                 :             : 
    3583                 :             : /*
    3584                 :             :  * GUC check_hook for autovacuum_work_mem
    3585                 :             :  */
    3586                 :             : bool
    3587                 :        1293 : check_autovacuum_work_mem(int *newval, void **extra, GucSource source)
    3588                 :             : {
    3589                 :             :     /*
    3590                 :             :      * -1 indicates fallback.
    3591                 :             :      *
    3592                 :             :      * If we haven't yet changed the boot_val default of -1, just let it be.
    3593                 :             :      * Autovacuum will look to maintenance_work_mem instead.
    3594                 :             :      */
    3595         [ +  + ]:        1293 :     if (*newval == -1)
    3596                 :        1291 :         return true;
    3597                 :             : 
    3598                 :             :     /*
    3599                 :             :      * We clamp manually-set values to at least 64kB.  Since
    3600                 :             :      * maintenance_work_mem is always set to at least this value, do the same
    3601                 :             :      * here.
    3602                 :             :      */
    3603         [ +  - ]:           2 :     if (*newval < 64)
    3604                 :           2 :         *newval = 64;
    3605                 :             : 
    3606                 :           2 :     return true;
    3607                 :             : }
    3608                 :             : 
    3609                 :             : /*
    3610                 :             :  * Returns whether there is a free autovacuum worker slot available.
    3611                 :             :  */
    3612                 :             : static bool
    3613                 :       12642 : av_worker_available(void)
    3614                 :             : {
    3615                 :             :     int         free_slots;
    3616                 :             :     int         reserved_slots;
    3617                 :             : 
    3618                 :       12642 :     free_slots = dclist_count(&AutoVacuumShmem->av_freeWorkers);
    3619                 :             : 
    3620                 :       12642 :     reserved_slots = autovacuum_worker_slots - autovacuum_max_workers;
    3621                 :       12642 :     reserved_slots = Max(0, reserved_slots);
    3622                 :             : 
    3623                 :       12642 :     return free_slots > reserved_slots;
    3624                 :             : }
    3625                 :             : 
    3626                 :             : /*
    3627                 :             :  * Emits a WARNING if autovacuum_worker_slots < autovacuum_max_workers.
    3628                 :             :  */
    3629                 :             : static void
    3630                 :         863 : check_av_worker_gucs(void)
    3631                 :             : {
    3632         [ -  + ]:         863 :     if (autovacuum_worker_slots < autovacuum_max_workers)
    3633         [ #  # ]:           0 :         ereport(WARNING,
    3634                 :             :                 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
    3635                 :             :                  errmsg("\"%s\" (%d) should be less than or equal to \"%s\" (%d)",
    3636                 :             :                         "autovacuum_max_workers", autovacuum_max_workers,
    3637                 :             :                         "autovacuum_worker_slots", autovacuum_worker_slots),
    3638                 :             :                  errdetail("The server will only start up to \"%s\" (%d) autovacuum workers at a given time.",
    3639                 :             :                            "autovacuum_worker_slots", autovacuum_worker_slots)));
    3640                 :         863 : }
    3641                 :             : 
    3642                 :             : /*
    3643                 :             :  * pg_stat_get_autovacuum_scores
    3644                 :             :  *
    3645                 :             :  * Returns current autovacuum scores for all relevant tables in the current
    3646                 :             :  * database.
    3647                 :             :  */
    3648                 :             : Datum
    3649                 :           0 : pg_stat_get_autovacuum_scores(PG_FUNCTION_ARGS)
    3650                 :             : {
    3651                 :             :     int         effective_multixact_freeze_max_age;
    3652                 :             :     Relation    rel;
    3653                 :             :     TableScanDesc scan;
    3654                 :             :     HeapTuple   tup;
    3655                 :           0 :     ReturnSetInfo *rsinfo = (ReturnSetInfo *) fcinfo->resultinfo;
    3656                 :             : 
    3657                 :           0 :     InitMaterializedSRF(fcinfo, 0);
    3658                 :             : 
    3659                 :             :     /* some prerequisite initialization */
    3660                 :           0 :     effective_multixact_freeze_max_age = MultiXactMemberFreezeThreshold();
    3661                 :           0 :     recentXid = ReadNextTransactionId();
    3662                 :           0 :     recentMulti = ReadNextMultiXactId();
    3663                 :             : 
    3664                 :             :     /* scan pg_class */
    3665                 :           0 :     rel = table_open(RelationRelationId, AccessShareLock);
    3666                 :           0 :     scan = table_beginscan_catalog(rel, 0, NULL);
    3667         [ #  # ]:           0 :     while ((tup = heap_getnext(scan, ForwardScanDirection)) != NULL)
    3668                 :             :     {
    3669                 :           0 :         Form_pg_class form = (Form_pg_class) GETSTRUCT(tup);
    3670                 :             :         AutoVacOpts *avopts;
    3671                 :             :         bool        dovacuum;
    3672                 :             :         bool        doanalyze;
    3673                 :             :         bool        wraparound;
    3674                 :             :         AutoVacuumScores scores;
    3675                 :             :         Datum       vals[10];
    3676                 :           0 :         bool        nulls[10] = {false};
    3677                 :             : 
    3678                 :             :         /* skip ineligible entries */
    3679         [ #  # ]:           0 :         if (form->relkind != RELKIND_RELATION &&
    3680         [ #  # ]:           0 :             form->relkind != RELKIND_MATVIEW &&
    3681         [ #  # ]:           0 :             form->relkind != RELKIND_TOASTVALUE)
    3682                 :           0 :             continue;
    3683         [ #  # ]:           0 :         if (form->relpersistence == RELPERSISTENCE_TEMP)
    3684                 :           0 :             continue;
    3685                 :             : 
    3686                 :           0 :         avopts = extract_autovac_opts(tup, RelationGetDescr(rel));
    3687                 :           0 :         relation_needs_vacanalyze(form->oid, avopts, form,
    3688                 :             :                                   effective_multixact_freeze_max_age,
    3689                 :             :                                   LOG_NEVER,
    3690                 :             :                                   &dovacuum, &doanalyze, &wraparound,
    3691                 :             :                                   &scores);
    3692         [ #  # ]:           0 :         if (avopts)
    3693                 :           0 :             pfree(avopts);
    3694                 :             : 
    3695                 :           0 :         vals[0] = ObjectIdGetDatum(form->oid);
    3696                 :           0 :         vals[1] = Float8GetDatum(scores.max);
    3697                 :           0 :         vals[2] = Float8GetDatum(scores.xid);
    3698                 :           0 :         vals[3] = Float8GetDatum(scores.mxid);
    3699                 :           0 :         vals[4] = Float8GetDatum(scores.vac);
    3700                 :           0 :         vals[5] = Float8GetDatum(scores.vac_ins);
    3701                 :           0 :         vals[6] = Float8GetDatum(scores.anl);
    3702                 :           0 :         vals[7] = BoolGetDatum(dovacuum);
    3703                 :           0 :         vals[8] = BoolGetDatum(doanalyze);
    3704                 :           0 :         vals[9] = BoolGetDatum(wraparound);
    3705                 :             : 
    3706                 :           0 :         tuplestore_putvalues(rsinfo->setResult, rsinfo->setDesc, vals, nulls);
    3707                 :             :     }
    3708                 :           0 :     table_endscan(scan);
    3709                 :           0 :     table_close(rel, AccessShareLock);
    3710                 :             : 
    3711                 :           0 :     return (Datum) 0;
    3712                 :             : }
        

Generated by: LCOV version 2.0-1