PostgreSQL Source Code git master
Loading...
Searching...
No Matches
autovacuum.h File Reference
#include "storage/block.h"
Include dependency graph for autovacuum.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  AutoVacuumWorkItemType { AVW_BRINSummarizeRange }
 

Functions

bool AutoVacuumingActive (void)
 
void autovac_init (void)
 
void AutoVacWorkerFailed (void)
 
pg_noreturn void AutoVacLauncherMain (const void *startup_data, size_t startup_data_len)
 
pg_noreturn void AutoVacWorkerMain (const void *startup_data, size_t startup_data_len)
 
bool AutoVacuumRequestWork (AutoVacuumWorkItemType type, Oid relationId, BlockNumber blkno)
 
Size AutoVacuumShmemSize (void)
 
void AutoVacuumShmemInit (void)
 

Variables

PGDLLIMPORT bool autovacuum_start_daemon
 
PGDLLIMPORT int autovacuum_worker_slots
 
PGDLLIMPORT int autovacuum_max_workers
 
PGDLLIMPORT int autovacuum_work_mem
 
PGDLLIMPORT int autovacuum_naptime
 
PGDLLIMPORT int autovacuum_vac_thresh
 
PGDLLIMPORT int autovacuum_vac_max_thresh
 
PGDLLIMPORT double autovacuum_vac_scale
 
PGDLLIMPORT int autovacuum_vac_ins_thresh
 
PGDLLIMPORT double autovacuum_vac_ins_scale
 
PGDLLIMPORT int autovacuum_anl_thresh
 
PGDLLIMPORT double autovacuum_anl_scale
 
PGDLLIMPORT int autovacuum_freeze_max_age
 
PGDLLIMPORT int autovacuum_multixact_freeze_max_age
 
PGDLLIMPORT double autovacuum_vac_cost_delay
 
PGDLLIMPORT int autovacuum_vac_cost_limit
 
PGDLLIMPORT int Log_autovacuum_min_duration
 
PGDLLIMPORT int Log_autoanalyze_min_duration
 

Enumeration Type Documentation

◆ AutoVacuumWorkItemType

Enumerator
AVW_BRINSummarizeRange 

Definition at line 23 of file autovacuum.h.

24{
AutoVacuumWorkItemType
Definition autovacuum.h:24
@ AVW_BRINSummarizeRange
Definition autovacuum.h:25

Function Documentation

◆ autovac_init()

void autovac_init ( void  )
extern

Definition at line 3346 of file autovacuum.c.

3347{
3349 return;
3350 else if (!pgstat_track_counts)
3352 (errmsg("autovacuum not started because of misconfiguration"),
3353 errhint("Enable the \"track_counts\" option.")));
3354 else
3356}
static void check_av_worker_gucs(void)
bool autovacuum_start_daemon
Definition autovacuum.c:117
int errhint(const char *fmt,...)
Definition elog.c:1330
int errmsg(const char *fmt,...)
Definition elog.c:1080
#define WARNING
Definition elog.h:36
#define ereport(elevel,...)
Definition elog.h:150
bool pgstat_track_counts
Definition pgstat.c:203

References autovacuum_start_daemon, check_av_worker_gucs(), ereport, errhint(), errmsg(), pgstat_track_counts, and WARNING.

Referenced by PostmasterMain().

◆ AutoVacLauncherMain()

pg_noreturn void AutoVacLauncherMain ( const void startup_data,
size_t  startup_data_len 
)
extern

Definition at line 375 of file autovacuum.c.

