2004-05-30 00:48:23 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* bgwriter.c
|
|
|
|
*
|
2007-01-17 01:17:21 +01:00
|
|
|
* The background writer (bgwriter) is new as of Postgres 8.0. It attempts
|
2004-05-30 00:48:23 +02:00
|
|
|
* to keep regular backends from having to write out dirty shared buffers
|
|
|
|
* (which they would only do when needing to free a shared buffer to read in
|
|
|
|
* another page). In the best scenario all writes from shared buffers will
|
2004-08-29 07:07:03 +02:00
|
|
|
* be issued by the background writer process. However, regular backends are
|
2004-05-30 00:48:23 +02:00
|
|
|
* still empowered to issue writes if the bgwriter fails to maintain enough
|
|
|
|
* clean shared buffers.
|
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* The bgwriter is also charged with handling all checkpoints. It will
|
2004-05-30 00:48:23 +02:00
|
|
|
* automatically dispatch a checkpoint after a certain amount of time has
|
|
|
|
* elapsed since the last one, and it can be signaled to perform requested
|
|
|
|
* checkpoints as well. (The GUC parameter that mandates a checkpoint every
|
|
|
|
* so many WAL segments is implemented by having backends signal the bgwriter
|
|
|
|
* when they fill WAL segments; the bgwriter itself doesn't watch for the
|
|
|
|
* condition.)
|
|
|
|
*
|
|
|
|
* The bgwriter is started by the postmaster as soon as the startup subprocess
|
|
|
|
* finishes. It remains alive until the postmaster commands it to terminate.
|
|
|
|
* Normal termination is by SIGUSR2, which instructs the bgwriter to execute
|
2004-08-29 07:07:03 +02:00
|
|
|
* a shutdown checkpoint and then exit(0). (All backends must be stopped
|
2004-05-30 00:48:23 +02:00
|
|
|
* before SIGUSR2 is issued!) Emergency termination is by SIGQUIT; like any
|
|
|
|
* backend, the bgwriter will simply abort and exit on SIGQUIT.
|
|
|
|
*
|
|
|
|
* If the bgwriter exits unexpectedly, the postmaster treats that the same
|
|
|
|
* as a backend crash: shared memory may be corrupted, so remaining backends
|
2004-05-31 05:48:10 +02:00
|
|
|
* should be killed by SIGQUIT and then a recovery cycle started. (Even if
|
|
|
|
* shared memory isn't corrupted, we have lost information about which
|
|
|
|
* files need to be fsync'd for the next checkpoint, and so a system
|
|
|
|
* restart needs to be forced.)
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
|
|
|
*
|
2007-01-05 23:20:05 +01:00
|
|
|
* Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2007-09-16 18:33:04 +02:00
|
|
|
* $PostgreSQL: pgsql/src/backend/postmaster/bgwriter.c,v 1.43 2007/09/16 16:33:04 tgl Exp $
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include <signal.h>
|
2007-06-28 02:02:40 +02:00
|
|
|
#include <sys/time.h>
|
2004-06-03 04:08:07 +02:00
|
|
|
#include <time.h>
|
2006-11-21 21:59:53 +01:00
|
|
|
#include <unistd.h>
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
#include "access/xlog_internal.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "libpq/pqsignal.h"
|
|
|
|
#include "miscadmin.h"
|
2007-03-30 20:34:56 +02:00
|
|
|
#include "pgstat.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "postmaster/bgwriter.h"
|
2005-12-08 20:19:22 +01:00
|
|
|
#include "storage/fd.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "storage/freespace.h"
|
|
|
|
#include "storage/ipc.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "storage/lwlock.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "storage/pmsignal.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "storage/shmem.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "storage/smgr.h"
|
2007-06-28 02:02:40 +02:00
|
|
|
#include "storage/spin.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "tcop/tcopprot.h"
|
|
|
|
#include "utils/guc.h"
|
2004-07-31 02:45:57 +02:00
|
|
|
#include "utils/memutils.h"
|
2005-12-08 20:19:22 +01:00
|
|
|
#include "utils/resowner.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/*----------
|
2004-05-30 00:48:23 +02:00
|
|
|
* Shared memory area for communication between bgwriter and backends
|
2004-05-31 05:48:10 +02:00
|
|
|
*
|
|
|
|
* The ckpt counters allow backends to watch for completion of a checkpoint
|
|
|
|
* request they send. Here's how it works:
|
2007-06-28 02:02:40 +02:00
|
|
|
* * At start of a checkpoint, bgwriter reads (and clears) the request flags
|
|
|
|
* and increments ckpt_started, while holding ckpt_lck.
|
2004-05-31 05:48:10 +02:00
|
|
|
* * On completion of a checkpoint, bgwriter sets ckpt_done to
|
|
|
|
* equal ckpt_started.
|
2007-06-28 02:02:40 +02:00
|
|
|
* * On failure of a checkpoint, bgwriter increments ckpt_failed
|
|
|
|
* and sets ckpt_done to equal ckpt_started.
|
|
|
|
*
|
|
|
|
* The algorithm for backends is:
|
|
|
|
* 1. Record current values of ckpt_failed and ckpt_started, and
|
|
|
|
* set request flags, while holding ckpt_lck.
|
2004-05-31 05:48:10 +02:00
|
|
|
* 2. Send signal to request checkpoint.
|
|
|
|
* 3. Sleep until ckpt_started changes. Now you know a checkpoint has
|
|
|
|
* begun since you started this algorithm (although *not* that it was
|
2007-06-28 02:02:40 +02:00
|
|
|
* specifically initiated by your signal), and that it is using your flags.
|
2004-05-31 05:48:10 +02:00
|
|
|
* 4. Record new value of ckpt_started.
|
|
|
|
* 5. Sleep until ckpt_done >= saved value of ckpt_started. (Use modulo
|
|
|
|
* arithmetic here in case counters wrap around.) Now you know a
|
|
|
|
* checkpoint has started and completed, but not whether it was
|
|
|
|
* successful.
|
|
|
|
* 6. If ckpt_failed is different from the originally saved value,
|
|
|
|
* assume request failed; otherwise it was definitely successful.
|
|
|
|
*
|
2007-06-28 02:02:40 +02:00
|
|
|
* ckpt_flags holds the OR of the checkpoint request flags sent by all
|
|
|
|
* requesting backends since the last checkpoint start. The flags are
|
|
|
|
* chosen so that OR'ing is the correct way to combine multiple requests.
|
2005-06-30 02:00:52 +02:00
|
|
|
*
|
2004-05-31 05:48:10 +02:00
|
|
|
* The requests array holds fsync requests sent by backends and not yet
|
2005-06-30 02:00:52 +02:00
|
|
|
* absorbed by the bgwriter. Unlike the checkpoint fields, the requests
|
|
|
|
* fields are protected by BgWriterCommLock.
|
2004-05-31 05:48:10 +02:00
|
|
|
*----------
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2004-05-31 05:48:10 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
RelFileNode rnode;
|
2007-01-17 17:25:01 +01:00
|
|
|
BlockNumber segno; /* see md.c for special values */
|
2007-01-17 01:17:21 +01:00
|
|
|
/* might add a real request-type field later; not needed yet */
|
2004-05-31 05:48:10 +02:00
|
|
|
} BgWriterRequest;
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
typedef struct
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
pid_t bgwriter_pid; /* PID of bgwriter (0 if not started) */
|
2004-05-31 05:48:10 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
slock_t ckpt_lck; /* protects all the ckpt_* fields */
|
|
|
|
|
|
|
|
int ckpt_started; /* advances when checkpoint starts */
|
|
|
|
int ckpt_done; /* advances when checkpoint done */
|
|
|
|
int ckpt_failed; /* advances when checkpoint fails */
|
2004-05-31 05:48:10 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
int ckpt_flags; /* checkpoint flags, as defined in xlog.h */
|
2005-06-30 02:00:52 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
int num_requests; /* current # of requests */
|
|
|
|
int max_requests; /* allocated array size */
|
|
|
|
BgWriterRequest requests[1]; /* VARIABLE LENGTH ARRAY */
|
2004-05-30 00:48:23 +02:00
|
|
|
} BgWriterShmemStruct;
|
|
|
|
|
|
|
|
static BgWriterShmemStruct *BgWriterShmem;
|
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/* interval for calling AbsorbFsyncRequests in CheckpointWriteDelay */
|
|
|
|
#define WRITES_PER_ABSORB 1000
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
|
|
|
* GUC parameters
|
|
|
|
*/
|
|
|
|
int BgWriterDelay = 200;
|
|
|
|
int CheckPointTimeout = 300;
|
|
|
|
int CheckPointWarning = 30;
|
2007-06-28 02:02:40 +02:00
|
|
|
double CheckPointCompletionTarget = 0.5;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Flags set by interrupt handlers for later service in the main loop.
|
|
|
|
*/
|
|
|
|
static volatile sig_atomic_t got_SIGHUP = false;
|
|
|
|
static volatile sig_atomic_t checkpoint_requested = false;
|
|
|
|
static volatile sig_atomic_t shutdown_requested = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Private state
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
static bool am_bg_writer = false;
|
2004-05-31 05:48:10 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
static bool ckpt_active = false;
|
2004-05-31 05:48:10 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/* these values are valid when ckpt_active is true: */
|
|
|
|
static time_t ckpt_start_time;
|
|
|
|
static XLogRecPtr ckpt_start_recptr;
|
|
|
|
static double ckpt_cached_elapsed;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
static time_t last_checkpoint_time;
|
2006-08-18 01:04:10 +02:00
|
|
|
static time_t last_xlog_switch_time;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/* Prototypes for private functions */
|
|
|
|
|
|
|
|
static void CheckArchiveTimeout(void);
|
|
|
|
static void BgWriterNap(void);
|
|
|
|
static bool IsCheckpointOnSchedule(double progress);
|
|
|
|
static bool ImmediateCheckpointRequested(void);
|
|
|
|
|
|
|
|
/* Signal handlers */
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
static void bg_quickdie(SIGNAL_ARGS);
|
|
|
|
static void BgSigHupHandler(SIGNAL_ARGS);
|
|
|
|
static void ReqCheckpointHandler(SIGNAL_ARGS);
|
|
|
|
static void ReqShutdownHandler(SIGNAL_ARGS);
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Main entry point for bgwriter process
|
|
|
|
*
|
|
|
|
* This is invoked from BootstrapMain, which has already created the basic
|
|
|
|
* execution environment, but not enabled signals yet.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BackgroundWriterMain(void)
|
|
|
|
{
|
2004-07-31 02:45:57 +02:00
|
|
|
sigjmp_buf local_sigjmp_buf;
|
2005-09-13 00:20:16 +02:00
|
|
|
MemoryContext bgwriter_context;
|
2004-07-31 02:45:57 +02:00
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
BgWriterShmem->bgwriter_pid = MyProcPid;
|
2004-05-31 05:48:10 +02:00
|
|
|
am_bg_writer = true;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2006-11-21 21:59:53 +01:00
|
|
|
/*
|
|
|
|
* If possible, make this process a group leader, so that the postmaster
|
|
|
|
* can signal any child processes too. (bgwriter probably never has
|
|
|
|
* any child processes, but for consistency we make all postmaster
|
|
|
|
* child processes do this.)
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_SETSID
|
|
|
|
if (setsid() < 0)
|
|
|
|
elog(FATAL, "setsid() failed: %m");
|
|
|
|
#endif
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
|
|
|
* Properly accept or ignore signals the postmaster might send us
|
|
|
|
*
|
|
|
|
* Note: we deliberately ignore SIGTERM, because during a standard Unix
|
2004-08-29 07:07:03 +02:00
|
|
|
* system shutdown cycle, init will SIGTERM all processes at once. We
|
2005-10-15 04:49:52 +02:00
|
|
|
* want to wait for the backends to exit, whereupon the postmaster will
|
|
|
|
* tell us it's okay to shut down (via SIGUSR2).
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* SIGUSR1 is presently unused; keep it spare in case someday we want this
|
|
|
|
* process to participate in sinval messaging.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
pqsignal(SIGHUP, BgSigHupHandler); /* set flag to read config file */
|
|
|
|
pqsignal(SIGINT, ReqCheckpointHandler); /* request checkpoint */
|
2004-08-29 07:07:03 +02:00
|
|
|
pqsignal(SIGTERM, SIG_IGN); /* ignore SIGTERM */
|
2004-05-30 00:48:23 +02:00
|
|
|
pqsignal(SIGQUIT, bg_quickdie); /* hard crash time */
|
|
|
|
pqsignal(SIGALRM, SIG_IGN);
|
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
2004-08-29 07:07:03 +02:00
|
|
|
pqsignal(SIGUSR1, SIG_IGN); /* reserve for sinval */
|
2004-05-30 00:48:23 +02:00
|
|
|
pqsignal(SIGUSR2, ReqShutdownHandler); /* request shutdown */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset some signals that are accepted by postmaster but not here
|
|
|
|
*/
|
|
|
|
pqsignal(SIGCHLD, SIG_DFL);
|
|
|
|
pqsignal(SIGTTIN, SIG_DFL);
|
|
|
|
pqsignal(SIGTTOU, SIG_DFL);
|
|
|
|
pqsignal(SIGCONT, SIG_DFL);
|
|
|
|
pqsignal(SIGWINCH, SIG_DFL);
|
|
|
|
|
|
|
|
/* We allow SIGQUIT (quickdie) at all times */
|
|
|
|
#ifdef HAVE_SIGPROCMASK
|
|
|
|
sigdelset(&BlockSig, SIGQUIT);
|
|
|
|
#else
|
|
|
|
BlockSig &= ~(sigmask(SIGQUIT));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Initialize so that first time-driven event happens at the correct time.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2006-08-18 01:04:10 +02:00
|
|
|
last_checkpoint_time = last_xlog_switch_time = time(NULL);
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2005-12-08 20:19:22 +01:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Create a resource owner to keep track of our resources (currently only
|
|
|
|
* buffer pins).
|
2005-12-08 20:19:22 +01:00
|
|
|
*/
|
|
|
|
CurrentResourceOwner = ResourceOwnerCreate(NULL, "Background Writer");
|
|
|
|
|
2005-09-13 00:20:16 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Create a memory context that we will do all our work in. We do this so
|
|
|
|
* that we can reset the context during error recovery and thereby avoid
|
|
|
|
* possible memory leaks. Formerly this code just ran in
|
2005-09-13 00:20:16 +02:00
|
|
|
* TopMemoryContext, but resetting that would be a really bad idea.
|
|
|
|
*/
|
|
|
|
bgwriter_context = AllocSetContextCreate(TopMemoryContext,
|
|
|
|
"Background Writer",
|
|
|
|
ALLOCSET_DEFAULT_MINSIZE,
|
|
|
|
ALLOCSET_DEFAULT_INITSIZE,
|
|
|
|
ALLOCSET_DEFAULT_MAXSIZE);
|
|
|
|
MemoryContextSwitchTo(bgwriter_context);
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
|
|
|
* If an exception is encountered, processing resumes here.
|
2004-07-31 02:45:57 +02:00
|
|
|
*
|
|
|
|
* See notes in postgres.c about the design of this coding.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2004-07-31 02:45:57 +02:00
|
|
|
if (sigsetjmp(local_sigjmp_buf, 1) != 0)
|
2004-05-30 00:48:23 +02:00
|
|
|
{
|
2004-07-31 02:45:57 +02:00
|
|
|
/* Since not using PG_TRY, must reset error stack by hand */
|
|
|
|
error_context_stack = NULL;
|
|
|
|
|
|
|
|
/* Prevent interrupts while cleaning up */
|
|
|
|
HOLD_INTERRUPTS();
|
|
|
|
|
|
|
|
/* Report the error to the server log */
|
|
|
|
EmitErrorReport();
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These operations are really just a minimal subset of
|
2004-08-29 07:07:03 +02:00
|
|
|
* AbortTransaction(). We don't have very many resources to worry
|
2005-12-08 20:19:22 +01:00
|
|
|
* about in bgwriter, but we do have LWLocks, buffers, and temp files.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
LWLockReleaseAll();
|
|
|
|
AbortBufferIO();
|
|
|
|
UnlockBuffers();
|
2005-12-08 20:19:22 +01:00
|
|
|
/* buffer pins are released here: */
|
|
|
|
ResourceOwnerRelease(CurrentResourceOwner,
|
|
|
|
RESOURCE_RELEASE_BEFORE_LOCKS,
|
|
|
|
false, true);
|
|
|
|
/* we needn't bother with the other ResourceOwnerRelease phases */
|
|
|
|
AtEOXact_Buffers(false);
|
|
|
|
AtEOXact_Files();
|
2007-09-11 19:15:33 +02:00
|
|
|
AtEOXact_HashTables(false);
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/* Warn any waiting backends that the checkpoint failed. */
|
|
|
|
if (ckpt_active)
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile BgWriterShmemStruct *bgs = BgWriterShmem;
|
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
SpinLockAcquire(&bgs->ckpt_lck);
|
2004-05-31 05:48:10 +02:00
|
|
|
bgs->ckpt_failed++;
|
|
|
|
bgs->ckpt_done = bgs->ckpt_started;
|
2007-06-28 02:02:40 +02:00
|
|
|
SpinLockRelease(&bgs->ckpt_lck);
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
ckpt_active = false;
|
|
|
|
}
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Now return to normal top-level context and clear ErrorContext for
|
|
|
|
* next time.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2005-09-13 00:20:16 +02:00
|
|
|
MemoryContextSwitchTo(bgwriter_context);
|
2004-07-31 02:45:57 +02:00
|
|
|
FlushErrorState();
|
|
|
|
|
2005-09-13 00:20:16 +02:00
|
|
|
/* Flush any leaked data in the top-level context */
|
|
|
|
MemoryContextResetAndDeleteChildren(bgwriter_context);
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/* Now we can allow interrupts again */
|
2004-05-30 00:48:23 +02:00
|
|
|
RESUME_INTERRUPTS();
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Sleep at least 1 second after any error. A write error is likely
|
|
|
|
* to be repeated, and we don't want to be filling the error logs as
|
|
|
|
* fast as we can.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
pg_usleep(1000000L);
|
2006-12-01 20:55:28 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Close all open files after any error. This is helpful on Windows,
|
|
|
|
* where holding deleted files open causes various strange errors.
|
|
|
|
* It's not clear we need it elsewhere, but shouldn't hurt.
|
|
|
|
*/
|
|
|
|
smgrcloseall();
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
|
2004-07-31 02:45:57 +02:00
|
|
|
/* We can now handle ereport(ERROR) */
|
|
|
|
PG_exception_stack = &local_sigjmp_buf;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Unblock signals (they were blocked when the postmaster forked us)
|
|
|
|
*/
|
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Loop forever
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
bool do_checkpoint = false;
|
2007-06-30 21:12:02 +02:00
|
|
|
int flags = 0;
|
2004-05-30 00:48:23 +02:00
|
|
|
time_t now;
|
|
|
|
int elapsed_secs;
|
|
|
|
|
|
|
|
/*
|
2004-05-31 05:48:10 +02:00
|
|
|
* Emergency bailout if postmaster has died. This is to avoid the
|
|
|
|
* necessity for manual cleanup of all postmaster children.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2004-05-31 05:48:10 +02:00
|
|
|
if (!PostmasterIsAlive(true))
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Process any requests or signals received recently.
|
|
|
|
*/
|
|
|
|
AbsorbFsyncRequests();
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
if (got_SIGHUP)
|
|
|
|
{
|
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
|
|
|
}
|
|
|
|
if (checkpoint_requested)
|
|
|
|
{
|
|
|
|
checkpoint_requested = false;
|
|
|
|
do_checkpoint = true;
|
2007-03-30 20:34:56 +02:00
|
|
|
BgWriterStats.m_requested_checkpoints++;
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
if (shutdown_requested)
|
|
|
|
{
|
2006-11-30 19:29:12 +01:00
|
|
|
/*
|
|
|
|
* From here on, elog(ERROR) should end with exit(1), not send
|
|
|
|
* control back to the sigsetjmp block above
|
|
|
|
*/
|
|
|
|
ExitOnAnyError = true;
|
|
|
|
/* Close down the database */
|
2004-05-30 00:48:23 +02:00
|
|
|
ShutdownXLOG(0, 0);
|
|
|
|
DumpFreeSpaceMap(0, 0);
|
|
|
|
/* Normal exit from the bgwriter is here */
|
|
|
|
proc_exit(0); /* done */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2007-06-30 21:12:02 +02:00
|
|
|
* Force a checkpoint if too much time has elapsed since the
|
|
|
|
* last one. Note that we count a timed checkpoint in stats only
|
|
|
|
* when this occurs without an external request, but we set the
|
|
|
|
* CAUSE_TIME flag bit even if there is also an external request.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
now = time(NULL);
|
|
|
|
elapsed_secs = now - last_checkpoint_time;
|
2007-06-30 21:12:02 +02:00
|
|
|
if (elapsed_secs >= CheckPointTimeout)
|
2007-03-30 20:34:56 +02:00
|
|
|
{
|
2007-06-30 21:12:02 +02:00
|
|
|
if (!do_checkpoint)
|
|
|
|
BgWriterStats.m_timed_checkpoints++;
|
2004-05-30 00:48:23 +02:00
|
|
|
do_checkpoint = true;
|
2007-06-30 21:12:02 +02:00
|
|
|
flags |= CHECKPOINT_CAUSE_TIME;
|
2007-03-30 20:34:56 +02:00
|
|
|
}
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Do a checkpoint if requested, otherwise do one cycle of
|
|
|
|
* dirty-buffer writing.
|
|
|
|
*/
|
|
|
|
if (do_checkpoint)
|
|
|
|
{
|
2007-06-28 02:02:40 +02:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile BgWriterShmemStruct *bgs = BgWriterShmem;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Atomically fetch the request flags to figure out what
|
|
|
|
* kind of a checkpoint we should perform, and increase the
|
|
|
|
* started-counter to acknowledge that we've started
|
|
|
|
* a new checkpoint.
|
|
|
|
*/
|
|
|
|
SpinLockAcquire(&bgs->ckpt_lck);
|
2007-06-30 21:12:02 +02:00
|
|
|
flags |= bgs->ckpt_flags;
|
2007-06-28 02:02:40 +02:00
|
|
|
bgs->ckpt_flags = 0;
|
|
|
|
bgs->ckpt_started++;
|
|
|
|
SpinLockRelease(&bgs->ckpt_lck);
|
|
|
|
|
2005-06-30 02:00:52 +02:00
|
|
|
/*
|
|
|
|
* We will warn if (a) too soon since last checkpoint (whatever
|
2007-06-30 21:12:02 +02:00
|
|
|
* caused it) and (b) somebody set the CHECKPOINT_CAUSE_XLOG flag
|
2005-10-15 04:49:52 +02:00
|
|
|
* since the last checkpoint start. Note in particular that this
|
|
|
|
* implementation will not generate warnings caused by
|
2005-06-30 02:00:52 +02:00
|
|
|
* CheckPointTimeout < CheckPointWarning.
|
|
|
|
*/
|
2007-06-30 21:12:02 +02:00
|
|
|
if ((flags & CHECKPOINT_CAUSE_XLOG) &&
|
2005-06-30 02:00:52 +02:00
|
|
|
elapsed_secs < CheckPointWarning)
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("checkpoints are occurring too frequently (%d seconds apart)",
|
|
|
|
elapsed_secs),
|
|
|
|
errhint("Consider increasing the configuration parameter \"checkpoint_segments\".")));
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* Initialize bgwriter-private variables used during checkpoint.
|
2004-05-31 05:48:10 +02:00
|
|
|
*/
|
|
|
|
ckpt_active = true;
|
2007-06-28 02:02:40 +02:00
|
|
|
ckpt_start_recptr = GetInsertRecPtr();
|
|
|
|
ckpt_start_time = now;
|
|
|
|
ckpt_cached_elapsed = 0;
|
2004-05-31 05:48:10 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/*
|
|
|
|
* Do the checkpoint.
|
|
|
|
*/
|
|
|
|
CreateCheckPoint(flags);
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2004-10-28 02:39:59 +02:00
|
|
|
/*
|
|
|
|
* After any checkpoint, close all smgr files. This is so we
|
2005-10-15 04:49:52 +02:00
|
|
|
* won't hang onto smgr references to deleted files indefinitely.
|
2004-10-28 02:39:59 +02:00
|
|
|
*/
|
|
|
|
smgrcloseall();
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/*
|
|
|
|
* Indicate checkpoint completion to any waiting backends.
|
|
|
|
*/
|
2007-06-28 02:02:40 +02:00
|
|
|
SpinLockAcquire(&bgs->ckpt_lck);
|
|
|
|
bgs->ckpt_done = bgs->ckpt_started;
|
|
|
|
SpinLockRelease(&bgs->ckpt_lck);
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
ckpt_active = false;
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
|
|
|
* Note we record the checkpoint start time not end time as
|
2005-10-15 04:49:52 +02:00
|
|
|
* last_checkpoint_time. This is so that time-driven checkpoints
|
|
|
|
* happen at a predictable spacing.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
last_checkpoint_time = now;
|
|
|
|
}
|
|
|
|
else
|
2005-03-04 21:21:07 +01:00
|
|
|
BgBufferSync();
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/* Check for archive_timeout and switch xlog files if necessary. */
|
|
|
|
CheckArchiveTimeout();
|
|
|
|
|
|
|
|
/* Nap for the configured time. */
|
|
|
|
BgWriterNap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CheckArchiveTimeout -- check for archive_timeout and switch xlog files
|
|
|
|
* if needed
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CheckArchiveTimeout(void)
|
|
|
|
{
|
|
|
|
time_t now;
|
|
|
|
time_t last_time;
|
|
|
|
|
|
|
|
if (XLogArchiveTimeout <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
now = time(NULL);
|
|
|
|
|
|
|
|
/* First we do a quick check using possibly-stale local state. */
|
|
|
|
if ((int) (now - last_xlog_switch_time) < XLogArchiveTimeout)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update local state ... note that last_xlog_switch_time is the
|
|
|
|
* last time a switch was performed *or requested*.
|
|
|
|
*/
|
|
|
|
last_time = GetLastSegSwitchTime();
|
|
|
|
|
|
|
|
last_xlog_switch_time = Max(last_xlog_switch_time, last_time);
|
|
|
|
|
|
|
|
/* Now we can do the real check */
|
|
|
|
if ((int) (now - last_xlog_switch_time) >= XLogArchiveTimeout)
|
|
|
|
{
|
|
|
|
XLogRecPtr switchpoint;
|
|
|
|
|
|
|
|
/* OK, it's time to switch */
|
|
|
|
switchpoint = RequestXLogSwitch();
|
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* If the returned pointer points exactly to a segment
|
|
|
|
* boundary, assume nothing happened.
|
2006-08-18 01:04:10 +02:00
|
|
|
*/
|
2007-06-28 02:02:40 +02:00
|
|
|
if ((switchpoint.xrecoff % XLogSegSize) != 0)
|
|
|
|
ereport(DEBUG1,
|
|
|
|
(errmsg("transaction log switch forced (archive_timeout=%d)",
|
|
|
|
XLogArchiveTimeout)));
|
2006-08-18 01:04:10 +02:00
|
|
|
|
2007-03-30 20:34:56 +02:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* Update state in any case, so we don't retry constantly when
|
|
|
|
* the system is idle.
|
2007-03-30 20:34:56 +02:00
|
|
|
*/
|
2007-06-28 02:02:40 +02:00
|
|
|
last_xlog_switch_time = now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BgWriterNap -- Nap for the configured time or until a signal is received.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
BgWriterNap(void)
|
|
|
|
{
|
|
|
|
long udelay;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Send off activity statistics to the stats collector
|
|
|
|
*/
|
|
|
|
pgstat_send_bgwriter();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Nap for the configured time, or sleep for 10 seconds if there is no
|
|
|
|
* bgwriter activity configured.
|
|
|
|
*
|
|
|
|
* On some platforms, signals won't interrupt the sleep. To ensure we
|
|
|
|
* respond reasonably promptly when someone signals us, break down the
|
|
|
|
* sleep into 1-second increments, and check for interrupts after each
|
|
|
|
* nap.
|
|
|
|
*
|
|
|
|
* We absorb pending requests after each short sleep.
|
|
|
|
*/
|
|
|
|
if ((bgwriter_lru_percent > 0.0 && bgwriter_lru_maxpages > 0) ||
|
|
|
|
ckpt_active)
|
|
|
|
udelay = BgWriterDelay * 1000L;
|
|
|
|
else if (XLogArchiveTimeout > 0)
|
|
|
|
udelay = 1000000L; /* One second */
|
|
|
|
else
|
|
|
|
udelay = 10000000L; /* Ten seconds */
|
|
|
|
|
|
|
|
while (udelay > 999999L)
|
|
|
|
{
|
|
|
|
if (got_SIGHUP || shutdown_requested ||
|
|
|
|
(ckpt_active ? ImmediateCheckpointRequested() : checkpoint_requested))
|
|
|
|
break;
|
|
|
|
pg_usleep(1000000L);
|
|
|
|
AbsorbFsyncRequests();
|
|
|
|
udelay -= 1000000L;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(got_SIGHUP || shutdown_requested ||
|
|
|
|
(ckpt_active ? ImmediateCheckpointRequested() : checkpoint_requested)))
|
|
|
|
pg_usleep(udelay);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns true if an immediate checkpoint request is pending. (Note that
|
|
|
|
* this does not check the *current* checkpoint's IMMEDIATE flag, but whether
|
|
|
|
* there is one pending behind it.)
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
ImmediateCheckpointRequested(void)
|
|
|
|
{
|
|
|
|
if (checkpoint_requested)
|
|
|
|
{
|
|
|
|
volatile BgWriterShmemStruct *bgs = BgWriterShmem;
|
2007-03-30 20:34:56 +02:00
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* We don't need to acquire the ckpt_lck in this case because we're
|
|
|
|
* only looking at a single flag bit.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2007-06-28 02:02:40 +02:00
|
|
|
if (bgs->ckpt_flags & CHECKPOINT_IMMEDIATE)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2006-08-18 01:04:10 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/*
|
|
|
|
* CheckpointWriteDelay -- yield control to bgwriter during a checkpoint
|
|
|
|
*
|
|
|
|
* This function is called after each page write performed by BufferSync().
|
|
|
|
* It is responsible for keeping the bgwriter's normal activities in
|
|
|
|
* progress during a long checkpoint, and for throttling BufferSync()'s
|
|
|
|
* write rate to hit checkpoint_completion_target.
|
|
|
|
*
|
|
|
|
* The checkpoint request flags should be passed in; currently the only one
|
|
|
|
* examined is CHECKPOINT_IMMEDIATE, which disables delays between writes.
|
|
|
|
*
|
|
|
|
* 'progress' is an estimate of how much of the work has been done, as a
|
|
|
|
* fraction between 0.0 meaning none, and 1.0 meaning all done.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CheckpointWriteDelay(int flags, double progress)
|
|
|
|
{
|
|
|
|
static int absorb_counter = WRITES_PER_ABSORB;
|
|
|
|
|
|
|
|
/* Do nothing if checkpoint is being executed by non-bgwriter process */
|
|
|
|
if (!am_bg_writer)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Perform the usual bgwriter duties and take a nap, unless we're behind
|
|
|
|
* schedule, in which case we just try to catch up as quickly as possible.
|
|
|
|
*/
|
|
|
|
if (!(flags & CHECKPOINT_IMMEDIATE) &&
|
|
|
|
!shutdown_requested &&
|
|
|
|
!ImmediateCheckpointRequested() &&
|
|
|
|
IsCheckpointOnSchedule(progress))
|
|
|
|
{
|
|
|
|
if (got_SIGHUP)
|
2004-05-30 00:48:23 +02:00
|
|
|
{
|
2007-06-28 02:02:40 +02:00
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
2007-06-28 02:02:40 +02:00
|
|
|
BgBufferSync();
|
|
|
|
CheckArchiveTimeout();
|
|
|
|
BgWriterNap();
|
2006-08-18 01:04:10 +02:00
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
AbsorbFsyncRequests();
|
|
|
|
absorb_counter = WRITES_PER_ABSORB;
|
|
|
|
}
|
|
|
|
else if (--absorb_counter <= 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Absorb pending fsync requests after each WRITES_PER_ABSORB write
|
|
|
|
* operations even when we don't sleep, to prevent overflow of the
|
|
|
|
* fsync request queue.
|
|
|
|
*/
|
|
|
|
AbsorbFsyncRequests();
|
|
|
|
absorb_counter = WRITES_PER_ABSORB;
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/*
|
|
|
|
* IsCheckpointOnSchedule -- are we on schedule to finish this checkpoint
|
|
|
|
* in time?
|
|
|
|
*
|
|
|
|
* Compares the current progress against the time/segments elapsed since last
|
|
|
|
* checkpoint, and returns true if the progress we've made this far is greater
|
|
|
|
* than the elapsed time/segments.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
IsCheckpointOnSchedule(double progress)
|
|
|
|
{
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
struct timeval now;
|
|
|
|
double elapsed_xlogs,
|
|
|
|
elapsed_time;
|
|
|
|
|
|
|
|
Assert(ckpt_active);
|
|
|
|
|
|
|
|
/* Scale progress according to checkpoint_completion_target. */
|
|
|
|
progress *= CheckPointCompletionTarget;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check against the cached value first. Only do the more expensive
|
|
|
|
* calculations once we reach the target previously calculated. Since
|
|
|
|
* neither time or WAL insert pointer moves backwards, a freshly
|
|
|
|
* calculated value can only be greater than or equal to the cached value.
|
|
|
|
*/
|
|
|
|
if (progress < ckpt_cached_elapsed)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check progress against WAL segments written and checkpoint_segments.
|
|
|
|
*
|
|
|
|
* We compare the current WAL insert location against the location
|
|
|
|
* computed before calling CreateCheckPoint. The code in XLogInsert that
|
|
|
|
* actually triggers a checkpoint when checkpoint_segments is exceeded
|
|
|
|
* compares against RedoRecptr, so this is not completely accurate.
|
|
|
|
* However, it's good enough for our purposes, we're only calculating
|
|
|
|
* an estimate anyway.
|
|
|
|
*/
|
|
|
|
recptr = GetInsertRecPtr();
|
|
|
|
elapsed_xlogs =
|
|
|
|
(((double) (int32) (recptr.xlogid - ckpt_start_recptr.xlogid)) * XLogSegsPerFile +
|
|
|
|
((double) (int32) (recptr.xrecoff - ckpt_start_recptr.xrecoff)) / XLogSegSize) /
|
|
|
|
CheckPointSegments;
|
|
|
|
|
|
|
|
if (progress < elapsed_xlogs)
|
|
|
|
{
|
|
|
|
ckpt_cached_elapsed = elapsed_xlogs;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check progress against time elapsed and checkpoint_timeout.
|
|
|
|
*/
|
|
|
|
gettimeofday(&now, NULL);
|
|
|
|
elapsed_time = ((double) (now.tv_sec - ckpt_start_time) +
|
|
|
|
now.tv_usec / 1000000.0) / CheckPointTimeout;
|
|
|
|
|
|
|
|
if (progress < elapsed_time)
|
|
|
|
{
|
|
|
|
ckpt_cached_elapsed = elapsed_time;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It looks like we're on schedule. */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* signal handler routines
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* bg_quickdie() occurs when signalled SIGQUIT by the postmaster.
|
|
|
|
*
|
|
|
|
* Some backend has bought the farm,
|
|
|
|
* so we need to stop what we're doing and exit.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bg_quickdie(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
PG_SETMASK(&BlockSig);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DO NOT proc_exit() -- we're here because shared memory may be
|
2005-10-15 04:49:52 +02:00
|
|
|
* corrupted, so we don't want to try to clean up our transaction. Just
|
|
|
|
* nail the windows shut and get out of town.
|
2004-05-30 00:48:23 +02:00
|
|
|
*
|
2006-11-21 01:49:55 +01:00
|
|
|
* Note we do exit(2) not exit(0). This is to force the postmaster into a
|
2005-10-15 04:49:52 +02:00
|
|
|
* system reset cycle if some idiot DBA sends a manual SIGQUIT to a random
|
|
|
|
* backend. This is necessary precisely because we don't clean up our
|
|
|
|
* shared memory state.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2006-11-21 01:49:55 +01:00
|
|
|
exit(2);
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGHUP: set flag to re-read config file at next convenient time */
|
|
|
|
static void
|
|
|
|
BgSigHupHandler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
got_SIGHUP = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGINT: set flag to run a normal checkpoint right away */
|
|
|
|
static void
|
|
|
|
ReqCheckpointHandler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
checkpoint_requested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SIGUSR2: set flag to run a shutdown checkpoint and exit */
|
|
|
|
static void
|
|
|
|
ReqShutdownHandler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
shutdown_requested = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* --------------------------------
|
|
|
|
* communication with backends
|
|
|
|
* --------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BgWriterShmemSize
|
|
|
|
* Compute space needed for bgwriter-related shared memory
|
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
Size
|
2004-05-30 00:48:23 +02:00
|
|
|
BgWriterShmemSize(void)
|
|
|
|
{
|
2005-08-21 01:26:37 +02:00
|
|
|
Size size;
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Currently, the size of the requests[] array is arbitrarily set equal to
|
|
|
|
* NBuffers. This may prove too large or small ...
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
size = offsetof(BgWriterShmemStruct, requests);
|
|
|
|
size = add_size(size, mul_size(NBuffers, sizeof(BgWriterRequest)));
|
|
|
|
|
|
|
|
return size;
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BgWriterShmemInit
|
|
|
|
* Allocate and initialize bgwriter-related shared memory
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
BgWriterShmemInit(void)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
bool found;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
|
|
|
BgWriterShmem = (BgWriterShmemStruct *)
|
|
|
|
ShmemInitStruct("Background Writer Data",
|
2004-05-31 05:48:10 +02:00
|
|
|
BgWriterShmemSize(),
|
2004-05-30 00:48:23 +02:00
|
|
|
&found);
|
|
|
|
if (BgWriterShmem == NULL)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode(ERRCODE_OUT_OF_MEMORY),
|
2004-10-12 23:54:45 +02:00
|
|
|
errmsg("not enough shared memory for background writer")));
|
2004-05-30 00:48:23 +02:00
|
|
|
if (found)
|
|
|
|
return; /* already initialized */
|
|
|
|
|
|
|
|
MemSet(BgWriterShmem, 0, sizeof(BgWriterShmemStruct));
|
2007-06-28 02:02:40 +02:00
|
|
|
SpinLockInit(&BgWriterShmem->ckpt_lck);
|
2004-05-31 05:48:10 +02:00
|
|
|
BgWriterShmem->max_requests = NBuffers;
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* RequestCheckpoint
|
2007-06-28 02:02:40 +02:00
|
|
|
* Called in backend processes to request a checkpoint
|
2005-06-30 02:00:52 +02:00
|
|
|
*
|
2007-06-28 02:02:40 +02:00
|
|
|
* flags is a bitwise OR of the following:
|
|
|
|
* CHECKPOINT_IS_SHUTDOWN: checkpoint is for database shutdown.
|
|
|
|
* CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
|
|
|
|
* ignoring checkpoint_completion_target parameter.
|
|
|
|
* CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occured
|
|
|
|
* since the last one (implied by CHECKPOINT_IS_SHUTDOWN).
|
|
|
|
* CHECKPOINT_WAIT: wait for completion before returning (otherwise,
|
|
|
|
* just signal bgwriter to do it, and return).
|
2007-06-30 21:12:02 +02:00
|
|
|
* CHECKPOINT_CAUSE_XLOG: checkpoint is requested due to xlog filling.
|
|
|
|
* (This affects logging, and in particular enables CheckPointWarning.)
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
void
|
2007-06-28 02:02:40 +02:00
|
|
|
RequestCheckpoint(int flags)
|
2004-05-30 00:48:23 +02:00
|
|
|
{
|
2004-05-31 05:48:10 +02:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile BgWriterShmemStruct *bgs = BgWriterShmem;
|
2007-06-28 02:02:40 +02:00
|
|
|
int old_failed, old_started;
|
2004-05-30 00:48:23 +02:00
|
|
|
|
2005-02-20 00:16:15 +01:00
|
|
|
/*
|
|
|
|
* If in a standalone backend, just do it ourselves.
|
|
|
|
*/
|
|
|
|
if (!IsPostmasterEnvironment)
|
|
|
|
{
|
2007-06-28 02:02:40 +02:00
|
|
|
/*
|
|
|
|
* There's no point in doing slow checkpoints in a standalone
|
|
|
|
* backend, because there's no other backends the checkpoint could
|
|
|
|
* disrupt.
|
|
|
|
*/
|
|
|
|
CreateCheckPoint(flags | CHECKPOINT_IMMEDIATE);
|
2005-02-20 00:16:15 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* After any checkpoint, close all smgr files. This is so we won't
|
|
|
|
* hang onto smgr references to deleted files indefinitely.
|
2005-02-20 00:16:15 +01:00
|
|
|
*/
|
|
|
|
smgrcloseall();
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/*
|
|
|
|
* Atomically set the request flags, and take a snapshot of the counters.
|
|
|
|
* When we see ckpt_started > old_started, we know the flags we set here
|
|
|
|
* have been seen by bgwriter.
|
|
|
|
*
|
|
|
|
* Note that we OR the flags with any existing flags, to avoid overriding
|
|
|
|
* a "stronger" request by another backend. The flag senses must be
|
|
|
|
* chosen to make this work!
|
|
|
|
*/
|
|
|
|
SpinLockAcquire(&bgs->ckpt_lck);
|
|
|
|
|
|
|
|
old_failed = bgs->ckpt_failed;
|
|
|
|
old_started = bgs->ckpt_started;
|
2007-06-30 21:12:02 +02:00
|
|
|
bgs->ckpt_flags |= flags;
|
2007-06-28 02:02:40 +02:00
|
|
|
|
|
|
|
SpinLockRelease(&bgs->ckpt_lck);
|
2005-06-30 02:00:52 +02:00
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* Send signal to request checkpoint. When not waiting, we
|
2004-08-29 07:07:03 +02:00
|
|
|
* consider failure to send the signal to be nonfatal.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
|
|
|
if (BgWriterShmem->bgwriter_pid == 0)
|
2007-06-28 02:02:40 +02:00
|
|
|
elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
|
2004-05-30 00:48:23 +02:00
|
|
|
"could not request checkpoint because bgwriter not running");
|
|
|
|
if (kill(BgWriterShmem->bgwriter_pid, SIGINT) != 0)
|
2007-06-28 02:02:40 +02:00
|
|
|
elog((flags & CHECKPOINT_WAIT) ? ERROR : LOG,
|
2004-05-30 00:48:23 +02:00
|
|
|
"could not signal for checkpoint: %m");
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If requested, wait for completion. We detect completion according to
|
|
|
|
* the algorithm given above.
|
2004-05-30 00:48:23 +02:00
|
|
|
*/
|
2007-06-28 02:02:40 +02:00
|
|
|
if (flags & CHECKPOINT_WAIT)
|
2004-05-30 00:48:23 +02:00
|
|
|
{
|
2007-06-28 02:02:40 +02:00
|
|
|
int new_started, new_failed;
|
|
|
|
|
|
|
|
/* Wait for a new checkpoint to start. */
|
|
|
|
for(;;)
|
2004-05-30 00:48:23 +02:00
|
|
|
{
|
2007-06-28 02:02:40 +02:00
|
|
|
SpinLockAcquire(&bgs->ckpt_lck);
|
|
|
|
new_started = bgs->ckpt_started;
|
|
|
|
SpinLockRelease(&bgs->ckpt_lck);
|
|
|
|
|
|
|
|
if (new_started != old_started)
|
|
|
|
break;
|
|
|
|
|
2004-05-30 00:48:23 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
2004-05-31 05:48:10 +02:00
|
|
|
pg_usleep(100000L);
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* We are waiting for ckpt_done >= new_started, in a modulo sense.
|
2004-05-31 05:48:10 +02:00
|
|
|
*/
|
2007-06-28 02:02:40 +02:00
|
|
|
for(;;)
|
2004-05-31 05:48:10 +02:00
|
|
|
{
|
2007-06-28 02:02:40 +02:00
|
|
|
int new_done;
|
|
|
|
|
|
|
|
SpinLockAcquire(&bgs->ckpt_lck);
|
|
|
|
new_done = bgs->ckpt_done;
|
|
|
|
new_failed = bgs->ckpt_failed;
|
|
|
|
SpinLockRelease(&bgs->ckpt_lck);
|
|
|
|
|
|
|
|
if (new_done - new_started >= 0)
|
|
|
|
break;
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
|
|
|
pg_usleep(100000L);
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|
2007-06-28 02:02:40 +02:00
|
|
|
|
|
|
|
if (new_failed != old_failed)
|
2004-05-31 05:48:10 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errmsg("checkpoint request failed"),
|
2006-05-30 15:58:49 +02:00
|
|
|
errhint("Consult recent messages in the server log for details.")));
|
2004-05-31 05:48:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ForwardFsyncRequest
|
|
|
|
* Forward a file-fsync request from a backend to the bgwriter
|
|
|
|
*
|
|
|
|
* Whenever a backend is compelled to write directly to a relation
|
|
|
|
* (which should be seldom, if the bgwriter is getting its job done),
|
|
|
|
* the backend calls this routine to pass over knowledge that the relation
|
|
|
|
* is dirty and must be fsync'd before next checkpoint.
|
|
|
|
*
|
2007-01-17 17:25:01 +01:00
|
|
|
* segno specifies which segment (not block!) of the relation needs to be
|
|
|
|
* fsync'd. (Since the valid range is much less than BlockNumber, we can
|
|
|
|
* use high values for special flags; that's all internal to md.c, which
|
|
|
|
* see for details.)
|
|
|
|
*
|
2004-05-31 05:48:10 +02:00
|
|
|
* If we are unable to pass over the request (at present, this can happen
|
|
|
|
* if the shared memory queue is full), we return false. That forces
|
|
|
|
* the backend to do its own fsync. We hope that will be even more seldom.
|
|
|
|
*
|
|
|
|
* Note: we presently make no attempt to eliminate duplicate requests
|
|
|
|
* in the requests[] queue. The bgwriter will have to eliminate dups
|
|
|
|
* internally anyway, so we may as well avoid holding the lock longer
|
|
|
|
* than we have to here.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
ForwardFsyncRequest(RelFileNode rnode, BlockNumber segno)
|
|
|
|
{
|
|
|
|
BgWriterRequest *request;
|
|
|
|
|
|
|
|
if (!IsUnderPostmaster)
|
|
|
|
return false; /* probably shouldn't even get here */
|
|
|
|
|
|
|
|
LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
|
|
|
|
if (BgWriterShmem->bgwriter_pid == 0 ||
|
|
|
|
BgWriterShmem->num_requests >= BgWriterShmem->max_requests)
|
|
|
|
{
|
|
|
|
LWLockRelease(BgWriterCommLock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
request = &BgWriterShmem->requests[BgWriterShmem->num_requests++];
|
|
|
|
request->rnode = rnode;
|
|
|
|
request->segno = segno;
|
|
|
|
LWLockRelease(BgWriterCommLock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* AbsorbFsyncRequests
|
|
|
|
* Retrieve queued fsync requests and pass them to local smgr.
|
|
|
|
*
|
2004-08-04 18:24:26 +02:00
|
|
|
* This is exported because it must be called during CreateCheckPoint;
|
2007-07-03 16:51:24 +02:00
|
|
|
* we have to be sure we have accepted all pending requests just before
|
|
|
|
* we start fsync'ing. Since CreateCheckPoint sometimes runs in
|
|
|
|
* non-bgwriter processes, do nothing if not bgwriter.
|
2004-05-31 05:48:10 +02:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
AbsorbFsyncRequests(void)
|
|
|
|
{
|
|
|
|
BgWriterRequest *requests = NULL;
|
|
|
|
BgWriterRequest *request;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
if (!am_bg_writer)
|
|
|
|
return;
|
|
|
|
|
2005-05-28 19:21:32 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We have to PANIC if we fail to absorb all the pending requests (eg,
|
|
|
|
* because our hashtable runs out of memory). This is because the system
|
|
|
|
* cannot run safely if we are unable to fsync what we have been told to
|
|
|
|
* fsync. Fortunately, the hashtable is so small that the problem is
|
|
|
|
* quite unlikely to arise in practice.
|
2005-05-28 19:21:32 +02:00
|
|
|
*/
|
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We try to avoid holding the lock for a long time by copying the request
|
|
|
|
* array.
|
2004-05-31 05:48:10 +02:00
|
|
|
*/
|
|
|
|
LWLockAcquire(BgWriterCommLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
n = BgWriterShmem->num_requests;
|
|
|
|
if (n > 0)
|
|
|
|
{
|
|
|
|
requests = (BgWriterRequest *) palloc(n * sizeof(BgWriterRequest));
|
|
|
|
memcpy(requests, BgWriterShmem->requests, n * sizeof(BgWriterRequest));
|
|
|
|
}
|
|
|
|
BgWriterShmem->num_requests = 0;
|
|
|
|
|
|
|
|
LWLockRelease(BgWriterCommLock);
|
|
|
|
|
|
|
|
for (request = requests; n > 0; request++, n--)
|
|
|
|
RememberFsyncRequest(request->rnode, request->segno);
|
2005-05-28 19:21:32 +02:00
|
|
|
|
2004-05-31 05:48:10 +02:00
|
|
|
if (requests)
|
|
|
|
pfree(requests);
|
2005-05-28 19:21:32 +02:00
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
2004-05-30 00:48:23 +02:00
|
|
|
}
|