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