376{
378
380
381 /* Release postmaster's working memory context */
383 {
386 }
387
390
392 (errmsg_internal("autovacuum launcher started")));
393
394 if (PostAuthDelay)
395 pg_usleep(PostAuthDelay * 1000000L);
396
398
399 /*
400 * Set up signal handlers. We operate on databases much like a regular
401 * backend, so we use the same signal handling. See equivalent code in
402 * tcop/postgres.c.
403 */
407 /* SIGQUIT handler was already set up by InitPostmasterChild */
408
409 InitializeTimeouts(); /* establishes SIGALRM handler */
410
416
417 /*
418 * Create a per-backend PGPROC struct in shared memory. We must do this
419 * before we can use LWLocks or access any shared memory.
420 */
421 InitProcess();
422
423 /* Early initialization */
424 BaseInit();
425
427
429
430 /*
431 * Create a memory context that we will do all our work in. We do this so
432 * that we can reset the context during error recovery and thereby avoid
433 * possible memory leaks.
434 */
436 "Autovacuum Launcher",
439
440 /*
441 * If an exception is encountered, processing resumes here.
442 *
443 * This code is a stripped down version of PostgresMain error recovery.
444 *
445 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
446 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
447 * signals other than SIGQUIT will be blocked until we complete error
448 * recovery. It might seem that this policy makes the HOLD_INTERRUPTS()
449 * call redundant, but it is not since InterruptPending might be set
450 * already.
451 */
452 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
453 {
454 /* since not using PG_TRY, must reset error stack by hand */
456
457 /* Prevents interrupts while cleaning up */
459
460 /* Forget any pending QueryCancel or timeout request */
462 QueryCancelPending = false; /* second to avoid race condition */
463
464 /* Report the error to the server log */
466
467 /* Abort the current transaction in order to recover */
469
470 /*
471 * Release any other resources, for the case where we were not in a
472 * transaction.
473 */
478 /* this is probably dead code, but let's be safe: */
481 AtEOXact_Buffers(false);
483 AtEOXact_Files(false);
484 AtEOXact_HashTables(false);
485
486 /*
487 * Now return to normal top-level context and clear ErrorContext for
488 * next time.
489 */
492
493 /* Flush any leaked data in the top-level context */
495
496 /* don't leave dangling pointers to freed memory */
499
500 /* Now we can allow interrupts again */
502
503 /* if in shutdown mode, no need for anything further; just go away */
506
507 /*
508 * Sleep at least 1 second after any error. We don't want to be
509 * filling the error logs as fast as we can.
510 */
511 pg_usleep(1000000L);
512 }
513
514 /* We can now handle ereport(ERROR) */
516
517 /* must unblock signals before calling rebuild_database_list */
519
520 /*
521 * Set always-secure search path. Launcher doesn't connect to a database,
522 * so this has no effect.
523 */
524 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
525
526 /*
527 * Force zero_damaged_pages OFF in the autovac process, even if it is set
528 * in postgresql.conf. We don't really want such a dangerous option being
529 * applied non-interactively.
530 */
531 SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
532
533 /*
534 * Force settable timeouts off to avoid letting these settings prevent
535 * regular maintenance from being executed.
536 */
537 SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
538 SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
539 SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
540 SetConfigOption("idle_in_transaction_session_timeout", "0",
542
543 /*
544 * Force default_transaction_isolation to READ COMMITTED. We don't want
545 * to pay the overhead of serializable mode, nor add any risk of causing
546 * deadlocks or delaying other transactions.
547 */
548 SetConfigOption("default_transaction_isolation", "read committed",
550
551 /*
552 * Even when system is configured to use a different fetch consistency,
553 * for autovac we always want fresh stats.
554 */
555 SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
556
557 /*
558 * In emergency mode, just start a worker (unless shutdown was requested)
559 * and go away.
560 */
561 if (!AutoVacuumingActive())
562 {
565 proc_exit(0); /* done */
566 }
567
569
570 /*
571 * Create the initial database list. The invariant we want this list to
572 * keep is that it's ordered by decreasing next_worker. As soon as an
573 * entry is updated to a higher time, it will be moved to the front (which
574 * is correct because the only operation is to add autovacuum_naptime to
575 * the entry, and time always increases).
576 */
578
579 /* loop until shutdown request */
581 {
582 struct timeval nap;
584 bool can_launch;
585
586 /*
587 * This loop is a bit different from the normal use of WaitLatch,
588 * because we'd like to sleep before the first launch of a child
589 * process. So it's WaitLatch, then ResetLatch, then check for
590 * wakening conditions.
591 */
592
594
595 /*
596 * Wait until naptime expires or we get some type of signal (all the
597 * signal handlers will wake us by calling SetLatch).
598 */
601 (nap.tv_sec * 1000L) + (nap.tv_usec / 1000L),
603
605
607
608 /*
609 * a worker finished, or postmaster signaled failure to start a worker
610 */
611 if (got_SIGUSR2)
612 {
613 got_SIGUSR2 = false;
614
615 /* rebalance cost limits, if needed */
617 {
622 }
623
625 {
626 /*
627 * If the postmaster failed to start a new worker, we sleep
628 * for a little while and resend the signal. The new worker's
629 * state is still in memory, so this is sufficient. After
630 * that, we restart the main loop.
631 *
632 * XXX should we put a limit to the number of times we retry?
633 * I don't think it makes much sense, because a future start
634 * of a worker will continue to fail in the same way.
635 */
637 pg_usleep(1000000L); /* 1s */
639 continue;
640 }
641 }
642
643 /*
644 * There are some conditions that we need to check before trying to
645 * start a worker. First, we need to make sure that there is a worker
646 * slot available. Second, we need to make sure that no other worker
647 * failed while starting up.
648 */
649
652
654
656 {
657 int waittime;
659
660 /*
661 * We can't launch another worker when another one is still
662 * starting up (or failed while doing so), so just sleep for a bit
663 * more; that worker will wake us up again as soon as it's ready.
664 * We will only wait autovacuum_naptime seconds (up to a maximum
665 * of 60 seconds) for this to happen however. Note that failure
666 * to connect to a particular database is not a problem here,
667 * because the worker removes itself from the startingWorker
668 * pointer before trying to connect. Problems detected by the
669 * postmaster (like fork() failure) are also reported and handled
670 * differently. The only problems that may cause this code to
671 * fire are errors in the earlier sections of AutoVacWorkerMain,
672 * before the worker removes the WorkerInfo from the
673 * startingWorker pointer.
674 */
675 waittime = Min(autovacuum_naptime, 60) * 1000;
677 waittime))
678 {
681
682 /*
683 * No other process can put a worker in starting mode, so if
684 * startingWorker is still INVALID after exchanging our lock,
685 * we assume it's the same one we saw above (so we don't
686 * recheck the launch time).
687 */
689 {
691 worker->wi_dboid = InvalidOid;
692 worker->wi_tableoid = InvalidOid;
693 worker->wi_sharedrel = false;
694 worker->wi_proc = NULL;
695 worker->wi_launchtime = 0;
697 &worker->wi_links);
700 errmsg("autovacuum worker took too long to start; canceled"));
701 }
702 }
703 else
704 can_launch = false;
705 }
706 LWLockRelease(AutovacuumLock); /* either shared or exclusive */
707
708 /* if we can't do anything, just go back to sleep */
709 if (!can_launch)
710 continue;
711
712 /* We're OK to start a new worker */
713
715 {
716 /*
717 * Special case when the list is empty: start a worker right away.
718 * This covers the initial case, when no database is in pgstats
719 * (thus the list is empty). Note that the constraints in
720 * launcher_determine_sleep keep us from starting workers too
721 * quickly (at most once every autovacuum_naptime when the list is
722 * empty).
723 */
725 }
726 else
727 {
728 /*
729 * because rebuild_database_list constructs a list with most
730 * distant adl_next_worker first, we obtain our database from the
731 * tail of the list.
732 */
734
736
737 /*
738 * launch a worker if next_worker is right now or it is in the
739 * past
740 */
741 if (TimestampDifferenceExceeds(avdb->adl_next_worker,
742 current_time, 0))
744 }
745 }
746
748}
void pgaio_error_cleanup(void)
Definition aio.c:1165
static Oid do_start_worker(void)
static void launcher_determine_sleep(bool canlaunch, bool recursing, struct timeval *nap)
Definition autovacuum.c:816
static volatile sig_atomic_t got_SIGUSR2
Definition autovacuum.c:155
static void avl_sigusr2_handler(SIGNAL_ARGS)
static bool av_worker_available(void)
int autovacuum_naptime
Definition autovacuum.c:121
bool AutoVacuumingActive(void)
static pg_noreturn void AutoVacLauncherShutdown(void)
Definition autovacuum.c:799
@ AutoVacRebalance
Definition autovacuum.c:252
@ AutoVacForkFailed
Definition autovacuum.c:251
static void launch_worker(TimestampTz now)
static dlist_head DatabaseList
Definition autovacuum.c:310
static void rebuild_database_list(Oid newdb)
Definition autovacuum.c:900
static AutoVacuumShmemStruct * AutoVacuumShmem
Definition autovacuum.c:304
static MemoryContext DatabaseListCxt
Definition autovacuum.c:311
static void autovac_recalculate_workers_for_balance(void)
static MemoryContext AutovacMemCxt
Definition autovacuum.c:168
static void ProcessAutoVacLauncherInterrupts(void)
Definition autovacuum.c:754
sigset_t UnBlockSig
Definition pqsignal.c:22
bool TimestampDifferenceExceeds(TimestampTz start_time, TimestampTz stop_time, int msec)
Definition timestamp.c:1781
TimestampTz GetCurrentTimestamp(void)
Definition timestamp.c:1645
void AtEOXact_Buffers(bool isCommit)
Definition bufmgr.c:4103
void UnlockBuffers(void)
Definition bufmgr.c:5709
#define Min(x, y)
Definition c.h:997
#define Assert(condition)
Definition c.h:873
int64 TimestampTz
Definition timestamp.h:39
void AtEOXact_HashTables(bool isCommit)
Definition dynahash.c:1931
int errmsg_internal(const char *fmt,...)
Definition elog.c:1170
void EmitErrorReport(void)
Definition elog.c:1704
ErrorContextCallback * error_context_stack
Definition elog.c:95
void FlushErrorState(void)
Definition elog.c:1884
sigjmp_buf * PG_exception_stack
Definition elog.c:97
#define DEBUG1
Definition elog.h:30
void AtEOXact_Files(bool isCommit)
Definition fd.c:3210
int MyProcPid
Definition globals.c:47
volatile sig_atomic_t QueryCancelPending
Definition globals.c:33
struct Latch * MyLatch
Definition globals.c:63
void SetConfigOption(const char *name, const char *value, GucContext context, GucSource source)
Definition guc.c:4196
@ PGC_S_OVERRIDE
Definition guc.h:123
@ PGC_SUSET
Definition guc.h:78
static void dlist_init(dlist_head *head)
Definition ilist.h:314
#define dlist_tail_element(type, membername, lhead)
Definition ilist.h:612
static bool dlist_is_empty(const dlist_head *head)
Definition ilist.h:336
static void dclist_push_head(dclist_head *head, dlist_node *node)
Definition ilist.h:693
void SignalHandlerForShutdownRequest(SIGNAL_ARGS)
Definition interrupt.c:104
volatile sig_atomic_t ShutdownRequestPending
Definition interrupt.c:28
void SignalHandlerForConfigReload(SIGNAL_ARGS)
Definition interrupt.c:61
void proc_exit(int code)
Definition ipc.c:105
void ResetLatch(Latch *latch)
Definition latch.c:374
int WaitLatch(Latch *latch, int wakeEvents, long timeout, uint32 wait_event_info)
Definition latch.c:172
bool LWLockAcquire(LWLock *lock, LWLockMode mode)
Definition lwlock.c:1176
void LWLockRelease(LWLock *lock)
Definition lwlock.c:1793
void LWLockReleaseAll(void)
Definition lwlock.c:1892
@ LW_SHARED
Definition lwlock.h:113
@ LW_EXCLUSIVE
Definition lwlock.h:112
void MemoryContextReset(MemoryContext context)
Definition mcxt.c:403
MemoryContext TopMemoryContext
Definition mcxt.c:166
MemoryContext PostmasterContext
Definition mcxt.c:168
void MemoryContextDelete(MemoryContext context)
Definition mcxt.c:472
#define AllocSetContextCreate
Definition memutils.h:129
#define ALLOCSET_DEFAULT_SIZES
Definition memutils.h:160
#define RESUME_INTERRUPTS()
Definition miscadmin.h:136
@ NormalProcessing
Definition miscadmin.h:472
@ InitProcessing
Definition miscadmin.h:471
#define GetProcessingMode()
Definition miscadmin.h:481
#define HOLD_INTERRUPTS()
Definition miscadmin.h:134
#define SetProcessingMode(mode)
Definition miscadmin.h:483
@ B_AUTOVAC_LAUNCHER
Definition miscadmin.h:344
BackendType MyBackendType
Definition miscinit.c:64
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition palloc.h:124
void SendPostmasterSignal(PMSignalReason reason)
Definition pmsignal.c:165
@ PMSIGNAL_START_AUTOVAC_WORKER
Definition pmsignal.h:40
#define pqsignal
Definition port.h:547
int PostAuthDelay
Definition postgres.c:100
void FloatExceptionHandler(SIGNAL_ARGS)
Definition postgres.c:3079
void StatementCancelHandler(SIGNAL_ARGS)
Definition postgres.c:3062
#define InvalidOid
void BaseInit(void)
Definition postinit.c:607
void InitPostgres(const char *in_dbname, Oid dboid, const char *username, Oid useroid, bits32 flags, char *out_dbname)
Definition postinit.c:710
static int fb(int x)
void procsignal_sigusr1_handler(SIGNAL_ARGS)
Definition procsignal.c:677
void init_ps_display(const char *fixed_part)
Definition ps_status.c:285
void ReleaseAuxProcessResources(bool isCommit)
Definition resowner.c:1016
ResourceOwner AuxProcessResourceOwner
Definition resowner.c:176
void pg_usleep(long microsec)
Definition signal.c:53
void AtEOXact_SMgr(void)
Definition smgr.c:1017
void InitProcess(void)
Definition proc.c:395
dclist_head av_freeWorkers
Definition autovacuum.c:297
WorkerInfo av_startingWorker
Definition autovacuum.c:299
sig_atomic_t av_signal[AutoVacNumSignals]
Definition autovacuum.c:295
TimestampTz wi_launchtime
Definition autovacuum.c:237
dlist_node wi_links
Definition autovacuum.c:233
PGPROC * wi_proc
Definition autovacuum.c:236
void disable_all_timeouts(bool keep_indicators)
Definition timeout.c:751
void InitializeTimeouts(void)
Definition timeout.c:470
static void pgstat_report_wait_end(void)
Definition wait_event.h:85
#define WL_TIMEOUT
#define WL_EXIT_ON_PM_DEATH
#define WL_LATCH_SET
#define SIGCHLD
Definition win32_port.h:168
#define SIGHUP
Definition win32_port.h:158
#define SIGPIPE
Definition win32_port.h:163
#define SIGUSR1
Definition win32_port.h:170
#define SIGUSR2
Definition win32_port.h:171
void AbortCurrentTransaction(void)
Definition xact.c:3472

