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