LCOV - code coverage report
Current view: top level - src/backend/postmaster - autovacuum.c (source / functions) Hit Total Coverage
Test: PostgreSQL 13beta1 Lines: 770 952 80.9 %
Date: 2020-05-29 01:06:25 Functions: 33 34 97.1 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13