References AbortCurrentTransaction(), ALLOCSET_DEFAULT_SIZES, AllocSetContextCreate, Assert, AtEOXact_Buffers(), AtEOXact_Files(), AtEOXact_HashTables(), AtEOXact_SMgr(), autovac_recalculate_workers_for_balance(), AutoVacForkFailed, AutoVacLauncherShutdown(), AutovacMemCxt, AutoVacRebalance, autovacuum_naptime, AutoVacuumingActive(), AutoVacuumShmem, AuxProcessResourceOwner, AutoVacuumShmemStruct::av_freeWorkers, AutoVacuumShmemStruct::av_launcherpid, AutoVacuumShmemStruct::av_signal, AutoVacuumShmemStruct::av_startingWorker, av_worker_available(), avl_sigusr2_handler(), B_AUTOVAC_LAUNCHER, BaseInit(), DatabaseList, DatabaseListCxt, dclist_push_head(), DEBUG1, disable_all_timeouts(), dlist_init(), dlist_is_empty(), dlist_tail_element, do_start_worker(), EmitErrorReport(), ereport, errmsg(), errmsg_internal(), error_context_stack, fb(), FloatExceptionHandler(), FlushErrorState(), GetCurrentTimestamp(), GetProcessingMode, got_SIGUSR2, HOLD_INTERRUPTS, init_ps_display(), InitializeTimeouts(), InitPostgres(), InitProcess(), InitProcessing, InvalidOid, launch_worker(), launcher_determine_sleep(), LW_EXCLUSIVE, LW_SHARED, LWLockAcquire(), LWLockRelease(), LWLockReleaseAll(), MemoryContextDelete(), MemoryContextReset(), MemoryContextSwitchTo(), Min, MyBackendType, MyLatch, MyProcPid, NormalProcessing, PG_exception_stack, pg_usleep(), pgaio_error_cleanup(), PGC_S_OVERRIDE, PGC_SUSET, pgstat_report_wait_end(), PMSIGNAL_START_AUTOVAC_WORKER, PostAuthDelay, PostmasterContext, pqsignal, proc_exit(), ProcessAutoVacLauncherInterrupts(), procsignal_sigusr1_handler(), QueryCancelPending, rebuild_database_list(), ReleaseAuxProcessResources(), ResetLatch(), RESUME_INTERRUPTS, SendPostmasterSignal(), SetConfigOption(), SetProcessingMode, ShutdownRequestPending, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SignalHandlerForShutdownRequest(), SIGPIPE, SIGUSR1, SIGUSR2, StatementCancelHandler(), TimestampDifferenceExceeds(), TopMemoryContext, UnBlockSig, UnlockBuffers(), WaitLatch(), WARNING, WorkerInfoData::wi_dboid, WorkerInfoData::wi_launchtime, WorkerInfoData::wi_links, WorkerInfoData::wi_proc, WorkerInfoData::wi_sharedrel, WorkerInfoData::wi_tableoid, WL_EXIT_ON_PM_DEATH, WL_LATCH_SET, and WL_TIMEOUT.

◆ AutoVacuumingActive()

bool AutoVacuumingActive ( void  )
extern

◆ AutoVacuumRequestWork()

bool AutoVacuumRequestWork ( AutoVacuumWorkItemType  type,
Oid  relationId,
BlockNumber  blkno 
)
extern

Definition at line 3304 of file autovacuum.c.

3306{
3307 int i;
3308 bool result = false;
3309
3311
3312 /*
3313 * Locate an unused work item and fill it with the given data.
3314 */
3315 for (i = 0; i < NUM_WORKITEMS; i++)
3316 {
3318
3319 if (workitem->avw_used)
3320 continue;
3321
3322 workitem->avw_used = true;
3323 workitem->avw_active = false;
3324 workitem->avw_type = type;
3325 workitem->avw_database = MyDatabaseId;
3326 workitem->avw_relation = relationId;
3327 workitem->avw_blockNumber = blkno;
3328 result = true;
3329
3330 /* done */
3331 break;
3332 }
3333
3335
3336 return result;
3337}
#define NUM_WORKITEMS
Definition autovacuum.c:273
Oid MyDatabaseId
Definition globals.c:94
int i
Definition isn.c:77
AutoVacuumWorkItem av_workItems[NUM_WORKITEMS]
Definition autovacuum.c:300
const char * type

References AutoVacuumShmem, AutoVacuumShmemStruct::av_workItems, AutoVacuumWorkItem::avw_used, fb(), i, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MyDatabaseId, NUM_WORKITEMS, and type.

Referenced by brininsert().

◆ AutoVacuumShmemInit()

void AutoVacuumShmemInit ( void  )
extern

Definition at line 3382 of file autovacuum.c.

3383{
3384 bool found;
3385
3387 ShmemInitStruct("AutoVacuum Data",
3389 &found);
3390
3391 if (!IsUnderPostmaster)
3392 {
3393 WorkerInfo worker;
3394 int i;
3395
3396 Assert(!found);
3397
3404
3405 worker = (WorkerInfo) ((char *) AutoVacuumShmem +
3407
3408 /* initialize the WorkerInfo free list */
3409 for (i = 0; i < autovacuum_worker_slots; i++)
3410 {
3412 &worker[i].wi_links);
3413 pg_atomic_init_flag(&worker[i].wi_dobalance);
3414 }
3415
3417
3418 }
3419 else
3420 Assert(found);
3421}
static void pg_atomic_init_u32(volatile pg_atomic_uint32 *ptr, uint32 val)
Definition atomics.h:219
static void pg_atomic_init_flag(volatile pg_atomic_flag *ptr)
Definition atomics.h:168
int autovacuum_worker_slots
Definition autovacuum.c:118
Size AutoVacuumShmemSize(void)
struct WorkerInfoData * WorkerInfo
Definition autovacuum.c:242
#define MAXALIGN(LEN)
Definition c.h:826
bool IsUnderPostmaster
Definition globals.c:120
static void dclist_init(dclist_head *head)
Definition ilist.h:671
void * ShmemInitStruct(const char *name, Size size, bool *foundPtr)
Definition shmem.c:389
pg_atomic_uint32 av_nworkersForBalance
Definition autovacuum.c:301
dlist_head av_runningWorkers
Definition autovacuum.c:298

References Assert, autovacuum_worker_slots, AutoVacuumShmem, AutoVacuumShmemSize(), AutoVacuumShmemStruct::av_freeWorkers, AutoVacuumShmemStruct::av_launcherpid, AutoVacuumShmemStruct::av_nworkersForBalance, AutoVacuumShmemStruct::av_runningWorkers, AutoVacuumShmemStruct::av_startingWorker, AutoVacuumShmemStruct::av_workItems, dclist_init(), dclist_push_head(), dlist_init(), fb(), i, IsUnderPostmaster, MAXALIGN, NUM_WORKITEMS, pg_atomic_init_flag(), pg_atomic_init_u32(), ShmemInitStruct(), and WorkerInfoData::wi_links.

Referenced by CreateOrAttachShmemStructs().

◆ AutoVacuumShmemSize()

Size AutoVacuumShmemSize ( void  )
extern

Definition at line 3363 of file autovacuum.c.

3364{
3365 Size size;
3366
3367 /*
3368 * Need the fixed struct and the array of WorkerInfoData.
3369 */
3370 size = sizeof(AutoVacuumShmemStruct);
3371 size = MAXALIGN(size);
3373 sizeof(WorkerInfoData)));
3374 return size;
3375}
size_t Size
Definition c.h:619
Size add_size(Size s1, Size s2)
Definition shmem.c:495
Size mul_size(Size s1, Size s2)
Definition shmem.c:510

References add_size(), autovacuum_worker_slots, MAXALIGN, and mul_size().

Referenced by AutoVacuumShmemInit(), and CalculateShmemSize().

◆ AutoVacWorkerFailed()

void AutoVacWorkerFailed ( void  )
extern

Definition at line 1365 of file autovacuum.c.

1366{
1368}

References AutoVacForkFailed, AutoVacuumShmem, and AutoVacuumShmemStruct::av_signal.

Referenced by StartAutovacuumWorker().

◆ AutoVacWorkerMain()

pg_noreturn void AutoVacWorkerMain ( const void startup_data,
size_t  startup_data_len 
)
extern

Definition at line 1387 of file autovacuum.c.

1388{
1390 Oid dbid;
1391
1393
1394 /* Release postmaster's working memory context */
1396 {
1399 }
1400
1403
1405
1406 /*
1407 * Set up signal handlers. We operate on databases much like a regular
1408 * backend, so we use the same signal handling. See equivalent code in
1409 * tcop/postgres.c.
1410 */
1412
1413 /*
1414 * SIGINT is used to signal canceling the current table's vacuum; SIGTERM
1415 * means abort and exit cleanly, and SIGQUIT means abandon ship.
1416 */
1419 /* SIGQUIT handler was already set up by InitPostmasterChild */
1420
1421 InitializeTimeouts(); /* establishes SIGALRM handler */
1422
1428
1429 /*
1430 * Create a per-backend PGPROC struct in shared memory. We must do this
1431 * before we can use LWLocks or access any shared memory.
1432 */
1433 InitProcess();
1434
1435 /* Early initialization */
1436 BaseInit();
1437
1438 /*
1439 * If an exception is encountered, processing resumes here.
1440 *
1441 * Unlike most auxiliary processes, we don't attempt to continue
1442 * processing after an error; we just clean up and exit. The autovac
1443 * launcher is responsible for spawning another worker later.
1444 *
1445 * Note that we use sigsetjmp(..., 1), so that the prevailing signal mask
1446 * (to wit, BlockSig) will be restored when longjmp'ing to here. Thus,
1447 * signals other than SIGQUIT will be blocked until we exit. It might
1448 * seem that this policy makes the HOLD_INTERRUPTS() call redundant, but
1449 * it is not since InterruptPending might be set already.
1450 */
1451 if (sigsetjmp(local_sigjmp_buf, 1) != 0)
1452 {
1453 /* since not using PG_TRY, must reset error stack by hand */
1455
1456 /* Prevents interrupts while cleaning up */
1458
1459 /* Report the error to the server log */
1461
1462 /*
1463 * We can now go away. Note that because we called InitProcess, a
1464 * callback was registered to do ProcKill, which will clean up
1465 * necessary state.
1466 */
1467 proc_exit(0);
1468 }
1469
1470 /* We can now handle ereport(ERROR) */
1472
1474
1475 /*
1476 * Set always-secure search path, so malicious users can't redirect user
1477 * code (e.g. pg_index.indexprs). (That code runs in a
1478 * SECURITY_RESTRICTED_OPERATION sandbox, so malicious users could not
1479 * take control of the entire autovacuum worker in any case.)
1480 */
1481 SetConfigOption("search_path", "", PGC_SUSET, PGC_S_OVERRIDE);
1482
1483 /*
1484 * Force zero_damaged_pages OFF in the autovac process, even if it is set
1485 * in postgresql.conf. We don't really want such a dangerous option being
1486 * applied non-interactively.
1487 */
1488 SetConfigOption("zero_damaged_pages", "false", PGC_SUSET, PGC_S_OVERRIDE);
1489
1490 /*
1491 * Force settable timeouts off to avoid letting these settings prevent
1492 * regular maintenance from being executed.
1493 */
1494 SetConfigOption("statement_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
1495 SetConfigOption("transaction_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
1496 SetConfigOption("lock_timeout", "0", PGC_SUSET, PGC_S_OVERRIDE);
1497 SetConfigOption("idle_in_transaction_session_timeout", "0",
1499
1500 /*
1501 * Force default_transaction_isolation to READ COMMITTED. We don't want
1502 * to pay the overhead of serializable mode, nor add any risk of causing
1503 * deadlocks or delaying other transactions.
1504 */
1505 SetConfigOption("default_transaction_isolation", "read committed",
1507
1508 /*
1509 * Force synchronous replication off to allow regular maintenance even if
1510 * we are waiting for standbys to connect. This is important to ensure we
1511 * aren't blocked from performing anti-wraparound tasks.
1512 */
1514 SetConfigOption("synchronous_commit", "local",
1516
1517 /*
1518 * Even when system is configured to use a different fetch consistency,
1519 * for autovac we always want fresh stats.
1520 */
1521 SetConfigOption("stats_fetch_consistency", "none", PGC_SUSET, PGC_S_OVERRIDE);
1522
1523 /*
1524 * Get the info about the database we're going to work on.
1525 */
1527
1528 /*
1529 * beware of startingWorker being INVALID; this should normally not
1530 * happen, but if a worker fails after forking and before this, the
1531 * launcher might have decided to remove it from the queue and start
1532 * again.
1533 */
1535 {
1537 dbid = MyWorkerInfo->wi_dboid;
1539
1540 /* insert into the running list */
1543
1544 /*
1545 * remove from the "starting" pointer, so that the launcher can start
1546 * a new worker if required
1547 */
1550
1552
1553 /* wake up the launcher */
1556 }
1557 else
1558 {
1559 /* no worker entry for me, go away */
1560 elog(WARNING, "autovacuum worker started without a worker entry");
1561 dbid = InvalidOid;
1563 }
1564
1565 if (OidIsValid(dbid))
1566 {
1567 char dbname[NAMEDATALEN];
1568
1569 /*
1570 * Report autovac startup to the cumulative stats system. We
1571 * deliberately do this before InitPostgres, so that the
1572 * last_autovac_time will get updated even if the connection attempt
1573 * fails. This is to prevent autovac from getting "stuck" repeatedly
1574 * selecting an unopenable database, rather than making any progress
1575 * on stuff it can connect to.
1576 */
1578
1579 /*
1580 * Connect to the selected database, specifying no particular user,
1581 * and ignoring datallowconn. Collect the database's name for
1582 * display.
1583 *
1584 * Note: if we have selected a just-deleted database (due to using
1585 * stale stats info), we'll fail and exit here.
1586 */
1589 dbname);
1593 (errmsg_internal("autovacuum: processing database \"%s\"", dbname)));
1594
1595 if (PostAuthDelay)
1596 pg_usleep(PostAuthDelay * 1000000L);
1597
1598 /* And do an appropriate amount of work */
1601 do_autovacuum();
1602 }
1603
1604 /* All done, go away */
1605 proc_exit(0);
1606}
static void FreeWorkerInfo(int code, Datum arg)
static TransactionId recentXid
Definition autovacuum.c:158
static void do_autovacuum(void)
static MultiXactId recentMulti
Definition autovacuum.c:159
static WorkerInfo MyWorkerInfo
Definition autovacuum.c:324
#define OidIsValid(objectId)
Definition c.h:788
#define elog(elevel,...)
Definition elog.h:226
static void dlist_push_head(dlist_head *head, dlist_node *node)
Definition ilist.h:347
void on_shmem_exit(pg_on_exit_callback function, Datum arg)
Definition ipc.c:372
@ B_AUTOVAC_WORKER
Definition miscadmin.h:345
#define INIT_PG_OVERRIDE_ALLOW_CONNS
Definition miscadmin.h:500
MultiXactId ReadNextMultiXactId(void)
Definition multixact.c:620
#define NAMEDATALEN
#define die(msg)
void pgstat_report_autovac(Oid dboid)
unsigned int Oid
static void set_ps_display(const char *activity)
Definition ps_status.h:40
PGPROC * MyProc
Definition proc.c:67
char * dbname
Definition streamutil.c:49
static TransactionId ReadNextTransactionId(void)
Definition transam.h:377
#define kill(pid, sig)
Definition win32_port.h:490
int synchronous_commit
Definition xact.c:88
@ SYNCHRONOUS_COMMIT_LOCAL_FLUSH
Definition xact.h:72

References Assert, AutoVacuumShmem, AutoVacuumShmemStruct::av_launcherpid, AutoVacuumShmemStruct::av_runningWorkers, AutoVacuumShmemStruct::av_startingWorker, B_AUTOVAC_WORKER, BaseInit(), dbname, DEBUG1, die, dlist_push_head(), do_autovacuum(), elog, EmitErrorReport(), ereport, errmsg_internal(), error_context_stack, fb(), FloatExceptionHandler(), FreeWorkerInfo(), GetProcessingMode, HOLD_INTERRUPTS, INIT_PG_OVERRIDE_ALLOW_CONNS, init_ps_display(), InitializeTimeouts(), InitPostgres(), InitProcess(), InitProcessing, InvalidOid, kill, LW_EXCLUSIVE, LWLockAcquire(), LWLockRelease(), MemoryContextDelete(), MyBackendType, MyProc, MyWorkerInfo, NAMEDATALEN, NormalProcessing, OidIsValid, on_shmem_exit(), PG_exception_stack, pg_usleep(), PGC_S_OVERRIDE, PGC_SUSET, pgstat_report_autovac(), PostAuthDelay, PostmasterContext, pqsignal, proc_exit(), procsignal_sigusr1_handler(), ReadNextMultiXactId(), ReadNextTransactionId(), recentMulti, recentXid, set_ps_display(), SetConfigOption(), SetProcessingMode, SIGCHLD, SIGHUP, SignalHandlerForConfigReload(), SIGPIPE, SIGUSR1, SIGUSR2, StatementCancelHandler(), synchronous_commit, SYNCHRONOUS_COMMIT_LOCAL_FLUSH, UnBlockSig, WARNING, WorkerInfoData::wi_dboid, WorkerInfoData::wi_links, and WorkerInfoData::wi_proc.

Variable Documentation

◆ autovacuum_anl_scale

PGDLLIMPORT double autovacuum_anl_scale
extern

Definition at line 128 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_anl_thresh

PGDLLIMPORT int autovacuum_anl_thresh
extern

Definition at line 127 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_freeze_max_age

◆ autovacuum_max_workers

PGDLLIMPORT int autovacuum_max_workers
extern

◆ autovacuum_multixact_freeze_max_age

PGDLLIMPORT int autovacuum_multixact_freeze_max_age
extern

◆ autovacuum_naptime

◆ autovacuum_start_daemon

PGDLLIMPORT bool autovacuum_start_daemon
extern

Definition at line 117 of file autovacuum.c.

Referenced by autovac_init(), AutoVacuumingActive(), and removable_cutoff().

◆ autovacuum_vac_cost_delay

PGDLLIMPORT double autovacuum_vac_cost_delay
extern

Definition at line 132 of file autovacuum.c.

Referenced by VacuumUpdateCosts().

◆ autovacuum_vac_cost_limit

PGDLLIMPORT int autovacuum_vac_cost_limit
extern

Definition at line 133 of file autovacuum.c.

Referenced by AutoVacuumUpdateCostLimit().

◆ autovacuum_vac_ins_scale

PGDLLIMPORT double autovacuum_vac_ins_scale
extern

Definition at line 126 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_ins_thresh

PGDLLIMPORT int autovacuum_vac_ins_thresh
extern

Definition at line 125 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_max_thresh

PGDLLIMPORT int autovacuum_vac_max_thresh
extern

Definition at line 123 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_scale

PGDLLIMPORT double autovacuum_vac_scale
extern

Definition at line 124 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_vac_thresh

PGDLLIMPORT int autovacuum_vac_thresh
extern

Definition at line 122 of file autovacuum.c.

Referenced by relation_needs_vacanalyze().

◆ autovacuum_work_mem

PGDLLIMPORT int autovacuum_work_mem
extern

Definition at line 120 of file autovacuum.c.

Referenced by dead_items_alloc(), and ginInsertCleanup().

◆ autovacuum_worker_slots

◆ Log_autoanalyze_min_duration

PGDLLIMPORT int Log_autoanalyze_min_duration
extern

Definition at line 136 of file autovacuum.c.

Referenced by table_recheck_autovac().

◆ Log_autovacuum_min_duration

PGDLLIMPORT int Log_autovacuum_min_duration
extern

Definition at line 135 of file autovacuum.c.

Referenced by table_recheck_autovac().