2000-04-12 19:17:23 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
1999-10-24 22:42:27 +02:00
|
|
|
*
|
|
|
|
* xlog.c
|
2001-03-13 21:32:37 +01:00
|
|
|
* PostgreSQL transaction log manager
|
1999-10-24 22:42:27 +02:00
|
|
|
*
|
|
|
|
*
|
2010-01-02 17:58:17 +01:00
|
|
|
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
|
2000-01-26 06:58:53 +01:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
1999-10-24 22:42:27 +02:00
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/access/transam/xlog.c
|
1999-10-24 22:42:27 +02:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
2000-03-20 08:25:39 +01:00
|
|
|
|
2000-11-21 22:16:06 +01:00
|
|
|
#include "postgres.h"
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
#include <ctype.h>
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
#include <signal.h>
|
2004-06-03 04:08:07 +02:00
|
|
|
#include <time.h>
|
2009-01-11 19:02:17 +01:00
|
|
|
#include <fcntl.h>
|
1999-09-27 17:48:12 +02:00
|
|
|
#include <sys/stat.h>
|
2004-05-21 18:08:47 +02:00
|
|
|
#include <sys/time.h>
|
2006-11-21 21:59:53 +01:00
|
|
|
#include <sys/wait.h>
|
|
|
|
#include <unistd.h>
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2001-08-25 20:52:43 +02:00
|
|
|
#include "access/clog.h"
|
2005-04-28 23:47:18 +02:00
|
|
|
#include "access/multixact.h"
|
2004-07-01 02:52:04 +02:00
|
|
|
#include "access/subtrans.h"
|
2006-07-13 18:49:20 +02:00
|
|
|
#include "access/transam.h"
|
2007-04-03 06:14:26 +02:00
|
|
|
#include "access/tuptoaster.h"
|
2005-06-18 00:32:51 +02:00
|
|
|
#include "access/twophase.h"
|
1999-09-27 17:48:12 +02:00
|
|
|
#include "access/xact.h"
|
2004-07-22 00:31:26 +02:00
|
|
|
#include "access/xlog_internal.h"
|
2001-08-25 20:52:43 +02:00
|
|
|
#include "access/xlogutils.h"
|
1999-10-24 22:42:27 +02:00
|
|
|
#include "catalog/catversion.h"
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
#include "catalog/pg_control.h"
|
2009-08-31 04:23:23 +02:00
|
|
|
#include "catalog/pg_database.h"
|
2006-08-18 01:04:10 +02:00
|
|
|
#include "catalog/pg_type.h"
|
|
|
|
#include "funcapi.h"
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
#include "libpq/pqsignal.h"
|
2004-07-22 00:31:26 +02:00
|
|
|
#include "miscadmin.h"
|
2005-08-11 23:11:50 +02:00
|
|
|
#include "pgstat.h"
|
2004-05-30 00:48:23 +02:00
|
|
|
#include "postmaster/bgwriter.h"
|
2010-01-15 10:19:10 +01:00
|
|
|
#include "replication/walreceiver.h"
|
|
|
|
#include "replication/walsender.h"
|
2008-06-09 00:00:48 +02:00
|
|
|
#include "storage/bufmgr.h"
|
2004-02-10 02:55:27 +01:00
|
|
|
#include "storage/fd.h"
|
2008-04-17 01:59:40 +02:00
|
|
|
#include "storage/ipc.h"
|
2010-09-15 12:35:05 +02:00
|
|
|
#include "storage/latch.h"
|
2004-07-19 04:47:16 +02:00
|
|
|
#include "storage/pmsignal.h"
|
2005-05-19 23:35:48 +02:00
|
|
|
#include "storage/procarray.h"
|
2007-11-15 21:36:40 +01:00
|
|
|
#include "storage/smgr.h"
|
1999-09-27 17:48:12 +02:00
|
|
|
#include "storage/spin.h"
|
2000-11-25 21:33:54 +01:00
|
|
|
#include "utils/builtins.h"
|
2008-09-23 11:20:39 +02:00
|
|
|
#include "utils/guc.h"
|
2007-09-29 20:32:56 +02:00
|
|
|
#include "utils/ps_status.h"
|
2010-02-07 21:48:13 +01:00
|
|
|
#include "utils/relmapper.h"
|
2008-12-17 02:39:04 +01:00
|
|
|
#include "pg_trace.h"
|
2001-02-18 05:39:42 +01:00
|
|
|
|
2005-08-21 01:26:37 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
/* File path names (all relative to $PGDATA) */
|
|
|
|
#define BACKUP_LABEL_FILE "backup_label"
|
2006-08-07 18:57:57 +02:00
|
|
|
#define BACKUP_LABEL_OLD "backup_label.old"
|
2005-07-04 06:51:52 +02:00
|
|
|
#define RECOVERY_COMMAND_FILE "recovery.conf"
|
|
|
|
#define RECOVERY_COMMAND_DONE "recovery.done"
|
|
|
|
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* User-settable parameters */
|
|
|
|
int CheckPointSegments = 3;
|
2010-04-20 13:15:06 +02:00
|
|
|
int wal_keep_segments = 0;
|
2000-11-20 06:18:40 +01:00
|
|
|
int XLOGbuffers = 8;
|
2006-08-18 01:04:10 +02:00
|
|
|
int XLogArchiveTimeout = 0;
|
2007-09-27 00:36:30 +02:00
|
|
|
bool XLogArchiveMode = false;
|
2004-07-19 04:47:16 +02:00
|
|
|
char *XLogArchiveCommand = NULL;
|
2010-04-29 23:36:19 +02:00
|
|
|
bool EnableHotStandby = false;
|
2005-07-06 01:18:10 +02:00
|
|
|
bool fullPageWrites = true;
|
2007-06-30 21:12:02 +02:00
|
|
|
bool log_checkpoints = false;
|
2009-06-11 16:49:15 +02:00
|
|
|
int sync_method = DEFAULT_SYNC_METHOD;
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
int wal_level = WAL_LEVEL_MINIMAL;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2004-01-06 18:26:23 +01:00
|
|
|
#ifdef WAL_DEBUG
|
|
|
|
bool XLOG_DEBUG = false;
|
|
|
|
#endif
|
|
|
|
|
2001-07-19 04:12:35 +02:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* XLOGfileslop is the maximum number of preallocated future XLOG segments.
|
|
|
|
* When we are done with an old XLOG segment file, we will recycle it as a
|
|
|
|
* future XLOG segment as long as there aren't already XLOGfileslop future
|
|
|
|
* segments; else we'll delete it. This could be made a separate GUC
|
|
|
|
* variable, but at present I think it's sufficient to hardwire it as
|
2007-11-15 22:14:46 +01:00
|
|
|
* 2*CheckPointSegments+1. Under normal conditions, a checkpoint will free
|
2007-06-28 02:02:40 +02:00
|
|
|
* no more than 2*CheckPointSegments log segments, and we want to recycle all
|
|
|
|
* of them; the +1 allows boundary cases to happen without wasting a
|
|
|
|
* delete/create-segment cycle.
|
2001-07-19 04:12:35 +02:00
|
|
|
*/
|
|
|
|
#define XLOGfileslop (2*CheckPointSegments + 1)
|
|
|
|
|
2008-05-12 10:35:05 +02:00
|
|
|
/*
|
|
|
|
* GUC support
|
|
|
|
*/
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
const struct config_enum_entry wal_level_options[] = {
|
|
|
|
{"minimal", WAL_LEVEL_MINIMAL, false},
|
|
|
|
{"archive", WAL_LEVEL_ARCHIVE, false},
|
|
|
|
{"hot_standby", WAL_LEVEL_HOT_STANDBY, false},
|
|
|
|
{NULL, 0, false}
|
|
|
|
};
|
|
|
|
|
2008-05-12 10:35:05 +02:00
|
|
|
const struct config_enum_entry sync_method_options[] = {
|
2008-05-28 17:22:05 +02:00
|
|
|
{"fsync", SYNC_METHOD_FSYNC, false},
|
2008-05-12 10:35:05 +02:00
|
|
|
#ifdef HAVE_FSYNC_WRITETHROUGH
|
2008-05-28 17:22:05 +02:00
|
|
|
{"fsync_writethrough", SYNC_METHOD_FSYNC_WRITETHROUGH, false},
|
2008-05-12 10:35:05 +02:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FDATASYNC
|
2008-05-28 17:22:05 +02:00
|
|
|
{"fdatasync", SYNC_METHOD_FDATASYNC, false},
|
2008-05-12 10:35:05 +02:00
|
|
|
#endif
|
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2008-05-28 17:22:05 +02:00
|
|
|
{"open_sync", SYNC_METHOD_OPEN, false},
|
2008-05-12 10:35:05 +02:00
|
|
|
#endif
|
|
|
|
#ifdef OPEN_DATASYNC_FLAG
|
2008-05-28 17:22:05 +02:00
|
|
|
{"open_datasync", SYNC_METHOD_OPEN_DSYNC, false},
|
2008-05-12 10:35:05 +02:00
|
|
|
#endif
|
2008-05-28 17:22:05 +02:00
|
|
|
{NULL, 0, false}
|
2008-05-12 10:35:05 +02:00
|
|
|
};
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2007-06-30 21:12:02 +02:00
|
|
|
/*
|
|
|
|
* Statistics for current checkpoint are collected in this global struct.
|
|
|
|
* Because only the background writer or a stand-alone backend can perform
|
|
|
|
* checkpoints, this will be unused in normal backends.
|
|
|
|
*/
|
|
|
|
CheckpointStatsData CheckpointStats;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* ThisTimeLineID will be same in all backends --- it identifies current
|
|
|
|
* WAL timeline for the database system.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
TimeLineID ThisTimeLineID = 0;
|
2000-10-21 17:43:36 +02:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Are we doing recovery from XLOG?
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*
|
2009-06-26 22:29:04 +02:00
|
|
|
* This is only ever true in the startup process; it should be read as meaning
|
|
|
|
* "this process is replaying WAL records", rather than "the system is in
|
|
|
|
* recovery mode". It should be examined primarily by functions that need
|
|
|
|
* to act differently when called from a WAL redo function (e.g., to skip WAL
|
|
|
|
* logging). To check whether the system is in recovery regardless of which
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
* process you're running in, use RecoveryInProgress() but only after shared
|
|
|
|
* memory startup and lock initialization.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
bool InRecovery = false;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/* Are we in Hot Standby mode? Only valid in startup process, see xlog.h */
|
2010-02-26 03:01:40 +01:00
|
|
|
HotStandbyState standbyState = STANDBY_DISABLED;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
static XLogRecPtr LastRec;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
|
|
|
* Local copy of SharedRecoveryInProgress variable. True actually means "not
|
2009-06-26 22:29:04 +02:00
|
|
|
* known, need to check the shared state".
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
static bool LocalRecoveryInProgress = true;
|
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
/*
|
|
|
|
* Local state for XLogInsertAllowed():
|
|
|
|
* 1: unconditionally allowed to insert XLOG
|
|
|
|
* 0: unconditionally not allowed to insert XLOG
|
|
|
|
* -1: must check RecoveryInProgress(); disallow until it is false
|
|
|
|
* Most processes start with -1 and transition to 1 after seeing that recovery
|
2010-02-26 03:01:40 +01:00
|
|
|
* is not in progress. But we can also force the value for special cases.
|
2009-06-26 22:29:04 +02:00
|
|
|
* The coding in XLogInsertAllowed() depends on the first two of these states
|
|
|
|
* being numerically the same as bool true and false.
|
|
|
|
*/
|
|
|
|
static int LocalXLogInsertAllowed = -1;
|
|
|
|
|
|
|
|
/* Are we recovering using offline XLOG archives? */
|
|
|
|
static bool InArchiveRecovery = false;
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Was the last xlog file restored from archive, or local? */
|
2004-08-29 07:07:03 +02:00
|
|
|
static bool restoredFromArchive = false;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/* options taken from recovery.conf for archive recovery */
|
2004-07-22 00:31:26 +02:00
|
|
|
static char *recoveryRestoreCommand = NULL;
|
2009-05-14 22:31:09 +02:00
|
|
|
static char *recoveryEndCommand = NULL;
|
2010-06-10 10:13:50 +02:00
|
|
|
static char *archiveCleanupCommand = NULL;
|
2010-03-19 12:05:15 +01:00
|
|
|
static RecoveryTargetType recoveryTarget = RECOVERY_TARGET_UNSET;
|
2004-07-19 04:47:16 +02:00
|
|
|
static bool recoveryTargetInclusive = true;
|
2004-08-29 07:07:03 +02:00
|
|
|
static TransactionId recoveryTargetXid;
|
2007-04-30 23:01:53 +02:00
|
|
|
static TimestampTz recoveryTargetTime;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/* options taken from recovery.conf for XLOG streaming */
|
|
|
|
static bool StandbyMode = false;
|
|
|
|
static char *PrimaryConnInfo = NULL;
|
2010-06-10 09:49:23 +02:00
|
|
|
static char *TriggerFile = NULL;
|
2010-01-15 10:19:10 +01:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* if recoveryStopsHere returns true, it saves actual stop xid/time here */
|
2004-08-29 07:07:03 +02:00
|
|
|
static TransactionId recoveryStopXid;
|
2007-04-30 23:01:53 +02:00
|
|
|
static TimestampTz recoveryStopTime;
|
2004-08-29 07:07:03 +02:00
|
|
|
static bool recoveryStopAfter;
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* During normal operation, the only timeline we care about is ThisTimeLineID.
|
|
|
|
* During recovery, however, things are more complicated. To simplify life
|
|
|
|
* for rmgr code, we keep ThisTimeLineID set to the "current" timeline as we
|
|
|
|
* scan through the WAL history (that is, it is the line that was active when
|
|
|
|
* the currently-scanned WAL record was generated). We also need these
|
|
|
|
* timeline values:
|
|
|
|
*
|
|
|
|
* recoveryTargetTLI: the desired timeline that we want to end in.
|
|
|
|
*
|
|
|
|
* expectedTLIs: an integer list of recoveryTargetTLI and the TLIs of
|
|
|
|
* its known parents, newest first (so recoveryTargetTLI is always the
|
2004-08-29 07:07:03 +02:00
|
|
|
* first list member). Only these TLIs are expected to be seen in the WAL
|
2004-07-22 00:31:26 +02:00
|
|
|
* segments we read, and indeed only these TLIs will be considered as
|
|
|
|
* candidate WAL files to open at all.
|
|
|
|
*
|
|
|
|
* curFileTLI: the TLI appearing in the name of the current input WAL file.
|
|
|
|
* (This is not necessarily the same as ThisTimeLineID, because we could
|
|
|
|
* be scanning data that was copied from an ancestor timeline when the current
|
|
|
|
* file was created.) During a sequential scan we do not allow this value
|
|
|
|
* to decrease.
|
|
|
|
*/
|
2004-08-29 07:07:03 +02:00
|
|
|
static TimeLineID recoveryTargetTLI;
|
|
|
|
static List *expectedTLIs;
|
|
|
|
static TimeLineID curFileTLI;
|
2004-07-22 00:31:26 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* ProcLastRecPtr points to the start of the last XLOG record inserted by the
|
2007-09-05 20:10:48 +02:00
|
|
|
* current backend. It is updated for all inserts. XactLastRecEnd points to
|
|
|
|
* end+1 of the last record, and is reset when we end a top-level transaction,
|
|
|
|
* or start a new one; so it can be used to tell if the current transaction has
|
|
|
|
* created any XLOG records.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
static XLogRecPtr ProcLastRecPtr = {0, 0};
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
XLogRecPtr XactLastRecEnd = {0, 0};
|
2002-03-15 20:20:36 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* RedoRecPtr is this backend's local copy of the REDO record pointer
|
|
|
|
* (which is almost but not quite the same as a pointer to the most recent
|
2001-03-22 05:01:46 +01:00
|
|
|
* CHECKPOINT record). We update this from the shared-memory copy,
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* XLogCtl->Insert.RedoRecPtr, whenever we can safely do so (ie, when we
|
2002-09-04 22:31:48 +02:00
|
|
|
* hold the Insert lock). See XLogInsert for details. We are also allowed
|
2002-03-15 20:20:36 +01:00
|
|
|
* to update from XLogCtl->Insert.RedoRecPtr if we hold the info_lck;
|
2004-05-27 19:12:57 +02:00
|
|
|
* see GetRedoRecPtr. A freshly spawned backend obtains the value during
|
|
|
|
* InitXLOGAccess.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2004-05-27 19:12:57 +02:00
|
|
|
static XLogRecPtr RedoRecPtr;
|
2000-11-25 21:33:54 +01:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* RedoStartLSN points to the checkpoint's REDO location which is specified
|
|
|
|
* in a backup label file, backup history file or control file. In standby
|
|
|
|
* mode, XLOG streaming usually starts from the position where an invalid
|
|
|
|
* record was found. But if we fail to read even the initial checkpoint
|
|
|
|
* record, we use the REDO location instead of the checkpoint location as
|
|
|
|
* the start position of XLOG streaming. Otherwise we would have to jump
|
|
|
|
* backwards to the REDO location after reading the checkpoint record,
|
|
|
|
* because the REDO record can precede the checkpoint record.
|
|
|
|
*/
|
|
|
|
static XLogRecPtr RedoStartLSN = {0, 0};
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*----------
|
|
|
|
* Shared-memory data structures for XLOG control
|
|
|
|
*
|
|
|
|
* LogwrtRqst indicates a byte position that we need to write and/or fsync
|
|
|
|
* the log up to (all records before that point must be written or fsynced).
|
|
|
|
* LogwrtResult indicates the byte positions we have already written/fsynced.
|
|
|
|
* These structs are identical but are declared separately to indicate their
|
|
|
|
* slightly different functions.
|
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* We do a lot of pushups to minimize the amount of access to lockable
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* shared memory values. There are actually three shared-memory copies of
|
|
|
|
* LogwrtResult, plus one unshared copy in each backend. Here's how it works:
|
|
|
|
* XLogCtl->LogwrtResult is protected by info_lck
|
2001-09-29 06:02:27 +02:00
|
|
|
* XLogCtl->Write.LogwrtResult is protected by WALWriteLock
|
|
|
|
* XLogCtl->Insert.LogwrtResult is protected by WALInsertLock
|
|
|
|
* One must hold the associated lock to read or write any of these, but
|
|
|
|
* of course no lock is needed to read/write the unshared LogwrtResult.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*
|
|
|
|
* XLogCtl->LogwrtResult and XLogCtl->Write.LogwrtResult are both "always
|
|
|
|
* right", since both are updated by a write or flush operation before
|
2001-09-29 06:02:27 +02:00
|
|
|
* it releases WALWriteLock. The point of keeping XLogCtl->Write.LogwrtResult
|
|
|
|
* is that it can be examined/modified by code that already holds WALWriteLock
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* without needing to grab info_lck as well.
|
|
|
|
*
|
|
|
|
* XLogCtl->Insert.LogwrtResult may lag behind the reality of the other two,
|
2001-03-22 05:01:46 +01:00
|
|
|
* but is updated when convenient. Again, it exists for the convenience of
|
2001-09-29 06:02:27 +02:00
|
|
|
* code that is already holding WALInsertLock but not the other locks.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*
|
|
|
|
* The unshared LogwrtResult may lag behind any or all of these, and again
|
|
|
|
* is updated when convenient.
|
|
|
|
*
|
|
|
|
* The request bookkeeping is simpler: there is a shared XLogCtl->LogwrtRqst
|
|
|
|
* (protected by info_lck), but we don't need to cache any copies of it.
|
|
|
|
*
|
|
|
|
* Note that this all works because the request and result positions can only
|
|
|
|
* advance forward, never back up, and so we can easily determine which of two
|
|
|
|
* values is "more up to date".
|
2001-09-29 06:02:27 +02:00
|
|
|
*
|
|
|
|
* info_lck is only held long enough to read/update the protected variables,
|
|
|
|
* so it's a plain spinlock. The other locks are held longer (potentially
|
|
|
|
* over I/O operations), so we use LWLocks for them. These locks are:
|
|
|
|
*
|
|
|
|
* WALInsertLock: must be held to insert a record into the WAL buffers.
|
|
|
|
*
|
|
|
|
* WALWriteLock: must be held to write WAL buffers to disk (XLogWrite or
|
|
|
|
* XLogFlush).
|
|
|
|
*
|
|
|
|
* ControlFileLock: must be held to read/update control file or create
|
|
|
|
* new log file.
|
|
|
|
*
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* CheckpointLock: must be held to do a checkpoint or restartpoint (ensures
|
2009-06-26 22:29:04 +02:00
|
|
|
* only one checkpointer at a time; currently, with all checkpoints done by
|
|
|
|
* the bgwriter, this is just pro forma).
|
2001-09-29 06:02:27 +02:00
|
|
|
*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*----------
|
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
typedef struct XLogwrtRqst
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLogRecPtr Write; /* last byte + 1 to write out */
|
|
|
|
XLogRecPtr Flush; /* last byte + 1 to flush */
|
2001-10-25 07:50:21 +02:00
|
|
|
} XLogwrtRqst;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
typedef struct XLogwrtResult
|
|
|
|
{
|
|
|
|
XLogRecPtr Write; /* last byte + 1 written out */
|
|
|
|
XLogRecPtr Flush; /* last byte + 1 flushed */
|
|
|
|
} XLogwrtResult;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Shared state data for XLogInsert.
|
|
|
|
*/
|
1999-09-27 17:48:12 +02:00
|
|
|
typedef struct XLogCtlInsert
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogwrtResult LogwrtResult; /* a recent value of LogwrtResult */
|
|
|
|
XLogRecPtr PrevRecord; /* start of previously-inserted record */
|
2005-08-22 02:41:28 +02:00
|
|
|
int curridx; /* current block index in cache */
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogPageHeader currpage; /* points to header of block in cache */
|
|
|
|
char *currpos; /* current insertion point in cache */
|
|
|
|
XLogRecPtr RedoRecPtr; /* current redo point for insertions */
|
2006-04-17 20:55:05 +02:00
|
|
|
bool forcePageWrites; /* forcing full-page writes for PITR? */
|
1999-09-27 17:48:12 +02:00
|
|
|
} XLogCtlInsert;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Shared state data for XLogWrite/XLogFlush.
|
|
|
|
*/
|
1999-09-27 17:48:12 +02:00
|
|
|
typedef struct XLogCtlWrite
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
XLogwrtResult LogwrtResult; /* current value of LogwrtResult */
|
|
|
|
int curridx; /* cache index of next block to write */
|
2008-02-17 03:09:32 +01:00
|
|
|
pg_time_t lastSegSwitchTime; /* time of last xlog segment switch */
|
1999-09-27 17:48:12 +02:00
|
|
|
} XLogCtlWrite;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Total shared-memory state for XLOG.
|
|
|
|
*/
|
1999-09-27 17:48:12 +02:00
|
|
|
typedef struct XLogCtlData
|
|
|
|
{
|
2001-09-29 06:02:27 +02:00
|
|
|
/* Protected by WALInsertLock: */
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogCtlInsert Insert;
|
2006-08-21 18:16:31 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Protected by info_lck: */
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogwrtRqst LogwrtRqst;
|
|
|
|
XLogwrtResult LogwrtResult;
|
2006-08-21 18:16:31 +02:00
|
|
|
uint32 ckptXidEpoch; /* nextXID & epoch of latest checkpoint */
|
|
|
|
TransactionId ckptXid;
|
2010-07-30 00:27:27 +02:00
|
|
|
XLogRecPtr asyncXactLSN; /* LSN of newest async commit/abort */
|
2010-07-06 21:19:02 +02:00
|
|
|
uint32 lastRemovedLog; /* latest removed/recycled XLOG segment */
|
2010-04-12 11:52:29 +02:00
|
|
|
uint32 lastRemovedSeg;
|
2006-08-21 18:16:31 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/* Protected by WALWriteLock: */
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogCtlWrite Write;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* These values do not change after startup, although the pointed-to pages
|
|
|
|
* and xlblocks values certainly do. Permission to read/write the pages
|
|
|
|
* and xlblocks values depends on WALInsertLock and WALWriteLock.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
char *pages; /* buffers for unwritten XLOG pages */
|
2006-04-04 01:35:05 +02:00
|
|
|
XLogRecPtr *xlblocks; /* 1st byte ptr-s + XLOG_BLCKSZ */
|
2005-08-22 02:41:28 +02:00
|
|
|
int XLogCacheBlck; /* highest allocated xlog buffer index */
|
2004-07-22 00:31:26 +02:00
|
|
|
TimeLineID ThisTimeLineID;
|
2010-01-15 10:19:10 +01:00
|
|
|
TimeLineID RecoveryTargetTLI;
|
2010-07-06 21:19:02 +02:00
|
|
|
|
2010-03-18 10:17:18 +01:00
|
|
|
/*
|
2010-06-10 10:13:50 +02:00
|
|
|
* archiveCleanupCommand is read from recovery.conf but needs to be in
|
2010-03-18 10:17:18 +01:00
|
|
|
* shared memory so that the bgwriter process can access it.
|
|
|
|
*/
|
2010-06-10 10:13:50 +02:00
|
|
|
char archiveCleanupCommand[MAXPGPATH];
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
|
|
|
* SharedRecoveryInProgress indicates if we're still in crash or archive
|
2009-06-26 22:29:04 +02:00
|
|
|
* recovery. Protected by info_lck.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
bool SharedRecoveryInProgress;
|
|
|
|
|
2010-09-15 12:35:05 +02:00
|
|
|
/*
|
|
|
|
* recoveryWakeupLatch is used to wake up the startup process to
|
|
|
|
* continue WAL replay, if it is waiting for WAL to arrive or failover
|
|
|
|
* trigger file to appear.
|
|
|
|
*/
|
|
|
|
Latch recoveryWakeupLatch;
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* During recovery, we keep a copy of the latest checkpoint record here.
|
|
|
|
* Used by the background writer when it wants to create a restartpoint.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*
|
|
|
|
* Protected by info_lck.
|
|
|
|
*/
|
|
|
|
XLogRecPtr lastCheckPointRecPtr;
|
|
|
|
CheckPoint lastCheckPoint;
|
|
|
|
|
|
|
|
/* end+1 of the last record replayed (or being replayed) */
|
|
|
|
XLogRecPtr replayEndRecPtr;
|
2010-01-15 10:19:10 +01:00
|
|
|
/* end+1 of the last record replayed */
|
|
|
|
XLogRecPtr recoveryLastRecPtr;
|
2010-07-04 00:15:45 +02:00
|
|
|
/* timestamp of last COMMIT/ABORT record replayed (or being replayed) */
|
|
|
|
TimestampTz recoveryLastXTime;
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2006-08-21 18:16:31 +02:00
|
|
|
slock_t info_lck; /* locks shared variables shown above */
|
1999-09-27 17:48:12 +02:00
|
|
|
} XLogCtlData;
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
static XLogCtlData *XLogCtl = NULL;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
/*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* We maintain an image of pg_control in shared memory.
|
2000-11-25 21:33:54 +01:00
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
static ControlFileData *ControlFile = NULL;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Macros for managing XLogInsert state. In most cases, the calling routine
|
|
|
|
* has local copies of XLogCtl->Insert and/or XLogCtl->Insert->curridx,
|
|
|
|
* so these are passed as parameters instead of being fetched via XLogCtl.
|
|
|
|
*/
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Free space remaining in the current xlog page buffer */
|
|
|
|
#define INSERT_FREESPACE(Insert) \
|
2006-04-04 01:35:05 +02:00
|
|
|
(XLOG_BLCKSZ - ((Insert)->currpos - (char *) (Insert)->currpage))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
/* Construct XLogRecPtr value for current insertion point */
|
|
|
|
#define INSERT_RECPTR(recptr,Insert,curridx) \
|
|
|
|
( \
|
|
|
|
(recptr).xlogid = XLogCtl->xlblocks[curridx].xlogid, \
|
|
|
|
(recptr).xrecoff = \
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogCtl->xlblocks[curridx].xrecoff - INSERT_FREESPACE(Insert) \
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
#define PrevBufIdx(idx) \
|
|
|
|
(((idx) == 0) ? XLogCtl->XLogCacheBlck : ((idx) - 1))
|
|
|
|
|
|
|
|
#define NextBufIdx(idx) \
|
|
|
|
(((idx) == XLogCtl->XLogCacheBlck) ? 0 : ((idx) + 1))
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Private, possibly out-of-date copy of shared LogwrtResult.
|
|
|
|
* See discussion above.
|
|
|
|
*/
|
2004-05-28 07:13:32 +02:00
|
|
|
static XLogwrtResult LogwrtResult = {{0, 0}, {0, 0}};
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2010-07-03 22:43:58 +02:00
|
|
|
/*
|
|
|
|
* Codes indicating where we got a WAL file from during recovery, or where
|
|
|
|
* to attempt to get one. These are chosen so that they can be OR'd together
|
|
|
|
* in a bitmask state variable.
|
|
|
|
*/
|
|
|
|
#define XLOG_FROM_ARCHIVE (1<<0) /* Restored using restore_command */
|
|
|
|
#define XLOG_FROM_PG_XLOG (1<<1) /* Existing file in pg_xlog */
|
|
|
|
#define XLOG_FROM_STREAM (1<<2) /* Streamed from master */
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* openLogFile is -1 or a kernel FD for an open log file segment.
|
|
|
|
* When it's open, openLogOff is the current seek offset in the file.
|
|
|
|
* openLogId/openLogSeg identify the segment. These variables are only
|
|
|
|
* used to write the XLOG, and so will normally refer to the active segment.
|
|
|
|
*/
|
|
|
|
static int openLogFile = -1;
|
|
|
|
static uint32 openLogId = 0;
|
|
|
|
static uint32 openLogSeg = 0;
|
|
|
|
static uint32 openLogOff = 0;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* These variables are used similarly to the ones above, but for reading
|
|
|
|
* the XLOG. Note, however, that readOff generally represents the offset
|
|
|
|
* of the page just read, not the seek position of the FD itself, which
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
* will be just past that page. readLen indicates how much of the current
|
2010-03-30 18:23:57 +02:00
|
|
|
* page has been read into readBuf, and readSource indicates where we got
|
|
|
|
* the currently open file from.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
static int readFile = -1;
|
|
|
|
static uint32 readId = 0;
|
|
|
|
static uint32 readSeg = 0;
|
|
|
|
static uint32 readOff = 0;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
static uint32 readLen = 0;
|
2010-07-06 21:19:02 +02:00
|
|
|
static int readSource = 0; /* XLOG_FROM_* code */
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
/*
|
|
|
|
* Keeps track of which sources we've tried to read the current WAL
|
|
|
|
* record from and failed.
|
|
|
|
*/
|
2010-07-06 21:19:02 +02:00
|
|
|
static int failedSources = 0; /* OR of XLOG_FROM_* codes */
|
2010-07-03 22:43:58 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* These variables track when we last obtained some WAL data to process,
|
|
|
|
* and where we got it from. (XLogReceiptSource is initially the same as
|
|
|
|
* readSource, but readSource gets reset to zero when we don't have data
|
|
|
|
* to process right now.)
|
|
|
|
*/
|
|
|
|
static TimestampTz XLogReceiptTime = 0;
|
2010-07-06 21:19:02 +02:00
|
|
|
static int XLogReceiptSource = 0; /* XLOG_FROM_* code */
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2006-04-04 01:35:05 +02:00
|
|
|
/* Buffer for currently read page (XLOG_BLCKSZ bytes) */
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
static char *readBuf = NULL;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2004-08-29 18:34:48 +02:00
|
|
|
/* Buffer for current ReadRecord result (expandable) */
|
|
|
|
static char *readRecordBuf = NULL;
|
|
|
|
static uint32 readRecordBufSize = 0;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* State information for XLOG reading */
|
2005-10-15 04:49:52 +02:00
|
|
|
static XLogRecPtr ReadRecPtr; /* start of last record read */
|
|
|
|
static XLogRecPtr EndRecPtr; /* end+1 of last record read */
|
2004-07-22 00:31:26 +02:00
|
|
|
static TimeLineID lastPageTLI = 0;
|
2009-06-26 22:29:04 +02:00
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
static XLogRecPtr minRecoveryPoint; /* local copy of
|
|
|
|
* ControlFile->minRecoveryPoint */
|
|
|
|
static bool updateMinRecoveryPoint = true;
|
2010-04-13 16:17:46 +02:00
|
|
|
static bool reachedMinRecoveryPoint = false;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2000-10-28 18:21:00 +02:00
|
|
|
static bool InRedo = false;
|
|
|
|
|
2010-06-09 17:04:07 +02:00
|
|
|
/* Have we launched bgwriter during recovery? */
|
|
|
|
static bool bgwriterLaunched = false;
|
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
/*
|
|
|
|
* Information logged when we detect a change in one of the parameters
|
|
|
|
* important for Hot Standby.
|
|
|
|
*/
|
|
|
|
typedef struct xl_parameter_change
|
|
|
|
{
|
|
|
|
int MaxConnections;
|
|
|
|
int max_prepared_xacts;
|
|
|
|
int max_locks_per_xact;
|
|
|
|
int wal_level;
|
|
|
|
} xl_parameter_change;
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
2009-06-26 22:29:04 +02:00
|
|
|
* Flags set by interrupt handlers for later service in the redo loop.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
2009-03-04 14:56:40 +01:00
|
|
|
static volatile sig_atomic_t got_SIGHUP = false;
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
static volatile sig_atomic_t shutdown_requested = false;
|
2009-06-11 16:49:15 +02:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
|
|
|
* Flag set when executing a restore command, to tell SIGTERM signal handler
|
2009-02-23 10:28:50 +01:00
|
|
|
* that it's safe to just proc_exit.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
static volatile sig_atomic_t in_restore_command = false;
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
static void XLogArchiveNotify(const char *xlog);
|
|
|
|
static void XLogArchiveNotifySeg(uint32 log, uint32 seg);
|
2008-09-08 18:42:15 +02:00
|
|
|
static bool XLogArchiveCheckDone(const char *xlog);
|
|
|
|
static bool XLogArchiveIsBusy(const char *xlog);
|
2004-07-19 04:47:16 +02:00
|
|
|
static void XLogArchiveCleanup(const char *xlog);
|
|
|
|
static void readRecoveryCommandFile(void);
|
2004-07-22 00:31:26 +02:00
|
|
|
static void exitArchiveRecovery(TimeLineID endTLI,
|
2004-08-29 07:07:03 +02:00
|
|
|
uint32 endLogId, uint32 endLogSeg);
|
2004-07-19 04:47:16 +02:00
|
|
|
static bool recoveryStopsHere(XLogRecord *record, bool *includeThis);
|
2010-07-04 00:15:45 +02:00
|
|
|
static void SetLatestXTime(TimestampTz xtime);
|
|
|
|
static TimestampTz GetLatestXTime(void);
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
static void CheckRequiredParameterValues(void);
|
|
|
|
static void XLogReportParameters(void);
|
2009-08-07 21:29:49 +02:00
|
|
|
static void LocalSetXLogInsertAllowed(void);
|
2007-06-28 02:02:40 +02:00
|
|
|
static void CheckPointGuts(XLogRecPtr checkPointRedo, int flags);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2006-04-17 20:55:05 +02:00
|
|
|
static bool XLogCheckBuffer(XLogRecData *rdata, bool doPageWrites,
|
2005-10-15 04:49:52 +02:00
|
|
|
XLogRecPtr *lsn, BkpBlock *bkpb);
|
2006-08-06 05:53:44 +02:00
|
|
|
static bool AdvanceXLInsertBuffer(bool new_segment);
|
2010-06-09 17:04:07 +02:00
|
|
|
static bool XLogCheckpointNeeded(uint32 logid, uint32 logseg);
|
2006-08-06 05:53:44 +02:00
|
|
|
static void XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch);
|
2005-04-15 20:48:10 +02:00
|
|
|
static bool InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
|
|
|
|
bool find_free, int *max_advance,
|
2001-10-25 07:50:21 +02:00
|
|
|
bool use_lock);
|
2010-02-26 03:01:40 +01:00
|
|
|
static int XLogFileRead(uint32 log, uint32 seg, int emode, TimeLineID tli,
|
2010-03-30 18:23:57 +02:00
|
|
|
int source, bool notexistOk);
|
2010-02-26 03:01:40 +01:00
|
|
|
static int XLogFileReadAnyTLI(uint32 log, uint32 seg, int emode,
|
2010-03-30 18:23:57 +02:00
|
|
|
int sources);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
static bool XLogPageRead(XLogRecPtr *RecPtr, int emode, bool fetching_ckpt,
|
|
|
|
bool randAccess);
|
2010-07-06 21:19:02 +02:00
|
|
|
static int emode_for_corrupt_record(int emode, XLogRecPtr RecPtr);
|
2006-10-04 02:30:14 +02:00
|
|
|
static void XLogFileClose(void);
|
2004-07-22 00:31:26 +02:00
|
|
|
static bool RestoreArchivedFile(char *path, const char *xlogfname,
|
2004-08-29 07:07:03 +02:00
|
|
|
const char *recovername, off_t expectedSize);
|
2010-03-18 10:17:18 +01:00
|
|
|
static void ExecuteRecoveryCommand(char *command, char *commandName,
|
|
|
|
bool failOnerror);
|
2007-06-30 21:12:02 +02:00
|
|
|
static void PreallocXlogFiles(XLogRecPtr endptr);
|
|
|
|
static void RemoveOldXlogFiles(uint32 log, uint32 seg, XLogRecPtr endptr);
|
2010-04-12 12:40:43 +02:00
|
|
|
static void UpdateLastRemovedPtr(char *filename);
|
2008-11-09 18:51:15 +01:00
|
|
|
static void ValidateXLOGDirectoryStructure(void);
|
2006-06-22 22:42:57 +02:00
|
|
|
static void CleanupBackupHistory(void);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
static void UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
static XLogRecord *ReadRecord(XLogRecPtr *RecPtr, int emode, bool fetching_ckpt);
|
2010-04-13 16:17:46 +02:00
|
|
|
static void CheckRecoveryConsistency(void);
|
2004-07-22 00:31:26 +02:00
|
|
|
static bool ValidXLOGHeader(XLogPageHeader hdr, int emode);
|
2004-08-29 18:34:48 +02:00
|
|
|
static XLogRecord *ReadCheckpointRecord(XLogRecPtr RecPtr, int whichChkpt);
|
2004-07-22 00:31:26 +02:00
|
|
|
static List *readTimeLineHistory(TimeLineID targetTLI);
|
|
|
|
static bool existsTimeLineHistory(TimeLineID probeTLI);
|
|
|
|
static TimeLineID findNewestTimeLine(TimeLineID startTLI);
|
|
|
|
static void writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
|
2004-08-29 07:07:03 +02:00
|
|
|
TimeLineID endTLI,
|
|
|
|
uint32 endLogId, uint32 endLogSeg);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
static void WriteControlFile(void);
|
|
|
|
static void ReadControlFile(void);
|
2007-08-04 03:26:54 +02:00
|
|
|
static char *str_time(pg_time_t tnow);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
static bool CheckForStandbyTrigger(void);
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2004-01-06 23:22:37 +01:00
|
|
|
#ifdef WAL_DEBUG
|
2006-03-24 05:32:13 +01:00
|
|
|
static void xlog_outrec(StringInfo buf, XLogRecord *record);
|
2004-01-06 23:22:37 +01:00
|
|
|
#endif
|
2008-04-17 01:59:40 +02:00
|
|
|
static void pg_start_backup_callback(int code, Datum arg);
|
2010-01-04 13:50:50 +01:00
|
|
|
static bool read_backup_label(XLogRecPtr *checkPointLoc);
|
2006-03-24 05:32:13 +01:00
|
|
|
static void rm_redo_error_callback(void *arg);
|
2009-06-11 16:49:15 +02:00
|
|
|
static int get_sync_bit(int method);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert an XLOG record having the specified RMID and info bytes,
|
|
|
|
* with the body of the record being the data chunk(s) described by
|
2005-06-06 22:22:58 +02:00
|
|
|
* the rdata chain (see xlog.h for notes about rdata).
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*
|
|
|
|
* Returns XLOG pointer to end of record (beginning of next record).
|
|
|
|
* This can be used as LSN for data pages affected by the logged action.
|
|
|
|
* (LSN is the XLOG point up to which the XLOG must be flushed to disk
|
|
|
|
* before the data page can be written out. This implements the basic
|
|
|
|
* WAL rule "write the log before the data".)
|
|
|
|
*
|
|
|
|
* NB: this routine feels free to scribble on the XLogRecData structs,
|
|
|
|
* though not on the data they reference. This is OK since the XLogRecData
|
|
|
|
* structs are always just temporaries in the calling code.
|
|
|
|
*/
|
1999-09-27 17:48:12 +02:00
|
|
|
XLogRecPtr
|
2000-12-28 14:00:29 +01:00
|
|
|
XLogInsert(RmgrId rmid, uint8 info, XLogRecData *rdata)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogCtlInsert *Insert = &XLogCtl->Insert;
|
|
|
|
XLogRecord *record;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLogContRecord *contrecord;
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecPtr RecPtr;
|
|
|
|
XLogRecPtr WriteRqst;
|
|
|
|
uint32 freespace;
|
2005-08-22 02:41:28 +02:00
|
|
|
int curridx;
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecData *rdt;
|
|
|
|
Buffer dtbuf[XLR_MAX_BKP_BLOCKS];
|
|
|
|
bool dtbuf_bkp[XLR_MAX_BKP_BLOCKS];
|
|
|
|
BkpBlock dtbuf_xlg[XLR_MAX_BKP_BLOCKS];
|
|
|
|
XLogRecPtr dtbuf_lsn[XLR_MAX_BKP_BLOCKS];
|
2005-06-02 07:55:29 +02:00
|
|
|
XLogRecData dtbuf_rdt1[XLR_MAX_BKP_BLOCKS];
|
|
|
|
XLogRecData dtbuf_rdt2[XLR_MAX_BKP_BLOCKS];
|
|
|
|
XLogRecData dtbuf_rdt3[XLR_MAX_BKP_BLOCKS];
|
|
|
|
pg_crc32 rdata_crc;
|
2001-03-22 05:01:46 +01:00
|
|
|
uint32 len,
|
|
|
|
write_len;
|
|
|
|
unsigned i;
|
|
|
|
bool updrqst;
|
2006-04-17 20:55:05 +02:00
|
|
|
bool doPageWrites;
|
2006-08-06 05:53:44 +02:00
|
|
|
bool isLogSwitch = (rmid == RM_XLOG_ID && info == XLOG_SWITCH);
|
2000-11-30 02:47:33 +01:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* cross-check on whether we should be here or not */
|
2009-06-26 22:29:04 +02:00
|
|
|
if (!XLogInsertAllowed())
|
|
|
|
elog(ERROR, "cannot make new WAL entries during recovery");
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
/* info's high bits are reserved for use by me */
|
2000-11-30 02:47:33 +01:00
|
|
|
if (info & XLR_INFO_MASK)
|
2007-09-05 20:10:48 +02:00
|
|
|
elog(PANIC, "invalid xlog info mask %02X", info);
|
2000-11-30 02:47:33 +01:00
|
|
|
|
2008-12-17 02:39:04 +01:00
|
|
|
TRACE_POSTGRESQL_XLOG_INSERT(rmid, info);
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* In bootstrap mode, we don't actually log anything but XLOG resources;
|
|
|
|
* return a phony record pointer.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2000-11-21 10:39:57 +01:00
|
|
|
if (IsBootstrapProcessingMode() && rmid != RM_XLOG_ID)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
|
|
|
RecPtr.xlogid = 0;
|
2005-10-15 04:49:52 +02:00
|
|
|
RecPtr.xrecoff = SizeOfXLogLongPHD; /* start of 1st chkpt record */
|
2006-01-11 09:43:13 +01:00
|
|
|
return RecPtr;
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-06-06 22:22:58 +02:00
|
|
|
* Here we scan the rdata chain, determine which buffers must be backed
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* up, and compute the CRC values for the data. Note that the record
|
2005-10-15 04:49:52 +02:00
|
|
|
* header isn't added into the CRC initially since we don't know the final
|
|
|
|
* length or info bits quite yet. Thus, the CRC will represent the CRC of
|
|
|
|
* the whole record in the order "rdata, then backup blocks, then record
|
|
|
|
* header".
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* We may have to loop back to here if a race condition is detected below.
|
|
|
|
* We could prevent the race by doing all this work while holding the
|
|
|
|
* insert lock, but it seems better to avoid doing CRC calculations while
|
|
|
|
* holding the lock. This means we have to be careful about modifying the
|
|
|
|
* rdata chain until we know we aren't going to loop back again. The only
|
2005-10-15 04:49:52 +02:00
|
|
|
* change we allow ourselves to make earlier is to set rdt->data = NULL in
|
|
|
|
* chain items we have decided we will have to back up the whole buffer
|
|
|
|
* for. This is OK because we will certainly decide the same thing again
|
|
|
|
* for those items if we do it over; doing it here saves an extra pass
|
|
|
|
* over the chain later.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2000-12-28 14:00:29 +01:00
|
|
|
begin:;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
|
|
|
{
|
|
|
|
dtbuf[i] = InvalidBuffer;
|
|
|
|
dtbuf_bkp[i] = false;
|
|
|
|
}
|
|
|
|
|
2006-04-17 20:55:05 +02:00
|
|
|
/*
|
|
|
|
* Decide if we need to do full-page writes in this XLOG record: true if
|
|
|
|
* full_page_writes is on or we have a PITR request for it. Since we
|
|
|
|
* don't yet have the insert lock, forcePageWrites could change under us,
|
|
|
|
* but we'll recheck it once we have the lock.
|
|
|
|
*/
|
|
|
|
doPageWrites = fullPageWrites || Insert->forcePageWrites;
|
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
INIT_CRC32(rdata_crc);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
len = 0;
|
2001-03-22 05:01:46 +01:00
|
|
|
for (rdt = rdata;;)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
|
|
|
if (rdt->buffer == InvalidBuffer)
|
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Simple data, just include it */
|
2000-12-28 14:00:29 +01:00
|
|
|
len += rdt->len;
|
2005-06-02 07:55:29 +02:00
|
|
|
COMP_CRC32(rdata_crc, rdt->data, rdt->len);
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
else
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Find info for buffer */
|
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (rdt->buffer == dtbuf[i])
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
2005-06-06 22:22:58 +02:00
|
|
|
/* Buffer already referenced by earlier chain item */
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (dtbuf_bkp[i])
|
|
|
|
rdt->data = NULL;
|
|
|
|
else if (rdt->data)
|
|
|
|
{
|
|
|
|
len += rdt->len;
|
2005-06-02 07:55:29 +02:00
|
|
|
COMP_CRC32(rdata_crc, rdt->data, rdt->len);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
break;
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (dtbuf[i] == InvalidBuffer)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* OK, put it in this slot */
|
|
|
|
dtbuf[i] = rdt->buffer;
|
2006-04-17 20:55:05 +02:00
|
|
|
if (XLogCheckBuffer(rdt, doPageWrites,
|
|
|
|
&(dtbuf_lsn[i]), &(dtbuf_xlg[i])))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
|
|
|
dtbuf_bkp[i] = true;
|
|
|
|
rdt->data = NULL;
|
|
|
|
}
|
|
|
|
else if (rdt->data)
|
|
|
|
{
|
|
|
|
len += rdt->len;
|
2005-06-02 07:55:29 +02:00
|
|
|
COMP_CRC32(rdata_crc, rdt->data, rdt->len);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
break;
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (i >= XLR_MAX_BKP_BLOCKS)
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(PANIC, "can backup at most %d blocks per xlog record",
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLR_MAX_BKP_BLOCKS);
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
2005-06-06 22:22:58 +02:00
|
|
|
/* Break out of loop when rdt points to last chain item */
|
2000-12-28 14:00:29 +01:00
|
|
|
if (rdt->next == NULL)
|
|
|
|
break;
|
|
|
|
rdt = rdt->next;
|
|
|
|
}
|
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
/*
|
|
|
|
* Now add the backup block headers and data into the CRC
|
|
|
|
*/
|
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
|
|
|
{
|
|
|
|
if (dtbuf_bkp[i])
|
|
|
|
{
|
|
|
|
BkpBlock *bkpb = &(dtbuf_xlg[i]);
|
|
|
|
char *page;
|
|
|
|
|
|
|
|
COMP_CRC32(rdata_crc,
|
|
|
|
(char *) bkpb,
|
|
|
|
sizeof(BkpBlock));
|
|
|
|
page = (char *) BufferGetBlock(dtbuf[i]);
|
|
|
|
if (bkpb->hole_length == 0)
|
|
|
|
{
|
|
|
|
COMP_CRC32(rdata_crc,
|
|
|
|
page,
|
|
|
|
BLCKSZ);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* must skip the hole */
|
|
|
|
COMP_CRC32(rdata_crc,
|
|
|
|
page,
|
|
|
|
bkpb->hole_offset);
|
|
|
|
COMP_CRC32(rdata_crc,
|
|
|
|
page + (bkpb->hole_offset + bkpb->hole_length),
|
|
|
|
BLCKSZ - (bkpb->hole_offset + bkpb->hole_length));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2006-08-06 05:53:44 +02:00
|
|
|
* NOTE: We disallow len == 0 because it provides a useful bit of extra
|
|
|
|
* error checking in ReadRecord. This means that all callers of
|
2006-10-04 02:30:14 +02:00
|
|
|
* XLogInsert must supply at least some not-in-a-buffer data. However, we
|
|
|
|
* make an exception for XLOG SWITCH records because we don't want them to
|
|
|
|
* ever cross a segment boundary.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2006-08-06 05:53:44 +02:00
|
|
|
if (len == 0 && !isLogSwitch)
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(PANIC, "invalid xlog record length %u", len);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
START_CRIT_SECTION();
|
2000-12-03 11:27:29 +01:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/* Now wait to get insert lock */
|
|
|
|
LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check to see if my RedoRecPtr is out of date. If so, may have to go
|
|
|
|
* back and recompute everything. This can only happen just after a
|
|
|
|
* checkpoint, so it's better to be slow in this case and fast otherwise.
|
2006-04-17 20:55:05 +02:00
|
|
|
*
|
|
|
|
* If we aren't doing full-page writes then RedoRecPtr doesn't actually
|
2006-10-04 02:30:14 +02:00
|
|
|
* affect the contents of the XLOG record, so we'll update our local copy
|
|
|
|
* but not force a recomputation.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
if (!XLByteEQ(RedoRecPtr, Insert->RedoRecPtr))
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
Assert(XLByteLT(RedoRecPtr, Insert->RedoRecPtr));
|
|
|
|
RedoRecPtr = Insert->RedoRecPtr;
|
|
|
|
|
2006-04-17 20:55:05 +02:00
|
|
|
if (doPageWrites)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
2006-04-17 20:55:05 +02:00
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2006-04-17 20:55:05 +02:00
|
|
|
if (dtbuf[i] == InvalidBuffer)
|
|
|
|
continue;
|
|
|
|
if (dtbuf_bkp[i] == false &&
|
|
|
|
XLByteLE(dtbuf_lsn[i], RedoRecPtr))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Oops, this buffer now needs to be backed up, but we
|
|
|
|
* didn't think so above. Start over.
|
|
|
|
*/
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
goto begin;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-17 20:55:05 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Also check to see if forcePageWrites was just turned on; if we weren't
|
|
|
|
* already doing full-page writes then go back and recompute. (If it was
|
|
|
|
* just turned off, we could recompute the record without full pages, but
|
|
|
|
* we choose not to bother.)
|
2006-04-17 20:55:05 +02:00
|
|
|
*/
|
|
|
|
if (Insert->forcePageWrites && !doPageWrites)
|
|
|
|
{
|
|
|
|
/* Oops, must redo it with full-page data */
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
goto begin;
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Make additional rdata chain entries for the backup blocks, so that we
|
|
|
|
* don't need to special-case them in the write loop. Note that we have
|
|
|
|
* now irrevocably changed the input rdata chain. At the exit of this
|
|
|
|
* loop, write_len includes the backup block data.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Also set the appropriate info bits to show which buffers were backed
|
|
|
|
* up. The i'th XLR_SET_BKP_BLOCK bit corresponds to the i'th distinct
|
|
|
|
* buffer value (ignoring InvalidBuffer) appearing in the rdata chain.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
write_len = len;
|
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
2005-06-02 07:55:29 +02:00
|
|
|
BkpBlock *bkpb;
|
|
|
|
char *page;
|
|
|
|
|
2005-06-06 22:22:58 +02:00
|
|
|
if (!dtbuf_bkp[i])
|
2000-12-28 14:00:29 +01:00
|
|
|
continue;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
info |= XLR_SET_BKP_BLOCK(i);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
bkpb = &(dtbuf_xlg[i]);
|
|
|
|
page = (char *) BufferGetBlock(dtbuf[i]);
|
|
|
|
|
|
|
|
rdt->next = &(dtbuf_rdt1[i]);
|
|
|
|
rdt = rdt->next;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
rdt->data = (char *) bkpb;
|
|
|
|
rdt->len = sizeof(BkpBlock);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
write_len += sizeof(BkpBlock);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
rdt->next = &(dtbuf_rdt2[i]);
|
|
|
|
rdt = rdt->next;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
if (bkpb->hole_length == 0)
|
|
|
|
{
|
|
|
|
rdt->data = page;
|
|
|
|
rdt->len = BLCKSZ;
|
|
|
|
write_len += BLCKSZ;
|
|
|
|
rdt->next = NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* must skip the hole */
|
|
|
|
rdt->data = page;
|
|
|
|
rdt->len = bkpb->hole_offset;
|
|
|
|
write_len += bkpb->hole_offset;
|
|
|
|
|
|
|
|
rdt->next = &(dtbuf_rdt3[i]);
|
|
|
|
rdt = rdt->next;
|
|
|
|
|
|
|
|
rdt->data = page + (bkpb->hole_offset + bkpb->hole_length);
|
|
|
|
rdt->len = BLCKSZ - (bkpb->hole_offset + bkpb->hole_length);
|
|
|
|
write_len += rdt->len;
|
|
|
|
rdt->next = NULL;
|
|
|
|
}
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
|
2007-05-20 23:08:19 +02:00
|
|
|
/*
|
|
|
|
* If we backed up any full blocks and online backup is not in progress,
|
|
|
|
* mark the backup blocks as removable. This allows the WAL archiver to
|
|
|
|
* know whether it is safe to compress archived WAL data by transforming
|
|
|
|
* full-block records into the non-full-block format.
|
|
|
|
*
|
|
|
|
* Note: we could just set the flag whenever !forcePageWrites, but
|
2007-11-15 22:14:46 +01:00
|
|
|
* defining it like this leaves the info bit free for some potential other
|
|
|
|
* use in records without any backup blocks.
|
2007-05-20 23:08:19 +02:00
|
|
|
*/
|
|
|
|
if ((info & XLR_BKP_BLOCK_MASK) && !Insert->forcePageWrites)
|
|
|
|
info |= XLR_BKP_REMOVABLE;
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* If there isn't enough space on the current XLOG page for a record
|
2005-10-15 04:49:52 +02:00
|
|
|
* header, advance to the next page (leaving the unused space as zeroes).
|
2004-02-11 23:55:26 +01:00
|
|
|
*/
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
updrqst = false;
|
|
|
|
freespace = INSERT_FREESPACE(Insert);
|
1999-09-27 17:48:12 +02:00
|
|
|
if (freespace < SizeOfXLogRecord)
|
|
|
|
{
|
2006-08-06 05:53:44 +02:00
|
|
|
updrqst = AdvanceXLInsertBuffer(false);
|
2004-02-11 23:55:26 +01:00
|
|
|
freespace = INSERT_FREESPACE(Insert);
|
|
|
|
}
|
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/* Compute record's XLOG location */
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
curridx = Insert->curridx;
|
2006-08-06 05:53:44 +02:00
|
|
|
INSERT_RECPTR(RecPtr, Insert, curridx);
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* If the record is an XLOG_SWITCH, and we are exactly at the start of a
|
|
|
|
* segment, we need not insert it (and don't want to because we'd like
|
|
|
|
* consecutive switch requests to be no-ops). Instead, make sure
|
|
|
|
* everything is written and flushed through the end of the prior segment,
|
|
|
|
* and return the prior segment's end address.
|
2006-08-06 05:53:44 +02:00
|
|
|
*/
|
|
|
|
if (isLogSwitch &&
|
|
|
|
(RecPtr.xrecoff % XLogSegSize) == SizeOfXLogLongPHD)
|
|
|
|
{
|
|
|
|
/* We can release insert lock immediately */
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
|
|
|
|
RecPtr.xrecoff -= SizeOfXLogLongPHD;
|
|
|
|
if (RecPtr.xrecoff == 0)
|
|
|
|
{
|
|
|
|
/* crossing a logid boundary */
|
|
|
|
RecPtr.xlogid -= 1;
|
|
|
|
RecPtr.xrecoff = XLogFileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
|
|
|
|
LogwrtResult = XLogCtl->Write.LogwrtResult;
|
|
|
|
if (!XLByteLE(RecPtr, LogwrtResult.Flush))
|
|
|
|
{
|
|
|
|
XLogwrtRqst FlushRqst;
|
|
|
|
|
|
|
|
FlushRqst.Write = RecPtr;
|
|
|
|
FlushRqst.Flush = RecPtr;
|
|
|
|
XLogWrite(FlushRqst, false, false);
|
|
|
|
}
|
|
|
|
LWLockRelease(WALWriteLock);
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
|
|
|
return RecPtr;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Insert record header */
|
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
record = (XLogRecord *) Insert->currpos;
|
1999-09-27 17:48:12 +02:00
|
|
|
record->xl_prev = Insert->PrevRecord;
|
2004-09-16 18:58:44 +02:00
|
|
|
record->xl_xid = GetCurrentTransactionIdIfAny();
|
2005-06-02 07:55:29 +02:00
|
|
|
record->xl_tot_len = SizeOfXLogRecord + write_len;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
record->xl_len = len; /* doesn't include backup blocks */
|
2000-12-28 14:00:29 +01:00
|
|
|
record->xl_info = info;
|
1999-09-27 17:48:12 +02:00
|
|
|
record->xl_rmid = rmid;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
/* Now we can finish computing the record's CRC */
|
|
|
|
COMP_CRC32(rdata_crc, (char *) record + sizeof(pg_crc32),
|
|
|
|
SizeOfXLogRecord - sizeof(pg_crc32));
|
|
|
|
FIN_CRC32(rdata_crc);
|
2000-12-28 14:00:29 +01:00
|
|
|
record->xl_crc = rdata_crc;
|
|
|
|
|
2004-01-06 18:26:23 +01:00
|
|
|
#ifdef WAL_DEBUG
|
2000-10-21 17:43:36 +02:00
|
|
|
if (XLOG_DEBUG)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
StringInfoData buf;
|
2000-10-21 17:43:36 +02:00
|
|
|
|
2006-03-24 05:32:13 +01:00
|
|
|
initStringInfo(&buf);
|
2006-08-06 05:53:44 +02:00
|
|
|
appendStringInfo(&buf, "INSERT @ %X/%X: ",
|
|
|
|
RecPtr.xlogid, RecPtr.xrecoff);
|
2006-03-24 05:32:13 +01:00
|
|
|
xlog_outrec(&buf, record);
|
2000-12-28 14:00:29 +01:00
|
|
|
if (rdata->data != NULL)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(&buf, " - ");
|
|
|
|
RmgrTable[record->xl_rmid].rm_desc(&buf, record->xl_info, rdata->data);
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
2006-03-24 05:32:13 +01:00
|
|
|
elog(LOG, "%s", buf.data);
|
|
|
|
pfree(buf.data);
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
2004-01-06 18:26:23 +01:00
|
|
|
#endif
|
2000-10-21 17:43:36 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Record begin of record in appropriate places */
|
|
|
|
ProcLastRecPtr = RecPtr;
|
|
|
|
Insert->PrevRecord = RecPtr;
|
|
|
|
|
1999-09-27 17:48:12 +02:00
|
|
|
Insert->currpos += SizeOfXLogRecord;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
freespace -= SizeOfXLogRecord;
|
2000-12-28 14:00:29 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Append the data, including backup blocks if any
|
|
|
|
*/
|
|
|
|
while (write_len)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2000-12-28 14:00:29 +01:00
|
|
|
while (rdata->data == NULL)
|
|
|
|
rdata = rdata->next;
|
|
|
|
|
|
|
|
if (freespace > 0)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2000-12-28 14:00:29 +01:00
|
|
|
if (rdata->len > freespace)
|
|
|
|
{
|
|
|
|
memcpy(Insert->currpos, rdata->data, freespace);
|
|
|
|
rdata->data += freespace;
|
|
|
|
rdata->len -= freespace;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
write_len -= freespace;
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
memcpy(Insert->currpos, rdata->data, rdata->len);
|
|
|
|
freespace -= rdata->len;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
write_len -= rdata->len;
|
2000-12-28 14:00:29 +01:00
|
|
|
Insert->currpos += rdata->len;
|
|
|
|
rdata = rdata->next;
|
|
|
|
continue;
|
|
|
|
}
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2000-12-28 14:00:29 +01:00
|
|
|
/* Use next buffer */
|
2006-08-06 05:53:44 +02:00
|
|
|
updrqst = AdvanceXLInsertBuffer(false);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
curridx = Insert->curridx;
|
|
|
|
/* Insert cont-record header */
|
|
|
|
Insert->currpage->xlp_info |= XLP_FIRST_IS_CONTRECORD;
|
|
|
|
contrecord = (XLogContRecord *) Insert->currpos;
|
|
|
|
contrecord->xl_rem_len = write_len;
|
|
|
|
Insert->currpos += SizeOfXLogContRecord;
|
2004-07-22 00:31:26 +02:00
|
|
|
freespace = INSERT_FREESPACE(Insert);
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
2000-12-28 14:00:29 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Ensure next record will be properly aligned */
|
|
|
|
Insert->currpos = (char *) Insert->currpage +
|
2001-03-22 05:01:46 +01:00
|
|
|
MAXALIGN(Insert->currpos - (char *) Insert->currpage);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
freespace = INSERT_FREESPACE(Insert);
|
2000-04-12 19:17:23 +02:00
|
|
|
|
2000-06-02 12:20:27 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The recptr I return is the beginning of the *next* record. This will be
|
|
|
|
* stored as LSN for changed data pages...
|
2000-06-02 12:20:27 +02:00
|
|
|
*/
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
INSERT_RECPTR(RecPtr, Insert, curridx);
|
2000-06-02 12:20:27 +02:00
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/*
|
|
|
|
* If the record is an XLOG_SWITCH, we must now write and flush all the
|
|
|
|
* existing data, and then forcibly advance to the start of the next
|
|
|
|
* segment. It's not good to do this I/O while holding the insert lock,
|
|
|
|
* but there seems too much risk of confusion if we try to release the
|
|
|
|
* lock sooner. Fortunately xlog switch needn't be a high-performance
|
|
|
|
* operation anyway...
|
|
|
|
*/
|
|
|
|
if (isLogSwitch)
|
|
|
|
{
|
|
|
|
XLogCtlWrite *Write = &XLogCtl->Write;
|
|
|
|
XLogwrtRqst FlushRqst;
|
|
|
|
XLogRecPtr OldSegEnd;
|
|
|
|
|
2008-12-17 02:39:04 +01:00
|
|
|
TRACE_POSTGRESQL_XLOG_SWITCH();
|
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Flush through the end of the page containing XLOG_SWITCH, and
|
|
|
|
* perform end-of-segment actions (eg, notifying archiver).
|
2006-08-06 05:53:44 +02:00
|
|
|
*/
|
|
|
|
WriteRqst = XLogCtl->xlblocks[curridx];
|
|
|
|
FlushRqst.Write = WriteRqst;
|
|
|
|
FlushRqst.Flush = WriteRqst;
|
|
|
|
XLogWrite(FlushRqst, false, true);
|
|
|
|
|
|
|
|
/* Set up the next buffer as first page of next segment */
|
|
|
|
/* Note: AdvanceXLInsertBuffer cannot need to do I/O here */
|
|
|
|
(void) AdvanceXLInsertBuffer(true);
|
|
|
|
|
|
|
|
/* There should be no unwritten data */
|
|
|
|
curridx = Insert->curridx;
|
|
|
|
Assert(curridx == Write->curridx);
|
|
|
|
|
|
|
|
/* Compute end address of old segment */
|
|
|
|
OldSegEnd = XLogCtl->xlblocks[curridx];
|
|
|
|
OldSegEnd.xrecoff -= XLOG_BLCKSZ;
|
|
|
|
if (OldSegEnd.xrecoff == 0)
|
|
|
|
{
|
|
|
|
/* crossing a logid boundary */
|
|
|
|
OldSegEnd.xlogid -= 1;
|
|
|
|
OldSegEnd.xrecoff = XLogFileSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make it look like we've written and synced all of old segment */
|
|
|
|
LogwrtResult.Write = OldSegEnd;
|
|
|
|
LogwrtResult.Flush = OldSegEnd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update shared-memory status --- this code should match XLogWrite
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->LogwrtResult = LogwrtResult;
|
|
|
|
if (XLByteLT(xlogctl->LogwrtRqst.Write, LogwrtResult.Write))
|
|
|
|
xlogctl->LogwrtRqst.Write = LogwrtResult.Write;
|
|
|
|
if (XLByteLT(xlogctl->LogwrtRqst.Flush, LogwrtResult.Flush))
|
|
|
|
xlogctl->LogwrtRqst.Flush = LogwrtResult.Flush;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
|
|
|
Write->LogwrtResult = LogwrtResult;
|
|
|
|
|
|
|
|
LWLockRelease(WALWriteLock);
|
|
|
|
|
|
|
|
updrqst = false; /* done already */
|
|
|
|
}
|
1999-09-27 17:48:12 +02:00
|
|
|
else
|
2006-08-06 05:53:44 +02:00
|
|
|
{
|
|
|
|
/* normal case, ie not xlog switch */
|
|
|
|
|
|
|
|
/* Need to update shared LogwrtRqst if some block was filled up */
|
|
|
|
if (freespace < SizeOfXLogRecord)
|
|
|
|
{
|
|
|
|
/* curridx is filled and available for writing out */
|
|
|
|
updrqst = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* if updrqst already set, write through end of previous buf */
|
|
|
|
curridx = PrevBufIdx(curridx);
|
|
|
|
}
|
|
|
|
WriteRqst = XLogCtl->xlblocks[curridx];
|
|
|
|
}
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(WALInsertLock);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
|
|
|
if (updrqst)
|
|
|
|
{
|
2001-12-28 19:16:43 +01:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* advance global request to include new block(s) */
|
2001-12-28 19:16:43 +01:00
|
|
|
if (XLByteLT(xlogctl->LogwrtRqst.Write, WriteRqst))
|
|
|
|
xlogctl->LogwrtRqst.Write = WriteRqst;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* update local result copy while I have the chance */
|
2001-12-28 19:16:43 +01:00
|
|
|
LogwrtResult = xlogctl->LogwrtResult;
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2007-09-05 20:10:48 +02:00
|
|
|
XactLastRecEnd = RecPtr;
|
2002-03-15 20:20:36 +01:00
|
|
|
|
2001-01-12 22:54:01 +01:00
|
|
|
END_CRIT_SECTION();
|
2001-12-28 19:16:43 +01:00
|
|
|
|
2006-01-11 09:43:13 +01:00
|
|
|
return RecPtr;
|
2000-04-12 19:17:23 +02:00
|
|
|
}
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
/*
|
2005-06-06 22:22:58 +02:00
|
|
|
* Determine whether the buffer referenced by an XLogRecData item has to
|
|
|
|
* be backed up, and if so fill a BkpBlock struct for it. In any case
|
|
|
|
* save the buffer's LSN at *lsn.
|
2005-06-02 07:55:29 +02:00
|
|
|
*/
|
2005-06-06 22:22:58 +02:00
|
|
|
static bool
|
2006-04-17 20:55:05 +02:00
|
|
|
XLogCheckBuffer(XLogRecData *rdata, bool doPageWrites,
|
2005-06-06 22:22:58 +02:00
|
|
|
XLogRecPtr *lsn, BkpBlock *bkpb)
|
2005-06-02 07:55:29 +02:00
|
|
|
{
|
2008-07-13 22:45:47 +02:00
|
|
|
Page page;
|
2005-06-06 22:22:58 +02:00
|
|
|
|
2008-07-13 22:45:47 +02:00
|
|
|
page = BufferGetPage(rdata->buffer);
|
2005-06-06 22:22:58 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* XXX We assume page LSN is first data on *every* page that can be passed
|
|
|
|
* to XLogInsert, whether it otherwise has the standard page layout or
|
|
|
|
* not.
|
2005-06-06 22:22:58 +02:00
|
|
|
*/
|
2008-07-13 22:45:47 +02:00
|
|
|
*lsn = PageGetLSN(page);
|
2005-06-06 22:22:58 +02:00
|
|
|
|
2006-04-17 20:55:05 +02:00
|
|
|
if (doPageWrites &&
|
2008-07-13 22:45:47 +02:00
|
|
|
XLByteLE(PageGetLSN(page), RedoRecPtr))
|
2005-06-02 07:55:29 +02:00
|
|
|
{
|
2005-06-06 22:22:58 +02:00
|
|
|
/*
|
|
|
|
* The page needs to be backed up, so set up *bkpb
|
|
|
|
*/
|
2008-08-11 13:05:11 +02:00
|
|
|
BufferGetTag(rdata->buffer, &bkpb->node, &bkpb->fork, &bkpb->block);
|
2005-06-02 07:55:29 +02:00
|
|
|
|
2005-06-06 22:22:58 +02:00
|
|
|
if (rdata->buffer_std)
|
|
|
|
{
|
|
|
|
/* Assume we can omit data between pd_lower and pd_upper */
|
2008-07-13 22:45:47 +02:00
|
|
|
uint16 lower = ((PageHeader) page)->pd_lower;
|
|
|
|
uint16 upper = ((PageHeader) page)->pd_upper;
|
2005-06-06 22:22:58 +02:00
|
|
|
|
|
|
|
if (lower >= SizeOfPageHeaderData &&
|
|
|
|
upper > lower &&
|
|
|
|
upper <= BLCKSZ)
|
|
|
|
{
|
|
|
|
bkpb->hole_offset = lower;
|
|
|
|
bkpb->hole_length = upper - lower;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No "hole" to compress out */
|
|
|
|
bkpb->hole_offset = 0;
|
|
|
|
bkpb->hole_length = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not a standard page header, don't try to eliminate "hole" */
|
|
|
|
bkpb->hole_offset = 0;
|
|
|
|
bkpb->hole_length = 0;
|
|
|
|
}
|
2005-06-02 07:55:29 +02:00
|
|
|
|
2005-06-06 22:22:58 +02:00
|
|
|
return true; /* buffer requires backup */
|
2005-06-02 07:55:29 +02:00
|
|
|
}
|
2005-06-06 22:22:58 +02:00
|
|
|
|
|
|
|
return false; /* buffer does not need to be backed up */
|
2005-06-02 07:55:29 +02:00
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* XLogArchiveNotify
|
|
|
|
*
|
|
|
|
* Create an archive notification file
|
|
|
|
*
|
|
|
|
* The name of the notification file is the message that will be picked up
|
2004-07-22 00:31:26 +02:00
|
|
|
* by the archiver, e.g. we write 0000000100000001000000C6.ready
|
2005-07-04 06:51:52 +02:00
|
|
|
* and the archiver then knows to archive XLOGDIR/0000000100000001000000C6,
|
2004-07-22 00:31:26 +02:00
|
|
|
* then when complete, rename it to 0000000100000001000000C6.done
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
XLogArchiveNotify(const char *xlog)
|
|
|
|
{
|
|
|
|
char archiveStatusPath[MAXPGPATH];
|
2004-08-29 07:07:03 +02:00
|
|
|
FILE *fd;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/* insert an otherwise empty file called <XLOG>.ready */
|
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".ready");
|
|
|
|
fd = AllocateFile(archiveStatusPath, "w");
|
2004-08-29 07:07:03 +02:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
2004-07-19 04:47:16 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not create archive status file \"%s\": %m",
|
|
|
|
archiveStatusPath)));
|
|
|
|
return;
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
if (FreeFile(fd))
|
|
|
|
{
|
2004-07-19 04:47:16 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not write archive status file \"%s\": %m",
|
|
|
|
archiveStatusPath)));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Notify archiver that it's got something to do */
|
|
|
|
if (IsUnderPostmaster)
|
|
|
|
SendPostmasterSignal(PMSIGNAL_WAKEN_ARCHIVER);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Convenience routine to notify using log/seg representation of filename
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
XLogArchiveNotifySeg(uint32 log, uint32 seg)
|
|
|
|
{
|
|
|
|
char xlog[MAXFNAMELEN];
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFileName(xlog, ThisTimeLineID, log, seg);
|
2004-07-19 04:47:16 +02:00
|
|
|
XLogArchiveNotify(xlog);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-06-22 22:42:57 +02:00
|
|
|
* XLogArchiveCheckDone
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2006-06-22 22:42:57 +02:00
|
|
|
* This is called when we are ready to delete or recycle an old XLOG segment
|
|
|
|
* file or backup history file. If it is okay to delete it then return true.
|
|
|
|
* If it is not time to delete it, make sure a .ready file exists, and return
|
|
|
|
* false.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
|
|
|
* If <XLOG>.done exists, then return true; else if <XLOG>.ready exists,
|
2006-06-22 22:42:57 +02:00
|
|
|
* then return false; else create <XLOG>.ready and return false.
|
|
|
|
*
|
|
|
|
* The reason we do things this way is so that if the original attempt to
|
|
|
|
* create <XLOG>.ready fails, we'll retry during subsequent checkpoints.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
static bool
|
2008-09-08 18:42:15 +02:00
|
|
|
XLogArchiveCheckDone(const char *xlog)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
|
|
|
char archiveStatusPath[MAXPGPATH];
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
2006-06-22 22:42:57 +02:00
|
|
|
/* Always deletable if archiving is off */
|
|
|
|
if (!XLogArchivingActive())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* First check for .done --- this means archiver is done with it */
|
2004-07-19 04:47:16 +02:00
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".done");
|
|
|
|
if (stat(archiveStatusPath, &stat_buf) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* check for .ready --- this means archiver is still busy with it */
|
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".ready");
|
|
|
|
if (stat(archiveStatusPath, &stat_buf) == 0)
|
2004-08-29 07:07:03 +02:00
|
|
|
return false;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/* Race condition --- maybe archiver just finished, so recheck */
|
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".done");
|
|
|
|
if (stat(archiveStatusPath, &stat_buf) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Retry creation of the .ready file */
|
2008-09-08 18:42:15 +02:00
|
|
|
XLogArchiveNotify(xlog);
|
2004-07-19 04:47:16 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-09-08 18:42:15 +02:00
|
|
|
/*
|
|
|
|
* XLogArchiveIsBusy
|
|
|
|
*
|
|
|
|
* Check to see if an XLOG segment file is still unarchived.
|
|
|
|
* This is almost but not quite the inverse of XLogArchiveCheckDone: in
|
|
|
|
* the first place we aren't chartered to recreate the .ready file, and
|
|
|
|
* in the second place we should consider that if the file is already gone
|
|
|
|
* then it's not busy. (This check is needed to handle the race condition
|
|
|
|
* that a checkpoint already deleted the no-longer-needed file.)
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
XLogArchiveIsBusy(const char *xlog)
|
|
|
|
{
|
|
|
|
char archiveStatusPath[MAXPGPATH];
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
/* First check for .done --- this means archiver is done with it */
|
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".done");
|
|
|
|
if (stat(archiveStatusPath, &stat_buf) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* check for .ready --- this means archiver is still busy with it */
|
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".ready");
|
|
|
|
if (stat(archiveStatusPath, &stat_buf) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Race condition --- maybe archiver just finished, so recheck */
|
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".done");
|
|
|
|
if (stat(archiveStatusPath, &stat_buf) == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Check to see if the WAL file has been removed by checkpoint, which
|
|
|
|
* implies it has already been archived, and explains why we can't see a
|
|
|
|
* status file for it.
|
2008-09-08 18:42:15 +02:00
|
|
|
*/
|
|
|
|
snprintf(archiveStatusPath, MAXPGPATH, XLOGDIR "/%s", xlog);
|
|
|
|
if (stat(archiveStatusPath, &stat_buf) != 0 &&
|
|
|
|
errno == ENOENT)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* XLogArchiveCleanup
|
|
|
|
*
|
2004-07-22 00:31:26 +02:00
|
|
|
* Cleanup archive notification file(s) for a particular xlog segment
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
XLogArchiveCleanup(const char *xlog)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char archiveStatusPath[MAXPGPATH];
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Remove the .done file */
|
2004-07-19 04:47:16 +02:00
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".done");
|
|
|
|
unlink(archiveStatusPath);
|
|
|
|
/* should we complain about failure? */
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
/* Remove the .ready file if present --- normally it shouldn't be */
|
|
|
|
StatusFilePath(archiveStatusPath, xlog, ".ready");
|
|
|
|
unlink(archiveStatusPath);
|
|
|
|
/* should we complain about failure? */
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Advance the Insert state to the next buffer page, writing out the next
|
|
|
|
* buffer if it still contains unwritten data.
|
|
|
|
*
|
2006-08-06 05:53:44 +02:00
|
|
|
* If new_segment is TRUE then we set up the next buffer page as the first
|
|
|
|
* page of the next xlog segment file, possibly but not usually the next
|
|
|
|
* consecutive file page.
|
|
|
|
*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* The global LogwrtRqst.Write pointer needs to be advanced to include the
|
2001-09-29 06:02:27 +02:00
|
|
|
* just-filled page. If we can do this for free (without an extra lock),
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* we do so here. Otherwise the caller must do it. We return TRUE if the
|
|
|
|
* request update still needs to be done, FALSE if we did it internally.
|
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* Must be called with WALInsertLock held.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
static bool
|
2006-08-06 05:53:44 +02:00
|
|
|
AdvanceXLInsertBuffer(bool new_segment)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLogCtlInsert *Insert = &XLogCtl->Insert;
|
|
|
|
XLogCtlWrite *Write = &XLogCtl->Write;
|
2005-08-22 02:41:28 +02:00
|
|
|
int nextidx = NextBufIdx(Insert->curridx);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
bool update_needed = true;
|
|
|
|
XLogRecPtr OldPageRqstPtr;
|
|
|
|
XLogwrtRqst WriteRqst;
|
2001-07-19 04:12:35 +02:00
|
|
|
XLogRecPtr NewPageEndPtr;
|
|
|
|
XLogPageHeader NewPage;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Use Insert->LogwrtResult copy if it's more fresh */
|
|
|
|
if (XLByteLT(LogwrtResult.Write, Insert->LogwrtResult.Write))
|
|
|
|
LogwrtResult = Insert->LogwrtResult;
|
2000-10-28 18:21:00 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Get ending-offset of the buffer page we need to replace (this may be
|
|
|
|
* zero if the buffer hasn't been used yet). Fall through if it's already
|
|
|
|
* written out.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
OldPageRqstPtr = XLogCtl->xlblocks[nextidx];
|
|
|
|
if (!XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
|
|
|
|
{
|
|
|
|
/* nope, got work to do... */
|
|
|
|
XLogRecPtr FinishedPageRqstPtr;
|
2000-12-03 11:27:29 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
FinishedPageRqstPtr = XLogCtl->xlblocks[Insert->curridx];
|
2000-12-03 11:27:29 +01:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/* Before waiting, get info_lck and update LogwrtResult */
|
2001-12-28 19:16:43 +01:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2001-12-28 19:16:43 +01:00
|
|
|
if (XLByteLT(xlogctl->LogwrtRqst.Write, FinishedPageRqstPtr))
|
|
|
|
xlogctl->LogwrtRqst.Write = FinishedPageRqstPtr;
|
|
|
|
LogwrtResult = xlogctl->LogwrtResult;
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
2001-12-28 19:16:43 +01:00
|
|
|
}
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
update_needed = false; /* Did the shared-request update */
|
|
|
|
|
|
|
|
if (XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
|
|
|
|
{
|
|
|
|
/* OK, someone wrote it already */
|
|
|
|
Insert->LogwrtResult = LogwrtResult;
|
|
|
|
}
|
|
|
|
else
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2001-09-29 06:02:27 +02:00
|
|
|
/* Must acquire write lock */
|
|
|
|
LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
|
|
|
|
LogwrtResult = Write->LogwrtResult;
|
|
|
|
if (XLByteLE(OldPageRqstPtr, LogwrtResult.Write))
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2001-09-29 06:02:27 +02:00
|
|
|
/* OK, someone wrote it already */
|
|
|
|
LWLockRelease(WALWriteLock);
|
|
|
|
Insert->LogwrtResult = LogwrtResult;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
2001-09-29 06:02:27 +02:00
|
|
|
else
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Have to write buffers while holding insert lock. This is
|
|
|
|
* not good, so only write as much as we absolutely must.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2008-12-24 21:41:29 +01:00
|
|
|
TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_START();
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
WriteRqst.Write = OldPageRqstPtr;
|
|
|
|
WriteRqst.Flush.xlogid = 0;
|
|
|
|
WriteRqst.Flush.xrecoff = 0;
|
2006-08-06 05:53:44 +02:00
|
|
|
XLogWrite(WriteRqst, false, false);
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(WALWriteLock);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
Insert->LogwrtResult = LogwrtResult;
|
2008-12-24 21:41:29 +01:00
|
|
|
TRACE_POSTGRESQL_WAL_BUFFER_WRITE_DIRTY_DONE();
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Now the next buffer slot is free and we can set it up to be the next
|
|
|
|
* output page.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2001-07-19 04:12:35 +02:00
|
|
|
NewPageEndPtr = XLogCtl->xlblocks[Insert->curridx];
|
2006-08-06 05:53:44 +02:00
|
|
|
|
|
|
|
if (new_segment)
|
|
|
|
{
|
|
|
|
/* force it to a segment start point */
|
|
|
|
NewPageEndPtr.xrecoff += XLogSegSize - 1;
|
|
|
|
NewPageEndPtr.xrecoff -= NewPageEndPtr.xrecoff % XLogSegSize;
|
|
|
|
}
|
|
|
|
|
2001-07-19 04:12:35 +02:00
|
|
|
if (NewPageEndPtr.xrecoff >= XLogFileSize)
|
2001-02-13 21:40:25 +01:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* crossing a logid boundary */
|
2001-07-19 04:12:35 +02:00
|
|
|
NewPageEndPtr.xlogid += 1;
|
2006-04-04 01:35:05 +02:00
|
|
|
NewPageEndPtr.xrecoff = XLOG_BLCKSZ;
|
2001-02-13 21:40:25 +01:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
else
|
2006-04-04 01:35:05 +02:00
|
|
|
NewPageEndPtr.xrecoff += XLOG_BLCKSZ;
|
2001-07-19 04:12:35 +02:00
|
|
|
XLogCtl->xlblocks[nextidx] = NewPageEndPtr;
|
2006-04-04 01:35:05 +02:00
|
|
|
NewPage = (XLogPageHeader) (XLogCtl->pages + nextidx * (Size) XLOG_BLCKSZ);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
Insert->curridx = nextidx;
|
2001-07-19 04:12:35 +02:00
|
|
|
Insert->currpage = NewPage;
|
2005-10-15 04:49:52 +02:00
|
|
|
|
|
|
|
Insert->currpos = ((char *) NewPage) +SizeOfXLogShortPHD;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Be sure to re-zero the buffer so that bytes beyond what we've written
|
|
|
|
* will look like zeroes and not valid XLOG records...
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2006-04-04 01:35:05 +02:00
|
|
|
MemSet((char *) NewPage, 0, XLOG_BLCKSZ);
|
2001-07-19 04:12:35 +02:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
|
|
|
* Fill the new page's header
|
|
|
|
*/
|
2005-10-15 04:49:52 +02:00
|
|
|
NewPage ->xlp_magic = XLOG_PAGE_MAGIC;
|
|
|
|
|
2001-10-28 07:26:15 +01:00
|
|
|
/* NewPage->xlp_info = 0; */ /* done by memset */
|
2005-10-15 04:49:52 +02:00
|
|
|
NewPage ->xlp_tli = ThisTimeLineID;
|
|
|
|
NewPage ->xlp_pageaddr.xlogid = NewPageEndPtr.xlogid;
|
2006-04-04 01:35:05 +02:00
|
|
|
NewPage ->xlp_pageaddr.xrecoff = NewPageEndPtr.xrecoff - XLOG_BLCKSZ;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* If first page of an XLOG segment file, make it a long header.
|
2004-02-11 23:55:26 +01:00
|
|
|
*/
|
|
|
|
if ((NewPage->xlp_pageaddr.xrecoff % XLogSegSize) == 0)
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogLongPageHeader NewLongPage = (XLogLongPageHeader) NewPage;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
NewLongPage->xlp_sysid = ControlFile->system_identifier;
|
|
|
|
NewLongPage->xlp_seg_size = XLogSegSize;
|
2006-04-05 05:34:05 +02:00
|
|
|
NewLongPage->xlp_xlog_blcksz = XLOG_BLCKSZ;
|
2005-10-15 04:49:52 +02:00
|
|
|
NewPage ->xlp_info |= XLP_LONG_HEADER;
|
|
|
|
|
|
|
|
Insert->currpos = ((char *) NewPage) +SizeOfXLogLongPHD;
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return update_needed;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2007-10-12 21:39:59 +02:00
|
|
|
/*
|
|
|
|
* Check whether we've consumed enough xlog space that a checkpoint is needed.
|
|
|
|
*
|
2010-06-09 17:04:07 +02:00
|
|
|
* logid/logseg indicate a log file that has just been filled up (or read
|
|
|
|
* during recovery). We measure the distance from RedoRecPtr to logid/logseg
|
|
|
|
* and see if that exceeds CheckPointSegments.
|
2007-10-12 21:39:59 +02:00
|
|
|
*
|
|
|
|
* Note: it is caller's responsibility that RedoRecPtr is up-to-date.
|
|
|
|
*/
|
|
|
|
static bool
|
2010-06-09 17:04:07 +02:00
|
|
|
XLogCheckpointNeeded(uint32 logid, uint32 logseg)
|
2007-10-12 21:39:59 +02:00
|
|
|
{
|
|
|
|
/*
|
2007-11-15 23:25:18 +01:00
|
|
|
* A straight computation of segment number could overflow 32 bits. Rather
|
|
|
|
* than assuming we have working 64-bit arithmetic, we compare the
|
2007-11-15 22:14:46 +01:00
|
|
|
* highest-order bits separately, and force a checkpoint immediately when
|
|
|
|
* they change.
|
2007-10-12 21:39:59 +02:00
|
|
|
*/
|
|
|
|
uint32 old_segno,
|
|
|
|
new_segno;
|
|
|
|
uint32 old_highbits,
|
|
|
|
new_highbits;
|
|
|
|
|
|
|
|
old_segno = (RedoRecPtr.xlogid % XLogSegSize) * XLogSegsPerFile +
|
|
|
|
(RedoRecPtr.xrecoff / XLogSegSize);
|
|
|
|
old_highbits = RedoRecPtr.xlogid / XLogSegSize;
|
2010-06-09 17:04:07 +02:00
|
|
|
new_segno = (logid % XLogSegSize) * XLogSegsPerFile + logseg;
|
|
|
|
new_highbits = logid / XLogSegSize;
|
2007-10-12 21:39:59 +02:00
|
|
|
if (new_highbits != old_highbits ||
|
2007-11-15 22:14:46 +01:00
|
|
|
new_segno >= old_segno + (uint32) (CheckPointSegments - 1))
|
2007-10-12 21:39:59 +02:00
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Write and/or fsync the log at least as far as WriteRqst indicates.
|
|
|
|
*
|
2005-08-23 01:59:04 +02:00
|
|
|
* If flexible == TRUE, we don't have to write as far as WriteRqst, but
|
|
|
|
* may stop at any convenient boundary (such as a cache or logfile boundary).
|
|
|
|
* This option allows us to avoid uselessly issuing multiple writes when a
|
|
|
|
* single one would do.
|
|
|
|
*
|
2006-08-06 05:53:44 +02:00
|
|
|
* If xlog_switch == TRUE, we are intending an xlog segment switch, so
|
|
|
|
* perform end-of-segment actions after writing the last page, even if
|
|
|
|
* it's not physically the end of its segment. (NB: this will work properly
|
|
|
|
* only if caller specifies WriteRqst == page-end and flexible == false,
|
|
|
|
* and there is some data to write.)
|
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* Must be called with WALWriteLock held.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
1999-09-27 17:48:12 +02:00
|
|
|
static void
|
2006-08-06 05:53:44 +02:00
|
|
|
XLogWrite(XLogwrtRqst WriteRqst, bool flexible, bool xlog_switch)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
XLogCtlWrite *Write = &XLogCtl->Write;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
bool ispartialpage;
|
2006-08-06 05:53:44 +02:00
|
|
|
bool last_iteration;
|
2005-08-23 01:59:04 +02:00
|
|
|
bool finishing_seg;
|
2001-03-17 21:54:13 +01:00
|
|
|
bool use_existent;
|
2005-08-23 01:59:04 +02:00
|
|
|
int curridx;
|
|
|
|
int npages;
|
|
|
|
int startidx;
|
|
|
|
uint32 startoffset;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2005-04-16 00:19:48 +02:00
|
|
|
/* We should always be inside a critical section here */
|
|
|
|
Assert(CritSectionCount > 0);
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Update local LogwrtResult (caller probably did this already, but...)
|
2001-03-22 05:01:46 +01:00
|
|
|
*/
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
LogwrtResult = Write->LogwrtResult;
|
|
|
|
|
2005-08-23 01:59:04 +02:00
|
|
|
/*
|
|
|
|
* Since successive pages in the xlog cache are consecutively allocated,
|
|
|
|
* we can usually gather multiple pages together and issue just one
|
2005-10-15 04:49:52 +02:00
|
|
|
* write() call. npages is the number of pages we have determined can be
|
|
|
|
* written together; startidx is the cache block index of the first one,
|
|
|
|
* and startoffset is the file offset at which it should go. The latter
|
|
|
|
* two variables are only valid when npages > 0, but we must initialize
|
|
|
|
* all of them to keep the compiler quiet.
|
2005-08-23 01:59:04 +02:00
|
|
|
*/
|
|
|
|
npages = 0;
|
|
|
|
startidx = 0;
|
|
|
|
startoffset = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Within the loop, curridx is the cache block index of the page to
|
|
|
|
* consider writing. We advance Write->curridx only after successfully
|
|
|
|
* writing pages. (Right now, this refinement is useless since we are
|
2005-10-15 04:49:52 +02:00
|
|
|
* going to PANIC if any error occurs anyway; but someday it may come in
|
|
|
|
* useful.)
|
2005-08-23 01:59:04 +02:00
|
|
|
*/
|
|
|
|
curridx = Write->curridx;
|
Use O_DIRECT if available when using O_SYNC for wal_sync_method.
Also, write multiple WAL buffers out in one write() operation.
ITAGAKI Takahiro
---------------------------------------------------------------------------
> If we disable writeback-cache and use open_sync, the per-page writing
> behavior in WAL module will show up as bad result. O_DIRECT is similar
> to O_DSYNC (at least on linux), so that the benefit of it will disappear
> behind the slow disk revolution.
>
> In the current source, WAL is written as:
> for (i = 0; i < N; i++) { write(&buffers[i], BLCKSZ); }
> Is this intentional? Can we rewrite it as follows?
> write(&buffers[0], N * BLCKSZ);
>
> In order to achieve it, I wrote a 'gather-write' patch (xlog.gw.diff).
> Aside from this, I'll also send the fixed direct io patch (xlog.dio.diff).
> These two patches are independent, so they can be applied either or both.
>
>
> I tested them on my machine and the results as follows. It shows that
> direct-io and gather-write is the best choice when writeback-cache is off.
> Are these two patches worth trying if they are used together?
>
>
> | writeback | fsync= | fdata | open_ | fsync_ | open_
> patch | cache | false | sync | sync | direct | direct
> ------------+-----------+--------+-------+-------+--------+---------
> direct io | off | 124.2 | 105.7 | 48.3 | 48.3 | 48.2
> direct io | on | 129.1 | 112.3 | 114.1 | 142.9 | 144.5
> gather-write| off | 124.3 | 108.7 | 105.4 | (N/A) | (N/A)
> both | off | 131.5 | 115.5 | 114.4 | 145.4 | 145.2
>
> - 20runs * pgbench -s 100 -c 50 -t 200
> - with tuning (wal_buffers=64, commit_delay=500, checkpoint_segments=8)
> - using 2 ATA disks:
> - hda(reiserfs) includes system and wal.
> - hdc(jfs) includes database files. writeback-cache is always on.
>
> ---
> ITAGAKI Takahiro
2005-07-29 05:22:33 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
while (XLByteLT(LogwrtResult.Write, WriteRqst.Write))
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2001-03-16 06:44:33 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Make sure we're not ahead of the insert process. This could happen
|
|
|
|
* if we're passed a bogus WriteRqst.Write that is past the end of the
|
|
|
|
* last page that's been initialized by AdvanceXLInsertBuffer.
|
2001-03-16 06:44:33 +01:00
|
|
|
*/
|
2005-08-23 01:59:04 +02:00
|
|
|
if (!XLByteLT(LogwrtResult.Write, XLogCtl->xlblocks[curridx]))
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(PANIC, "xlog write request %X/%X is past end of log %X/%X",
|
2001-09-06 04:02:48 +02:00
|
|
|
LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
|
2005-08-23 01:59:04 +02:00
|
|
|
XLogCtl->xlblocks[curridx].xlogid,
|
|
|
|
XLogCtl->xlblocks[curridx].xrecoff);
|
2001-03-16 06:44:33 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Advance LogwrtResult.Write to end of current buffer page */
|
2005-08-23 01:59:04 +02:00
|
|
|
LogwrtResult.Write = XLogCtl->xlblocks[curridx];
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ispartialpage = XLByteLT(WriteRqst.Write, LogwrtResult.Write);
|
|
|
|
|
|
|
|
if (!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-08-23 01:59:04 +02:00
|
|
|
* Switch to new logfile segment. We cannot have any pending
|
|
|
|
* pages here (since we dump what we have at segment end).
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2005-08-23 01:59:04 +02:00
|
|
|
Assert(npages == 0);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (openLogFile >= 0)
|
2006-06-15 21:15:00 +02:00
|
|
|
XLogFileClose();
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
|
|
|
|
|
2001-03-17 21:54:13 +01:00
|
|
|
/* create/use new log file */
|
|
|
|
use_existent = true;
|
|
|
|
openLogFile = XLogFileInit(openLogId, openLogSeg,
|
|
|
|
&use_existent, true);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
openLogOff = 0;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2005-08-23 01:59:04 +02:00
|
|
|
/* Make sure we have the current logfile open */
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (openLogFile < 0)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
|
2004-07-22 00:31:26 +02:00
|
|
|
openLogFile = XLogFileOpen(openLogId, openLogSeg);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
openLogOff = 0;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2005-08-23 01:59:04 +02:00
|
|
|
/* Add current page to the set of pending pages-to-dump */
|
|
|
|
if (npages == 0)
|
|
|
|
{
|
|
|
|
/* first of group */
|
|
|
|
startidx = curridx;
|
2006-04-04 01:35:05 +02:00
|
|
|
startoffset = (LogwrtResult.Write.xrecoff - XLOG_BLCKSZ) % XLogSegSize;
|
2005-08-23 01:59:04 +02:00
|
|
|
}
|
|
|
|
npages++;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Dump the set if this will be the last loop iteration, or if we are
|
|
|
|
* at the last page of the cache area (since the next page won't be
|
|
|
|
* contiguous in memory), or if we are at the end of the logfile
|
|
|
|
* segment.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2006-08-06 05:53:44 +02:00
|
|
|
last_iteration = !XLByteLT(LogwrtResult.Write, WriteRqst.Write);
|
|
|
|
|
2005-08-23 01:59:04 +02:00
|
|
|
finishing_seg = !ispartialpage &&
|
2006-04-04 01:35:05 +02:00
|
|
|
(startoffset + npages * XLOG_BLCKSZ) >= XLogSegSize;
|
2005-08-23 01:59:04 +02:00
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
if (last_iteration ||
|
2005-08-23 01:59:04 +02:00
|
|
|
curridx == XLogCtl->XLogCacheBlck ||
|
|
|
|
finishing_seg)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2005-08-23 01:59:04 +02:00
|
|
|
char *from;
|
|
|
|
Size nbytes;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2005-08-23 01:59:04 +02:00
|
|
|
/* Need to seek in the file? */
|
|
|
|
if (openLogOff != startoffset)
|
|
|
|
{
|
|
|
|
if (lseek(openLogFile, (off_t) startoffset, SEEK_SET) < 0)
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not seek in log file %u, "
|
|
|
|
"segment %u to offset %u: %m",
|
|
|
|
openLogId, openLogSeg, startoffset)));
|
|
|
|
openLogOff = startoffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* OK to write the page(s) */
|
2006-04-04 01:35:05 +02:00
|
|
|
from = XLogCtl->pages + startidx * (Size) XLOG_BLCKSZ;
|
|
|
|
nbytes = npages * (Size) XLOG_BLCKSZ;
|
2005-08-23 01:59:04 +02:00
|
|
|
errno = 0;
|
|
|
|
if (write(openLogFile, from, nbytes) != nbytes)
|
|
|
|
{
|
|
|
|
/* if write didn't set errno, assume no disk space */
|
|
|
|
if (errno == 0)
|
|
|
|
errno = ENOSPC;
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not write to log file %u, segment %u "
|
2005-10-29 02:31:52 +02:00
|
|
|
"at offset %u, length %lu: %m",
|
2005-08-23 01:59:04 +02:00
|
|
|
openLogId, openLogSeg,
|
|
|
|
openLogOff, (unsigned long) nbytes)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Update state for write */
|
|
|
|
openLogOff += nbytes;
|
|
|
|
Write->curridx = ispartialpage ? curridx : NextBufIdx(curridx);
|
|
|
|
npages = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we just wrote the whole last page of a logfile segment,
|
|
|
|
* fsync the segment immediately. This avoids having to go back
|
|
|
|
* and re-open prior segments when an fsync request comes along
|
|
|
|
* later. Doing it here ensures that one and only one backend will
|
|
|
|
* perform this fsync.
|
|
|
|
*
|
2006-08-06 05:53:44 +02:00
|
|
|
* We also do this if this is the last page written for an xlog
|
|
|
|
* switch.
|
|
|
|
*
|
2005-08-23 01:59:04 +02:00
|
|
|
* This is also the right place to notify the Archiver that the
|
2006-10-04 02:30:14 +02:00
|
|
|
* segment is ready to copy to archival storage, and to update the
|
2006-12-08 20:50:53 +01:00
|
|
|
* timer for archive_timeout, and to signal for a checkpoint if
|
|
|
|
* too many logfile segments have been used since the last
|
|
|
|
* checkpoint.
|
2005-08-23 01:59:04 +02:00
|
|
|
*/
|
2006-08-06 05:53:44 +02:00
|
|
|
if (finishing_seg || (xlog_switch && last_iteration))
|
2005-08-23 01:59:04 +02:00
|
|
|
{
|
2010-01-15 10:19:10 +01:00
|
|
|
issue_xlog_fsync(openLogFile, openLogId, openLogSeg);
|
2005-10-15 04:49:52 +02:00
|
|
|
LogwrtResult.Flush = LogwrtResult.Write; /* end of page */
|
2005-08-23 01:59:04 +02:00
|
|
|
|
|
|
|
if (XLogArchivingActive())
|
|
|
|
XLogArchiveNotifySeg(openLogId, openLogSeg);
|
2006-08-18 01:04:10 +02:00
|
|
|
|
2008-02-17 03:09:32 +01:00
|
|
|
Write->lastSegSwitchTime = (pg_time_t) time(NULL);
|
2006-12-08 20:50:53 +01:00
|
|
|
|
|
|
|
/*
|
2007-06-30 21:12:02 +02:00
|
|
|
* Signal bgwriter to start a checkpoint if we've consumed too
|
2007-10-12 21:39:59 +02:00
|
|
|
* much xlog since the last one. For speed, we first check
|
2007-11-15 22:14:46 +01:00
|
|
|
* using the local copy of RedoRecPtr, which might be out of
|
|
|
|
* date; if it looks like a checkpoint is needed, forcibly
|
|
|
|
* update RedoRecPtr and recheck.
|
2006-12-08 20:50:53 +01:00
|
|
|
*/
|
2007-10-12 21:39:59 +02:00
|
|
|
if (IsUnderPostmaster &&
|
2010-06-09 17:04:07 +02:00
|
|
|
XLogCheckpointNeeded(openLogId, openLogSeg))
|
2006-12-08 20:50:53 +01:00
|
|
|
{
|
2007-10-12 21:39:59 +02:00
|
|
|
(void) GetRedoRecPtr();
|
2010-06-09 17:04:07 +02:00
|
|
|
if (XLogCheckpointNeeded(openLogId, openLogSeg))
|
2007-06-30 21:12:02 +02:00
|
|
|
RequestCheckpoint(CHECKPOINT_CAUSE_XLOG);
|
2006-12-08 20:50:53 +01:00
|
|
|
}
|
2005-08-23 01:59:04 +02:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (ispartialpage)
|
|
|
|
{
|
|
|
|
/* Only asked to write a partial page */
|
|
|
|
LogwrtResult.Write = WriteRqst.Write;
|
|
|
|
break;
|
|
|
|
}
|
2005-08-23 01:59:04 +02:00
|
|
|
curridx = NextBufIdx(curridx);
|
|
|
|
|
|
|
|
/* If flexible, break out of loop as soon as we wrote something */
|
|
|
|
if (flexible && npages == 0)
|
|
|
|
break;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
2005-08-23 01:59:04 +02:00
|
|
|
|
|
|
|
Assert(npages == 0);
|
|
|
|
Assert(curridx == Write->curridx);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* If asked to flush, do so
|
|
|
|
*/
|
|
|
|
if (XLByteLT(LogwrtResult.Flush, WriteRqst.Flush) &&
|
|
|
|
XLByteLT(LogwrtResult.Flush, LogwrtResult.Write))
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Could get here without iterating above loop, in which case we might
|
|
|
|
* have no open file or the wrong one. However, we do not need to
|
|
|
|
* fsync more than one file.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2008-05-16 21:15:05 +02:00
|
|
|
if (sync_method != SYNC_METHOD_OPEN &&
|
|
|
|
sync_method != SYNC_METHOD_OPEN_DSYNC)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2001-03-16 06:44:33 +01:00
|
|
|
if (openLogFile >= 0 &&
|
2005-10-15 04:49:52 +02:00
|
|
|
!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
|
2006-06-15 21:15:00 +02:00
|
|
|
XLogFileClose();
|
2001-03-16 06:44:33 +01:00
|
|
|
if (openLogFile < 0)
|
|
|
|
{
|
|
|
|
XLByteToPrevSeg(LogwrtResult.Write, openLogId, openLogSeg);
|
2004-07-22 00:31:26 +02:00
|
|
|
openLogFile = XLogFileOpen(openLogId, openLogSeg);
|
2001-03-16 06:44:33 +01:00
|
|
|
openLogOff = 0;
|
|
|
|
}
|
2010-01-15 10:19:10 +01:00
|
|
|
issue_xlog_fsync(openLogFile, openLogId, openLogSeg);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
LogwrtResult.Flush = LogwrtResult.Write;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Update shared-memory status
|
|
|
|
*
|
2001-03-22 05:01:46 +01:00
|
|
|
* We make sure that the shared 'request' values do not fall behind the
|
2005-10-15 04:49:52 +02:00
|
|
|
* 'result' values. This is not absolutely essential, but it saves some
|
|
|
|
* code in a couple of places.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2001-12-28 19:16:43 +01:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2001-12-28 19:16:43 +01:00
|
|
|
xlogctl->LogwrtResult = LogwrtResult;
|
|
|
|
if (XLByteLT(xlogctl->LogwrtRqst.Write, LogwrtResult.Write))
|
|
|
|
xlogctl->LogwrtRqst.Write = LogwrtResult.Write;
|
|
|
|
if (XLByteLT(xlogctl->LogwrtRqst.Flush, LogwrtResult.Flush))
|
|
|
|
xlogctl->LogwrtRqst.Flush = LogwrtResult.Flush;
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
2001-12-28 19:16:43 +01:00
|
|
|
}
|
2000-12-29 22:31:21 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
Write->LogwrtResult = LogwrtResult;
|
|
|
|
}
|
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
/*
|
2010-07-30 00:27:27 +02:00
|
|
|
* Record the LSN for an asynchronous transaction commit/abort.
|
|
|
|
* (This should not be called for for synchronous commits.)
|
2007-08-02 00:45:09 +02:00
|
|
|
*/
|
|
|
|
void
|
2010-07-30 00:27:27 +02:00
|
|
|
XLogSetAsyncXactLSN(XLogRecPtr asyncXactLSN)
|
2007-08-02 00:45:09 +02:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2010-07-30 00:27:27 +02:00
|
|
|
if (XLByteLT(xlogctl->asyncXactLSN, asyncXactLSN))
|
|
|
|
xlogctl->asyncXactLSN = asyncXactLSN;
|
2007-08-02 00:45:09 +02:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
|
|
|
* Advance minRecoveryPoint in control file.
|
|
|
|
*
|
|
|
|
* If we crash during recovery, we must reach this point again before the
|
2009-06-11 16:49:15 +02:00
|
|
|
* database is consistent.
|
|
|
|
*
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* If 'force' is true, 'lsn' argument is ignored. Otherwise, minRecoveryPoint
|
2009-06-26 22:29:04 +02:00
|
|
|
* is only updated if it's not already greater than or equal to 'lsn'.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
UpdateMinRecoveryPoint(XLogRecPtr lsn, bool force)
|
|
|
|
{
|
|
|
|
/* Quick check using our local copy of the variable */
|
|
|
|
if (!updateMinRecoveryPoint || (!force && XLByteLE(lsn, minRecoveryPoint)))
|
|
|
|
return;
|
|
|
|
|
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
/* update local copy */
|
|
|
|
minRecoveryPoint = ControlFile->minRecoveryPoint;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An invalid minRecoveryPoint means that we need to recover all the WAL,
|
2009-06-26 22:29:04 +02:00
|
|
|
* i.e., we're doing crash recovery. We never modify the control file's
|
|
|
|
* value in that case, so we can short-circuit future checks here too.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
if (minRecoveryPoint.xlogid == 0 && minRecoveryPoint.xrecoff == 0)
|
|
|
|
updateMinRecoveryPoint = false;
|
|
|
|
else if (force || XLByteLT(minRecoveryPoint, lsn))
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
2009-06-11 16:49:15 +02:00
|
|
|
XLogRecPtr newMinRecoveryPoint;
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid having to update the control file too often, we update it
|
|
|
|
* all the way to the last record being replayed, even though 'lsn'
|
2009-06-26 22:29:04 +02:00
|
|
|
* would suffice for correctness. This also allows the 'force' case
|
|
|
|
* to not need a valid 'lsn' value.
|
|
|
|
*
|
|
|
|
* Another important reason for doing it this way is that the passed
|
2010-02-26 03:01:40 +01:00
|
|
|
* 'lsn' value could be bogus, i.e., past the end of available WAL, if
|
|
|
|
* the caller got it from a corrupted heap page. Accepting such a
|
|
|
|
* value as the min recovery point would prevent us from coming up at
|
|
|
|
* all. Instead, we just log a warning and continue with recovery.
|
2009-06-26 22:29:04 +02:00
|
|
|
* (See also the comments about corrupt LSNs in XLogFlush.)
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
newMinRecoveryPoint = xlogctl->replayEndRecPtr;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
if (!force && XLByteLT(newMinRecoveryPoint, lsn))
|
|
|
|
elog(WARNING,
|
2010-02-26 03:01:40 +01:00
|
|
|
"xlog min recovery request %X/%X is past current point %X/%X",
|
2009-06-26 22:29:04 +02:00
|
|
|
lsn.xlogid, lsn.xrecoff,
|
|
|
|
newMinRecoveryPoint.xlogid, newMinRecoveryPoint.xrecoff);
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* update control file */
|
|
|
|
if (XLByteLT(ControlFile->minRecoveryPoint, newMinRecoveryPoint))
|
|
|
|
{
|
|
|
|
ControlFile->minRecoveryPoint = newMinRecoveryPoint;
|
|
|
|
UpdateControlFile();
|
|
|
|
minRecoveryPoint = newMinRecoveryPoint;
|
|
|
|
|
|
|
|
ereport(DEBUG2,
|
|
|
|
(errmsg("updated min recovery point to %X/%X",
|
|
|
|
minRecoveryPoint.xlogid, minRecoveryPoint.xrecoff)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LWLockRelease(ControlFileLock);
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Ensure that all XLOG data through the given position is flushed to disk.
|
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* NOTE: this differs from XLogWrite mainly in that the WALWriteLock is not
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* already held, and we try to avoid acquiring it if possible.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
XLogFlush(XLogRecPtr record)
|
|
|
|
{
|
|
|
|
XLogRecPtr WriteRqstPtr;
|
|
|
|
XLogwrtRqst WriteRqst;
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
2009-06-26 22:29:04 +02:00
|
|
|
* During REDO, we are reading not writing WAL. Therefore, instead of
|
2010-02-26 03:01:40 +01:00
|
|
|
* trying to flush the WAL, we should update minRecoveryPoint instead. We
|
|
|
|
* test XLogInsertAllowed(), not InRecovery, because we need the bgwriter
|
|
|
|
* to act this way too, and because when the bgwriter tries to write the
|
|
|
|
* end-of-recovery checkpoint, it should indeed flush.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
2009-06-26 22:29:04 +02:00
|
|
|
if (!XLogInsertAllowed())
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
{
|
|
|
|
UpdateMinRecoveryPoint(record, false);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return;
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
/* Quick exit if already known flushed */
|
|
|
|
if (XLByteLE(record, LogwrtResult.Flush))
|
|
|
|
return;
|
|
|
|
|
2004-01-06 18:26:23 +01:00
|
|
|
#ifdef WAL_DEBUG
|
2003-02-21 01:06:22 +01:00
|
|
|
if (XLOG_DEBUG)
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(LOG, "xlog flush request %X/%X; write %X/%X; flush %X/%X",
|
2003-02-21 01:06:22 +01:00
|
|
|
record.xlogid, record.xrecoff,
|
|
|
|
LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
|
|
|
|
LogwrtResult.Flush.xlogid, LogwrtResult.Flush.xrecoff);
|
2004-01-06 18:26:23 +01:00
|
|
|
#endif
|
2003-02-21 01:06:22 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since fsync is usually a horribly expensive operation, we try to
|
2005-10-15 04:49:52 +02:00
|
|
|
* piggyback as much data as we can on each fsync: if we see any more data
|
|
|
|
* entered into the xlog buffer, we'll write and fsync that too, so that
|
|
|
|
* the final value of LogwrtResult.Flush is as large as possible. This
|
|
|
|
* gives us some chance of avoiding another fsync immediately after.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* initialize to given target; may increase below */
|
|
|
|
WriteRqstPtr = record;
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
/* read LogwrtResult and update local state */
|
2001-12-28 19:16:43 +01:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2001-12-28 19:16:43 +01:00
|
|
|
if (XLByteLT(WriteRqstPtr, xlogctl->LogwrtRqst.Write))
|
|
|
|
WriteRqstPtr = xlogctl->LogwrtRqst.Write;
|
|
|
|
LogwrtResult = xlogctl->LogwrtResult;
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
2001-12-28 19:16:43 +01:00
|
|
|
}
|
2001-09-29 06:02:27 +02:00
|
|
|
|
|
|
|
/* done already? */
|
|
|
|
if (!XLByteLE(record, LogwrtResult.Flush))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2001-09-29 06:02:27 +02:00
|
|
|
/* now wait for the write lock */
|
|
|
|
LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
|
|
|
|
LogwrtResult = XLogCtl->Write.LogwrtResult;
|
|
|
|
if (!XLByteLE(record, LogwrtResult.Flush))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2002-10-07 19:04:30 +02:00
|
|
|
/* try to write/flush later additions to XLOG as well */
|
|
|
|
if (LWLockConditionalAcquire(WALInsertLock, LW_EXCLUSIVE))
|
|
|
|
{
|
|
|
|
XLogCtlInsert *Insert = &XLogCtl->Insert;
|
|
|
|
uint32 freespace = INSERT_FREESPACE(Insert);
|
|
|
|
|
2003-08-04 02:43:34 +02:00
|
|
|
if (freespace < SizeOfXLogRecord) /* buffer is full */
|
2002-10-07 19:04:30 +02:00
|
|
|
WriteRqstPtr = XLogCtl->xlblocks[Insert->curridx];
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WriteRqstPtr = XLogCtl->xlblocks[Insert->curridx];
|
|
|
|
WriteRqstPtr.xrecoff -= freespace;
|
|
|
|
}
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
WriteRqst.Write = WriteRqstPtr;
|
|
|
|
WriteRqst.Flush = WriteRqstPtr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WriteRqst.Write = WriteRqstPtr;
|
|
|
|
WriteRqst.Flush = record;
|
|
|
|
}
|
2006-08-06 05:53:44 +02:00
|
|
|
XLogWrite(WriteRqst, false, false);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(WALWriteLock);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
2002-01-14 18:55:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we still haven't flushed to the request point then we have a
|
2005-10-15 04:49:52 +02:00
|
|
|
* problem; most likely, the requested flush point is past end of XLOG.
|
|
|
|
* This has been seen to occur when a disk page has a corrupted LSN.
|
2002-01-14 18:55:57 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Formerly we treated this as a PANIC condition, but that hurts the
|
|
|
|
* system's robustness rather than helping it: we do not want to take down
|
|
|
|
* the whole system due to corruption on one data page. In particular, if
|
|
|
|
* the bad page is encountered again during recovery then we would be
|
2009-06-26 22:29:04 +02:00
|
|
|
* unable to restart the database at all! (This scenario actually
|
2010-02-26 03:01:40 +01:00
|
|
|
* happened in the field several times with 7.1 releases.) As of 8.4, bad
|
|
|
|
* LSNs encountered during recovery are UpdateMinRecoveryPoint's problem;
|
|
|
|
* the only time we can reach here during recovery is while flushing the
|
|
|
|
* end-of-recovery checkpoint record, and we don't expect that to have a
|
|
|
|
* bad LSN.
|
2002-01-14 18:55:57 +01:00
|
|
|
*
|
2010-02-26 03:01:40 +01:00
|
|
|
* Note that for calls from xact.c, the ERROR will be promoted to PANIC
|
|
|
|
* since xact.c calls this routine inside a critical section. However,
|
|
|
|
* calls from bufmgr.c are not within critical sections and so we will not
|
|
|
|
* force a restart for a bad LSN on a data page.
|
2002-01-14 18:55:57 +01:00
|
|
|
*/
|
|
|
|
if (XLByteLT(LogwrtResult.Flush, record))
|
2009-06-26 22:29:04 +02:00
|
|
|
elog(ERROR,
|
2005-10-15 04:49:52 +02:00
|
|
|
"xlog flush request %X/%X is not satisfied --- flushed only to %X/%X",
|
2002-01-14 18:55:57 +01:00
|
|
|
record.xlogid, record.xrecoff,
|
|
|
|
LogwrtResult.Flush.xlogid, LogwrtResult.Flush.xrecoff);
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2007-07-24 06:54:09 +02:00
|
|
|
/*
|
|
|
|
* Flush xlog, but without specifying exactly where to flush to.
|
|
|
|
*
|
|
|
|
* We normally flush only completed blocks; but if there is nothing to do on
|
|
|
|
* that basis, we check for unflushed async commits in the current incomplete
|
|
|
|
* block, and flush through the latest one of those. Thus, if async commits
|
2007-11-15 22:14:46 +01:00
|
|
|
* are not being used, we will flush complete blocks only. We can guarantee
|
2007-07-24 06:54:09 +02:00
|
|
|
* that async commits reach disk after at most three cycles; normally only
|
2007-11-15 22:14:46 +01:00
|
|
|
* one or two. (We allow XLogWrite to write "flexibly", meaning it can stop
|
2007-07-24 06:54:09 +02:00
|
|
|
* at the end of the buffer ring; this makes a difference only with very high
|
|
|
|
* load or long wal_writer_delay, but imposes one extra cycle for the worst
|
|
|
|
* case for async commits.)
|
|
|
|
*
|
|
|
|
* This routine is invoked periodically by the background walwriter process.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
XLogBackgroundFlush(void)
|
|
|
|
{
|
|
|
|
XLogRecPtr WriteRqstPtr;
|
|
|
|
bool flexible = true;
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* XLOG doesn't need flushing during recovery */
|
|
|
|
if (RecoveryInProgress())
|
|
|
|
return;
|
|
|
|
|
2007-07-24 06:54:09 +02:00
|
|
|
/* read LogwrtResult and update local state */
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
LogwrtResult = xlogctl->LogwrtResult;
|
|
|
|
WriteRqstPtr = xlogctl->LogwrtRqst.Write;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* back off to last completed page boundary */
|
|
|
|
WriteRqstPtr.xrecoff -= WriteRqstPtr.xrecoff % XLOG_BLCKSZ;
|
|
|
|
|
|
|
|
/* if we have already flushed that far, consider async commit records */
|
|
|
|
if (XLByteLE(WriteRqstPtr, LogwrtResult.Flush))
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2010-07-30 00:27:27 +02:00
|
|
|
WriteRqstPtr = xlogctl->asyncXactLSN;
|
2007-08-02 00:45:09 +02:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
2007-07-24 06:54:09 +02:00
|
|
|
flexible = false; /* ensure it all gets written */
|
|
|
|
}
|
|
|
|
|
2010-06-09 12:54:45 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* If already known flushed, we're done. Just need to check if we are
|
|
|
|
* holding an open file handle to a logfile that's no longer in use,
|
|
|
|
* preventing the file from being deleted.
|
2010-06-09 12:54:45 +02:00
|
|
|
*/
|
2007-07-24 06:54:09 +02:00
|
|
|
if (XLByteLE(WriteRqstPtr, LogwrtResult.Flush))
|
2010-06-09 12:54:45 +02:00
|
|
|
{
|
2010-07-06 21:19:02 +02:00
|
|
|
if (openLogFile >= 0)
|
|
|
|
{
|
2010-06-09 12:54:45 +02:00
|
|
|
if (!XLByteInPrevSeg(LogwrtResult.Write, openLogId, openLogSeg))
|
|
|
|
{
|
|
|
|
XLogFileClose();
|
|
|
|
}
|
|
|
|
}
|
2007-07-24 06:54:09 +02:00
|
|
|
return;
|
2010-06-09 12:54:45 +02:00
|
|
|
}
|
2007-07-24 06:54:09 +02:00
|
|
|
|
|
|
|
#ifdef WAL_DEBUG
|
|
|
|
if (XLOG_DEBUG)
|
|
|
|
elog(LOG, "xlog bg flush request %X/%X; write %X/%X; flush %X/%X",
|
|
|
|
WriteRqstPtr.xlogid, WriteRqstPtr.xrecoff,
|
|
|
|
LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff,
|
|
|
|
LogwrtResult.Flush.xlogid, LogwrtResult.Flush.xrecoff);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
|
|
|
/* now wait for the write lock */
|
|
|
|
LWLockAcquire(WALWriteLock, LW_EXCLUSIVE);
|
|
|
|
LogwrtResult = XLogCtl->Write.LogwrtResult;
|
|
|
|
if (!XLByteLE(WriteRqstPtr, LogwrtResult.Flush))
|
|
|
|
{
|
|
|
|
XLogwrtRqst WriteRqst;
|
|
|
|
|
|
|
|
WriteRqst.Write = WriteRqstPtr;
|
|
|
|
WriteRqst.Flush = WriteRqstPtr;
|
|
|
|
XLogWrite(WriteRqst, flexible, false);
|
|
|
|
}
|
|
|
|
LWLockRelease(WALWriteLock);
|
|
|
|
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
}
|
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
/*
|
|
|
|
* Test whether XLOG data has been flushed up to (at least) the given position.
|
|
|
|
*
|
|
|
|
* Returns true if a flush is still needed. (It may be that someone else
|
|
|
|
* is already in process of flushing that far, however.)
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
XLogNeedsFlush(XLogRecPtr record)
|
|
|
|
{
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
|
|
|
* During recovery, we don't flush WAL but update minRecoveryPoint
|
|
|
|
* instead. So "needs flush" is taken to mean whether minRecoveryPoint
|
|
|
|
* would need to be updated.
|
|
|
|
*/
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
if (RecoveryInProgress())
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
{
|
|
|
|
/* Quick exit if already known updated */
|
|
|
|
if (XLByteLE(record, minRecoveryPoint) || !updateMinRecoveryPoint)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update local copy of minRecoveryPoint. But if the lock is busy,
|
|
|
|
* just return a conservative guess.
|
|
|
|
*/
|
|
|
|
if (!LWLockConditionalAcquire(ControlFileLock, LW_SHARED))
|
|
|
|
return true;
|
|
|
|
minRecoveryPoint = ControlFile->minRecoveryPoint;
|
|
|
|
LWLockRelease(ControlFileLock);
|
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* An invalid minRecoveryPoint means that we need to recover all the
|
|
|
|
* WAL, i.e., we're doing crash recovery. We never modify the control
|
|
|
|
* file's value in that case, so we can short-circuit future checks
|
|
|
|
* here too.
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
*/
|
|
|
|
if (minRecoveryPoint.xlogid == 0 && minRecoveryPoint.xrecoff == 0)
|
|
|
|
updateMinRecoveryPoint = false;
|
|
|
|
|
|
|
|
/* check again */
|
|
|
|
if (XLByteLE(record, minRecoveryPoint) || !updateMinRecoveryPoint)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
return true;
|
|
|
|
}
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2007-05-30 22:12:03 +02:00
|
|
|
/* Quick exit if already known flushed */
|
|
|
|
if (XLByteLE(record, LogwrtResult.Flush))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* read LogwrtResult and update local state */
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
LogwrtResult = xlogctl->LogwrtResult;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* check again */
|
|
|
|
if (XLByteLE(record, LogwrtResult.Flush))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Create a new XLOG file segment, or open a pre-existing one.
|
|
|
|
*
|
2001-03-17 21:54:13 +01:00
|
|
|
* log, seg: identify segment to be created/opened.
|
|
|
|
*
|
|
|
|
* *use_existent: if TRUE, OK to use a pre-existing file (else, any
|
2001-03-22 05:01:46 +01:00
|
|
|
* pre-existing file will be deleted). On return, TRUE if a pre-existing
|
2001-03-17 21:54:13 +01:00
|
|
|
* file was used.
|
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* use_lock: if TRUE, acquire ControlFileLock while moving file into
|
2001-03-17 21:54:13 +01:00
|
|
|
* place. This should be TRUE except during bootstrap log creation. The
|
2001-09-29 06:02:27 +02:00
|
|
|
* caller must *not* hold the lock at call.
|
2001-03-17 21:54:13 +01:00
|
|
|
*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* Returns FD of opened file.
|
2005-04-16 00:19:48 +02:00
|
|
|
*
|
|
|
|
* Note: errors here are ERROR not PANIC because we might or might not be
|
|
|
|
* inside a critical section (eg, during checkpoint there is no reason to
|
|
|
|
* take down the system on failure). They will promote to PANIC if we are
|
|
|
|
* in a critical section.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2010-01-15 10:19:10 +01:00
|
|
|
int
|
2001-03-17 21:54:13 +01:00
|
|
|
XLogFileInit(uint32 log, uint32 seg,
|
|
|
|
bool *use_existent, bool use_lock)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char path[MAXPGPATH];
|
2001-03-17 21:54:13 +01:00
|
|
|
char tmppath[MAXPGPATH];
|
2007-06-30 21:12:02 +02:00
|
|
|
char *zbuffer;
|
2005-04-15 20:48:10 +02:00
|
|
|
uint32 installed_log;
|
|
|
|
uint32 installed_seg;
|
|
|
|
int max_advance;
|
2000-04-12 19:17:23 +02:00
|
|
|
int fd;
|
2001-02-18 05:39:42 +01:00
|
|
|
int nbytes;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFilePath(path, ThisTimeLineID, log, seg);
|
2000-11-05 23:50:21 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Try to use existent file (checkpoint maker may have created it already)
|
2000-11-05 23:50:21 +01:00
|
|
|
*/
|
2001-03-17 21:54:13 +01:00
|
|
|
if (*use_existent)
|
2000-11-05 23:50:21 +01:00
|
|
|
{
|
2008-05-14 16:02:57 +02:00
|
|
|
fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method),
|
2001-03-16 06:44:33 +01:00
|
|
|
S_IRUSR | S_IWUSR);
|
2000-11-05 23:50:21 +01:00
|
|
|
if (fd < 0)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
|
2003-07-21 22:29:40 +02:00
|
|
|
path, log, seg)));
|
2000-11-05 23:50:21 +01:00
|
|
|
}
|
|
|
|
else
|
2006-01-11 09:43:13 +01:00
|
|
|
return fd;
|
2000-11-05 23:50:21 +01:00
|
|
|
}
|
|
|
|
|
2001-03-17 21:54:13 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Initialize an empty (all zeroes) segment. NOTE: it is possible that
|
|
|
|
* another process is doing the same thing. If so, we will end up
|
|
|
|
* pre-creating an extra log segment. That seems OK, and better than
|
|
|
|
* holding the lock throughout this lengthy process.
|
2001-03-17 21:54:13 +01:00
|
|
|
*/
|
2007-06-30 21:12:02 +02:00
|
|
|
elog(DEBUG2, "creating and filling new WAL file");
|
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
|
2001-03-17 21:54:13 +01:00
|
|
|
|
|
|
|
unlink(tmppath);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
/* do not use get_sync_bit() here --- want to fsync only at end of fill */
|
2001-03-17 21:54:13 +01:00
|
|
|
fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
S_IRUSR | S_IWUSR);
|
1999-09-27 17:48:12 +02:00
|
|
|
if (fd < 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not create file \"%s\": %m", tmppath)));
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2001-02-18 05:39:42 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Zero-fill the file. We have to do this the hard way to ensure that all
|
|
|
|
* the file space has really been allocated --- on platforms that allow
|
|
|
|
* "holes" in files, just seeking to the end doesn't allocate intermediate
|
|
|
|
* space. This way, we know that we have all the space and (after the
|
|
|
|
* fsync below) that all the indirect blocks are down on disk. Therefore,
|
|
|
|
* fdatasync(2) or O_DSYNC will be sufficient to sync future writes to the
|
|
|
|
* log file.
|
2007-06-30 21:12:02 +02:00
|
|
|
*
|
|
|
|
* Note: palloc zbuffer, instead of just using a local char array, to
|
|
|
|
* ensure it is reasonably well-aligned; this may save a few cycles
|
|
|
|
* transferring data to the kernel.
|
2001-02-18 05:39:42 +01:00
|
|
|
*/
|
2007-06-30 21:12:02 +02:00
|
|
|
zbuffer = (char *) palloc0(XLOG_BLCKSZ);
|
|
|
|
for (nbytes = 0; nbytes < XLogSegSize; nbytes += XLOG_BLCKSZ)
|
2001-02-18 05:39:42 +01:00
|
|
|
{
|
2001-06-06 19:07:46 +02:00
|
|
|
errno = 0;
|
2007-06-30 21:12:02 +02:00
|
|
|
if ((int) write(fd, zbuffer, XLOG_BLCKSZ) != (int) XLOG_BLCKSZ)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int save_errno = errno;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we fail to make the file, delete it to release disk space
|
2001-03-22 05:01:46 +01:00
|
|
|
*/
|
2001-03-17 21:54:13 +01:00
|
|
|
unlink(tmppath);
|
2001-06-06 19:07:46 +02:00
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
errno = save_errno ? save_errno : ENOSPC;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not write to file \"%s\": %m", tmppath)));
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
2001-02-18 05:39:42 +01:00
|
|
|
}
|
2007-06-30 21:12:02 +02:00
|
|
|
pfree(zbuffer);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2000-12-08 23:21:33 +01:00
|
|
|
if (pg_fsync(fd) != 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not fsync file \"%s\": %m", tmppath)));
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2004-01-26 23:35:32 +01:00
|
|
|
if (close(fd))
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-01-26 23:35:32 +01:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close file \"%s\": %m", tmppath)));
|
2000-11-27 06:36:12 +01:00
|
|
|
|
2001-02-18 05:39:42 +01:00
|
|
|
/*
|
2001-07-19 04:12:35 +02:00
|
|
|
* Now move the segment into place with its final name.
|
|
|
|
*
|
2001-03-17 21:54:13 +01:00
|
|
|
* If caller didn't want to use a pre-existing file, get rid of any
|
2005-10-15 04:49:52 +02:00
|
|
|
* pre-existing file. Otherwise, cope with possibility that someone else
|
|
|
|
* has created the file while we were filling ours: if so, use ours to
|
|
|
|
* pre-create a future log segment.
|
2001-03-17 21:54:13 +01:00
|
|
|
*/
|
2005-04-15 20:48:10 +02:00
|
|
|
installed_log = log;
|
|
|
|
installed_seg = seg;
|
|
|
|
max_advance = XLOGfileslop;
|
|
|
|
if (!InstallXLogFileSegment(&installed_log, &installed_seg, tmppath,
|
|
|
|
*use_existent, &max_advance,
|
2001-07-19 04:12:35 +02:00
|
|
|
use_lock))
|
|
|
|
{
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
/*
|
|
|
|
* No need for any more future segments, or InstallXLogFileSegment()
|
|
|
|
* failed to rename the file into place. If the rename failed, opening
|
|
|
|
* the file below will fail.
|
|
|
|
*/
|
2001-07-19 04:12:35 +02:00
|
|
|
unlink(tmppath);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set flag to tell caller there was no existent file */
|
|
|
|
*use_existent = false;
|
|
|
|
|
|
|
|
/* Now open original target segment (might not be file I just made) */
|
2008-05-14 16:02:57 +02:00
|
|
|
fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method),
|
2001-07-19 04:12:35 +02:00
|
|
|
S_IRUSR | S_IWUSR);
|
|
|
|
if (fd < 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
|
|
|
|
path, log, seg)));
|
2001-07-19 04:12:35 +02:00
|
|
|
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
elog(DEBUG2, "done creating and filling new WAL file");
|
|
|
|
|
2006-01-11 09:43:13 +01:00
|
|
|
return fd;
|
2001-07-19 04:12:35 +02:00
|
|
|
}
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
|
|
|
* Create a new XLOG file segment by copying a pre-existing one.
|
|
|
|
*
|
|
|
|
* log, seg: identify segment to be created.
|
|
|
|
*
|
|
|
|
* srcTLI, srclog, srcseg: identify segment to be copied (could be from
|
|
|
|
* a different timeline)
|
|
|
|
*
|
|
|
|
* Currently this is only used during recovery, and so there are no locking
|
2004-08-29 07:07:03 +02:00
|
|
|
* considerations. But we should be just as tense as XLogFileInit to avoid
|
2004-07-22 00:31:26 +02:00
|
|
|
* emplacing a bogus file.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
XLogFileCopy(uint32 log, uint32 seg,
|
|
|
|
TimeLineID srcTLI, uint32 srclog, uint32 srcseg)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char tmppath[MAXPGPATH];
|
2006-04-04 01:35:05 +02:00
|
|
|
char buffer[XLOG_BLCKSZ];
|
2004-07-22 00:31:26 +02:00
|
|
|
int srcfd;
|
|
|
|
int fd;
|
|
|
|
int nbytes;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the source file
|
|
|
|
*/
|
|
|
|
XLogFilePath(path, srcTLI, srclog, srcseg);
|
|
|
|
srcfd = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
|
|
|
|
if (srcfd < 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open file \"%s\": %m", path)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copy into a temp file name.
|
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
unlink(tmppath);
|
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
/* do not use get_sync_bit() here --- want to fsync only at end of fill */
|
2004-07-22 00:31:26 +02:00
|
|
|
fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
|
|
|
|
S_IRUSR | S_IWUSR);
|
|
|
|
if (fd < 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not create file \"%s\": %m", tmppath)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do the data copying.
|
|
|
|
*/
|
|
|
|
for (nbytes = 0; nbytes < XLogSegSize; nbytes += sizeof(buffer))
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
if ((int) read(srcfd, buffer, sizeof(buffer)) != (int) sizeof(buffer))
|
|
|
|
{
|
|
|
|
if (errno != 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m", path)));
|
|
|
|
else
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("not enough data in file \"%s\"", path)));
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
errno = 0;
|
|
|
|
if ((int) write(fd, buffer, sizeof(buffer)) != (int) sizeof(buffer))
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we fail to make the file, delete it to release disk space
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
unlink(tmppath);
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
errno = save_errno ? save_errno : ENOSPC;
|
|
|
|
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not write to file \"%s\": %m", tmppath)));
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pg_fsync(fd) != 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not fsync file \"%s\": %m", tmppath)));
|
|
|
|
|
|
|
|
if (close(fd))
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close file \"%s\": %m", tmppath)));
|
|
|
|
|
|
|
|
close(srcfd);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now move the segment into place with its final name.
|
|
|
|
*/
|
2005-04-15 20:48:10 +02:00
|
|
|
if (!InstallXLogFileSegment(&log, &seg, tmppath, false, NULL, false))
|
2005-04-16 00:19:48 +02:00
|
|
|
elog(ERROR, "InstallXLogFileSegment should not have failed");
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
|
2001-07-19 04:12:35 +02:00
|
|
|
/*
|
|
|
|
* Install a new XLOG segment file as a current or future log segment.
|
|
|
|
*
|
|
|
|
* This is used both to install a newly-created segment (which has a temp
|
|
|
|
* filename while it's being created) and to recycle an old segment.
|
|
|
|
*
|
2005-04-15 20:48:10 +02:00
|
|
|
* *log, *seg: identify segment to install as (or first possible target).
|
|
|
|
* When find_free is TRUE, these are modified on return to indicate the
|
|
|
|
* actual installation location or last segment searched.
|
2001-07-19 04:12:35 +02:00
|
|
|
*
|
|
|
|
* tmppath: initial name of file to install. It will be renamed into place.
|
|
|
|
*
|
|
|
|
* find_free: if TRUE, install the new segment at the first empty log/seg
|
|
|
|
* number at or after the passed numbers. If FALSE, install the new segment
|
|
|
|
* exactly where specified, deleting any existing segment file there.
|
|
|
|
*
|
2005-04-15 20:48:10 +02:00
|
|
|
* *max_advance: maximum number of log/seg slots to advance past the starting
|
|
|
|
* point. Fail if no free slot is found in this range. On return, reduced
|
|
|
|
* by the number of slots skipped over. (Irrelevant, and may be NULL,
|
|
|
|
* when find_free is FALSE.)
|
2001-07-19 04:12:35 +02:00
|
|
|
*
|
2001-09-29 06:02:27 +02:00
|
|
|
* use_lock: if TRUE, acquire ControlFileLock while moving file into
|
2001-07-19 04:12:35 +02:00
|
|
|
* place. This should be TRUE except during bootstrap log creation. The
|
2001-09-29 06:02:27 +02:00
|
|
|
* caller must *not* hold the lock at call.
|
2001-07-19 04:12:35 +02:00
|
|
|
*
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
* Returns TRUE if the file was installed successfully. FALSE indicates that
|
|
|
|
* max_advance limit was exceeded, or an error occurred while renaming the
|
|
|
|
* file into place.
|
2001-07-19 04:12:35 +02:00
|
|
|
*/
|
|
|
|
static bool
|
2005-04-15 20:48:10 +02:00
|
|
|
InstallXLogFileSegment(uint32 *log, uint32 *seg, char *tmppath,
|
|
|
|
bool find_free, int *max_advance,
|
2001-07-19 04:12:35 +02:00
|
|
|
bool use_lock)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
2003-01-25 04:06:04 +01:00
|
|
|
struct stat stat_buf;
|
2001-07-19 04:12:35 +02:00
|
|
|
|
2005-04-15 20:48:10 +02:00
|
|
|
XLogFilePath(path, ThisTimeLineID, *log, *seg);
|
2001-07-19 04:12:35 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We want to be sure that only one process does this at a time.
|
|
|
|
*/
|
|
|
|
if (use_lock)
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
2001-03-17 21:54:13 +01:00
|
|
|
|
2001-07-19 04:12:35 +02:00
|
|
|
if (!find_free)
|
|
|
|
{
|
|
|
|
/* Force installation: get rid of any pre-existing segment file */
|
|
|
|
unlink(path);
|
|
|
|
}
|
2001-03-17 21:54:13 +01:00
|
|
|
else
|
|
|
|
{
|
2001-07-19 04:12:35 +02:00
|
|
|
/* Find a free slot to put it in */
|
2003-01-25 04:06:04 +01:00
|
|
|
while (stat(path, &stat_buf) == 0)
|
2001-03-17 21:54:13 +01:00
|
|
|
{
|
2005-04-15 20:48:10 +02:00
|
|
|
if (*max_advance <= 0)
|
2001-07-19 04:12:35 +02:00
|
|
|
{
|
|
|
|
/* Failed to find a free slot within specified range */
|
|
|
|
if (use_lock)
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(ControlFileLock);
|
2001-07-19 04:12:35 +02:00
|
|
|
return false;
|
|
|
|
}
|
2005-04-15 20:48:10 +02:00
|
|
|
NextLogSeg(*log, *seg);
|
|
|
|
(*max_advance)--;
|
|
|
|
XLogFilePath(path, ThisTimeLineID, *log, *seg);
|
2001-03-17 21:54:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prefer link() to rename() here just to be really sure that we don't
|
|
|
|
* overwrite an existing logfile. However, there shouldn't be one, so
|
|
|
|
* rename() is an acceptable substitute except for the truly paranoid.
|
2001-02-18 05:39:42 +01:00
|
|
|
*/
|
2003-04-18 03:03:42 +02:00
|
|
|
#if HAVE_WORKING_LINK
|
2001-07-19 04:12:35 +02:00
|
|
|
if (link(tmppath, path) < 0)
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
{
|
|
|
|
if (use_lock)
|
|
|
|
LWLockRelease(ControlFileLock);
|
|
|
|
ereport(LOG,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not link file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m",
|
2005-04-15 20:48:10 +02:00
|
|
|
tmppath, path, *log, *seg)));
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
2001-03-17 21:54:13 +01:00
|
|
|
unlink(tmppath);
|
2000-12-18 19:45:05 +01:00
|
|
|
#else
|
2001-07-19 04:12:35 +02:00
|
|
|
if (rename(tmppath, path) < 0)
|
2006-11-08 21:12:05 +01:00
|
|
|
{
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
if (use_lock)
|
|
|
|
LWLockRelease(ControlFileLock);
|
|
|
|
ereport(LOG,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not rename file \"%s\" to \"%s\" (initialization of log file %u, segment %u): %m",
|
2005-04-15 20:48:10 +02:00
|
|
|
tmppath, path, *log, *seg)));
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
return false;
|
2006-11-08 21:12:05 +01:00
|
|
|
}
|
2001-02-18 05:39:42 +01:00
|
|
|
#endif
|
2000-11-05 23:50:21 +01:00
|
|
|
|
2001-03-17 21:54:13 +01:00
|
|
|
if (use_lock)
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(ControlFileLock);
|
2001-03-17 21:54:13 +01:00
|
|
|
|
2001-07-19 04:12:35 +02:00
|
|
|
return true;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* Open a pre-existing logfile segment for writing.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2010-01-15 10:19:10 +01:00
|
|
|
int
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFileOpen(uint32 log, uint32 seg)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
char path[MAXPGPATH];
|
|
|
|
int fd;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFilePath(path, ThisTimeLineID, log, seg);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
fd = BasicOpenFile(path, O_RDWR | PG_BINARY | get_sync_bit(sync_method),
|
2001-03-16 06:44:33 +01:00
|
|
|
S_IRUSR | S_IWUSR);
|
1999-09-27 17:48:12 +02:00
|
|
|
if (fd < 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
|
|
|
|
path, log, seg)));
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a logfile segment for reading (during recovery).
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*
|
2010-06-02 11:28:44 +02:00
|
|
|
* If source = XLOG_FROM_ARCHIVE, the segment is retrieved from archive.
|
2010-07-03 22:43:58 +02:00
|
|
|
* Otherwise, it's assumed to be already available in pg_xlog.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
static int
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
XLogFileRead(uint32 log, uint32 seg, int emode, TimeLineID tli,
|
2010-03-30 18:23:57 +02:00
|
|
|
int source, bool notfoundOk)
|
2004-07-22 00:31:26 +02:00
|
|
|
{
|
|
|
|
char xlogfname[MAXFNAMELEN];
|
2007-09-29 20:32:56 +02:00
|
|
|
char activitymsg[MAXFNAMELEN + 16];
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
char path[MAXPGPATH];
|
2004-07-22 00:31:26 +02:00
|
|
|
int fd;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
XLogFileName(xlogfname, tli, log, seg);
|
2007-09-30 19:28:56 +02:00
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
switch (source)
|
2010-02-26 03:01:40 +01:00
|
|
|
{
|
2010-03-30 18:23:57 +02:00
|
|
|
case XLOG_FROM_ARCHIVE:
|
|
|
|
/* Report recovery progress in PS display */
|
|
|
|
snprintf(activitymsg, sizeof(activitymsg), "waiting for %s",
|
|
|
|
xlogfname);
|
|
|
|
set_ps_display(activitymsg, false);
|
2004-07-22 00:31:26 +02:00
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
restoredFromArchive = RestoreArchivedFile(path, xlogfname,
|
|
|
|
"RECOVERYXLOG",
|
|
|
|
XLogSegSize);
|
|
|
|
if (!restoredFromArchive)
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case XLOG_FROM_PG_XLOG:
|
2010-07-03 22:43:58 +02:00
|
|
|
case XLOG_FROM_STREAM:
|
2010-03-30 18:23:57 +02:00
|
|
|
XLogFilePath(path, tli, log, seg);
|
|
|
|
restoredFromArchive = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
elog(ERROR, "invalid XLogFileRead source %d", source);
|
2010-02-26 03:01:40 +01:00
|
|
|
}
|
2007-09-29 20:32:56 +02:00
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
fd = BasicOpenFile(path, O_RDONLY | PG_BINARY, 0);
|
|
|
|
if (fd >= 0)
|
|
|
|
{
|
|
|
|
/* Success! */
|
|
|
|
curFileTLI = tli;
|
2007-09-29 20:32:56 +02:00
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
/* Report recovery progress in PS display */
|
|
|
|
snprintf(activitymsg, sizeof(activitymsg), "recovering %s",
|
|
|
|
xlogfname);
|
|
|
|
set_ps_display(activitymsg, false);
|
|
|
|
|
2010-07-03 22:43:58 +02:00
|
|
|
/* Track source of data in assorted state variables */
|
2010-03-30 18:23:57 +02:00
|
|
|
readSource = source;
|
2010-07-03 22:43:58 +02:00
|
|
|
XLogReceiptSource = source;
|
|
|
|
/* In FROM_STREAM case, caller tracks receipt time, not me */
|
|
|
|
if (source != XLOG_FROM_STREAM)
|
|
|
|
XLogReceiptTime = GetCurrentTimestamp();
|
|
|
|
|
2010-02-26 03:01:40 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
if (errno != ENOENT || !notfoundOk) /* unexpected failure? */
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
|
|
|
|
path, log, seg)));
|
|
|
|
return -1;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open a logfile segment for reading (during recovery).
|
|
|
|
*
|
|
|
|
* This version searches for the segment with any TLI listed in expectedTLIs.
|
|
|
|
*/
|
|
|
|
static int
|
2010-03-30 18:23:57 +02:00
|
|
|
XLogFileReadAnyTLI(uint32 log, uint32 seg, int emode, int sources)
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
ListCell *cell;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Loop looking for a suitable timeline ID: we might need to read any of
|
|
|
|
* the timelines listed in expectedTLIs.
|
|
|
|
*
|
|
|
|
* We expect curFileTLI on entry to be the TLI of the preceding file in
|
|
|
|
* sequence, or 0 if there was no predecessor. We do not allow curFileTLI
|
|
|
|
* to go backwards; this prevents us from picking up the wrong file when a
|
|
|
|
* parent timeline extends to higher segment numbers than the child we
|
|
|
|
* want to read.
|
|
|
|
*/
|
|
|
|
foreach(cell, expectedTLIs)
|
|
|
|
{
|
|
|
|
TimeLineID tli = (TimeLineID) lfirst_int(cell);
|
|
|
|
|
|
|
|
if (tli < curFileTLI)
|
|
|
|
break; /* don't bother looking at too-old TLIs */
|
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
if (sources & XLOG_FROM_ARCHIVE)
|
|
|
|
{
|
|
|
|
fd = XLogFileRead(log, seg, emode, tli, XLOG_FROM_ARCHIVE, true);
|
|
|
|
if (fd != -1)
|
|
|
|
{
|
|
|
|
elog(DEBUG1, "got WAL segment from archive");
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
if (sources & XLOG_FROM_PG_XLOG)
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
{
|
2010-03-30 18:23:57 +02:00
|
|
|
fd = XLogFileRead(log, seg, emode, tli, XLOG_FROM_PG_XLOG, true);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
if (fd != -1)
|
|
|
|
return fd;
|
|
|
|
}
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Couldn't find it. For simplicity, complain about front timeline */
|
|
|
|
XLogFilePath(path, recoveryTargetTLI, log, seg);
|
|
|
|
errno = ENOENT;
|
|
|
|
ereport(emode,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not open file \"%s\" (log file %u, segment %u): %m",
|
|
|
|
path, log, seg)));
|
2004-07-22 00:31:26 +02:00
|
|
|
return -1;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2006-06-15 21:15:00 +02:00
|
|
|
/*
|
|
|
|
* Close the current logfile segment for writing.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
XLogFileClose(void)
|
|
|
|
{
|
|
|
|
Assert(openLogFile >= 0);
|
|
|
|
|
|
|
|
/*
|
2006-06-18 20:30:21 +02:00
|
|
|
* WAL segment files will not be re-read in normal operation, so we advise
|
2009-06-11 16:49:15 +02:00
|
|
|
* the OS to release any cached pages. But do not do so if WAL archiving
|
2010-02-26 03:01:40 +01:00
|
|
|
* or streaming is active, because archiver and walsender process could
|
|
|
|
* use the cache to read the WAL segment.
|
2006-06-15 21:15:00 +02:00
|
|
|
*/
|
2009-01-11 19:02:17 +01:00
|
|
|
#if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_DONTNEED)
|
2010-02-19 11:51:04 +01:00
|
|
|
if (!XLogIsNeeded())
|
2009-01-11 19:02:17 +01:00
|
|
|
(void) posix_fadvise(openLogFile, 0, 0, POSIX_FADV_DONTNEED);
|
2006-06-15 21:15:00 +02:00
|
|
|
#endif
|
2006-06-27 20:59:17 +02:00
|
|
|
|
2006-06-15 21:15:00 +02:00
|
|
|
if (close(openLogFile))
|
|
|
|
ereport(PANIC,
|
2006-10-04 02:30:14 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close log file %u, segment %u: %m",
|
|
|
|
openLogId, openLogSeg)));
|
2006-06-15 21:15:00 +02:00
|
|
|
openLogFile = -1;
|
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* Attempt to retrieve the specified file from off-line archival storage.
|
2004-07-19 04:47:16 +02:00
|
|
|
* If successful, fill "path" with its complete path (note that this will be
|
2004-07-22 00:31:26 +02:00
|
|
|
* a temp file name that doesn't follow the normal naming convention), and
|
|
|
|
* return TRUE.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2004-07-22 00:31:26 +02:00
|
|
|
* If not successful, fill "path" with the name of the normal on-line file
|
|
|
|
* (which may or may not actually exist, but we'll try to use it), and return
|
|
|
|
* FALSE.
|
2004-07-22 22:18:40 +02:00
|
|
|
*
|
|
|
|
* For fixed-size files, the caller may pass the expected size as an
|
|
|
|
* additional crosscheck on successful recovery. If the file size is not
|
|
|
|
* known, set expectedSize = 0.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
static bool
|
|
|
|
RestoreArchivedFile(char *path, const char *xlogfname,
|
2004-07-22 22:18:40 +02:00
|
|
|
const char *recovername, off_t expectedSize)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char xlogpath[MAXPGPATH];
|
|
|
|
char xlogRestoreCmd[MAXPGPATH];
|
2007-09-27 00:36:30 +02:00
|
|
|
char lastRestartPointFname[MAXPGPATH];
|
2004-08-29 07:07:03 +02:00
|
|
|
char *dp;
|
|
|
|
char *endp;
|
2004-07-19 04:47:16 +02:00
|
|
|
const char *sp;
|
2004-08-29 07:07:03 +02:00
|
|
|
int rc;
|
2006-11-21 21:59:53 +01:00
|
|
|
bool signaled;
|
2004-07-19 04:47:16 +02:00
|
|
|
struct stat stat_buf;
|
2007-11-15 22:14:46 +01:00
|
|
|
uint32 restartLog;
|
|
|
|
uint32 restartSeg;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/* In standby mode, restore_command might not be supplied */
|
2010-01-28 20:17:22 +01:00
|
|
|
if (recoveryRestoreCommand == NULL)
|
2010-01-15 10:19:10 +01:00
|
|
|
goto not_available;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* When doing archive recovery, we always prefer an archived log file even
|
|
|
|
* if a file of the same name exists in XLOGDIR. The reason is that the
|
|
|
|
* file in XLOGDIR could be an old, un-filled or partly-filled version
|
|
|
|
* that was copied and restored as part of backing up $PGDATA.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* We could try to optimize this slightly by checking the local copy
|
2005-10-15 04:49:52 +02:00
|
|
|
* lastchange timestamp against the archived copy, but we have no API to
|
|
|
|
* do this, nor can we guarantee that the lastchange timestamp was
|
|
|
|
* preserved correctly when we copied to archive. Our aim is robustness,
|
|
|
|
* so we elect not to do this.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* If we cannot obtain the log file from the archive, however, we will try
|
|
|
|
* to use the XLOGDIR file if it exists. This is so that we can make use
|
|
|
|
* of log segments that weren't yet transferred to the archive.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Notice that we don't actually overwrite any files when we copy back
|
|
|
|
* from archive because the recoveryRestoreCommand may inadvertently
|
|
|
|
* restore inappropriate xlogs, or they may be corrupt, so we may wish to
|
|
|
|
* fallback to the segments remaining in current XLOGDIR later. The
|
2005-10-15 04:49:52 +02:00
|
|
|
* copy-from-archive filename is always the same, ensuring that we don't
|
|
|
|
* run out of disk space on long recoveries.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(xlogpath, MAXPGPATH, XLOGDIR "/%s", recovername);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* Make sure there is no existing file named recovername.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
if (stat(xlogpath, &stat_buf) != 0)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
2004-10-12 23:54:45 +02:00
|
|
|
errmsg("could not stat file \"%s\": %m",
|
2004-07-19 04:47:16 +02:00
|
|
|
xlogpath)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (unlink(xlogpath) != 0)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
2004-08-01 19:45:43 +02:00
|
|
|
errmsg("could not remove file \"%s\": %m",
|
2004-07-19 04:47:16 +02:00
|
|
|
xlogpath)));
|
|
|
|
}
|
|
|
|
|
2008-05-09 16:27:47 +02:00
|
|
|
/*
|
|
|
|
* Calculate the archive file cutoff point for use during log shipping
|
2009-06-11 16:49:15 +02:00
|
|
|
* replication. All files earlier than this point can be deleted from the
|
|
|
|
* archive, though there is no requirement to do so.
|
2008-05-09 16:27:47 +02:00
|
|
|
*
|
|
|
|
* We initialise this with the filename of an InvalidXLogRecPtr, which
|
2009-06-11 16:49:15 +02:00
|
|
|
* will prevent the deletion of any WAL files from the archive because of
|
|
|
|
* the alphabetic sorting property of WAL filenames.
|
2008-05-09 16:27:47 +02:00
|
|
|
*
|
|
|
|
* Once we have successfully located the redo pointer of the checkpoint
|
|
|
|
* from which we start recovery we never request a file prior to the redo
|
2009-06-11 16:49:15 +02:00
|
|
|
* pointer of the last restartpoint. When redo begins we know that we have
|
|
|
|
* successfully located it, so there is no need for additional status
|
|
|
|
* flags to signify the point when we can begin deleting WAL files from
|
|
|
|
* the archive.
|
2008-05-09 16:27:47 +02:00
|
|
|
*/
|
|
|
|
if (InRedo)
|
|
|
|
{
|
|
|
|
XLByteToSeg(ControlFile->checkPointCopy.redo,
|
|
|
|
restartLog, restartSeg);
|
|
|
|
XLogFileName(lastRestartPointFname,
|
|
|
|
ControlFile->checkPointCopy.ThisTimeLineID,
|
|
|
|
restartLog, restartSeg);
|
|
|
|
/* we shouldn't need anything earlier than last restart point */
|
2008-05-09 17:27:17 +02:00
|
|
|
Assert(strcmp(lastRestartPointFname, xlogfname) <= 0);
|
2008-05-09 16:27:47 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
XLogFileName(lastRestartPointFname, 0, 0, 0);
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* construct the command to be executed
|
|
|
|
*/
|
|
|
|
dp = xlogRestoreCmd;
|
|
|
|
endp = xlogRestoreCmd + MAXPGPATH - 1;
|
|
|
|
*endp = '\0';
|
|
|
|
|
|
|
|
for (sp = recoveryRestoreCommand; *sp; sp++)
|
|
|
|
{
|
|
|
|
if (*sp == '%')
|
|
|
|
{
|
|
|
|
switch (sp[1])
|
|
|
|
{
|
|
|
|
case 'p':
|
2006-11-10 23:32:20 +01:00
|
|
|
/* %p: relative path of target file */
|
2004-07-19 04:47:16 +02:00
|
|
|
sp++;
|
2004-08-29 07:07:03 +02:00
|
|
|
StrNCpy(dp, xlogpath, endp - dp);
|
2004-08-12 20:32:52 +02:00
|
|
|
make_native_path(dp);
|
2004-07-19 04:47:16 +02:00
|
|
|
dp += strlen(dp);
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
/* %f: filename of desired file */
|
|
|
|
sp++;
|
2004-08-29 07:07:03 +02:00
|
|
|
StrNCpy(dp, xlogfname, endp - dp);
|
2004-07-19 04:47:16 +02:00
|
|
|
dp += strlen(dp);
|
|
|
|
break;
|
2007-09-27 00:36:30 +02:00
|
|
|
case 'r':
|
|
|
|
/* %r: filename of last restartpoint */
|
|
|
|
sp++;
|
|
|
|
StrNCpy(dp, lastRestartPointFname, endp - dp);
|
|
|
|
dp += strlen(dp);
|
|
|
|
break;
|
2004-07-19 04:47:16 +02:00
|
|
|
case '%':
|
|
|
|
/* convert %% to a single % */
|
|
|
|
sp++;
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* otherwise treat the % as not special */
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
ereport(DEBUG3,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errmsg_internal("executing restore command \"%s\"",
|
2004-07-19 04:47:16 +02:00
|
|
|
xlogRestoreCmd)));
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
|
|
|
* Set in_restore_command to tell the signal handler that we should exit
|
2009-06-26 22:29:04 +02:00
|
|
|
* right away on SIGTERM. We know that we're at a safe point to do that.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* Check if we had already received the signal, so that we don't miss a
|
|
|
|
* shutdown request received just before this.
|
|
|
|
*/
|
|
|
|
in_restore_command = true;
|
|
|
|
if (shutdown_requested)
|
2009-02-23 10:28:50 +01:00
|
|
|
proc_exit(1);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2005-07-04 06:51:52 +02:00
|
|
|
* Copy xlog from archival storage to XLOGDIR
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
rc = system(xlogRestoreCmd);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
in_restore_command = false;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
if (rc == 0)
|
|
|
|
{
|
2004-07-22 22:18:40 +02:00
|
|
|
/*
|
|
|
|
* command apparently succeeded, but let's make sure the file is
|
|
|
|
* really there now and has the correct size.
|
|
|
|
*/
|
|
|
|
if (stat(xlogpath, &stat_buf) == 0)
|
|
|
|
{
|
|
|
|
if (expectedSize > 0 && stat_buf.st_size != expectedSize)
|
2010-02-12 08:36:44 +01:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int elevel;
|
2010-02-12 08:36:44 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we find a partial file in standby mode, we assume it's
|
|
|
|
* because it's just being copied to the archive, and keep
|
|
|
|
* trying.
|
|
|
|
*
|
|
|
|
* Otherwise treat a wrong-sized file as FATAL to ensure the
|
2010-02-26 03:01:40 +01:00
|
|
|
* DBA would notice it, but is that too strong? We could try
|
2010-02-12 08:36:44 +01:00
|
|
|
* to plow ahead with a local copy of the file ... but the
|
|
|
|
* problem is that there probably isn't one, and we'd
|
2010-02-26 03:01:40 +01:00
|
|
|
* incorrectly conclude we've reached the end of WAL and we're
|
|
|
|
* done recovering ...
|
2010-02-12 08:36:44 +01:00
|
|
|
*/
|
|
|
|
if (StandbyMode && stat_buf.st_size < expectedSize)
|
|
|
|
elevel = DEBUG1;
|
|
|
|
else
|
|
|
|
elevel = FATAL;
|
|
|
|
ereport(elevel,
|
2004-07-22 22:18:40 +02:00
|
|
|
(errmsg("archive file \"%s\" has wrong size: %lu instead of %lu",
|
|
|
|
xlogfname,
|
|
|
|
(unsigned long) stat_buf.st_size,
|
|
|
|
(unsigned long) expectedSize)));
|
2010-02-12 08:36:44 +01:00
|
|
|
return false;
|
|
|
|
}
|
2004-07-22 22:18:40 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("restored log file \"%s\" from archive",
|
|
|
|
xlogfname)));
|
|
|
|
strcpy(path, xlogpath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* stat failed */
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
2004-10-12 23:54:45 +02:00
|
|
|
errmsg("could not stat file \"%s\": %m",
|
2004-07-22 22:18:40 +02:00
|
|
|
xlogpath)));
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-11-21 21:59:53 +01:00
|
|
|
* Remember, we rollforward UNTIL the restore fails so failure here is
|
2004-08-29 07:07:03 +02:00
|
|
|
* just part of the process... that makes it difficult to determine
|
2005-10-15 04:49:52 +02:00
|
|
|
* whether the restore failed because there isn't an archive to restore,
|
|
|
|
* or because the administrator has specified the restore program
|
|
|
|
* incorrectly. We have to assume the former.
|
2006-11-21 21:59:53 +01:00
|
|
|
*
|
|
|
|
* However, if the failure was due to any sort of signal, it's best to
|
2007-11-15 22:14:46 +01:00
|
|
|
* punt and abort recovery. (If we "return false" here, upper levels will
|
|
|
|
* assume that recovery is complete and start up the database!) It's
|
|
|
|
* essential to abort on child SIGINT and SIGQUIT, because per spec
|
2006-11-21 21:59:53 +01:00
|
|
|
* system() ignores SIGINT and SIGQUIT while waiting; if we see one of
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* those it's a good bet we should have gotten it too.
|
|
|
|
*
|
|
|
|
* On SIGTERM, assume we have received a fast shutdown request, and exit
|
|
|
|
* cleanly. It's pure chance whether we receive the SIGTERM first, or the
|
|
|
|
* child process. If we receive it first, the signal handler will call
|
2009-06-11 16:49:15 +02:00
|
|
|
* proc_exit, otherwise we do it here. If we or the child process received
|
|
|
|
* SIGTERM for any other reason than a fast shutdown request, postmaster
|
|
|
|
* will perform an immediate shutdown when it sees us exiting
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* unexpectedly.
|
2006-11-21 21:59:53 +01:00
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Per the Single Unix Spec, shells report exit status > 128 when a called
|
|
|
|
* command died on a signal. Also, 126 and 127 are used to report
|
|
|
|
* problems such as an unfindable command; treat those as fatal errors
|
|
|
|
* too.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2009-06-26 22:29:04 +02:00
|
|
|
if (WIFSIGNALED(rc) && WTERMSIG(rc) == SIGTERM)
|
2009-02-23 10:28:50 +01:00
|
|
|
proc_exit(1);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2006-11-21 21:59:53 +01:00
|
|
|
signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
|
|
|
|
|
|
|
|
ereport(signaled ? FATAL : DEBUG2,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("could not restore file \"%s\" from archive: return code %d",
|
|
|
|
xlogfname, rc)));
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
not_available:
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* if an archived file is not available, there might still be a version of
|
|
|
|
* this file in XLOGDIR, so return that as the filename to open.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* In many recovery scenarios we expect this to fail also, but if so that
|
|
|
|
* just means we've reached the end of WAL.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlogfname);
|
2004-07-22 00:31:26 +02:00
|
|
|
return false;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
2009-05-14 22:31:09 +02:00
|
|
|
/*
|
2010-03-18 10:17:18 +01:00
|
|
|
* Attempt to execute an external shell command during recovery.
|
|
|
|
*
|
|
|
|
* 'command' is the shell command to be executed, 'commandName' is a
|
|
|
|
* human-readable name describing the command emitted in the logs. If
|
|
|
|
* 'failonSignal' is true and the command is killed by a signal, a FATAL
|
|
|
|
* error is thrown. Otherwise a WARNING is emitted.
|
|
|
|
*
|
2010-06-10 10:13:50 +02:00
|
|
|
* This is currently used for restore_end_command and archive_cleanup_command.
|
2009-05-14 22:31:09 +02:00
|
|
|
*/
|
|
|
|
static void
|
2010-03-18 10:17:18 +01:00
|
|
|
ExecuteRecoveryCommand(char *command, char *commandName, bool failOnSignal)
|
2009-05-14 22:31:09 +02:00
|
|
|
{
|
2010-03-18 10:17:18 +01:00
|
|
|
char xlogRecoveryCmd[MAXPGPATH];
|
2009-05-14 22:31:09 +02:00
|
|
|
char lastRestartPointFname[MAXPGPATH];
|
|
|
|
char *dp;
|
|
|
|
char *endp;
|
|
|
|
const char *sp;
|
|
|
|
int rc;
|
|
|
|
bool signaled;
|
|
|
|
uint32 restartLog;
|
|
|
|
uint32 restartSeg;
|
|
|
|
|
2010-03-18 10:17:18 +01:00
|
|
|
Assert(command && commandName);
|
2009-05-14 22:31:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Calculate the archive file cutoff point for use during log shipping
|
2009-06-11 16:49:15 +02:00
|
|
|
* replication. All files earlier than this point can be deleted from the
|
|
|
|
* archive, though there is no requirement to do so.
|
2009-05-14 22:31:09 +02:00
|
|
|
*/
|
2010-03-18 10:17:18 +01:00
|
|
|
LWLockAcquire(ControlFileLock, LW_SHARED);
|
|
|
|
XLByteToSeg(ControlFile->checkPointCopy.redo,
|
|
|
|
restartLog, restartSeg);
|
|
|
|
XLogFileName(lastRestartPointFname,
|
|
|
|
ControlFile->checkPointCopy.ThisTimeLineID,
|
|
|
|
restartLog, restartSeg);
|
|
|
|
LWLockRelease(ControlFileLock);
|
2009-05-14 22:31:09 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* construct the command to be executed
|
|
|
|
*/
|
2010-03-18 10:17:18 +01:00
|
|
|
dp = xlogRecoveryCmd;
|
|
|
|
endp = xlogRecoveryCmd + MAXPGPATH - 1;
|
2009-05-14 22:31:09 +02:00
|
|
|
*endp = '\0';
|
|
|
|
|
2010-03-18 10:17:18 +01:00
|
|
|
for (sp = command; *sp; sp++)
|
2009-05-14 22:31:09 +02:00
|
|
|
{
|
|
|
|
if (*sp == '%')
|
|
|
|
{
|
|
|
|
switch (sp[1])
|
|
|
|
{
|
|
|
|
case 'r':
|
|
|
|
/* %r: filename of last restartpoint */
|
|
|
|
sp++;
|
|
|
|
StrNCpy(dp, lastRestartPointFname, endp - dp);
|
|
|
|
dp += strlen(dp);
|
|
|
|
break;
|
|
|
|
case '%':
|
|
|
|
/* convert %% to a single % */
|
|
|
|
sp++;
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* otherwise treat the % as not special */
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dp < endp)
|
|
|
|
*dp++ = *sp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*dp = '\0';
|
|
|
|
|
|
|
|
ereport(DEBUG3,
|
2010-03-18 10:17:18 +01:00
|
|
|
(errmsg_internal("executing %s \"%s\"", commandName, command)));
|
2009-05-14 22:31:09 +02:00
|
|
|
|
|
|
|
/*
|
2009-05-14 23:28:35 +02:00
|
|
|
* execute the constructed command
|
2009-05-14 22:31:09 +02:00
|
|
|
*/
|
2010-03-18 10:17:18 +01:00
|
|
|
rc = system(xlogRecoveryCmd);
|
2009-05-14 22:31:09 +02:00
|
|
|
if (rc != 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the failure was due to any sort of signal, it's best to punt and
|
2009-06-11 16:49:15 +02:00
|
|
|
* abort recovery. See also detailed comments on signals in
|
2009-05-14 22:31:09 +02:00
|
|
|
* RestoreArchivedFile().
|
|
|
|
*/
|
|
|
|
signaled = WIFSIGNALED(rc) || WEXITSTATUS(rc) > 125;
|
|
|
|
|
2010-03-18 10:17:18 +01:00
|
|
|
/*
|
|
|
|
* translator: First %s represents a recovery.conf parameter name like
|
|
|
|
* "recovery_end_command", and the 2nd is the value of that parameter.
|
|
|
|
*/
|
|
|
|
ereport((signaled && failOnSignal) ? FATAL : WARNING,
|
|
|
|
(errmsg("%s \"%s\": return code %d", commandName,
|
|
|
|
command, rc)));
|
2009-05-14 22:31:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-11-05 23:50:21 +01:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* Preallocate log files beyond the specified log endpoint.
|
|
|
|
*
|
|
|
|
* XXX this is currently extremely conservative, since it forces only one
|
|
|
|
* future log segment to exist, and even that only if we are 75% done with
|
|
|
|
* the current one. This is only appropriate for very low-WAL-volume systems.
|
|
|
|
* High-volume systems will be OK once they've built up a sufficient set of
|
|
|
|
* recycled log segments, but the startup transient is likely to include
|
|
|
|
* a lot of segment creations by foreground processes, which is not so good.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2007-06-30 21:12:02 +02:00
|
|
|
static void
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
PreallocXlogFiles(XLogRecPtr endptr)
|
|
|
|
{
|
|
|
|
uint32 _logId;
|
|
|
|
uint32 _logSeg;
|
|
|
|
int lf;
|
2001-03-17 21:54:13 +01:00
|
|
|
bool use_existent;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
XLByteToPrevSeg(endptr, _logId, _logSeg);
|
2002-08-30 18:50:50 +02:00
|
|
|
if ((endptr.xrecoff - 1) % XLogSegSize >=
|
2002-09-04 22:31:48 +02:00
|
|
|
(uint32) (0.75 * XLogSegSize))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
|
|
|
NextLogSeg(_logId, _logSeg);
|
2001-03-17 21:54:13 +01:00
|
|
|
use_existent = true;
|
|
|
|
lf = XLogFileInit(_logId, _logSeg, &use_existent, true);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
close(lf);
|
2004-10-29 02:16:08 +02:00
|
|
|
if (!use_existent)
|
2007-06-30 21:12:02 +02:00
|
|
|
CheckpointStats.ckpt_segs_added++;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-04-12 11:52:29 +02:00
|
|
|
/*
|
|
|
|
* Get the log/seg of the latest removed or recycled WAL segment.
|
|
|
|
* Returns 0 if no WAL segments have been removed since startup.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
XLogGetLastRemoved(uint32 *log, uint32 *seg)
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
*log = xlogctl->lastRemovedLog;
|
|
|
|
*seg = xlogctl->lastRemovedSeg;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
2010-04-12 12:40:43 +02:00
|
|
|
/*
|
|
|
|
* Update the last removed log/seg pointer in shared memory, to reflect
|
|
|
|
* that the given XLOG file has been removed.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
UpdateLastRemovedPtr(char *filename)
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
uint32 tli,
|
|
|
|
log,
|
|
|
|
seg;
|
|
|
|
|
|
|
|
XLogFromFileName(filename, &tli, &log, &seg);
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
if (log > xlogctl->lastRemovedLog ||
|
|
|
|
(log == xlogctl->lastRemovedLog && seg > xlogctl->lastRemovedSeg))
|
|
|
|
{
|
|
|
|
xlogctl->lastRemovedLog = log;
|
|
|
|
xlogctl->lastRemovedSeg = seg;
|
|
|
|
}
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* Recycle or remove all log files older or equal to passed log/seg#
|
2001-07-19 04:12:35 +02:00
|
|
|
*
|
|
|
|
* endptr is current (or recent) end of xlog; this is used to determine
|
|
|
|
* whether we want to recycle rather than delete no-longer-wanted log files.
|
2000-11-05 23:50:21 +01:00
|
|
|
*/
|
|
|
|
static void
|
2007-06-30 21:12:02 +02:00
|
|
|
RemoveOldXlogFiles(uint32 log, uint32 seg, XLogRecPtr endptr)
|
2000-11-05 23:50:21 +01:00
|
|
|
{
|
2001-07-19 04:12:35 +02:00
|
|
|
uint32 endlogId;
|
|
|
|
uint32 endlogSeg;
|
2005-04-15 20:48:10 +02:00
|
|
|
int max_advance;
|
2001-03-22 05:01:46 +01:00
|
|
|
DIR *xldir;
|
|
|
|
struct dirent *xlde;
|
2004-07-19 04:47:16 +02:00
|
|
|
char lastoff[MAXFNAMELEN];
|
2001-03-22 05:01:46 +01:00
|
|
|
char path[MAXPGPATH];
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-10 11:42:10 +02:00
|
|
|
#ifdef WIN32
|
|
|
|
char newpath[MAXPGPATH];
|
|
|
|
#endif
|
2009-06-02 08:18:06 +02:00
|
|
|
struct stat statbuf;
|
2010-04-12 11:52:29 +02:00
|
|
|
|
2005-04-15 20:48:10 +02:00
|
|
|
/*
|
|
|
|
* Initialize info about where to try to recycle to. We allow recycling
|
|
|
|
* segments up to XLOGfileslop segments beyond the current XLOG location.
|
|
|
|
*/
|
2001-07-19 04:12:35 +02:00
|
|
|
XLByteToPrevSeg(endptr, endlogId, endlogSeg);
|
2005-04-15 20:48:10 +02:00
|
|
|
max_advance = XLOGfileslop;
|
2000-11-05 23:50:21 +01:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
xldir = AllocateDir(XLOGDIR);
|
2000-11-05 23:50:21 +01:00
|
|
|
if (xldir == NULL)
|
2004-02-24 00:03:10 +01:00
|
|
|
ereport(ERROR,
|
2003-07-21 22:29:40 +02:00
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not open transaction log directory \"%s\": %m",
|
|
|
|
XLOGDIR)));
|
2000-11-05 23:50:21 +01:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFileName(lastoff, ThisTimeLineID, log, seg);
|
2000-11-05 23:50:21 +01:00
|
|
|
|
2010-08-30 17:37:41 +02:00
|
|
|
elog(DEBUG2, "attempting to remove WAL segments older than log file %s",
|
|
|
|
lastoff);
|
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
|
2000-11-05 23:50:21 +01:00
|
|
|
{
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* We ignore the timeline part of the XLOG segment identifiers in
|
2005-10-15 04:49:52 +02:00
|
|
|
* deciding whether a segment is still needed. This ensures that we
|
|
|
|
* won't prematurely remove a segment from a parent timeline. We could
|
|
|
|
* probably be a little more proactive about removing segments of
|
|
|
|
* non-parent timelines, but that would be a whole lot more
|
|
|
|
* complicated.
|
2004-07-22 00:31:26 +02:00
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We use the alphanumeric sorting property of the filenames to decide
|
|
|
|
* which ones are earlier than the lastoff segment.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
if (strlen(xlde->d_name) == 24 &&
|
|
|
|
strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
|
|
|
|
strcmp(xlde->d_name + 8, lastoff + 8) <= 0)
|
2000-11-05 23:50:21 +01:00
|
|
|
{
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Normally we don't delete old XLOG files during recovery to
|
|
|
|
* avoid accidentally deleting a file that looks stale due to a
|
|
|
|
* bug or hardware issue, but in fact contains important data.
|
|
|
|
* During streaming recovery, however, we will eventually fill the
|
|
|
|
* disk if we never clean up, so we have to. That's not an issue
|
|
|
|
* with file-based archive recovery because in that case we
|
|
|
|
* restore one XLOG file at a time, on-demand, and with a
|
|
|
|
* different filename that can't be confused with regular XLOG
|
|
|
|
* files.
|
|
|
|
*/
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
if (WalRcvInProgress() || XLogArchiveCheckDone(xlde->d_name))
|
2001-07-19 04:12:35 +02:00
|
|
|
{
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlde->d_name);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2010-04-12 12:40:43 +02:00
|
|
|
/* Update the last removed location in shared memory first */
|
|
|
|
UpdateLastRemovedPtr(xlde->d_name);
|
|
|
|
|
2001-07-19 04:12:35 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Before deleting the file, see if it can be recycled as a
|
2009-06-02 08:18:06 +02:00
|
|
|
* future log segment. Only recycle normal files, pg_standby
|
|
|
|
* for example can create symbolic links pointing to a
|
|
|
|
* separate archive directory.
|
2001-07-19 04:12:35 +02:00
|
|
|
*/
|
2009-06-02 08:18:06 +02:00
|
|
|
if (lstat(path, &statbuf) == 0 && S_ISREG(statbuf.st_mode) &&
|
|
|
|
InstallXLogFileSegment(&endlogId, &endlogSeg, path,
|
|
|
|
true, &max_advance, true))
|
2001-07-19 04:12:35 +02:00
|
|
|
{
|
2004-10-29 02:16:08 +02:00
|
|
|
ereport(DEBUG2,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("recycled transaction log file \"%s\"",
|
|
|
|
xlde->d_name)));
|
2007-06-30 21:12:02 +02:00
|
|
|
CheckpointStats.ckpt_segs_recycled++;
|
2005-04-16 00:19:48 +02:00
|
|
|
/* Needn't recheck that slot on future iterations */
|
|
|
|
if (max_advance > 0)
|
|
|
|
{
|
|
|
|
NextLogSeg(endlogId, endlogSeg);
|
|
|
|
max_advance--;
|
|
|
|
}
|
2001-07-19 04:12:35 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No need for any more future segments... */
|
2010-02-26 03:01:40 +01:00
|
|
|
int rc;
|
2009-09-10 11:42:10 +02:00
|
|
|
|
2004-10-29 02:16:08 +02:00
|
|
|
ereport(DEBUG2,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("removing transaction log file \"%s\"",
|
|
|
|
xlde->d_name)));
|
2009-09-10 11:42:10 +02:00
|
|
|
|
|
|
|
#ifdef WIN32
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2009-09-10 11:42:10 +02:00
|
|
|
/*
|
|
|
|
* On Windows, if another process (e.g another backend)
|
|
|
|
* holds the file open in FILE_SHARE_DELETE mode, unlink
|
|
|
|
* will succeed, but the file will still show up in
|
2010-02-26 03:01:40 +01:00
|
|
|
* directory listing until the last handle is closed. To
|
|
|
|
* avoid confusing the lingering deleted file for a live
|
|
|
|
* WAL file that needs to be archived, rename it before
|
|
|
|
* deleting it.
|
2009-09-10 11:42:10 +02:00
|
|
|
*
|
|
|
|
* If another process holds the file open without
|
|
|
|
* FILE_SHARE_DELETE flag, rename will fail. We'll try
|
|
|
|
* again at the next checkpoint.
|
|
|
|
*/
|
|
|
|
snprintf(newpath, MAXPGPATH, "%s.deleted", path);
|
|
|
|
if (rename(path, newpath) != 0)
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
{
|
|
|
|
ereport(LOG,
|
2009-09-10 11:42:10 +02:00
|
|
|
(errcode_for_file_access(),
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
errmsg("could not rename old transaction log file \"%s\": %m",
|
2009-09-10 11:42:10 +02:00
|
|
|
path)));
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
continue;
|
|
|
|
}
|
2009-09-10 11:42:10 +02:00
|
|
|
rc = unlink(newpath);
|
|
|
|
#else
|
|
|
|
rc = unlink(path);
|
|
|
|
#endif
|
|
|
|
if (rc != 0)
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
{
|
|
|
|
ereport(LOG,
|
2009-09-10 11:42:10 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not remove old transaction log file \"%s\": %m",
|
|
|
|
path)));
|
Don't error out if recycling or removing an old WAL segment fails at the end
of checkpoint. Although the checkpoint has been written to WAL at that point
already, so that all data is safe, and we'll retry removing the WAL segment at
the next checkpoint, if such a failure persists we won't be able to remove any
other old WAL segments either and will eventually run out of disk space. It's
better to treat the failure as non-fatal, and move on to clean any other WAL
segment and continue with any other end-of-checkpoint cleanup.
We don't normally expect any such failures, but on Windows it can happen with
some anti-virus or backup software that lock files without FILE_SHARE_DELETE
flag.
Also, the loop in pgrename() to retry when the file is locked was broken. If a
file is locked on Windows, you get ERROR_SHARE_VIOLATION, not
ERROR_ACCESS_DENIED, at least on modern versions. Fix that, although I left
the check for ERROR_ACCESS_DENIED in there as well (presumably it was correct
in some environment), and added ERROR_LOCK_VIOLATION to be consistent with
similar checks in pgwin32_open(). Reduce the timeout on the loop from 30s to
10s, on the grounds that since it's been broken, we've effectively had a
timeout of 0s and no-one has complained, so a smaller timeout is actually
closer to the old behavior. A longer timeout would mean that if recycling a
WAL file fails because it's locked for some reason, InstallXLogFileSegment()
will hold ControlFileLock for longer, potentially blocking other backends, so
a long timeout isn't totally harmless.
While we're at it, set errno correctly in pgrename().
Backpatch to 8.2, which is the oldest version supported on Windows. The xlog.c
changes would make sense on other platforms and thus on older versions as
well, but since there's no such locking issues on other platforms, it's not
worth it.
2009-09-13 20:32:08 +02:00
|
|
|
continue;
|
|
|
|
}
|
2007-06-30 21:12:02 +02:00
|
|
|
CheckpointStats.ckpt_segs_removed++;
|
2001-07-19 04:12:35 +02:00
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
XLogArchiveCleanup(xlde->d_name);
|
2001-07-19 04:12:35 +02:00
|
|
|
}
|
2000-11-05 23:50:21 +01:00
|
|
|
}
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-02-24 00:03:10 +01:00
|
|
|
FreeDir(xldir);
|
2000-11-05 23:50:21 +01:00
|
|
|
}
|
|
|
|
|
2008-11-09 18:51:15 +01:00
|
|
|
/*
|
|
|
|
* Verify whether pg_xlog and pg_xlog/archive_status exist.
|
|
|
|
* If the latter does not exist, recreate it.
|
|
|
|
*
|
|
|
|
* It is not the goal of this function to verify the contents of these
|
|
|
|
* directories, but to help in cases where someone has performed a cluster
|
|
|
|
* copy for PITR purposes but omitted pg_xlog from the copy.
|
|
|
|
*
|
|
|
|
* We could also recreate pg_xlog if it doesn't exist, but a deliberate
|
|
|
|
* policy decision was made not to. It is fairly common for pg_xlog to be
|
|
|
|
* a symlink, and if that was the DBA's intent then automatically making a
|
|
|
|
* plain directory would result in degraded performance with no notice.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
ValidateXLOGDirectoryStructure(void)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
2009-06-11 16:49:15 +02:00
|
|
|
struct stat stat_buf;
|
2008-11-09 18:51:15 +01:00
|
|
|
|
|
|
|
/* Check for pg_xlog; if it doesn't exist, error out */
|
|
|
|
if (stat(XLOGDIR, &stat_buf) != 0 ||
|
|
|
|
!S_ISDIR(stat_buf.st_mode))
|
2009-06-11 16:49:15 +02:00
|
|
|
ereport(FATAL,
|
2008-11-09 18:51:15 +01:00
|
|
|
(errmsg("required WAL directory \"%s\" does not exist",
|
|
|
|
XLOGDIR)));
|
|
|
|
|
|
|
|
/* Check for archive_status */
|
|
|
|
snprintf(path, MAXPGPATH, XLOGDIR "/archive_status");
|
|
|
|
if (stat(path, &stat_buf) == 0)
|
|
|
|
{
|
|
|
|
/* Check for weird cases where it exists but isn't a directory */
|
|
|
|
if (!S_ISDIR(stat_buf.st_mode))
|
2009-06-11 16:49:15 +02:00
|
|
|
ereport(FATAL,
|
2008-11-09 18:51:15 +01:00
|
|
|
(errmsg("required WAL directory \"%s\" does not exist",
|
|
|
|
path)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("creating missing WAL directory \"%s\"", path)));
|
|
|
|
if (mkdir(path, 0700) < 0)
|
2009-06-11 16:49:15 +02:00
|
|
|
ereport(FATAL,
|
2008-11-09 18:51:15 +01:00
|
|
|
(errmsg("could not create missing directory \"%s\": %m",
|
|
|
|
path)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-06-15 03:36:08 +02:00
|
|
|
/*
|
2006-06-22 22:42:57 +02:00
|
|
|
* Remove previous backup history files. This also retries creation of
|
|
|
|
* .ready files for any backup history files for which XLogArchiveNotify
|
|
|
|
* failed earlier.
|
2005-06-15 03:36:08 +02:00
|
|
|
*/
|
|
|
|
static void
|
2006-06-22 22:42:57 +02:00
|
|
|
CleanupBackupHistory(void)
|
2005-06-15 03:36:08 +02:00
|
|
|
{
|
|
|
|
DIR *xldir;
|
|
|
|
struct dirent *xlde;
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
xldir = AllocateDir(XLOGDIR);
|
2005-06-15 03:36:08 +02:00
|
|
|
if (xldir == NULL)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not open transaction log directory \"%s\": %m",
|
|
|
|
XLOGDIR)));
|
2005-06-15 03:36:08 +02:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
while ((xlde = ReadDir(xldir, XLOGDIR)) != NULL)
|
2005-06-15 03:36:08 +02:00
|
|
|
{
|
|
|
|
if (strlen(xlde->d_name) > 24 &&
|
|
|
|
strspn(xlde->d_name, "0123456789ABCDEF") == 24 &&
|
|
|
|
strcmp(xlde->d_name + strlen(xlde->d_name) - strlen(".backup"),
|
|
|
|
".backup") == 0)
|
|
|
|
{
|
2008-09-08 18:42:15 +02:00
|
|
|
if (XLogArchiveCheckDone(xlde->d_name))
|
2005-06-15 03:36:08 +02:00
|
|
|
{
|
|
|
|
ereport(DEBUG2,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("removing transaction log backup history file \"%s\"",
|
|
|
|
xlde->d_name)));
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(path, MAXPGPATH, XLOGDIR "/%s", xlde->d_name);
|
2005-06-15 03:36:08 +02:00
|
|
|
unlink(path);
|
|
|
|
XLogArchiveCleanup(xlde->d_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeDir(xldir);
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Restore the backup blocks present in an XLOG record, if any.
|
|
|
|
*
|
|
|
|
* We assume all of the record has been read into memory at *record.
|
2004-08-08 05:22:08 +02:00
|
|
|
*
|
|
|
|
* Note: when a backup block is available in XLOG, we restore it
|
|
|
|
* unconditionally, even if the page in the database appears newer.
|
|
|
|
* This is to protect ourselves against database pages that were partially
|
|
|
|
* or incorrectly written during a crash. We assume that the XLOG data
|
|
|
|
* must be good because it has passed a CRC check, while the database
|
|
|
|
* page might not be. This will force us to replay all subsequent
|
|
|
|
* modifications of the page that appear in XLOG, rather than possibly
|
|
|
|
* ignoring them as already applied, but that's not a huge drawback.
|
2009-01-20 19:59:37 +01:00
|
|
|
*
|
|
|
|
* If 'cleanup' is true, a cleanup lock is used when restoring blocks.
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
* Otherwise, a normal exclusive lock is used. During crash recovery, that's
|
|
|
|
* just pro forma because there can't be any regular backends in the system,
|
|
|
|
* but in hot standby mode the distinction is important. The 'cleanup'
|
|
|
|
* argument applies to all backup blocks in the WAL record, that suffices for
|
|
|
|
* now.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2009-01-20 19:59:37 +01:00
|
|
|
void
|
|
|
|
RestoreBkpBlocks(XLogRecPtr lsn, XLogRecord *record, bool cleanup)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
BkpBlock bkpb;
|
|
|
|
char *blk;
|
|
|
|
int i;
|
|
|
|
|
2009-01-23 12:19:34 +01:00
|
|
|
if (!(record->xl_info & XLR_BKP_BLOCK_MASK))
|
|
|
|
return;
|
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
blk = (char *) XLogRecGetData(record) + record->xl_len;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
|
2000-12-28 14:00:29 +01:00
|
|
|
continue;
|
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
memcpy(&bkpb, blk, sizeof(BkpBlock));
|
2000-12-28 14:00:29 +01:00
|
|
|
blk += sizeof(BkpBlock);
|
|
|
|
|
Unite ReadBufferWithFork, ReadBufferWithStrategy, and ZeroOrReadBuffer
functions into one ReadBufferExtended function, that takes the strategy
and mode as argument. There's three modes, RBM_NORMAL which is the default
used by plain ReadBuffer(), RBM_ZERO, which replaces ZeroOrReadBuffer, and
a new mode RBM_ZERO_ON_ERROR, which allows callers to read corrupt pages
without throwing an error. The FSM needs the new mode to recover from
corrupt pages, which could happend if we crash after extending an FSM file,
and the new page is "torn".
Add fork number to some error messages in bufmgr.c, that still lacked it.
2008-10-31 16:05:00 +01:00
|
|
|
buffer = XLogReadBufferExtended(bkpb.node, bkpb.fork, bkpb.block,
|
|
|
|
RBM_ZERO);
|
2006-03-29 23:17:39 +02:00
|
|
|
Assert(BufferIsValid(buffer));
|
2009-01-20 19:59:37 +01:00
|
|
|
if (cleanup)
|
|
|
|
LockBufferForCleanup(buffer);
|
|
|
|
else
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
2006-03-29 23:17:39 +02:00
|
|
|
page = (Page) BufferGetPage(buffer);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2006-03-29 23:17:39 +02:00
|
|
|
if (bkpb.hole_length == 0)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
2006-03-29 23:17:39 +02:00
|
|
|
memcpy((char *) page, blk, BLCKSZ);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* must zero-fill the hole */
|
|
|
|
MemSet((char *) page, 0, BLCKSZ);
|
|
|
|
memcpy((char *) page, blk, bkpb.hole_offset);
|
|
|
|
memcpy((char *) page + (bkpb.hole_offset + bkpb.hole_length),
|
|
|
|
blk + bkpb.hole_offset,
|
|
|
|
BLCKSZ - (bkpb.hole_offset + bkpb.hole_length));
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
|
2006-03-29 23:17:39 +02:00
|
|
|
PageSetLSN(page, lsn);
|
|
|
|
PageSetTLI(page, ThisTimeLineID);
|
2006-04-01 01:32:07 +02:00
|
|
|
MarkBufferDirty(buffer);
|
|
|
|
UnlockReleaseBuffer(buffer);
|
2006-03-29 23:17:39 +02:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
blk += BLCKSZ - bkpb.hole_length;
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* CRC-check an XLOG record. We do not believe the contents of an XLOG
|
|
|
|
* record (other than to the minimal extent of computing the amount of
|
|
|
|
* data to read in) until we've checked the CRCs.
|
|
|
|
*
|
|
|
|
* We assume all of the record has been read into memory at *record.
|
|
|
|
*/
|
2000-12-28 14:00:29 +01:00
|
|
|
static bool
|
|
|
|
RecordIsValid(XLogRecord *record, XLogRecPtr recptr, int emode)
|
|
|
|
{
|
2005-06-02 07:55:29 +02:00
|
|
|
pg_crc32 crc;
|
2000-12-28 14:00:29 +01:00
|
|
|
int i;
|
|
|
|
uint32 len = record->xl_len;
|
2005-06-02 07:55:29 +02:00
|
|
|
BkpBlock bkpb;
|
2000-12-28 14:00:29 +01:00
|
|
|
char *blk;
|
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
/* First the rmgr data */
|
|
|
|
INIT_CRC32(crc);
|
|
|
|
COMP_CRC32(crc, XLogRecGetData(record), len);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
/* Add in the backup blocks, if any */
|
2001-03-22 05:01:46 +01:00
|
|
|
blk = (char *) XLogRecGetData(record) + len;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
2005-10-15 04:49:52 +02:00
|
|
|
uint32 blen;
|
2005-06-02 07:55:29 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (!(record->xl_info & XLR_SET_BKP_BLOCK(i)))
|
2000-12-28 14:00:29 +01:00
|
|
|
continue;
|
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
memcpy(&bkpb, blk, sizeof(BkpBlock));
|
|
|
|
if (bkpb.hole_offset + bkpb.hole_length > BLCKSZ)
|
2000-12-28 14:00:29 +01:00
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recptr),
|
2005-06-02 07:55:29 +02:00
|
|
|
(errmsg("incorrect hole size in record at %X/%X",
|
|
|
|
recptr.xlogid, recptr.xrecoff)));
|
|
|
|
return false;
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
2005-06-02 07:55:29 +02:00
|
|
|
blen = sizeof(BkpBlock) + BLCKSZ - bkpb.hole_length;
|
|
|
|
COMP_CRC32(crc, blk, blen);
|
|
|
|
blk += blen;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check that xl_tot_len agrees with our calculation */
|
|
|
|
if (blk != (char *) record + record->xl_tot_len)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recptr),
|
2005-06-02 07:55:29 +02:00
|
|
|
(errmsg("incorrect total length in record at %X/%X",
|
|
|
|
recptr.xlogid, recptr.xrecoff)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally include the record header */
|
|
|
|
COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
|
|
|
|
SizeOfXLogRecord - sizeof(pg_crc32));
|
|
|
|
FIN_CRC32(crc);
|
|
|
|
|
|
|
|
if (!EQ_CRC32(record->xl_crc, crc))
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recptr),
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("incorrect resource manager data checksum in record at %X/%X",
|
|
|
|
recptr.xlogid, recptr.xrecoff)));
|
2005-06-02 07:55:29 +02:00
|
|
|
return false;
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
return true;
|
2000-12-28 14:00:29 +01:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Attempt to read an XLOG record.
|
|
|
|
*
|
|
|
|
* If RecPtr is not NULL, try to read a record at that position. Otherwise
|
|
|
|
* try to read a record just after the last one previously read.
|
|
|
|
*
|
Commit to match discussed elog() changes. Only update is that LOG is
now just below FATAL in server_min_messages. Added more text to
highlight ordering difference between it and client_min_messages.
---------------------------------------------------------------------------
REALLYFATAL => PANIC
STOP => PANIC
New INFO level the prints to client by default
New LOG level the prints to server log by default
Cause VACUUM information to print only to the client
NOTICE => INFO where purely information messages are sent
DEBUG => LOG for purely server status messages
DEBUG removed, kept as backward compatible
DEBUG5, DEBUG4, DEBUG3, DEBUG2, DEBUG1 added
DebugLvl removed in favor of new DEBUG[1-5] symbols
New server_min_messages GUC parameter with values:
DEBUG[5-1], INFO, NOTICE, ERROR, LOG, FATAL, PANIC
New client_min_messages GUC parameter with values:
DEBUG[5-1], LOG, INFO, NOTICE, ERROR, FATAL, PANIC
Server startup now logged with LOG instead of DEBUG
Remove debug_level GUC parameter
elog() numbers now start at 10
Add test to print error message if older elog() values are passed to elog()
Bootstrap mode now has a -d that requires an argument, like postmaster
2002-03-02 22:39:36 +01:00
|
|
|
* If no valid record is available, returns NULL, or fails if emode is PANIC.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
* (emode must be either PANIC, LOG)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*
|
2004-08-29 18:34:48 +02:00
|
|
|
* The record is copied into readRecordBuf, so that on successful return,
|
|
|
|
* the returned record pointer always points there.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2000-04-12 19:17:23 +02:00
|
|
|
static XLogRecord *
|
2010-03-30 18:23:57 +02:00
|
|
|
ReadRecord(XLogRecPtr *RecPtr, int emode, bool fetching_ckpt)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
XLogRecord *record;
|
2004-08-29 18:34:48 +02:00
|
|
|
char *buffer;
|
2000-04-12 19:17:23 +02:00
|
|
|
XLogRecPtr tmpRecPtr = EndRecPtr;
|
2004-07-22 00:31:26 +02:00
|
|
|
bool randAccess = false;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
uint32 len,
|
|
|
|
total_len;
|
2004-07-22 00:31:26 +02:00
|
|
|
uint32 targetRecOff;
|
|
|
|
uint32 pageHeaderSize;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
if (readBuf == NULL)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* First time through, permanently allocate readBuf. We do it this
|
|
|
|
* way, rather than just making a static array, for two reasons: (1)
|
|
|
|
* no need to waste the storage in most instantiations of the backend;
|
|
|
|
* (2) a static char array isn't guaranteed to have any particular
|
|
|
|
* alignment, whereas malloc() will provide MAXALIGN'd storage.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2006-04-04 01:35:05 +02:00
|
|
|
readBuf = (char *) malloc(XLOG_BLCKSZ);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
Assert(readBuf != NULL);
|
|
|
|
}
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (RecPtr == NULL)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
1999-10-06 23:58:18 +02:00
|
|
|
RecPtr = &tmpRecPtr;
|
2010-01-15 10:19:10 +01:00
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* Align recptr to next page if no more records can fit on the current
|
|
|
|
* page.
|
2010-01-15 10:19:10 +01:00
|
|
|
*/
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
if (XLOG_BLCKSZ - (RecPtr->xrecoff % XLOG_BLCKSZ) < SizeOfXLogRecord)
|
|
|
|
{
|
2010-01-15 10:19:10 +01:00
|
|
|
NextLogPage(tmpRecPtr);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* We will account for page header size below */
|
|
|
|
}
|
2010-01-27 17:41:09 +01:00
|
|
|
|
|
|
|
if (tmpRecPtr.xrecoff >= XLogFileSize)
|
|
|
|
{
|
|
|
|
(tmpRecPtr.xlogid)++;
|
|
|
|
tmpRecPtr.xrecoff = 0;
|
|
|
|
}
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!XRecOffIsValid(RecPtr->xrecoff))
|
|
|
|
ereport(PANIC,
|
|
|
|
(errmsg("invalid record offset at %X/%X",
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Since we are going to a random position in WAL, forget any prior
|
|
|
|
* state about what timeline we were in, and allow it to be any
|
|
|
|
* timeline in expectedTLIs. We also set a flag to allow curFileTLI
|
|
|
|
* to go backwards (but we can't reset that variable right here, since
|
|
|
|
* we might not change files at all).
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
lastPageTLI = 0; /* see comment in ValidXLOGHeader */
|
|
|
|
randAccess = true; /* allow curFileTLI to go backwards too */
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
/* This is the first try to read this page. */
|
|
|
|
failedSources = 0;
|
|
|
|
retry:
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* Read the page containing the record */
|
|
|
|
if (!XLogPageRead(RecPtr, emode, fetching_ckpt, randAccess))
|
|
|
|
return NULL;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) readBuf);
|
2006-04-04 01:35:05 +02:00
|
|
|
targetRecOff = RecPtr->xrecoff % XLOG_BLCKSZ;
|
2004-07-22 00:31:26 +02:00
|
|
|
if (targetRecOff == 0)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Can only get here in the continuing-from-prev-page case, because
|
|
|
|
* XRecOffIsValid eliminated the zero-page-offset case otherwise. Need
|
|
|
|
* to skip over the new page's header.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
tmpRecPtr.xrecoff += pageHeaderSize;
|
|
|
|
targetRecOff = pageHeaderSize;
|
|
|
|
}
|
|
|
|
else if (targetRecOff < pageHeaderSize)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2004-07-22 00:31:26 +02:00
|
|
|
(errmsg("invalid record offset at %X/%X",
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if ((((XLogPageHeader) readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD) &&
|
2004-07-22 00:31:26 +02:00
|
|
|
targetRecOff == pageHeaderSize)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2003-07-21 22:29:40 +02:00
|
|
|
(errmsg("contrecord is requested by %X/%X",
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
1999-09-27 17:48:12 +02:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2006-04-04 01:35:05 +02:00
|
|
|
record = (XLogRecord *) ((char *) readBuf + RecPtr->xrecoff % XLOG_BLCKSZ);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* xl_len == 0 is bad data for everything except XLOG SWITCH, where it is
|
|
|
|
* required.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2006-08-06 05:53:44 +02:00
|
|
|
if (record->xl_rmid == RM_XLOG_ID && record->xl_info == XLOG_SWITCH)
|
|
|
|
{
|
|
|
|
if (record->xl_len != 0)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2006-08-06 05:53:44 +02:00
|
|
|
(errmsg("invalid xlog switch record at %X/%X",
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (record->xl_len == 0)
|
2001-01-09 07:24:33 +01:00
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2003-07-21 22:29:40 +02:00
|
|
|
(errmsg("record with zero length at %X/%X",
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
2001-01-09 07:24:33 +01:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2005-06-02 07:55:29 +02:00
|
|
|
if (record->xl_tot_len < SizeOfXLogRecord + record->xl_len ||
|
|
|
|
record->xl_tot_len > SizeOfXLogRecord + record->xl_len +
|
|
|
|
XLR_MAX_BKP_BLOCKS * (sizeof(BkpBlock) + BLCKSZ))
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2005-06-02 07:55:29 +02:00
|
|
|
(errmsg("invalid record length at %X/%X",
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2004-08-29 18:34:48 +02:00
|
|
|
if (record->xl_rmid > RM_MAX_ID)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2004-08-29 18:34:48 +02:00
|
|
|
(errmsg("invalid resource manager ID %u at %X/%X",
|
2005-10-15 04:49:52 +02:00
|
|
|
record->xl_rmid, RecPtr->xlogid, RecPtr->xrecoff)));
|
2004-08-29 18:34:48 +02:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2005-05-31 21:10:28 +02:00
|
|
|
if (randAccess)
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We can't exactly verify the prev-link, but surely it should be less
|
|
|
|
* than the record's own address.
|
2005-05-31 21:10:28 +02:00
|
|
|
*/
|
|
|
|
if (!XLByteLT(record->xl_prev, *RecPtr))
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2005-05-31 21:10:28 +02:00
|
|
|
(errmsg("record with incorrect prev-link %X/%X at %X/%X",
|
|
|
|
record->xl_prev.xlogid, record->xl_prev.xrecoff,
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Record's prev-link should exactly match our previous location. This
|
|
|
|
* check guards against torn WAL pages where a stale but valid-looking
|
|
|
|
* WAL record starts on a sector boundary.
|
2005-05-31 21:10:28 +02:00
|
|
|
*/
|
|
|
|
if (!XLByteEQ(record->xl_prev, ReadRecPtr))
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2005-05-31 21:10:28 +02:00
|
|
|
(errmsg("record with incorrect prev-link %X/%X at %X/%X",
|
|
|
|
record->xl_prev.xlogid, record->xl_prev.xrecoff,
|
|
|
|
RecPtr->xlogid, RecPtr->xrecoff)));
|
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Allocate or enlarge readRecordBuf as needed. To avoid useless small
|
2006-04-04 01:35:05 +02:00
|
|
|
* increases, round its size to a multiple of XLOG_BLCKSZ, and make sure
|
|
|
|
* it's at least 4*Max(BLCKSZ, XLOG_BLCKSZ) to start with. (That is
|
|
|
|
* enough for all "normal" records, but very large commit or abort records
|
|
|
|
* might need more space.)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2005-06-02 07:55:29 +02:00
|
|
|
total_len = record->xl_tot_len;
|
2004-08-29 18:34:48 +02:00
|
|
|
if (total_len > readRecordBufSize)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2004-08-29 18:34:48 +02:00
|
|
|
uint32 newSize = total_len;
|
|
|
|
|
2006-04-04 01:35:05 +02:00
|
|
|
newSize += XLOG_BLCKSZ - (newSize % XLOG_BLCKSZ);
|
|
|
|
newSize = Max(newSize, 4 * Max(BLCKSZ, XLOG_BLCKSZ));
|
2004-08-29 18:34:48 +02:00
|
|
|
if (readRecordBuf)
|
|
|
|
free(readRecordBuf);
|
|
|
|
readRecordBuf = (char *) malloc(newSize);
|
|
|
|
if (!readRecordBuf)
|
|
|
|
{
|
|
|
|
readRecordBufSize = 0;
|
|
|
|
/* We treat this as a "bogus data" condition */
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2004-08-29 18:34:48 +02:00
|
|
|
(errmsg("record length %u at %X/%X too long",
|
|
|
|
total_len, RecPtr->xlogid, RecPtr->xrecoff)));
|
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
|
|
|
readRecordBufSize = newSize;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
2004-08-29 18:34:48 +02:00
|
|
|
|
|
|
|
buffer = readRecordBuf;
|
2006-04-04 01:35:05 +02:00
|
|
|
len = XLOG_BLCKSZ - RecPtr->xrecoff % XLOG_BLCKSZ;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (total_len > len)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Need to reassemble record */
|
|
|
|
XLogContRecord *contrecord;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
XLogRecPtr pagelsn;
|
2001-03-22 05:01:46 +01:00
|
|
|
uint32 gotlen = len;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* Initialize pagelsn to the beginning of the page this record is on */
|
|
|
|
pagelsn = *RecPtr;
|
|
|
|
pagelsn.xrecoff = (pagelsn.xrecoff / XLOG_BLCKSZ) * XLOG_BLCKSZ;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
memcpy(buffer, record, len);
|
2000-04-12 19:17:23 +02:00
|
|
|
record = (XLogRecord *) buffer;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
buffer += len;
|
2000-04-12 19:17:23 +02:00
|
|
|
for (;;)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* Calculate pointer to beginning of next page */
|
|
|
|
pagelsn.xrecoff += XLOG_BLCKSZ;
|
|
|
|
if (pagelsn.xrecoff >= XLogFileSize)
|
2010-01-15 10:19:10 +01:00
|
|
|
{
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
(pagelsn.xlogid)++;
|
|
|
|
pagelsn.xrecoff = 0;
|
2010-01-15 10:19:10 +01:00
|
|
|
}
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* Wait for the next page to become available */
|
|
|
|
if (!XLogPageRead(&pagelsn, emode, false, false))
|
|
|
|
return NULL;
|
2010-01-15 10:19:10 +01:00
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* Check that the continuation record looks valid */
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (!(((XLogPageHeader) readBuf)->xlp_info & XLP_FIRST_IS_CONTRECORD))
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2003-07-21 22:29:40 +02:00
|
|
|
(errmsg("there is no contrecord flag in log file %u, segment %u, offset %u",
|
|
|
|
readId, readSeg, readOff)));
|
1999-09-27 17:48:12 +02:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2004-07-22 00:31:26 +02:00
|
|
|
pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) readBuf);
|
|
|
|
contrecord = (XLogContRecord *) ((char *) readBuf + pageHeaderSize);
|
2001-03-22 05:01:46 +01:00
|
|
|
if (contrecord->xl_rem_len == 0 ||
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
total_len != (contrecord->xl_rem_len + gotlen))
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
2003-07-21 22:29:40 +02:00
|
|
|
(errmsg("invalid contrecord length %u in log file %u, segment %u, offset %u",
|
|
|
|
contrecord->xl_rem_len,
|
|
|
|
readId, readSeg, readOff)));
|
1999-09-27 17:48:12 +02:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2006-04-04 01:35:05 +02:00
|
|
|
len = XLOG_BLCKSZ - pageHeaderSize - SizeOfXLogContRecord;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (contrecord->xl_rem_len > len)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
memcpy(buffer, (char *) contrecord + SizeOfXLogContRecord, len);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
gotlen += len;
|
|
|
|
buffer += len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
memcpy(buffer, (char *) contrecord + SizeOfXLogContRecord,
|
|
|
|
contrecord->xl_rem_len);
|
|
|
|
break;
|
|
|
|
}
|
2010-04-16 10:58:16 +02:00
|
|
|
if (!RecordIsValid(record, *RecPtr, emode))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
goto next_record_is_invalid;
|
2004-07-22 00:31:26 +02:00
|
|
|
pageHeaderSize = XLogPageHeaderSize((XLogPageHeader) readBuf);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
EndRecPtr.xlogid = readId;
|
|
|
|
EndRecPtr.xrecoff = readSeg * XLogSegSize + readOff +
|
2005-06-02 07:55:29 +02:00
|
|
|
pageHeaderSize +
|
|
|
|
MAXALIGN(SizeOfXLogContRecord + contrecord->xl_rem_len);
|
2010-01-15 10:19:10 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ReadRecPtr = *RecPtr;
|
2006-08-06 05:53:44 +02:00
|
|
|
/* needn't worry about XLOG SWITCH, it can't cross page boundaries */
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return record;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Record does not cross a page boundary */
|
2010-04-16 10:58:16 +02:00
|
|
|
if (!RecordIsValid(record, *RecPtr, emode))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
EndRecPtr.xlogid = RecPtr->xlogid;
|
|
|
|
EndRecPtr.xrecoff = RecPtr->xrecoff + MAXALIGN(total_len);
|
2010-01-15 10:19:10 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ReadRecPtr = *RecPtr;
|
|
|
|
memcpy(buffer, record, total_len);
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/*
|
|
|
|
* Special processing if it's an XLOG SWITCH record
|
|
|
|
*/
|
|
|
|
if (record->xl_rmid == RM_XLOG_ID && record->xl_info == XLOG_SWITCH)
|
|
|
|
{
|
|
|
|
/* Pretend it extends to end of segment */
|
|
|
|
EndRecPtr.xrecoff += XLogSegSize - 1;
|
|
|
|
EndRecPtr.xrecoff -= EndRecPtr.xrecoff % XLogSegSize;
|
2006-10-04 02:30:14 +02:00
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Pretend that readBuf contains the last page of the segment. This is
|
|
|
|
* just to avoid Assert failure in StartupXLOG if XLOG ends with this
|
|
|
|
* segment.
|
2006-08-06 05:53:44 +02:00
|
|
|
*/
|
|
|
|
readOff = XLogSegSize - XLOG_BLCKSZ;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return (XLogRecord *) buffer;
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
next_record_is_invalid:
|
|
|
|
failedSources |= readSource;
|
|
|
|
|
2008-05-13 22:53:52 +02:00
|
|
|
if (readFile >= 0)
|
|
|
|
{
|
|
|
|
close(readFile);
|
|
|
|
readFile = -1;
|
|
|
|
}
|
2010-03-30 18:23:57 +02:00
|
|
|
|
|
|
|
/* In standby-mode, keep trying */
|
|
|
|
if (StandbyMode)
|
|
|
|
goto retry;
|
|
|
|
else
|
|
|
|
return NULL;
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
|
|
|
|
2001-03-13 21:32:37 +01:00
|
|
|
/*
|
|
|
|
* Check whether the xlog header of a page just read in looks valid.
|
|
|
|
*
|
|
|
|
* This is just a convenience subroutine to avoid duplicated code in
|
2001-03-22 05:01:46 +01:00
|
|
|
* ReadRecord. It's not intended for use from anywhere else.
|
2001-03-13 21:32:37 +01:00
|
|
|
*/
|
|
|
|
static bool
|
2004-07-22 00:31:26 +02:00
|
|
|
ValidXLOGHeader(XLogPageHeader hdr, int emode)
|
2001-03-13 21:32:37 +01:00
|
|
|
{
|
2001-07-19 04:12:35 +02:00
|
|
|
XLogRecPtr recaddr;
|
|
|
|
|
2010-04-16 10:58:16 +02:00
|
|
|
recaddr.xlogid = readId;
|
|
|
|
recaddr.xrecoff = readSeg * XLogSegSize + readOff;
|
|
|
|
|
2001-03-13 21:32:37 +01:00
|
|
|
if (hdr->xlp_magic != XLOG_PAGE_MAGIC)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2003-07-21 22:29:40 +02:00
|
|
|
(errmsg("invalid magic number %04X in log file %u, segment %u, offset %u",
|
|
|
|
hdr->xlp_magic, readId, readSeg, readOff)));
|
2001-03-13 21:32:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((hdr->xlp_info & ~XLP_ALL_FLAGS) != 0)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2003-07-21 22:29:40 +02:00
|
|
|
(errmsg("invalid info bits %04X in log file %u, segment %u, offset %u",
|
|
|
|
hdr->xlp_info, readId, readSeg, readOff)));
|
2001-03-13 21:32:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
2004-07-22 00:31:26 +02:00
|
|
|
if (hdr->xlp_info & XLP_LONG_HEADER)
|
2001-07-19 04:12:35 +02:00
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogLongPageHeader longhdr = (XLogLongPageHeader) hdr;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
if (longhdr->xlp_sysid != ControlFile->system_identifier)
|
2001-03-13 21:32:37 +01:00
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
char fhdrident_str[32];
|
|
|
|
char sysident_str[32];
|
2001-03-13 21:32:37 +01:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Format sysids separately to keep platform-dependent format code
|
|
|
|
* out of the translatable message string.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
snprintf(fhdrident_str, sizeof(fhdrident_str), UINT64_FORMAT,
|
|
|
|
longhdr->xlp_sysid);
|
|
|
|
snprintf(sysident_str, sizeof(sysident_str), UINT64_FORMAT,
|
|
|
|
ControlFile->system_identifier);
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2010-03-21 01:17:59 +01:00
|
|
|
(errmsg("WAL file is from different database system"),
|
|
|
|
errdetail("WAL file database system identifier is %s, pg_control database system identifier is %s.",
|
2005-10-15 04:49:52 +02:00
|
|
|
fhdrident_str, sysident_str)));
|
2004-07-22 00:31:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (longhdr->xlp_seg_size != XLogSegSize)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2010-03-21 01:17:59 +01:00
|
|
|
(errmsg("WAL file is from different database system"),
|
2005-10-15 04:49:52 +02:00
|
|
|
errdetail("Incorrect XLOG_SEG_SIZE in page header.")));
|
2004-07-22 00:31:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
2006-04-05 05:34:05 +02:00
|
|
|
if (longhdr->xlp_xlog_blcksz != XLOG_BLCKSZ)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2010-03-21 01:17:59 +01:00
|
|
|
(errmsg("WAL file is from different database system"),
|
2006-04-05 05:34:05 +02:00
|
|
|
errdetail("Incorrect XLOG_BLCKSZ in page header.")));
|
|
|
|
return false;
|
|
|
|
}
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
2006-04-20 06:07:38 +02:00
|
|
|
else if (readOff == 0)
|
|
|
|
{
|
|
|
|
/* hmm, first page of file doesn't have a long header? */
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2006-04-20 06:07:38 +02:00
|
|
|
(errmsg("invalid info bits %04X in log file %u, segment %u, offset %u",
|
|
|
|
hdr->xlp_info, readId, readSeg, readOff)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
if (!XLByteEQ(hdr->xlp_pageaddr, recaddr))
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2004-07-22 00:31:26 +02:00
|
|
|
(errmsg("unexpected pageaddr %X/%X in log file %u, segment %u, offset %u",
|
2005-10-15 04:49:52 +02:00
|
|
|
hdr->xlp_pageaddr.xlogid, hdr->xlp_pageaddr.xrecoff,
|
2004-07-22 00:31:26 +02:00
|
|
|
readId, readSeg, readOff)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check page TLI is one of the expected values.
|
|
|
|
*/
|
|
|
|
if (!list_member_int(expectedTLIs, (int) hdr->xlp_tli))
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2004-07-22 00:31:26 +02:00
|
|
|
(errmsg("unexpected timeline ID %u in log file %u, segment %u, offset %u",
|
|
|
|
hdr->xlp_tli,
|
|
|
|
readId, readSeg, readOff)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Since child timelines are always assigned a TLI greater than their
|
|
|
|
* immediate parent's TLI, we should never see TLI go backwards across
|
|
|
|
* successive pages of a consistent WAL sequence.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Of course this check should only be applied when advancing sequentially
|
|
|
|
* across pages; therefore ReadRecord resets lastPageTLI to zero when
|
|
|
|
* going to a random page.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
if (hdr->xlp_tli < lastPageTLI)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, recaddr),
|
2004-07-22 00:31:26 +02:00
|
|
|
(errmsg("out-of-sequence timeline ID %u (after %u) in log file %u, segment %u, offset %u",
|
|
|
|
hdr->xlp_tli, lastPageTLI,
|
|
|
|
readId, readSeg, readOff)));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
lastPageTLI = hdr->xlp_tli;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to read a timeline's history file.
|
|
|
|
*
|
|
|
|
* If successful, return the list of component TLIs (the given TLI followed by
|
2004-08-29 07:07:03 +02:00
|
|
|
* its ancestor TLIs). If we can't find the history file, assume that the
|
2004-07-22 00:31:26 +02:00
|
|
|
* timeline has no parents, and return a list of just the specified timeline
|
|
|
|
* ID.
|
|
|
|
*/
|
|
|
|
static List *
|
|
|
|
readTimeLineHistory(TimeLineID targetTLI)
|
|
|
|
{
|
|
|
|
List *result;
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char histfname[MAXFNAMELEN];
|
|
|
|
char fline[MAXPGPATH];
|
2004-08-29 07:07:03 +02:00
|
|
|
FILE *fd;
|
2004-07-22 00:31:26 +02:00
|
|
|
|
2010-01-26 01:07:13 +01:00
|
|
|
/* Timeline 1 does not have a history file, so no need to check */
|
|
|
|
if (targetTLI == 1)
|
|
|
|
return list_make1_int((int) targetTLI);
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
if (InArchiveRecovery)
|
|
|
|
{
|
|
|
|
TLHistoryFileName(histfname, targetTLI);
|
2004-07-22 22:18:40 +02:00
|
|
|
RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0);
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
TLHistoryFilePath(path, targetTLI);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
fd = AllocateFile(path, "r");
|
2004-07-22 00:31:26 +02:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
2004-10-12 23:54:45 +02:00
|
|
|
errmsg("could not open file \"%s\": %m", path)));
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Not there, so assume no parents */
|
|
|
|
return list_make1_int((int) targetTLI);
|
|
|
|
}
|
|
|
|
|
|
|
|
result = NIL;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
|
|
|
* Parse the file...
|
|
|
|
*/
|
2007-02-08 12:10:27 +01:00
|
|
|
while (fgets(fline, sizeof(fline), fd) != NULL)
|
2004-07-22 00:31:26 +02:00
|
|
|
{
|
|
|
|
/* skip leading whitespace and check for # comment */
|
2004-08-29 07:07:03 +02:00
|
|
|
char *ptr;
|
|
|
|
char *endptr;
|
|
|
|
TimeLineID tli;
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
for (ptr = fline; *ptr; ptr++)
|
|
|
|
{
|
|
|
|
if (!isspace((unsigned char) *ptr))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*ptr == '\0' || *ptr == '#')
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* expect a numeric timeline ID as first field of line */
|
|
|
|
tli = (TimeLineID) strtoul(ptr, &endptr, 0);
|
|
|
|
if (endptr == ptr)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("syntax error in history file: %s", fline),
|
|
|
|
errhint("Expected a numeric timeline ID.")));
|
|
|
|
|
|
|
|
if (result &&
|
|
|
|
tli <= (TimeLineID) linitial_int(result))
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("invalid data in history file: %s", fline),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("Timeline IDs must be in increasing sequence.")));
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
/* Build list with newest item first */
|
|
|
|
result = lcons_int((int) tli, result);
|
|
|
|
|
|
|
|
/* we ignore the remainder of each line */
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeFile(fd);
|
|
|
|
|
|
|
|
if (result &&
|
|
|
|
targetTLI <= (TimeLineID) linitial_int(result))
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("invalid data in history file \"%s\"", path),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("Timeline IDs must be less than child timeline's ID.")));
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
result = lcons_int((int) targetTLI, result);
|
|
|
|
|
|
|
|
ereport(DEBUG3,
|
|
|
|
(errmsg_internal("history of timeline %u is %s",
|
|
|
|
targetTLI, nodeToString(result))));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Probe whether a timeline history file exists for the given timeline ID
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
existsTimeLineHistory(TimeLineID probeTLI)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char histfname[MAXFNAMELEN];
|
2004-08-29 07:07:03 +02:00
|
|
|
FILE *fd;
|
2004-07-22 00:31:26 +02:00
|
|
|
|
2010-01-26 01:07:13 +01:00
|
|
|
/* Timeline 1 does not have a history file, so no need to check */
|
|
|
|
if (probeTLI == 1)
|
|
|
|
return false;
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
if (InArchiveRecovery)
|
|
|
|
{
|
|
|
|
TLHistoryFileName(histfname, probeTLI);
|
2004-07-22 22:18:40 +02:00
|
|
|
RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0);
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
TLHistoryFilePath(path, probeTLI);
|
|
|
|
|
|
|
|
fd = AllocateFile(path, "r");
|
|
|
|
if (fd != NULL)
|
|
|
|
{
|
|
|
|
FreeFile(fd);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
2004-10-12 23:54:45 +02:00
|
|
|
errmsg("could not open file \"%s\": %m", path)));
|
2004-07-22 00:31:26 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the newest existing timeline, assuming that startTLI exists.
|
|
|
|
*
|
|
|
|
* Note: while this is somewhat heuristic, it does positively guarantee
|
|
|
|
* that (result + 1) is not a known timeline, and therefore it should
|
|
|
|
* be safe to assign that ID to a new timeline.
|
|
|
|
*/
|
|
|
|
static TimeLineID
|
|
|
|
findNewestTimeLine(TimeLineID startTLI)
|
|
|
|
{
|
|
|
|
TimeLineID newestTLI;
|
|
|
|
TimeLineID probeTLI;
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* The algorithm is just to probe for the existence of timeline history
|
|
|
|
* files. XXX is it useful to allow gaps in the sequence?
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
newestTLI = startTLI;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
for (probeTLI = startTLI + 1;; probeTLI++)
|
2004-07-22 00:31:26 +02:00
|
|
|
{
|
|
|
|
if (existsTimeLineHistory(probeTLI))
|
|
|
|
{
|
|
|
|
newestTLI = probeTLI; /* probeTLI exists */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* doesn't exist, assume we're done */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return newestTLI;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create a new timeline history file.
|
|
|
|
*
|
|
|
|
* newTLI: ID of the new timeline
|
|
|
|
* parentTLI: ID of its immediate parent
|
|
|
|
* endTLI et al: ID of the last used WAL file, for annotation purposes
|
|
|
|
*
|
|
|
|
* Currently this is only used during recovery, and so there are no locking
|
2004-08-29 07:07:03 +02:00
|
|
|
* considerations. But we should be just as tense as XLogFileInit to avoid
|
2004-07-22 00:31:26 +02:00
|
|
|
* emplacing a bogus file.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
writeTimeLineHistory(TimeLineID newTLI, TimeLineID parentTLI,
|
|
|
|
TimeLineID endTLI, uint32 endLogId, uint32 endLogSeg)
|
|
|
|
{
|
|
|
|
char path[MAXPGPATH];
|
|
|
|
char tmppath[MAXPGPATH];
|
|
|
|
char histfname[MAXFNAMELEN];
|
|
|
|
char xlogfname[MAXFNAMELEN];
|
|
|
|
char buffer[BLCKSZ];
|
|
|
|
int srcfd;
|
|
|
|
int fd;
|
|
|
|
int nbytes;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
Assert(newTLI > parentTLI); /* else bad selection of newTLI */
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write into a temp file name.
|
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(tmppath, MAXPGPATH, XLOGDIR "/xlogtemp.%d", (int) getpid());
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
unlink(tmppath);
|
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
/* do not use get_sync_bit() here --- want to fsync only at end of fill */
|
2004-07-22 00:31:26 +02:00
|
|
|
fd = BasicOpenFile(tmppath, O_RDWR | O_CREAT | O_EXCL,
|
|
|
|
S_IRUSR | S_IWUSR);
|
|
|
|
if (fd < 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not create file \"%s\": %m", tmppath)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If a history file exists for the parent, copy it verbatim
|
|
|
|
*/
|
|
|
|
if (InArchiveRecovery)
|
|
|
|
{
|
|
|
|
TLHistoryFileName(histfname, parentTLI);
|
2004-07-22 22:18:40 +02:00
|
|
|
RestoreArchivedFile(path, histfname, "RECOVERYHISTORY", 0);
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
TLHistoryFilePath(path, parentTLI);
|
|
|
|
|
|
|
|
srcfd = BasicOpenFile(path, O_RDONLY, 0);
|
|
|
|
if (srcfd < 0)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
2004-10-12 23:54:45 +02:00
|
|
|
errmsg("could not open file \"%s\": %m", path)));
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Not there, so assume parent has no parents */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
nbytes = (int) read(srcfd, buffer, sizeof(buffer));
|
|
|
|
if (nbytes < 0 || errno != 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m", path)));
|
|
|
|
if (nbytes == 0)
|
|
|
|
break;
|
|
|
|
errno = 0;
|
|
|
|
if ((int) write(fd, buffer, nbytes) != nbytes)
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we fail to make the file, delete it to release disk
|
|
|
|
* space
|
|
|
|
*/
|
|
|
|
unlink(tmppath);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* if write didn't set errno, assume problem is no disk space
|
2004-08-29 07:07:03 +02:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
errno = save_errno ? save_errno : ENOSPC;
|
|
|
|
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not write to file \"%s\": %m", tmppath)));
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
close(srcfd);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Append one line with the details of this timeline split.
|
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* If we did have a parent file, insert an extra newline just in case the
|
|
|
|
* parent file failed to end with one.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
XLogFileName(xlogfname, endTLI, endLogId, endLogSeg);
|
|
|
|
|
2010-03-19 12:05:15 +01:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Write comment to history file to explain why and where timeline
|
|
|
|
* changed. Comment varies according to the recovery target used.
|
2010-03-19 12:05:15 +01:00
|
|
|
*/
|
|
|
|
if (recoveryTarget == RECOVERY_TARGET_XID)
|
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"%s%u\t%s\t%s transaction %u\n",
|
|
|
|
(srcfd < 0) ? "" : "\n",
|
|
|
|
parentTLI,
|
|
|
|
xlogfname,
|
|
|
|
recoveryStopAfter ? "after" : "before",
|
|
|
|
recoveryStopXid);
|
2010-10-14 20:06:06 +02:00
|
|
|
else if (recoveryTarget == RECOVERY_TARGET_TIME)
|
2010-03-19 12:05:15 +01:00
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"%s%u\t%s\t%s %s\n",
|
|
|
|
(srcfd < 0) ? "" : "\n",
|
|
|
|
parentTLI,
|
|
|
|
xlogfname,
|
|
|
|
recoveryStopAfter ? "after" : "before",
|
|
|
|
timestamptz_to_str(recoveryStopTime));
|
|
|
|
else
|
|
|
|
snprintf(buffer, sizeof(buffer),
|
|
|
|
"%s%u\t%s\tno recovery target specified\n",
|
|
|
|
(srcfd < 0) ? "" : "\n",
|
|
|
|
parentTLI,
|
|
|
|
xlogfname);
|
2004-07-22 00:31:26 +02:00
|
|
|
|
|
|
|
nbytes = strlen(buffer);
|
|
|
|
errno = 0;
|
|
|
|
if ((int) write(fd, buffer, nbytes) != nbytes)
|
|
|
|
{
|
|
|
|
int save_errno = errno;
|
|
|
|
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* If we fail to make the file, delete it to release disk space
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
unlink(tmppath);
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
errno = save_errno ? save_errno : ENOSPC;
|
|
|
|
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not write to file \"%s\": %m", tmppath)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pg_fsync(fd) != 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not fsync file \"%s\": %m", tmppath)));
|
|
|
|
|
|
|
|
if (close(fd))
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close file \"%s\": %m", tmppath)));
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now move the completed history file into place with its final name.
|
|
|
|
*/
|
|
|
|
TLHistoryFilePath(path, newTLI);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Prefer link() to rename() here just to be really sure that we don't
|
|
|
|
* overwrite an existing logfile. However, there shouldn't be one, so
|
|
|
|
* rename() is an acceptable substitute except for the truly paranoid.
|
|
|
|
*/
|
|
|
|
#if HAVE_WORKING_LINK
|
|
|
|
if (link(tmppath, path) < 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not link file \"%s\" to \"%s\": %m",
|
|
|
|
tmppath, path)));
|
|
|
|
unlink(tmppath);
|
|
|
|
#else
|
|
|
|
if (rename(tmppath, path) < 0)
|
2005-04-16 00:19:48 +02:00
|
|
|
ereport(ERROR,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not rename file \"%s\" to \"%s\": %m",
|
|
|
|
tmppath, path)));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* The history file can be archived immediately. */
|
|
|
|
TLHistoryFileName(histfname, newTLI);
|
|
|
|
XLogArchiveNotify(histfname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* I/O routines for pg_control
|
2000-11-25 21:33:54 +01:00
|
|
|
*
|
|
|
|
* *ControlFile is a buffer in shared memory that holds an image of the
|
2001-03-22 05:01:46 +01:00
|
|
|
* contents of pg_control. WriteControlFile() initializes pg_control
|
2000-11-25 21:33:54 +01:00
|
|
|
* given a preloaded buffer, ReadControlFile() loads the buffer from
|
|
|
|
* the pg_control file (during postmaster or standalone-backend startup),
|
|
|
|
* and UpdateControlFile() rewrites pg_control after we modify xlog state.
|
|
|
|
*
|
|
|
|
* For simplicity, WriteControlFile() initializes the fields of pg_control
|
|
|
|
* that are related to checking backend/database compatibility, and
|
|
|
|
* ReadControlFile() verifies they are correct. We could split out the
|
|
|
|
* I/O and compatibility-check functions, but there seems no need currently.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
WriteControlFile(void)
|
|
|
|
{
|
|
|
|
int fd;
|
2006-10-04 02:30:14 +02:00
|
|
|
char buffer[PG_CONTROL_SIZE]; /* need not be aligned */
|
2000-11-25 21:33:54 +01:00
|
|
|
|
|
|
|
/*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* Initialize version and compatibility-check fields
|
2000-11-25 21:33:54 +01:00
|
|
|
*/
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ControlFile->pg_control_version = PG_CONTROL_VERSION;
|
|
|
|
ControlFile->catalog_version_no = CATALOG_VERSION_NO;
|
2005-10-03 02:28:43 +02:00
|
|
|
|
|
|
|
ControlFile->maxAlign = MAXIMUM_ALIGNOF;
|
|
|
|
ControlFile->floatFormat = FLOATFORMAT_VALUE;
|
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
ControlFile->blcksz = BLCKSZ;
|
|
|
|
ControlFile->relseg_size = RELSEG_SIZE;
|
2006-04-04 01:35:05 +02:00
|
|
|
ControlFile->xlog_blcksz = XLOG_BLCKSZ;
|
2004-02-11 23:55:26 +01:00
|
|
|
ControlFile->xlog_seg_size = XLOG_SEG_SIZE;
|
2002-04-21 21:08:02 +02:00
|
|
|
|
|
|
|
ControlFile->nameDataLen = NAMEDATALEN;
|
2005-03-29 05:01:32 +02:00
|
|
|
ControlFile->indexMaxKeys = INDEX_MAX_KEYS;
|
2002-04-21 21:08:02 +02:00
|
|
|
|
2007-04-03 06:14:26 +02:00
|
|
|
ControlFile->toast_max_chunk_size = TOAST_MAX_CHUNK_SIZE;
|
|
|
|
|
2002-04-21 21:08:02 +02:00
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2008-04-21 02:26:47 +02:00
|
|
|
ControlFile->enableIntTimes = true;
|
2002-04-21 21:08:02 +02:00
|
|
|
#else
|
2008-04-21 02:26:47 +02:00
|
|
|
ControlFile->enableIntTimes = false;
|
2002-04-21 21:08:02 +02:00
|
|
|
#endif
|
2008-04-21 02:26:47 +02:00
|
|
|
ControlFile->float4ByVal = FLOAT4PASSBYVAL;
|
|
|
|
ControlFile->float8ByVal = FLOAT8PASSBYVAL;
|
2002-04-21 21:08:02 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Contents are protected with a CRC */
|
2005-06-02 07:55:29 +02:00
|
|
|
INIT_CRC32(ControlFile->crc);
|
|
|
|
COMP_CRC32(ControlFile->crc,
|
|
|
|
(char *) ControlFile,
|
|
|
|
offsetof(ControlFileData, crc));
|
|
|
|
FIN_CRC32(ControlFile->crc);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
/*
|
2006-04-05 00:39:59 +02:00
|
|
|
* We write out PG_CONTROL_SIZE bytes into pg_control, zero-padding the
|
|
|
|
* excess over sizeof(ControlFileData). This reduces the odds of
|
|
|
|
* premature-EOF errors when reading pg_control. We'll still fail when we
|
|
|
|
* check the contents of the file, but hopefully with a more specific
|
|
|
|
* error than "couldn't read pg_control".
|
2000-11-25 21:33:54 +01:00
|
|
|
*/
|
2006-04-05 00:39:59 +02:00
|
|
|
if (sizeof(ControlFileData) > PG_CONTROL_SIZE)
|
|
|
|
elog(PANIC, "sizeof(ControlFileData) is larger than PG_CONTROL_SIZE; fix either one");
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2006-04-05 00:39:59 +02:00
|
|
|
memset(buffer, 0, PG_CONTROL_SIZE);
|
2000-11-25 21:33:54 +01:00
|
|
|
memcpy(buffer, ControlFile, sizeof(ControlFileData));
|
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
fd = BasicOpenFile(XLOG_CONTROL_FILE,
|
|
|
|
O_RDWR | O_CREAT | O_EXCL | PG_BINARY,
|
2001-03-16 06:44:33 +01:00
|
|
|
S_IRUSR | S_IWUSR);
|
2000-11-25 21:33:54 +01:00
|
|
|
if (fd < 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not create control file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
XLOG_CONTROL_FILE)));
|
2000-11-25 21:33:54 +01:00
|
|
|
|
2001-06-06 19:07:46 +02:00
|
|
|
errno = 0;
|
2006-04-05 00:39:59 +02:00
|
|
|
if (write(fd, buffer, PG_CONTROL_SIZE) != PG_CONTROL_SIZE)
|
2001-06-06 19:07:46 +02:00
|
|
|
{
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
if (errno == 0)
|
|
|
|
errno = ENOSPC;
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not write to control file: %m")));
|
2001-06-06 19:07:46 +02:00
|
|
|
}
|
2000-11-25 21:33:54 +01:00
|
|
|
|
2000-12-08 23:21:33 +01:00
|
|
|
if (pg_fsync(fd) != 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2003-09-26 17:27:37 +02:00
|
|
|
errmsg("could not fsync control file: %m")));
|
2000-11-25 21:33:54 +01:00
|
|
|
|
2004-01-26 23:35:32 +01:00
|
|
|
if (close(fd))
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close control file: %m")));
|
2000-11-25 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
ReadControlFile(void)
|
|
|
|
{
|
2005-06-02 07:55:29 +02:00
|
|
|
pg_crc32 crc;
|
2000-11-25 21:33:54 +01:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read data...
|
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
fd = BasicOpenFile(XLOG_CONTROL_FILE,
|
|
|
|
O_RDWR | PG_BINARY,
|
|
|
|
S_IRUSR | S_IWUSR);
|
2000-11-25 21:33:54 +01:00
|
|
|
if (fd < 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open control file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
XLOG_CONTROL_FILE)));
|
2000-11-25 21:33:54 +01:00
|
|
|
|
|
|
|
if (read(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not read from control file: %m")));
|
2000-11-25 21:33:54 +01:00
|
|
|
|
|
|
|
close(fd);
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Check for expected pg_control format version. If this is wrong, the
|
|
|
|
* CRC check will likely fail because we'll be checking the wrong number
|
|
|
|
* of bytes. Complaining about wrong version will probably be more
|
|
|
|
* enlightening than complaining about wrong CRC.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2008-01-21 12:17:46 +01:00
|
|
|
|
|
|
|
if (ControlFile->pg_control_version != PG_CONTROL_VERSION && ControlFile->pg_control_version % 65536 == 0 && ControlFile->pg_control_version / 65536 != 0)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d (0x%08x),"
|
2009-06-11 16:49:15 +02:00
|
|
|
" but the server was compiled with PG_CONTROL_VERSION %d (0x%08x).",
|
|
|
|
ControlFile->pg_control_version, ControlFile->pg_control_version,
|
2008-01-21 12:17:46 +01:00
|
|
|
PG_CONTROL_VERSION, PG_CONTROL_VERSION),
|
|
|
|
errhint("This could be a problem of mismatched byte ordering. It looks like you need to initdb.")));
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (ControlFile->pg_control_version != PG_CONTROL_VERSION)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized with PG_CONTROL_VERSION %d,"
|
2005-10-15 04:49:52 +02:00
|
|
|
" but the server was compiled with PG_CONTROL_VERSION %d.",
|
|
|
|
ControlFile->pg_control_version, PG_CONTROL_VERSION),
|
2003-07-21 22:29:40 +02:00
|
|
|
errhint("It looks like you need to initdb.")));
|
2008-01-21 12:17:46 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Now check the CRC. */
|
2005-06-02 07:55:29 +02:00
|
|
|
INIT_CRC32(crc);
|
|
|
|
COMP_CRC32(crc,
|
|
|
|
(char *) ControlFile,
|
|
|
|
offsetof(ControlFileData, crc));
|
|
|
|
FIN_CRC32(crc);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
if (!EQ_CRC32(crc, ControlFile->crc))
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
2003-09-25 08:58:07 +02:00
|
|
|
(errmsg("incorrect checksum in control file")));
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
/*
|
2009-02-07 11:49:36 +01:00
|
|
|
* Do compatibility checking immediately. If the database isn't
|
2009-06-11 16:49:15 +02:00
|
|
|
* compatible with the backend executable, we want to abort before we can
|
|
|
|
* possibly do any damage.
|
2000-11-25 21:33:54 +01:00
|
|
|
*/
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (ControlFile->catalog_version_no != CATALOG_VERSION_NO)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized with CATALOG_VERSION_NO %d,"
|
2005-10-15 04:49:52 +02:00
|
|
|
" but the server was compiled with CATALOG_VERSION_NO %d.",
|
|
|
|
ControlFile->catalog_version_no, CATALOG_VERSION_NO),
|
2003-07-21 22:29:40 +02:00
|
|
|
errhint("It looks like you need to initdb.")));
|
2005-10-03 02:28:43 +02:00
|
|
|
if (ControlFile->maxAlign != MAXIMUM_ALIGNOF)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2005-10-15 04:49:52 +02:00
|
|
|
errdetail("The database cluster was initialized with MAXALIGN %d,"
|
|
|
|
" but the server was compiled with MAXALIGN %d.",
|
|
|
|
ControlFile->maxAlign, MAXIMUM_ALIGNOF),
|
|
|
|
errhint("It looks like you need to initdb.")));
|
2005-10-03 02:28:43 +02:00
|
|
|
if (ControlFile->floatFormat != FLOATFORMAT_VALUE)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2005-10-29 02:31:52 +02:00
|
|
|
errdetail("The database cluster appears to use a different floating-point number format than the server executable."),
|
2005-10-03 02:28:43 +02:00
|
|
|
errhint("It looks like you need to initdb.")));
|
2000-11-25 21:33:54 +01:00
|
|
|
if (ControlFile->blcksz != BLCKSZ)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2005-10-15 04:49:52 +02:00
|
|
|
errdetail("The database cluster was initialized with BLCKSZ %d,"
|
|
|
|
" but the server was compiled with BLCKSZ %d.",
|
|
|
|
ControlFile->blcksz, BLCKSZ),
|
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2000-11-25 21:33:54 +01:00
|
|
|
if (ControlFile->relseg_size != RELSEG_SIZE)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2005-10-15 04:49:52 +02:00
|
|
|
errdetail("The database cluster was initialized with RELSEG_SIZE %d,"
|
|
|
|
" but the server was compiled with RELSEG_SIZE %d.",
|
|
|
|
ControlFile->relseg_size, RELSEG_SIZE),
|
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2006-04-04 01:35:05 +02:00
|
|
|
if (ControlFile->xlog_blcksz != XLOG_BLCKSZ)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2006-10-04 02:30:14 +02:00
|
|
|
errdetail("The database cluster was initialized with XLOG_BLCKSZ %d,"
|
|
|
|
" but the server was compiled with XLOG_BLCKSZ %d.",
|
|
|
|
ControlFile->xlog_blcksz, XLOG_BLCKSZ),
|
2006-04-04 01:35:05 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2004-02-11 23:55:26 +01:00
|
|
|
if (ControlFile->xlog_seg_size != XLOG_SEG_SIZE)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized with XLOG_SEG_SIZE %d,"
|
2005-10-15 04:49:52 +02:00
|
|
|
" but the server was compiled with XLOG_SEG_SIZE %d.",
|
2004-02-11 23:55:26 +01:00
|
|
|
ControlFile->xlog_seg_size, XLOG_SEG_SIZE),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2002-04-21 21:08:02 +02:00
|
|
|
if (ControlFile->nameDataLen != NAMEDATALEN)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2005-10-15 04:49:52 +02:00
|
|
|
errdetail("The database cluster was initialized with NAMEDATALEN %d,"
|
|
|
|
" but the server was compiled with NAMEDATALEN %d.",
|
|
|
|
ControlFile->nameDataLen, NAMEDATALEN),
|
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2005-03-29 05:01:32 +02:00
|
|
|
if (ControlFile->indexMaxKeys != INDEX_MAX_KEYS)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2005-03-29 05:01:32 +02:00
|
|
|
errdetail("The database cluster was initialized with INDEX_MAX_KEYS %d,"
|
2005-10-15 04:49:52 +02:00
|
|
|
" but the server was compiled with INDEX_MAX_KEYS %d.",
|
2005-03-29 05:01:32 +02:00
|
|
|
ControlFile->indexMaxKeys, INDEX_MAX_KEYS),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2007-04-03 06:14:26 +02:00
|
|
|
if (ControlFile->toast_max_chunk_size != TOAST_MAX_CHUNK_SIZE)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized with TOAST_MAX_CHUNK_SIZE %d,"
|
2007-11-15 22:14:46 +01:00
|
|
|
" but the server was compiled with TOAST_MAX_CHUNK_SIZE %d.",
|
|
|
|
ControlFile->toast_max_chunk_size, (int) TOAST_MAX_CHUNK_SIZE),
|
2007-04-03 06:14:26 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2002-04-21 21:08:02 +02:00
|
|
|
|
|
|
|
#ifdef HAVE_INT64_TIMESTAMP
|
2008-04-21 02:26:47 +02:00
|
|
|
if (ControlFile->enableIntTimes != true)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized without HAVE_INT64_TIMESTAMP"
|
2005-10-15 04:49:52 +02:00
|
|
|
" but the server was compiled with HAVE_INT64_TIMESTAMP."),
|
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2002-04-21 21:08:02 +02:00
|
|
|
#else
|
2008-04-21 02:26:47 +02:00
|
|
|
if (ControlFile->enableIntTimes != false)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized with HAVE_INT64_TIMESTAMP"
|
2005-10-15 04:49:52 +02:00
|
|
|
" but the server was compiled without HAVE_INT64_TIMESTAMP."),
|
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
2002-04-21 21:08:02 +02:00
|
|
|
#endif
|
|
|
|
|
2008-04-21 02:26:47 +02:00
|
|
|
#ifdef USE_FLOAT4_BYVAL
|
|
|
|
if (ControlFile->float4ByVal != true)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized without USE_FLOAT4_BYVAL"
|
2009-06-11 16:49:15 +02:00
|
|
|
" but the server was compiled with USE_FLOAT4_BYVAL."),
|
2008-04-21 02:26:47 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
|
|
|
#else
|
|
|
|
if (ControlFile->float4ByVal != false)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2009-06-11 16:49:15 +02:00
|
|
|
errdetail("The database cluster was initialized with USE_FLOAT4_BYVAL"
|
|
|
|
" but the server was compiled without USE_FLOAT4_BYVAL."),
|
2008-04-21 02:26:47 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_FLOAT8_BYVAL
|
|
|
|
if (ControlFile->float8ByVal != true)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
|
|
|
errdetail("The database cluster was initialized without USE_FLOAT8_BYVAL"
|
2009-06-11 16:49:15 +02:00
|
|
|
" but the server was compiled with USE_FLOAT8_BYVAL."),
|
2008-04-21 02:26:47 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
|
|
|
#else
|
|
|
|
if (ControlFile->float8ByVal != false)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("database files are incompatible with server"),
|
2009-06-11 16:49:15 +02:00
|
|
|
errdetail("The database cluster was initialized with USE_FLOAT8_BYVAL"
|
|
|
|
" but the server was compiled without USE_FLOAT8_BYVAL."),
|
2008-04-21 02:26:47 +02:00
|
|
|
errhint("It looks like you need to recompile or initdb.")));
|
|
|
|
#endif
|
2000-11-25 21:33:54 +01:00
|
|
|
}
|
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
void
|
2000-11-25 21:33:54 +01:00
|
|
|
UpdateControlFile(void)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
int fd;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
INIT_CRC32(ControlFile->crc);
|
|
|
|
COMP_CRC32(ControlFile->crc,
|
|
|
|
(char *) ControlFile,
|
|
|
|
offsetof(ControlFileData, crc));
|
|
|
|
FIN_CRC32(ControlFile->crc);
|
2000-12-28 14:00:29 +01:00
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
fd = BasicOpenFile(XLOG_CONTROL_FILE,
|
|
|
|
O_RDWR | PG_BINARY,
|
|
|
|
S_IRUSR | S_IWUSR);
|
1999-10-06 23:58:18 +02:00
|
|
|
if (fd < 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not open control file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
XLOG_CONTROL_FILE)));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2001-06-06 19:07:46 +02:00
|
|
|
errno = 0;
|
2000-11-25 21:33:54 +01:00
|
|
|
if (write(fd, ControlFile, sizeof(ControlFileData)) != sizeof(ControlFileData))
|
2001-06-06 19:07:46 +02:00
|
|
|
{
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
if (errno == 0)
|
|
|
|
errno = ENOSPC;
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not write to control file: %m")));
|
2001-06-06 19:07:46 +02:00
|
|
|
}
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2000-12-08 23:21:33 +01:00
|
|
|
if (pg_fsync(fd) != 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2003-09-25 08:58:07 +02:00
|
|
|
errmsg("could not fsync control file: %m")));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2004-01-26 23:35:32 +01:00
|
|
|
if (close(fd))
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not close control file: %m")));
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Returns the unique system identifier from control file.
|
|
|
|
*/
|
|
|
|
uint64
|
|
|
|
GetSystemIdentifier(void)
|
|
|
|
{
|
|
|
|
Assert(ControlFile != NULL);
|
|
|
|
return ControlFile->system_identifier;
|
|
|
|
}
|
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
/*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* Initialization of shared memory for XLOG
|
2000-11-25 21:33:54 +01:00
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
Size
|
2000-11-21 22:16:06 +01:00
|
|
|
XLOGShmemSize(void)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2005-08-21 01:26:37 +02:00
|
|
|
Size size;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2005-08-21 01:26:37 +02:00
|
|
|
/* XLogCtl */
|
|
|
|
size = sizeof(XLogCtlData);
|
|
|
|
/* xlblocks array */
|
|
|
|
size = add_size(size, mul_size(sizeof(XLogRecPtr), XLOGbuffers));
|
|
|
|
/* extra alignment padding for XLOG I/O buffers */
|
|
|
|
size = add_size(size, ALIGNOF_XLOG_BUFFER);
|
|
|
|
/* and the buffers themselves */
|
2006-04-04 01:35:05 +02:00
|
|
|
size = add_size(size, mul_size(XLOG_BLCKSZ, XLOGbuffers));
|
2005-08-21 01:26:37 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Note: we don't count ControlFileData, it comes out of the "slop factor"
|
|
|
|
* added by CreateSharedMemoryAndSemaphores. This lets us use this
|
|
|
|
* routine again below to compute the actual allocation size.
|
2005-08-21 01:26:37 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
return size;
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
XLOGShmemInit(void)
|
|
|
|
{
|
2005-08-22 02:41:28 +02:00
|
|
|
bool foundCFile,
|
|
|
|
foundXLog;
|
2005-08-21 01:26:37 +02:00
|
|
|
char *allocptr;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
ControlFile = (ControlFileData *)
|
2003-12-20 18:31:21 +01:00
|
|
|
ShmemInitStruct("Control File", sizeof(ControlFileData), &foundCFile);
|
2005-08-22 02:41:28 +02:00
|
|
|
XLogCtl = (XLogCtlData *)
|
|
|
|
ShmemInitStruct("XLOG Ctl", XLOGShmemSize(), &foundXLog);
|
2003-12-20 18:31:21 +01:00
|
|
|
|
2005-08-22 02:41:28 +02:00
|
|
|
if (foundCFile || foundXLog)
|
2003-12-20 18:31:21 +01:00
|
|
|
{
|
|
|
|
/* both should be present or neither */
|
2005-08-22 02:41:28 +02:00
|
|
|
Assert(foundCFile && foundXLog);
|
2003-12-20 18:31:21 +01:00
|
|
|
return;
|
|
|
|
}
|
2000-11-25 21:33:54 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
memset(XLogCtl, 0, sizeof(XLogCtlData));
|
2001-03-22 05:01:46 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Since XLogCtlData contains XLogRecPtr fields, its sizeof should be a
|
|
|
|
* multiple of the alignment for same, so no extra alignment padding is
|
|
|
|
* needed here.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
allocptr = ((char *) XLogCtl) + sizeof(XLogCtlData);
|
|
|
|
XLogCtl->xlblocks = (XLogRecPtr *) allocptr;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
memset(XLogCtl->xlblocks, 0, sizeof(XLogRecPtr) * XLOGbuffers);
|
2005-08-21 01:26:37 +02:00
|
|
|
allocptr += sizeof(XLogRecPtr) * XLOGbuffers;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-08-21 01:26:37 +02:00
|
|
|
* Align the start of the page buffers to an ALIGNOF_XLOG_BUFFER boundary.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2005-08-21 01:26:37 +02:00
|
|
|
allocptr = (char *) TYPEALIGN(ALIGNOF_XLOG_BUFFER, allocptr);
|
|
|
|
XLogCtl->pages = allocptr;
|
2006-04-04 01:35:05 +02:00
|
|
|
memset(XLogCtl->pages, 0, (Size) XLOG_BLCKSZ * XLOGbuffers);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Do basic initialization of XLogCtl shared data. (StartupXLOG will fill
|
|
|
|
* in additional info.)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
XLogCtl->XLogCacheBlck = XLOGbuffers - 1;
|
2009-06-26 22:29:04 +02:00
|
|
|
XLogCtl->SharedRecoveryInProgress = true;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLogCtl->Insert.currpage = (XLogPageHeader) (XLogCtl->pages);
|
2001-09-29 06:02:27 +02:00
|
|
|
SpinLockInit(&XLogCtl->info_lck);
|
2010-09-15 12:35:05 +02:00
|
|
|
InitSharedLatch(&XLogCtl->recoveryWakeupLatch);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we are not in bootstrap mode, pg_control should already exist. Read
|
|
|
|
* and validate it immediately (see comments in ReadControlFile() for the
|
|
|
|
* reasons why).
|
2000-11-25 21:33:54 +01:00
|
|
|
*/
|
|
|
|
if (!IsBootstrapProcessingMode())
|
|
|
|
ReadControlFile();
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* This func must be called ONCE on system install. It creates pg_control
|
|
|
|
* and the initial XLOG segment.
|
1999-10-06 23:58:18 +02:00
|
|
|
*/
|
|
|
|
void
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
BootStrapXLOG(void)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
CheckPoint checkPoint;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
char *buffer;
|
|
|
|
XLogPageHeader page;
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogLongPageHeader longpage;
|
2000-04-12 19:17:23 +02:00
|
|
|
XLogRecord *record;
|
2001-03-22 05:01:46 +01:00
|
|
|
bool use_existent;
|
2004-02-11 23:55:26 +01:00
|
|
|
uint64 sysidentifier;
|
|
|
|
struct timeval tv;
|
2005-06-02 07:55:29 +02:00
|
|
|
pg_crc32 crc;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Select a hopefully-unique system identifier code for this installation.
|
|
|
|
* We use the result of gettimeofday(), including the fractional seconds
|
|
|
|
* field, as being about as unique as we can easily get. (Think not to
|
|
|
|
* use random(), since it hasn't been seeded and there's no portable way
|
|
|
|
* to seed it other than the system clock value...) The upper half of the
|
|
|
|
* uint64 value is just the tv_sec part, while the lower half is the XOR
|
|
|
|
* of tv_sec and tv_usec. This is to ensure that we don't lose uniqueness
|
|
|
|
* unnecessarily if "uint64" is really only 32 bits wide. A person
|
|
|
|
* knowing this encoding can determine the initialization time of the
|
|
|
|
* installation, which could perhaps be useful sometimes.
|
2004-02-11 23:55:26 +01:00
|
|
|
*/
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
sysidentifier = ((uint64) tv.tv_sec) << 32;
|
|
|
|
sysidentifier |= (uint32) (tv.tv_sec | tv.tv_usec);
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* First timeline ID is always 1 */
|
|
|
|
ThisTimeLineID = 1;
|
|
|
|
|
2005-08-21 01:26:37 +02:00
|
|
|
/* page buffer must be aligned suitably for O_DIRECT */
|
2006-04-04 01:35:05 +02:00
|
|
|
buffer = (char *) palloc(XLOG_BLCKSZ + ALIGNOF_XLOG_BUFFER);
|
2005-08-21 01:26:37 +02:00
|
|
|
page = (XLogPageHeader) TYPEALIGN(ALIGNOF_XLOG_BUFFER, buffer);
|
2006-04-04 01:35:05 +02:00
|
|
|
memset(page, 0, XLOG_BLCKSZ);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Set up information for the initial checkpoint record */
|
1999-10-06 23:58:18 +02:00
|
|
|
checkPoint.redo.xlogid = 0;
|
2004-07-22 00:31:26 +02:00
|
|
|
checkPoint.redo.xrecoff = SizeOfXLogLongPHD;
|
|
|
|
checkPoint.ThisTimeLineID = ThisTimeLineID;
|
2006-08-21 18:16:31 +02:00
|
|
|
checkPoint.nextXidEpoch = 0;
|
2001-08-24 01:06:38 +02:00
|
|
|
checkPoint.nextXid = FirstNormalTransactionId;
|
2005-04-13 20:54:57 +02:00
|
|
|
checkPoint.nextOid = FirstBootstrapObjectId;
|
2005-04-28 23:47:18 +02:00
|
|
|
checkPoint.nextMulti = FirstMultiXactId;
|
2005-06-08 17:50:28 +02:00
|
|
|
checkPoint.nextMultiOffset = 0;
|
2009-08-31 04:23:23 +02:00
|
|
|
checkPoint.oldestXid = FirstNormalTransactionId;
|
|
|
|
checkPoint.oldestXidDB = TemplateDbOid;
|
2008-02-17 03:09:32 +01:00
|
|
|
checkPoint.time = (pg_time_t) time(NULL);
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
checkPoint.oldestActiveXid = InvalidTransactionId;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2000-11-21 03:11:06 +01:00
|
|
|
ShmemVariableCache->nextXid = checkPoint.nextXid;
|
|
|
|
ShmemVariableCache->nextOid = checkPoint.nextOid;
|
|
|
|
ShmemVariableCache->oidCount = 0;
|
2005-06-08 17:50:28 +02:00
|
|
|
MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
|
2010-02-17 04:10:33 +01:00
|
|
|
SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
|
2000-11-21 03:11:06 +01:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Set up the XLOG page header */
|
1999-10-06 23:58:18 +02:00
|
|
|
page->xlp_magic = XLOG_PAGE_MAGIC;
|
2004-07-22 00:31:26 +02:00
|
|
|
page->xlp_info = XLP_LONG_HEADER;
|
|
|
|
page->xlp_tli = ThisTimeLineID;
|
2001-07-19 04:12:35 +02:00
|
|
|
page->xlp_pageaddr.xlogid = 0;
|
|
|
|
page->xlp_pageaddr.xrecoff = 0;
|
2004-07-22 00:31:26 +02:00
|
|
|
longpage = (XLogLongPageHeader) page;
|
|
|
|
longpage->xlp_sysid = sysidentifier;
|
|
|
|
longpage->xlp_seg_size = XLogSegSize;
|
2006-04-05 05:34:05 +02:00
|
|
|
longpage->xlp_xlog_blcksz = XLOG_BLCKSZ;
|
2004-02-11 23:55:26 +01:00
|
|
|
|
|
|
|
/* Insert the initial checkpoint record */
|
2004-07-22 00:31:26 +02:00
|
|
|
record = (XLogRecord *) ((char *) page + SizeOfXLogLongPHD);
|
2004-02-11 23:55:26 +01:00
|
|
|
record->xl_prev.xlogid = 0;
|
2004-07-22 00:31:26 +02:00
|
|
|
record->xl_prev.xrecoff = 0;
|
1999-10-06 23:58:18 +02:00
|
|
|
record->xl_xid = InvalidTransactionId;
|
2005-06-02 07:55:29 +02:00
|
|
|
record->xl_tot_len = SizeOfXLogRecord + sizeof(checkPoint);
|
1999-10-06 23:58:18 +02:00
|
|
|
record->xl_len = sizeof(checkPoint);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
record->xl_info = XLOG_CHECKPOINT_SHUTDOWN;
|
1999-10-06 23:58:18 +02:00
|
|
|
record->xl_rmid = RM_XLOG_ID;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
memcpy(XLogRecGetData(record), &checkPoint, sizeof(checkPoint));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2005-06-02 07:55:29 +02:00
|
|
|
INIT_CRC32(crc);
|
|
|
|
COMP_CRC32(crc, &checkPoint, sizeof(checkPoint));
|
|
|
|
COMP_CRC32(crc, (char *) record + sizeof(pg_crc32),
|
|
|
|
SizeOfXLogRecord - sizeof(pg_crc32));
|
|
|
|
FIN_CRC32(crc);
|
2000-12-28 14:00:29 +01:00
|
|
|
record->xl_crc = crc;
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Create first XLOG segment file */
|
2001-03-17 21:54:13 +01:00
|
|
|
use_existent = false;
|
|
|
|
openLogFile = XLogFileInit(0, 0, &use_existent, false);
|
2000-03-20 08:25:39 +01:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Write the first page with the initial record */
|
2001-06-06 19:07:46 +02:00
|
|
|
errno = 0;
|
2006-04-04 01:35:05 +02:00
|
|
|
if (write(openLogFile, page, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
2001-06-06 19:07:46 +02:00
|
|
|
{
|
|
|
|
/* if write didn't set errno, assume problem is no disk space */
|
|
|
|
if (errno == 0)
|
|
|
|
errno = ENOSPC;
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not write bootstrap transaction log file: %m")));
|
2001-06-06 19:07:46 +02:00
|
|
|
}
|
1999-10-06 23:58:18 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (pg_fsync(openLogFile) != 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not fsync bootstrap transaction log file: %m")));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2004-01-26 23:35:32 +01:00
|
|
|
if (close(openLogFile))
|
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not close bootstrap transaction log file: %m")));
|
2004-01-26 23:35:32 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
openLogFile = -1;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/* Now create pg_control */
|
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
memset(ControlFile, 0, sizeof(ControlFileData));
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Initialize pg_control status fields */
|
2004-02-11 23:55:26 +01:00
|
|
|
ControlFile->system_identifier = sysidentifier;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ControlFile->state = DB_SHUTDOWNED;
|
|
|
|
ControlFile->time = checkPoint.time;
|
1999-10-06 23:58:18 +02:00
|
|
|
ControlFile->checkPoint = checkPoint.redo;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ControlFile->checkPointCopy = checkPoint;
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
|
|
|
|
/* Set important parameter values for use when replaying WAL */
|
|
|
|
ControlFile->MaxConnections = MaxConnections;
|
|
|
|
ControlFile->max_prepared_xacts = max_prepared_xacts;
|
|
|
|
ControlFile->max_locks_per_xact = max_locks_per_xact;
|
|
|
|
ControlFile->wal_level = wal_level;
|
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
/* some additional ControlFile fields are set in WriteControlFile() */
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2000-11-25 21:33:54 +01:00
|
|
|
WriteControlFile();
|
2001-08-25 20:52:43 +02:00
|
|
|
|
|
|
|
/* Bootstrap the commit log, too */
|
|
|
|
BootStrapCLOG();
|
2004-07-01 02:52:04 +02:00
|
|
|
BootStrapSUBTRANS();
|
2005-04-28 23:47:18 +02:00
|
|
|
BootStrapMultiXact();
|
2005-06-18 00:32:51 +02:00
|
|
|
|
2005-08-21 01:26:37 +02:00
|
|
|
pfree(buffer);
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
|
2000-04-12 19:17:23 +02:00
|
|
|
static char *
|
2007-08-04 03:26:54 +02:00
|
|
|
str_time(pg_time_t tnow)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2004-03-22 05:16:57 +01:00
|
|
|
static char buf[128];
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2007-08-04 03:26:54 +02:00
|
|
|
pg_strftime(buf, sizeof(buf),
|
|
|
|
"%Y-%m-%d %H:%M:%S %Z",
|
|
|
|
pg_localtime(&tnow, log_timezone));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2000-11-21 23:27:26 +01:00
|
|
|
return buf;
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
|
2010-04-07 12:58:49 +02:00
|
|
|
/*
|
|
|
|
* Parse one line from recovery.conf. 'cmdline' is the raw line from the
|
|
|
|
* file. If the line is parsed successfully, returns true, false indicates
|
|
|
|
* syntax error. On success, *key_p and *value_p are set to the parameter
|
|
|
|
* name and value on the line, respectively. If the line is an empty line,
|
|
|
|
* consisting entirely of whitespace and comments, function returns true
|
|
|
|
* and *keyp_p and *value_p are set to NULL.
|
|
|
|
*
|
|
|
|
* The pointers returned in *key_p and *value_p point to an internal buffer
|
|
|
|
* that is valid only until the next call of parseRecoveryCommandFile().
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
parseRecoveryCommandFileLine(char *cmdline, char **key_p, char **value_p)
|
|
|
|
{
|
|
|
|
char *ptr;
|
|
|
|
char *bufp;
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
static char *buf = NULL;
|
|
|
|
|
|
|
|
*key_p = *value_p = NULL;
|
|
|
|
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Allocate the buffer on first use. It's used to hold both the parameter
|
|
|
|
* name and value.
|
2010-04-07 12:58:49 +02:00
|
|
|
*/
|
|
|
|
if (buf == NULL)
|
|
|
|
buf = malloc(MAXPGPATH + 1);
|
|
|
|
bufp = buf;
|
|
|
|
|
|
|
|
/* Skip any whitespace at the beginning of line */
|
|
|
|
for (ptr = cmdline; *ptr; ptr++)
|
|
|
|
{
|
|
|
|
if (!isspace((unsigned char) *ptr))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Ignore empty lines */
|
|
|
|
if (*ptr == '\0' || *ptr == '#')
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Read the parameter name */
|
|
|
|
key = bufp;
|
|
|
|
while (*ptr && !isspace((unsigned char) *ptr) &&
|
|
|
|
*ptr != '=' && *ptr != '\'')
|
|
|
|
*(bufp++) = *(ptr++);
|
|
|
|
*(bufp++) = '\0';
|
|
|
|
|
|
|
|
/* Skip to the beginning quote of the parameter value */
|
|
|
|
ptr = strchr(ptr, '\'');
|
|
|
|
if (!ptr)
|
|
|
|
return false;
|
|
|
|
ptr++;
|
|
|
|
|
|
|
|
/* Read the parameter value to *bufp. Collapse any '' escapes as we go. */
|
|
|
|
value = bufp;
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (*ptr == '\'')
|
|
|
|
{
|
|
|
|
ptr++;
|
|
|
|
if (*ptr == '\'')
|
|
|
|
*(bufp++) = '\'';
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* end of parameter */
|
|
|
|
*bufp = '\0';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (*ptr == '\0')
|
2010-07-06 21:19:02 +02:00
|
|
|
return false; /* unterminated quoted string */
|
2010-04-07 12:58:49 +02:00
|
|
|
else
|
|
|
|
*(bufp++) = *ptr;
|
|
|
|
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
*(bufp++) = '\0';
|
|
|
|
|
|
|
|
/* Check that there's no garbage after the value */
|
|
|
|
while (*ptr)
|
|
|
|
{
|
|
|
|
if (*ptr == '#')
|
|
|
|
break;
|
|
|
|
if (!isspace((unsigned char) *ptr))
|
|
|
|
return false;
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Success! */
|
|
|
|
*key_p = key;
|
|
|
|
*value_p = value;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* See if there is a recovery command file (recovery.conf), and if so
|
2010-01-15 10:19:10 +01:00
|
|
|
* read in parameters for archive recovery and XLOG streaming.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
|
|
|
* XXX longer term intention is to expand this to
|
|
|
|
* cater for additional parameters and controls
|
|
|
|
* possibly use a flex lexer similar to the GUC one
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
readRecoveryCommandFile(void)
|
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
FILE *fd;
|
|
|
|
char cmdline[MAXPGPATH];
|
|
|
|
TimeLineID rtli = 0;
|
|
|
|
bool rtliGiven = false;
|
|
|
|
bool syntaxError = false;
|
|
|
|
|
2005-07-04 06:51:52 +02:00
|
|
|
fd = AllocateFile(RECOVERY_COMMAND_FILE, "r");
|
2004-07-19 04:47:16 +02:00
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
if (errno == ENOENT)
|
|
|
|
return; /* not there, so no archive recovery */
|
|
|
|
ereport(FATAL,
|
2004-08-29 07:07:03 +02:00
|
|
|
(errcode_for_file_access(),
|
2004-07-19 04:47:16 +02:00
|
|
|
errmsg("could not open recovery command file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
RECOVERY_COMMAND_FILE)));
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
/*
|
|
|
|
* Parse the file...
|
|
|
|
*/
|
2007-02-08 12:10:27 +01:00
|
|
|
while (fgets(cmdline, sizeof(cmdline), fd) != NULL)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char *tok1;
|
|
|
|
char *tok2;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2010-04-07 12:58:49 +02:00
|
|
|
if (!parseRecoveryCommandFileLine(cmdline, &tok1, &tok2))
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
|
|
|
syntaxError = true;
|
|
|
|
break;
|
|
|
|
}
|
2010-04-07 12:58:49 +02:00
|
|
|
if (tok1 == NULL)
|
|
|
|
continue;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (strcmp(tok1, "restore_command") == 0)
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
recoveryRestoreCommand = pstrdup(tok2);
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2007-11-15 23:02:12 +01:00
|
|
|
(errmsg("restore_command = '%s'",
|
2004-07-19 04:47:16 +02:00
|
|
|
recoveryRestoreCommand)));
|
|
|
|
}
|
2009-05-14 22:31:09 +02:00
|
|
|
else if (strcmp(tok1, "recovery_end_command") == 0)
|
|
|
|
{
|
|
|
|
recoveryEndCommand = pstrdup(tok2);
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2009-05-14 22:31:09 +02:00
|
|
|
(errmsg("recovery_end_command = '%s'",
|
|
|
|
recoveryEndCommand)));
|
|
|
|
}
|
2010-06-10 10:13:50 +02:00
|
|
|
else if (strcmp(tok1, "archive_cleanup_command") == 0)
|
2010-03-18 10:17:18 +01:00
|
|
|
{
|
2010-06-10 10:13:50 +02:00
|
|
|
archiveCleanupCommand = pstrdup(tok2);
|
2010-03-18 10:17:18 +01:00
|
|
|
ereport(DEBUG2,
|
2010-06-10 10:13:50 +02:00
|
|
|
(errmsg("archive_cleanup_command = '%s'",
|
|
|
|
archiveCleanupCommand)));
|
2010-03-18 10:17:18 +01:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
else if (strcmp(tok1, "recovery_target_timeline") == 0)
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
rtliGiven = true;
|
|
|
|
if (strcmp(tok2, "latest") == 0)
|
|
|
|
rtli = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
errno = 0;
|
|
|
|
rtli = (TimeLineID) strtoul(tok2, NULL, 0);
|
|
|
|
if (errno == EINVAL || errno == ERANGE)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("recovery_target_timeline is not a valid number: \"%s\"",
|
|
|
|
tok2)));
|
|
|
|
}
|
|
|
|
if (rtli)
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errmsg("recovery_target_timeline = %u", rtli)));
|
|
|
|
else
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errmsg("recovery_target_timeline = latest")));
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
else if (strcmp(tok1, "recovery_target_xid") == 0)
|
|
|
|
{
|
2004-07-19 04:47:16 +02:00
|
|
|
errno = 0;
|
|
|
|
recoveryTargetXid = (TransactionId) strtoul(tok2, NULL, 0);
|
|
|
|
if (errno == EINVAL || errno == ERANGE)
|
|
|
|
ereport(FATAL,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("recovery_target_xid is not a valid number: \"%s\"",
|
|
|
|
tok2)));
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2004-07-19 04:47:16 +02:00
|
|
|
(errmsg("recovery_target_xid = %u",
|
|
|
|
recoveryTargetXid)));
|
2010-03-19 12:05:15 +01:00
|
|
|
recoveryTarget = RECOVERY_TARGET_XID;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
else if (strcmp(tok1, "recovery_target_time") == 0)
|
|
|
|
{
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* if recovery_target_xid specified, then this overrides
|
|
|
|
* recovery_target_time
|
|
|
|
*/
|
2010-03-19 12:05:15 +01:00
|
|
|
if (recoveryTarget == RECOVERY_TARGET_XID)
|
2004-07-19 04:47:16 +02:00
|
|
|
continue;
|
2010-03-19 12:05:15 +01:00
|
|
|
recoveryTarget = RECOVERY_TARGET_TIME;
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2007-04-30 23:01:53 +02:00
|
|
|
* Convert the time string given by the user to TimestampTz form.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2007-04-30 23:01:53 +02:00
|
|
|
recoveryTargetTime =
|
|
|
|
DatumGetTimestampTz(DirectFunctionCall3(timestamptz_in,
|
2007-11-15 22:14:46 +01:00
|
|
|
CStringGetDatum(tok2),
|
2007-04-30 23:01:53 +02:00
|
|
|
ObjectIdGetDatum(InvalidOid),
|
|
|
|
Int32GetDatum(-1)));
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2007-11-15 23:02:12 +01:00
|
|
|
(errmsg("recovery_target_time = '%s'",
|
2007-04-30 23:01:53 +02:00
|
|
|
timestamptz_to_str(recoveryTargetTime))));
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
else if (strcmp(tok1, "recovery_target_inclusive") == 0)
|
|
|
|
{
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* does nothing if a recovery_target is not also set
|
|
|
|
*/
|
2008-07-01 00:10:43 +02:00
|
|
|
if (!parse_bool(tok2, &recoveryTargetInclusive))
|
2009-06-11 16:49:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2010-08-26 21:23:41 +02:00
|
|
|
errmsg("parameter \"%s\" requires a Boolean value", "recovery_target_inclusive")));
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2004-07-19 04:47:16 +02:00
|
|
|
(errmsg("recovery_target_inclusive = %s", tok2)));
|
|
|
|
}
|
2010-01-15 10:19:10 +01:00
|
|
|
else if (strcmp(tok1, "standby_mode") == 0)
|
|
|
|
{
|
|
|
|
if (!parse_bool(tok2, &StandbyMode))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2010-08-26 21:23:41 +02:00
|
|
|
errmsg("parameter \"%s\" requires a Boolean value", "standby_mode")));
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2010-01-15 10:19:10 +01:00
|
|
|
(errmsg("standby_mode = '%s'", tok2)));
|
|
|
|
}
|
|
|
|
else if (strcmp(tok1, "primary_conninfo") == 0)
|
|
|
|
{
|
|
|
|
PrimaryConnInfo = pstrdup(tok2);
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2010-01-15 10:19:10 +01:00
|
|
|
(errmsg("primary_conninfo = '%s'",
|
|
|
|
PrimaryConnInfo)));
|
|
|
|
}
|
|
|
|
else if (strcmp(tok1, "trigger_file") == 0)
|
|
|
|
{
|
|
|
|
TriggerFile = pstrdup(tok2);
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG2,
|
2010-01-15 10:19:10 +01:00
|
|
|
(errmsg("trigger_file = '%s'",
|
|
|
|
TriggerFile)));
|
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
else
|
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("unrecognized recovery parameter \"%s\"",
|
|
|
|
tok1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeFile(fd);
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (syntaxError)
|
|
|
|
ereport(FATAL,
|
2004-07-19 04:47:16 +02:00
|
|
|
(errmsg("syntax error in recovery command file: %s",
|
|
|
|
cmdline),
|
2005-10-15 04:49:52 +02:00
|
|
|
errhint("Lines should have the format parameter = 'value'.")));
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2010-04-02 23:50:40 +02:00
|
|
|
/*
|
|
|
|
* Check for compulsory parameters
|
|
|
|
*/
|
|
|
|
if (StandbyMode)
|
|
|
|
{
|
|
|
|
if (PrimaryConnInfo == NULL && recoveryRestoreCommand == NULL)
|
2010-04-06 19:51:58 +02:00
|
|
|
ereport(WARNING,
|
2010-04-02 23:50:40 +02:00
|
|
|
(errmsg("recovery command file \"%s\" specified neither primary_conninfo nor restore_command",
|
2010-04-06 19:51:58 +02:00
|
|
|
RECOVERY_COMMAND_FILE),
|
|
|
|
errhint("The database server will regularly poll the pg_xlog subdirectory to check for files placed there.")));
|
2010-04-02 23:50:40 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (recoveryRestoreCommand == NULL)
|
|
|
|
ereport(FATAL,
|
2010-04-06 19:51:58 +02:00
|
|
|
(errmsg("recovery command file \"%s\" must specify restore_command when standby mode is not enabled",
|
2010-04-02 23:50:40 +02:00
|
|
|
RECOVERY_COMMAND_FILE)));
|
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Enable fetching from archive recovery area */
|
|
|
|
InArchiveRecovery = true;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If user specified recovery_target_timeline, validate it or compute the
|
|
|
|
* "latest" value. We can't do this until after we've gotten the restore
|
|
|
|
* command and set InArchiveRecovery, because we need to fetch timeline
|
|
|
|
* history files from the archive.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
if (rtliGiven)
|
|
|
|
{
|
|
|
|
if (rtli)
|
|
|
|
{
|
|
|
|
/* Timeline 1 does not have a history file, all else should */
|
|
|
|
if (rtli != 1 && !existsTimeLineHistory(rtli))
|
|
|
|
ereport(FATAL,
|
2007-11-15 23:02:12 +01:00
|
|
|
(errmsg("recovery target timeline %u does not exist",
|
2005-10-15 04:49:52 +02:00
|
|
|
rtli)));
|
2004-07-22 00:31:26 +02:00
|
|
|
recoveryTargetTLI = rtli;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We start the "latest" search from pg_control's timeline */
|
|
|
|
recoveryTargetTLI = findNewestTimeLine(recoveryTargetTLI);
|
|
|
|
}
|
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Exit archive-recovery state
|
|
|
|
*/
|
|
|
|
static void
|
2004-07-22 00:31:26 +02:00
|
|
|
exitArchiveRecovery(TimeLineID endTLI, uint32 endLogId, uint32 endLogSeg)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2004-08-29 07:07:03 +02:00
|
|
|
char recoveryPath[MAXPGPATH];
|
|
|
|
char xlogpath[MAXPGPATH];
|
2009-06-25 23:36:00 +02:00
|
|
|
XLogRecPtr InvalidXLogRecPtr = {0, 0};
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
2004-07-22 00:31:26 +02:00
|
|
|
* We are no longer in archive recovery state.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
InArchiveRecovery = false;
|
|
|
|
|
2009-06-25 23:36:00 +02:00
|
|
|
/*
|
|
|
|
* Update min recovery point one last time.
|
|
|
|
*/
|
|
|
|
UpdateMinRecoveryPoint(InvalidXLogRecPtr, true);
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* If the ending log segment is still open, close it (to avoid problems on
|
|
|
|
* Windows with trying to rename or delete an open file).
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2010-02-10 09:25:25 +01:00
|
|
|
if (readFile >= 0)
|
|
|
|
{
|
|
|
|
close(readFile);
|
|
|
|
readFile = -1;
|
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If the segment was fetched from archival storage, we want to replace
|
|
|
|
* the existing xlog segment (if any) with the archival version. This is
|
|
|
|
* because whatever is in XLOGDIR is very possibly older than what we have
|
|
|
|
* from the archives, since it could have come from restoring a PGDATA
|
|
|
|
* backup. In any case, the archival version certainly is more
|
|
|
|
* descriptive of what our current database state is, because that is what
|
|
|
|
* we replayed from.
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note that if we are establishing a new timeline, ThisTimeLineID is
|
|
|
|
* already set to the new value, and so we will create a new file instead
|
2007-09-29 03:36:10 +02:00
|
|
|
* of overwriting any existing file. (This is, in fact, always the case
|
|
|
|
* at present.)
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYXLOG");
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFilePath(xlogpath, ThisTimeLineID, endLogId, endLogSeg);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
if (restoredFromArchive)
|
|
|
|
{
|
|
|
|
ereport(DEBUG3,
|
|
|
|
(errmsg_internal("moving last restored xlog to \"%s\"",
|
|
|
|
xlogpath)));
|
|
|
|
unlink(xlogpath); /* might or might not exist */
|
|
|
|
if (rename(recoveryPath, xlogpath) != 0)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
2004-08-01 19:45:43 +02:00
|
|
|
errmsg("could not rename file \"%s\" to \"%s\": %m",
|
2004-07-19 04:47:16 +02:00
|
|
|
recoveryPath, xlogpath)));
|
|
|
|
/* XXX might we need to fix permissions on the file? */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If the latest segment is not archival, but there's still a
|
|
|
|
* RECOVERYXLOG laying about, get rid of it.
|
|
|
|
*/
|
|
|
|
unlink(recoveryPath); /* ignore any error */
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If we are establishing a new timeline, we have to copy data from
|
|
|
|
* the last WAL segment of the old timeline to create a starting WAL
|
|
|
|
* segment for the new timeline.
|
2009-04-22 21:51:12 +02:00
|
|
|
*
|
|
|
|
* Notify the archiver that the last WAL segment of the old timeline
|
|
|
|
* is ready to copy to archival storage. Otherwise, it is not archived
|
|
|
|
* for a while.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
if (endTLI != ThisTimeLineID)
|
2009-04-22 21:51:12 +02:00
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFileCopy(endLogId, endLogSeg,
|
|
|
|
endTLI, endLogId, endLogSeg);
|
2009-04-22 21:51:12 +02:00
|
|
|
|
|
|
|
if (XLogArchivingActive())
|
|
|
|
{
|
|
|
|
XLogFileName(xlogpath, endTLI, endLogId, endLogSeg);
|
|
|
|
XLogArchiveNotify(xlogpath);
|
|
|
|
}
|
|
|
|
}
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Let's just make real sure there are not .ready or .done flags posted
|
|
|
|
* for the new segment.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
XLogFileName(xlogpath, ThisTimeLineID, endLogId, endLogSeg);
|
|
|
|
XLogArchiveCleanup(xlogpath);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Get rid of any remaining recovered timeline-history file, too */
|
2005-07-04 06:51:52 +02:00
|
|
|
snprintf(recoveryPath, MAXPGPATH, XLOGDIR "/RECOVERYHISTORY");
|
2004-08-29 07:07:03 +02:00
|
|
|
unlink(recoveryPath); /* ignore any error */
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Rename the config file out of the way, so that we don't accidentally
|
|
|
|
* re-enter archive recovery mode in a subsequent crash.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
unlink(RECOVERY_COMMAND_DONE);
|
|
|
|
if (rename(RECOVERY_COMMAND_FILE, RECOVERY_COMMAND_DONE) != 0)
|
2004-07-19 04:47:16 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
2004-08-01 19:45:43 +02:00
|
|
|
errmsg("could not rename file \"%s\" to \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
RECOVERY_COMMAND_FILE, RECOVERY_COMMAND_DONE)));
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("archive recovery complete")));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* For point-in-time recovery, this function decides whether we want to
|
|
|
|
* stop applying the XLOG at or after the current record.
|
|
|
|
*
|
|
|
|
* Returns TRUE if we are stopping, FALSE otherwise. On TRUE return,
|
|
|
|
* *includeThis is set TRUE if we should apply this record before stopping.
|
2008-10-30 05:06:16 +01:00
|
|
|
*
|
|
|
|
* We also track the timestamp of the latest applied COMMIT/ABORT record
|
2010-07-04 00:15:45 +02:00
|
|
|
* in XLogCtl->recoveryLastXTime, for logging purposes.
|
2004-07-22 00:31:26 +02:00
|
|
|
* Also, some information is saved in recoveryStopXid et al for use in
|
|
|
|
* annotating the new timeline's history file.
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
recoveryStopsHere(XLogRecord *record, bool *includeThis)
|
|
|
|
{
|
|
|
|
bool stopsHere;
|
2004-08-29 07:07:03 +02:00
|
|
|
uint8 record_info;
|
2007-11-15 22:14:46 +01:00
|
|
|
TimestampTz recordXtime;
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/* We only consider stopping at COMMIT or ABORT records */
|
2010-07-04 00:15:45 +02:00
|
|
|
if (record->xl_rmid != RM_XACT_ID)
|
|
|
|
return false;
|
|
|
|
record_info = record->xl_info & ~XLR_INFO_MASK;
|
|
|
|
if (record_info == XLOG_XACT_COMMIT)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2010-07-04 00:15:45 +02:00
|
|
|
xl_xact_commit *recordXactCommitData;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
2010-07-04 00:15:45 +02:00
|
|
|
recordXactCommitData = (xl_xact_commit *) XLogRecGetData(record);
|
|
|
|
recordXtime = recordXactCommitData->xact_time;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
2010-07-04 00:15:45 +02:00
|
|
|
else if (record_info == XLOG_XACT_ABORT)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
2010-07-04 00:15:45 +02:00
|
|
|
xl_xact_abort *recordXactAbortData;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
2010-07-04 00:15:45 +02:00
|
|
|
recordXactAbortData = (xl_xact_abort *) XLogRecGetData(record);
|
|
|
|
recordXtime = recordXactAbortData->xact_time;
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
2007-09-27 00:36:30 +02:00
|
|
|
/* Do we have a PITR target at all? */
|
2010-03-19 12:05:15 +01:00
|
|
|
if (recoveryTarget == RECOVERY_TARGET_UNSET)
|
2008-10-30 05:06:16 +01:00
|
|
|
{
|
2010-07-04 00:15:45 +02:00
|
|
|
SetLatestXTime(recordXtime);
|
2007-09-27 00:36:30 +02:00
|
|
|
return false;
|
2008-10-30 05:06:16 +01:00
|
|
|
}
|
2007-09-27 00:36:30 +02:00
|
|
|
|
2010-03-19 12:05:15 +01:00
|
|
|
if (recoveryTarget == RECOVERY_TARGET_XID)
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* there can be only one transaction end record with this exact
|
|
|
|
* transactionid
|
2004-07-19 04:47:16 +02:00
|
|
|
*
|
2004-08-29 07:07:03 +02:00
|
|
|
* when testing for an xid, we MUST test for equality only, since
|
2005-10-15 04:49:52 +02:00
|
|
|
* transactions are numbered in the order they start, not the order
|
|
|
|
* they complete. A higher numbered xid will complete before you about
|
|
|
|
* 50% of the time...
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
stopsHere = (record->xl_xid == recoveryTargetXid);
|
|
|
|
if (stopsHere)
|
|
|
|
*includeThis = recoveryTargetInclusive;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* there can be many transactions that share the same commit time, so
|
|
|
|
* we stop after the last one, if we are inclusive, or stop at the
|
|
|
|
* first one if we are exclusive
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
if (recoveryTargetInclusive)
|
|
|
|
stopsHere = (recordXtime > recoveryTargetTime);
|
|
|
|
else
|
|
|
|
stopsHere = (recordXtime >= recoveryTargetTime);
|
|
|
|
if (stopsHere)
|
|
|
|
*includeThis = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stopsHere)
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
recoveryStopXid = record->xl_xid;
|
|
|
|
recoveryStopTime = recordXtime;
|
|
|
|
recoveryStopAfter = *includeThis;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
if (record_info == XLOG_XACT_COMMIT)
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
if (recoveryStopAfter)
|
2004-07-19 04:47:16 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("recovery stopping after commit of transaction %u, time %s",
|
2007-04-30 23:01:53 +02:00
|
|
|
recoveryStopXid,
|
|
|
|
timestamptz_to_str(recoveryStopTime))));
|
2004-07-19 04:47:16 +02:00
|
|
|
else
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("recovery stopping before commit of transaction %u, time %s",
|
2007-04-30 23:01:53 +02:00
|
|
|
recoveryStopXid,
|
|
|
|
timestamptz_to_str(recoveryStopTime))));
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
if (recoveryStopAfter)
|
2004-07-19 04:47:16 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("recovery stopping after abort of transaction %u, time %s",
|
2007-04-30 23:01:53 +02:00
|
|
|
recoveryStopXid,
|
|
|
|
timestamptz_to_str(recoveryStopTime))));
|
2004-07-19 04:47:16 +02:00
|
|
|
else
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("recovery stopping before abort of transaction %u, time %s",
|
2007-04-30 23:01:53 +02:00
|
|
|
recoveryStopXid,
|
|
|
|
timestamptz_to_str(recoveryStopTime))));
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
2008-10-30 05:06:16 +01:00
|
|
|
|
|
|
|
if (recoveryStopAfter)
|
2010-07-04 00:15:45 +02:00
|
|
|
SetLatestXTime(recordXtime);
|
2004-07-19 04:47:16 +02:00
|
|
|
}
|
2008-10-30 05:06:16 +01:00
|
|
|
else
|
2010-07-04 00:15:45 +02:00
|
|
|
SetLatestXTime(recordXtime);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
return stopsHere;
|
|
|
|
}
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
2010-07-04 00:15:45 +02:00
|
|
|
* Save timestamp of latest processed commit/abort record.
|
|
|
|
*
|
|
|
|
* We keep this in XLogCtl, not a simple static variable, so that it can be
|
|
|
|
* seen by processes other than the startup process. Note in particular
|
|
|
|
* that CreateRestartPoint is executed in the bgwriter.
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
*/
|
2010-07-04 00:15:45 +02:00
|
|
|
static void
|
|
|
|
SetLatestXTime(TimestampTz xtime)
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
{
|
2010-07-04 00:15:45 +02:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->recoveryLastXTime = xtime;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-07-04 00:15:45 +02:00
|
|
|
* Fetch timestamp of latest processed commit/abort record.
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
*/
|
2010-07-03 22:43:58 +02:00
|
|
|
static TimestampTz
|
2010-07-04 00:15:45 +02:00
|
|
|
GetLatestXTime(void)
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
2010-07-04 00:15:45 +02:00
|
|
|
TimestampTz xtime;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2010-07-04 00:15:45 +02:00
|
|
|
xtime = xlogctl->recoveryLastXTime;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
2010-07-04 00:15:45 +02:00
|
|
|
return xtime;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns bool with current recovery mode, a global state.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_is_in_recovery(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
PG_RETURN_BOOL(RecoveryInProgress());
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
}
|
|
|
|
|
2010-07-03 22:43:58 +02:00
|
|
|
/*
|
|
|
|
* Returns time of receipt of current chunk of XLOG data, as well as
|
|
|
|
* whether it was received from streaming replication or from archives.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
GetXLogReceiptTime(TimestampTz *rtime, bool *fromStream)
|
|
|
|
{
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* This must be executed in the startup process, since we don't export the
|
|
|
|
* relevant state to shared memory.
|
2010-07-03 22:43:58 +02:00
|
|
|
*/
|
|
|
|
Assert(InRecovery);
|
|
|
|
|
|
|
|
*rtime = XLogReceiptTime;
|
|
|
|
*fromStream = (XLogReceiptSource == XLOG_FROM_STREAM);
|
|
|
|
}
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
* Note that text field supplied is a parameter name and does not require
|
|
|
|
* translation
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
*/
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
#define RecoveryRequiresIntParameter(param_name, currValue, minValue) \
|
2010-04-29 23:36:19 +02:00
|
|
|
do { \
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
if (currValue < minValue) \
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
ereport(ERROR, \
|
2010-04-29 23:36:19 +02:00
|
|
|
(errmsg("hot standby is not possible because " \
|
|
|
|
"%s = %d is a lower setting than on the master server " \
|
|
|
|
"(its value was %d)", \
|
|
|
|
param_name, \
|
|
|
|
currValue, \
|
|
|
|
minValue))); \
|
|
|
|
} while(0)
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check to see if required parameters are set high enough on this server
|
|
|
|
* for various aspects of recovery operation.
|
|
|
|
*/
|
|
|
|
static void
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
CheckRequiredParameterValues(void)
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
{
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* For archive recovery, the WAL must be generated with at least 'archive'
|
|
|
|
* wal_level.
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
*/
|
|
|
|
if (InArchiveRecovery && ControlFile->wal_level == WAL_LEVEL_MINIMAL)
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
2010-06-03 23:02:12 +02:00
|
|
|
(errmsg("WAL was generated with wal_level=minimal, data may be missing"),
|
|
|
|
errhint("This happens if you temporarily set wal_level=minimal without taking a new base backup.")));
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
}
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* For Hot Standby, the WAL must be generated with 'hot_standby' mode, and
|
|
|
|
* we must have at least as many backend slots as the primary.
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
*/
|
2010-04-29 23:36:19 +02:00
|
|
|
if (InArchiveRecovery && EnableHotStandby)
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
{
|
|
|
|
if (ControlFile->wal_level < WAL_LEVEL_HOT_STANDBY)
|
|
|
|
ereport(ERROR,
|
2010-04-29 23:36:19 +02:00
|
|
|
(errmsg("hot standby is not possible because wal_level was not set to \"hot_standby\" on the master server"),
|
|
|
|
errhint("Either set wal_level to \"hot_standby\" on the master, or turn off hot_standby here.")));
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
/* We ignore autovacuum_max_workers when we make this test. */
|
|
|
|
RecoveryRequiresIntParameter("max_connections",
|
2010-04-29 23:36:19 +02:00
|
|
|
MaxConnections,
|
|
|
|
ControlFile->MaxConnections);
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
RecoveryRequiresIntParameter("max_prepared_xacts",
|
2010-04-29 23:36:19 +02:00
|
|
|
max_prepared_xacts,
|
|
|
|
ControlFile->max_prepared_xacts);
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
RecoveryRequiresIntParameter("max_locks_per_xact",
|
2010-04-29 23:36:19 +02:00
|
|
|
max_locks_per_xact,
|
|
|
|
ControlFile->max_locks_per_xact);
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
}
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
}
|
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
/*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* This must be called ONCE during postmaster or standalone-backend startup
|
1999-10-06 23:58:18 +02:00
|
|
|
*/
|
|
|
|
void
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
StartupXLOG(void)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2000-04-12 19:17:23 +02:00
|
|
|
XLogCtlInsert *Insert;
|
|
|
|
CheckPoint checkPoint;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
bool wasShutdown;
|
2007-09-29 03:36:10 +02:00
|
|
|
bool reachedStopPoint = false;
|
2006-08-07 18:57:57 +02:00
|
|
|
bool haveBackupLabel = false;
|
2000-04-12 19:17:23 +02:00
|
|
|
XLogRecPtr RecPtr,
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
checkPointLoc,
|
|
|
|
EndOfLog;
|
2004-07-19 04:47:16 +02:00
|
|
|
uint32 endLogId;
|
|
|
|
uint32 endLogSeg;
|
2000-04-12 19:17:23 +02:00
|
|
|
XLogRecord *record;
|
2003-07-17 18:45:04 +02:00
|
|
|
uint32 freespace;
|
2005-06-18 00:32:51 +02:00
|
|
|
TransactionId oldestActiveXID;
|
2000-04-12 19:17:23 +02:00
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
/*
|
2000-11-25 21:33:54 +01:00
|
|
|
* Read control file and check XLOG status looks valid.
|
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note: in most control paths, *ControlFile is already valid and we need
|
|
|
|
* not do ReadControlFile() here, but might as well do it to be sure.
|
1999-10-06 23:58:18 +02:00
|
|
|
*/
|
2000-11-25 21:33:54 +01:00
|
|
|
ReadControlFile();
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2006-12-08 20:50:53 +01:00
|
|
|
if (ControlFile->state < DB_SHUTDOWNED ||
|
2000-04-12 19:17:23 +02:00
|
|
|
ControlFile->state > DB_IN_PRODUCTION ||
|
1999-10-06 23:58:18 +02:00
|
|
|
!XRecOffIsValid(ControlFile->checkPoint.xrecoff))
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("control file contains invalid data")));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
|
|
|
if (ControlFile->state == DB_SHUTDOWNED)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("database system was shut down at %s",
|
|
|
|
str_time(ControlFile->time))));
|
2010-06-03 05:20:00 +02:00
|
|
|
else if (ControlFile->state == DB_SHUTDOWNED_IN_RECOVERY)
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("database system was shut down in recovery at %s",
|
|
|
|
str_time(ControlFile->time))));
|
1999-10-06 23:58:18 +02:00
|
|
|
else if (ControlFile->state == DB_SHUTDOWNING)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
2006-12-08 20:50:53 +01:00
|
|
|
(errmsg("database system shutdown was interrupted; last known up at %s",
|
2003-07-21 22:29:40 +02:00
|
|
|
str_time(ControlFile->time))));
|
2006-08-07 18:57:57 +02:00
|
|
|
else if (ControlFile->state == DB_IN_CRASH_RECOVERY)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("database system was interrupted while in recovery at %s",
|
|
|
|
str_time(ControlFile->time)),
|
|
|
|
errhint("This probably means that some data is corrupted and"
|
|
|
|
" you will have to use the last backup for recovery.")));
|
2006-08-07 18:57:57 +02:00
|
|
|
else if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY)
|
|
|
|
ereport(LOG,
|
2006-10-04 02:30:14 +02:00
|
|
|
(errmsg("database system was interrupted while in recovery at log time %s",
|
|
|
|
str_time(ControlFile->checkPointCopy.time)),
|
Wording cleanup for error messages. Also change can't -> cannot.
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
2007-02-01 20:10:30 +01:00
|
|
|
errhint("If this has occurred more than once some data might be corrupted"
|
2007-11-15 22:14:46 +01:00
|
|
|
" and you might need to choose an earlier recovery target.")));
|
1999-10-06 23:58:18 +02:00
|
|
|
else if (ControlFile->state == DB_IN_PRODUCTION)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
2007-11-15 22:14:46 +01:00
|
|
|
(errmsg("database system was interrupted; last known up at %s",
|
|
|
|
str_time(ControlFile->time))));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2003-02-21 01:06:22 +01:00
|
|
|
/* This is just to allow attaching to startup process with a debugger */
|
|
|
|
#ifdef XLOG_REPLAY_DELAY
|
2004-02-11 23:55:26 +01:00
|
|
|
if (ControlFile->state != DB_SHUTDOWNED)
|
2004-04-19 19:42:59 +02:00
|
|
|
pg_usleep(60000000L);
|
2003-02-21 01:06:22 +01:00
|
|
|
#endif
|
|
|
|
|
2008-11-09 18:51:15 +01:00
|
|
|
/*
|
|
|
|
* Verify that pg_xlog and pg_xlog/archive_status exist. In cases where
|
2009-06-11 16:49:15 +02:00
|
|
|
* someone has performed a copy for PITR, these directories may have been
|
|
|
|
* excluded and need to be re-created.
|
2008-11-09 18:51:15 +01:00
|
|
|
*/
|
|
|
|
ValidateXLOGDirectoryStructure();
|
|
|
|
|
2009-08-12 22:53:31 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* Clear out any old relcache cache files. This is *necessary* if we do
|
|
|
|
* any WAL replay, since that would probably result in the cache files
|
|
|
|
* being out of sync with database reality. In theory we could leave them
|
|
|
|
* in place if the database had been cleanly shut down, but it seems
|
|
|
|
* safest to just remove them always and let them be rebuilt during the
|
|
|
|
* first backend startup.
|
2009-08-12 22:53:31 +02:00
|
|
|
*/
|
|
|
|
RelationCacheInitFileRemove();
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Initialize on the assumption we want to recover to the same timeline
|
|
|
|
* that's active according to pg_control.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
recoveryTargetTLI = ControlFile->checkPointCopy.ThisTimeLineID;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Check for recovery control file, and if so set up state for offline
|
|
|
|
* recovery
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
readRecoveryCommandFile();
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* Now we can determine the list of expected TLIs */
|
|
|
|
expectedTLIs = readTimeLineHistory(recoveryTargetTLI);
|
|
|
|
|
2004-07-22 23:09:37 +02:00
|
|
|
/*
|
|
|
|
* If pg_control's timeline is not in expectedTLIs, then we cannot
|
|
|
|
* proceed: the backup is not part of the history of the requested
|
|
|
|
* timeline.
|
|
|
|
*/
|
|
|
|
if (!list_member_int(expectedTLIs,
|
2005-10-15 04:49:52 +02:00
|
|
|
(int) ControlFile->checkPointCopy.ThisTimeLineID))
|
2004-07-22 23:09:37 +02:00
|
|
|
ereport(FATAL,
|
|
|
|
(errmsg("requested timeline %u is not a child of database system timeline %u",
|
|
|
|
recoveryTargetTLI,
|
|
|
|
ControlFile->checkPointCopy.ThisTimeLineID)));
|
|
|
|
|
2010-03-18 10:17:18 +01:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Save the selected recovery target timeline ID and
|
|
|
|
* archive_cleanup_command in shared memory so that other processes can
|
|
|
|
* see them
|
2010-03-18 10:17:18 +01:00
|
|
|
*/
|
2010-01-15 10:19:10 +01:00
|
|
|
XLogCtl->RecoveryTargetTLI = recoveryTargetTLI;
|
2010-06-10 10:13:50 +02:00
|
|
|
strncpy(XLogCtl->archiveCleanupCommand,
|
|
|
|
archiveCleanupCommand ? archiveCleanupCommand : "",
|
|
|
|
sizeof(XLogCtl->archiveCleanupCommand));
|
2010-01-15 10:19:10 +01:00
|
|
|
|
2010-04-02 15:10:56 +02:00
|
|
|
if (InArchiveRecovery)
|
|
|
|
{
|
|
|
|
if (StandbyMode)
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("entering standby mode")));
|
|
|
|
else if (recoveryTarget == RECOVERY_TARGET_XID)
|
|
|
|
ereport(LOG,
|
2010-07-06 21:19:02 +02:00
|
|
|
(errmsg("starting point-in-time recovery to XID %u",
|
|
|
|
recoveryTargetXid)));
|
2010-04-02 15:10:56 +02:00
|
|
|
else if (recoveryTarget == RECOVERY_TARGET_TIME)
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("starting point-in-time recovery to %s",
|
|
|
|
timestamptz_to_str(recoveryTargetTime))));
|
|
|
|
else
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("starting archive recovery")));
|
|
|
|
}
|
|
|
|
|
2010-09-15 12:35:05 +02:00
|
|
|
/*
|
2010-09-15 15:58:22 +02:00
|
|
|
* Take ownership of the wakeup latch if we're going to sleep during
|
2010-09-15 12:35:05 +02:00
|
|
|
* recovery.
|
|
|
|
*/
|
|
|
|
if (StandbyMode)
|
|
|
|
OwnLatch(&XLogCtl->recoveryWakeupLatch);
|
|
|
|
|
2010-01-04 13:50:50 +01:00
|
|
|
if (read_backup_label(&checkPointLoc))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2004-08-04 18:25:02 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* When a backup_label file is present, we want to roll forward from
|
|
|
|
* the checkpoint it identifies, rather than using pg_control.
|
2004-08-04 18:25:02 +02:00
|
|
|
*/
|
2004-08-29 18:34:48 +02:00
|
|
|
record = ReadCheckpointRecord(checkPointLoc, 0);
|
2004-08-04 18:25:02 +02:00
|
|
|
if (record != NULL)
|
|
|
|
{
|
2007-05-31 09:36:12 +02:00
|
|
|
ereport(DEBUG1,
|
2004-08-04 18:25:02 +02:00
|
|
|
(errmsg("checkpoint record is at %X/%X",
|
2005-10-15 04:49:52 +02:00
|
|
|
checkPointLoc.xlogid, checkPointLoc.xrecoff)));
|
2004-08-04 18:25:02 +02:00
|
|
|
InRecovery = true; /* force recovery even if SHUTDOWNED */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(PANIC,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("could not locate required checkpoint record"),
|
|
|
|
errhint("If you are not restoring from a backup, try removing the file \"%s/backup_label\".", DataDir)));
|
2004-08-04 18:25:02 +02:00
|
|
|
}
|
2006-08-07 18:57:57 +02:00
|
|
|
/* set flag to delete it later */
|
|
|
|
haveBackupLabel = true;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2004-08-04 18:25:02 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Get the last valid checkpoint record. If the latest one according
|
|
|
|
* to pg_control is broken, try the next-to-last one.
|
2004-08-04 18:25:02 +02:00
|
|
|
*/
|
|
|
|
checkPointLoc = ControlFile->checkPoint;
|
2010-01-15 10:19:10 +01:00
|
|
|
RedoStartLSN = ControlFile->checkPointCopy.redo;
|
2004-08-29 18:34:48 +02:00
|
|
|
record = ReadCheckpointRecord(checkPointLoc, 1);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (record != NULL)
|
|
|
|
{
|
2007-05-31 09:36:12 +02:00
|
|
|
ereport(DEBUG1,
|
2004-08-04 18:25:02 +02:00
|
|
|
(errmsg("checkpoint record is at %X/%X",
|
2005-10-15 04:49:52 +02:00
|
|
|
checkPointLoc.xlogid, checkPointLoc.xrecoff)));
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
else if (StandbyMode)
|
2010-01-15 10:19:10 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The last valid checkpoint record required for a streaming
|
|
|
|
* recovery exists in neither standby nor the primary.
|
|
|
|
*/
|
|
|
|
ereport(PANIC,
|
|
|
|
(errmsg("could not locate a valid checkpoint record")));
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
else
|
2004-08-04 18:25:02 +02:00
|
|
|
{
|
|
|
|
checkPointLoc = ControlFile->prevCheckPoint;
|
2004-08-29 18:34:48 +02:00
|
|
|
record = ReadCheckpointRecord(checkPointLoc, 2);
|
2004-08-04 18:25:02 +02:00
|
|
|
if (record != NULL)
|
|
|
|
{
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("using previous checkpoint record at %X/%X",
|
|
|
|
checkPointLoc.xlogid, checkPointLoc.xrecoff)));
|
|
|
|
InRecovery = true; /* force recovery even if SHUTDOWNED */
|
2004-08-04 18:25:02 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ereport(PANIC,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("could not locate a valid checkpoint record")));
|
2004-08-04 18:25:02 +02:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
2004-08-04 18:25:02 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
LastRec = RecPtr = checkPointLoc;
|
|
|
|
memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
|
|
|
|
wasShutdown = (record->xl_info == XLOG_CHECKPOINT_SHUTDOWN);
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2007-05-31 09:36:12 +02:00
|
|
|
ereport(DEBUG1,
|
2007-11-15 22:14:46 +01:00
|
|
|
(errmsg("redo record is at %X/%X; shutdown %s",
|
|
|
|
checkPoint.redo.xlogid, checkPoint.redo.xrecoff,
|
|
|
|
wasShutdown ? "TRUE" : "FALSE")));
|
2007-05-31 09:36:12 +02:00
|
|
|
ereport(DEBUG1,
|
2006-08-21 18:16:31 +02:00
|
|
|
(errmsg("next transaction ID: %u/%u; next OID: %u",
|
|
|
|
checkPoint.nextXidEpoch, checkPoint.nextXid,
|
|
|
|
checkPoint.nextOid)));
|
2007-05-31 09:36:12 +02:00
|
|
|
ereport(DEBUG1,
|
2005-06-08 17:50:28 +02:00
|
|
|
(errmsg("next MultiXactId: %u; next MultiXactOffset: %u",
|
|
|
|
checkPoint.nextMulti, checkPoint.nextMultiOffset)));
|
2009-08-31 04:23:23 +02:00
|
|
|
ereport(DEBUG1,
|
|
|
|
(errmsg("oldest unfrozen transaction ID: %u, in database %u",
|
|
|
|
checkPoint.oldestXid, checkPoint.oldestXidDB)));
|
2001-08-24 01:06:38 +02:00
|
|
|
if (!TransactionIdIsNormal(checkPoint.nextXid))
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
2003-09-25 08:58:07 +02:00
|
|
|
(errmsg("invalid next transaction ID")));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
|
|
|
ShmemVariableCache->nextXid = checkPoint.nextXid;
|
|
|
|
ShmemVariableCache->nextOid = checkPoint.nextOid;
|
2000-11-03 12:39:36 +01:00
|
|
|
ShmemVariableCache->oidCount = 0;
|
2005-06-08 17:50:28 +02:00
|
|
|
MultiXactSetNextMXact(checkPoint.nextMulti, checkPoint.nextMultiOffset);
|
2010-02-17 04:10:33 +01:00
|
|
|
SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2003-05-22 16:39:28 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We must replay WAL entries using the same TimeLineID they were created
|
|
|
|
* under, so temporarily adopt the TLI indicated by the checkpoint (see
|
|
|
|
* also xlog_redo()).
|
2003-05-22 16:39:28 +02:00
|
|
|
*/
|
2004-07-22 00:31:26 +02:00
|
|
|
ThisTimeLineID = checkPoint.ThisTimeLineID;
|
2003-05-22 16:39:28 +02:00
|
|
|
|
2004-05-27 19:12:57 +02:00
|
|
|
RedoRecPtr = XLogCtl->Insert.RedoRecPtr = checkPoint.redo;
|
2000-10-21 17:43:36 +02:00
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
if (XLByteLT(RecPtr, checkPoint.redo))
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errmsg("invalid redo in checkpoint record")));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2004-07-22 22:18:40 +02:00
|
|
|
/*
|
2004-08-29 07:07:03 +02:00
|
|
|
* Check whether we need to force recovery from WAL. If it appears to
|
2005-10-15 04:49:52 +02:00
|
|
|
* have been a clean shutdown and we did not have a recovery.conf file,
|
|
|
|
* then assume no recovery needed.
|
2004-07-22 22:18:40 +02:00
|
|
|
*/
|
2007-03-03 21:02:27 +01:00
|
|
|
if (XLByteLT(checkPoint.redo, RecPtr))
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (wasShutdown)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
2007-11-15 22:14:46 +01:00
|
|
|
(errmsg("invalid redo record in shutdown checkpoint")));
|
2000-10-28 18:21:00 +02:00
|
|
|
InRecovery = true;
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
else if (ControlFile->state != DB_SHUTDOWNED)
|
2000-10-28 18:21:00 +02:00
|
|
|
InRecovery = true;
|
2004-08-04 18:25:02 +02:00
|
|
|
else if (InArchiveRecovery)
|
|
|
|
{
|
|
|
|
/* force recovery due to presence of recovery.conf */
|
|
|
|
InRecovery = true;
|
|
|
|
}
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2000-10-28 18:21:00 +02:00
|
|
|
/* REDO */
|
2004-08-04 18:25:02 +02:00
|
|
|
if (InRecovery)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int rmid;
|
2010-07-06 21:19:02 +02:00
|
|
|
|
2010-04-13 16:17:46 +02:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
2003-02-21 01:06:22 +01:00
|
|
|
|
2006-08-07 18:57:57 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Update pg_control to show that we are recovering and to show the
|
|
|
|
* selected checkpoint as the place we are starting from. We also mark
|
|
|
|
* pg_control with any minimum recovery stop point obtained from a
|
|
|
|
* backup history file.
|
2006-08-07 18:57:57 +02:00
|
|
|
*/
|
2004-08-04 18:25:02 +02:00
|
|
|
if (InArchiveRecovery)
|
2006-08-07 18:57:57 +02:00
|
|
|
ControlFile->state = DB_IN_ARCHIVE_RECOVERY;
|
2004-07-22 22:18:40 +02:00
|
|
|
else
|
2006-08-07 18:57:57 +02:00
|
|
|
{
|
2004-07-22 22:18:40 +02:00
|
|
|
ereport(LOG,
|
2004-08-04 18:25:02 +02:00
|
|
|
(errmsg("database system was not properly shut down; "
|
|
|
|
"automatic recovery in progress")));
|
2006-08-07 18:57:57 +02:00
|
|
|
ControlFile->state = DB_IN_CRASH_RECOVERY;
|
|
|
|
}
|
|
|
|
ControlFile->prevCheckPoint = ControlFile->checkPoint;
|
|
|
|
ControlFile->checkPoint = checkPointLoc;
|
|
|
|
ControlFile->checkPointCopy = checkPoint;
|
2010-01-04 13:50:50 +01:00
|
|
|
if (InArchiveRecovery)
|
|
|
|
{
|
|
|
|
/* initialize minRecoveryPoint if not set yet */
|
|
|
|
if (XLByteLT(ControlFile->minRecoveryPoint, checkPoint.redo))
|
|
|
|
ControlFile->minRecoveryPoint = checkPoint.redo;
|
|
|
|
}
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2010-01-04 13:50:50 +01:00
|
|
|
/*
|
|
|
|
* set backupStartupPoint if we're starting archive recovery from a
|
|
|
|
* base backup
|
|
|
|
*/
|
|
|
|
if (haveBackupLabel)
|
|
|
|
ControlFile->backupStartPoint = checkPoint.redo;
|
2008-02-17 03:09:32 +01:00
|
|
|
ControlFile->time = (pg_time_t) time(NULL);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* No need to hold ControlFileLock yet, we aren't up far enough */
|
1999-10-06 23:58:18 +02:00
|
|
|
UpdateControlFile();
|
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
/* initialize our local copy of minRecoveryPoint */
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
minRecoveryPoint = ControlFile->minRecoveryPoint;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Reset pgstat data, because it may be invalid after recovery.
|
|
|
|
*/
|
|
|
|
pgstat_reset_all();
|
|
|
|
|
2006-08-07 18:57:57 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* If there was a backup label file, it's done its job and the info
|
|
|
|
* has now been propagated into pg_control. We must get rid of the
|
|
|
|
* label file so that if we crash during recovery, we'll pick up at
|
|
|
|
* the latest recovery restartpoint instead of going all the way back
|
|
|
|
* to the backup start point. It seems prudent though to just rename
|
|
|
|
* the file out of the way rather than delete it completely.
|
2006-08-07 18:57:57 +02:00
|
|
|
*/
|
|
|
|
if (haveBackupLabel)
|
|
|
|
{
|
|
|
|
unlink(BACKUP_LABEL_OLD);
|
|
|
|
if (rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD) != 0)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not rename file \"%s\" to \"%s\": %m",
|
|
|
|
BACKUP_LABEL_FILE, BACKUP_LABEL_OLD)));
|
|
|
|
}
|
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
/* Check that the GUCs used to generate the WAL allow recovery */
|
|
|
|
CheckRequiredParameterValues();
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Initialize for Hot Standby, if enabled. We won't let backends in
|
|
|
|
* yet, not until we've reached the min recovery point specified in
|
2010-02-26 03:01:40 +01:00
|
|
|
* control file and we've established a recovery snapshot from a
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
* running-xacts WAL record.
|
|
|
|
*/
|
2010-04-29 23:36:19 +02:00
|
|
|
if (InArchiveRecovery && EnableHotStandby)
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
{
|
|
|
|
TransactionId *xids;
|
2010-02-26 03:01:40 +01:00
|
|
|
int nxids;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
2010-02-12 10:49:08 +01:00
|
|
|
ereport(DEBUG1,
|
2010-04-29 23:36:19 +02:00
|
|
|
(errmsg("initializing for hot standby")));
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
|
|
|
|
InitRecoveryTransactionEnvironment();
|
|
|
|
|
|
|
|
if (wasShutdown)
|
|
|
|
oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
|
|
|
|
else
|
|
|
|
oldestActiveXID = checkPoint.oldestActiveXid;
|
|
|
|
Assert(TransactionIdIsValid(oldestActiveXID));
|
|
|
|
|
|
|
|
/* Startup commit log and related stuff */
|
|
|
|
StartupCLOG();
|
|
|
|
StartupSUBTRANS(oldestActiveXID);
|
|
|
|
StartupMultiXact();
|
|
|
|
|
|
|
|
ProcArrayInitRecoveryInfo(oldestActiveXID);
|
2010-04-13 16:17:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're beginning at a shutdown checkpoint, we know that
|
2010-07-06 21:19:02 +02:00
|
|
|
* nothing was running on the master at this point. So fake-up an
|
|
|
|
* empty running-xacts record and use that here and now. Recover
|
|
|
|
* additional standby state for prepared transactions.
|
2010-04-13 16:17:46 +02:00
|
|
|
*/
|
|
|
|
if (wasShutdown)
|
|
|
|
{
|
|
|
|
RunningTransactionsData running;
|
2010-05-13 13:15:38 +02:00
|
|
|
TransactionId latestCompletedXid;
|
2010-04-13 16:17:46 +02:00
|
|
|
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Construct a RunningTransactions snapshot representing a
|
|
|
|
* shut down server, with only prepared transactions still
|
|
|
|
* alive. We're never overflowed at this point because all
|
|
|
|
* subxids are listed with their parent prepared transactions.
|
2010-04-13 16:17:46 +02:00
|
|
|
*/
|
|
|
|
running.xcnt = nxids;
|
|
|
|
running.subxid_overflow = false;
|
|
|
|
running.nextXid = checkPoint.nextXid;
|
|
|
|
running.oldestRunningXid = oldestActiveXID;
|
2010-05-13 13:15:38 +02:00
|
|
|
latestCompletedXid = checkPoint.nextXid;
|
|
|
|
TransactionIdRetreat(latestCompletedXid);
|
2010-05-14 09:11:49 +02:00
|
|
|
Assert(TransactionIdIsNormal(latestCompletedXid));
|
2010-05-13 13:15:38 +02:00
|
|
|
running.latestCompletedXid = latestCompletedXid;
|
2010-04-13 16:17:46 +02:00
|
|
|
running.xids = xids;
|
|
|
|
|
|
|
|
ProcArrayApplyRecoveryInfo(&running);
|
|
|
|
|
|
|
|
StandbyRecoverPreparedTransactions(false);
|
|
|
|
}
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
}
|
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
/* Initialize resource managers */
|
2003-02-21 01:06:22 +01:00
|
|
|
for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
|
|
|
|
{
|
|
|
|
if (RmgrTable[rmid].rm_startup != NULL)
|
|
|
|
RmgrTable[rmid].rm_startup();
|
|
|
|
}
|
|
|
|
|
2010-04-13 16:17:46 +02:00
|
|
|
/*
|
2010-07-04 00:15:45 +02:00
|
|
|
* Initialize shared replayEndRecPtr, recoveryLastRecPtr, and
|
|
|
|
* recoveryLastXTime.
|
2010-04-13 16:17:46 +02:00
|
|
|
*
|
|
|
|
* This is slightly confusing if we're starting from an online
|
2010-07-06 21:19:02 +02:00
|
|
|
* checkpoint; we've just read and replayed the chekpoint record, but
|
|
|
|
* we're going to start replay from its redo pointer, which precedes
|
|
|
|
* the location of the checkpoint record itself. So even though the
|
|
|
|
* last record we've replayed is indeed ReadRecPtr, we haven't
|
|
|
|
* replayed all the preceding records yet. That's OK for the current
|
|
|
|
* use of these variables.
|
2010-04-13 16:17:46 +02:00
|
|
|
*/
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->replayEndRecPtr = ReadRecPtr;
|
|
|
|
xlogctl->recoveryLastRecPtr = ReadRecPtr;
|
2010-07-04 00:15:45 +02:00
|
|
|
xlogctl->recoveryLastXTime = 0;
|
2010-04-13 16:17:46 +02:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
2010-07-03 22:43:58 +02:00
|
|
|
/* Also ensure XLogReceiptTime has a sane value */
|
|
|
|
XLogReceiptTime = GetCurrentTimestamp();
|
|
|
|
|
2010-04-13 16:17:46 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Let postmaster know we've started redo now, so that it can launch
|
|
|
|
* bgwriter to perform restartpoints. We don't bother during crash
|
|
|
|
* recovery as restartpoints can only be performed during archive
|
|
|
|
* recovery. And we'd like to keep crash recovery simple, to avoid
|
|
|
|
* introducing bugs that could affect you when recovering after crash.
|
2010-04-13 16:17:46 +02:00
|
|
|
*
|
|
|
|
* After this point, we can no longer assume that we're the only
|
|
|
|
* process in addition to postmaster! Also, fsync requests are
|
|
|
|
* subsequently to be handled by the bgwriter, not locally.
|
|
|
|
*/
|
|
|
|
if (InArchiveRecovery && IsUnderPostmaster)
|
|
|
|
{
|
|
|
|
SetForwardFsyncRequests();
|
|
|
|
SendPostmasterSignal(PMSIGNAL_RECOVERY_STARTED);
|
|
|
|
bgwriterLaunched = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Allow read-only connections immediately if we're consistent
|
|
|
|
* already.
|
2010-04-13 16:17:46 +02:00
|
|
|
*/
|
|
|
|
CheckRecoveryConsistency();
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Find the first record that logically follows the checkpoint --- it
|
|
|
|
* might physically precede it, though.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
1999-10-06 23:58:18 +02:00
|
|
|
if (XLByteLT(checkPoint.redo, RecPtr))
|
2004-07-22 00:31:26 +02:00
|
|
|
{
|
|
|
|
/* back up to find the record */
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
record = ReadRecord(&(checkPoint.redo), PANIC, false);
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
else
|
2001-08-24 01:06:38 +02:00
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
/* just have to read next record after CheckPoint */
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
record = ReadRecord(NULL, LOG, false);
|
2001-08-24 01:06:38 +02:00
|
|
|
}
|
1999-10-06 23:58:18 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (record != NULL)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2004-07-19 04:47:16 +02:00
|
|
|
bool recoveryContinue = true;
|
|
|
|
bool recoveryApply = true;
|
2006-10-04 02:30:14 +02:00
|
|
|
ErrorContextCallback errcontext;
|
2010-07-04 00:15:45 +02:00
|
|
|
TimestampTz xtime;
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2000-10-28 18:21:00 +02:00
|
|
|
InRedo = true;
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2010-01-04 13:50:50 +01:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("redo starts at %X/%X",
|
|
|
|
ReadRecPtr.xlogid, ReadRecPtr.xrecoff)));
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* main redo apply loop
|
|
|
|
*/
|
1999-10-06 23:58:18 +02:00
|
|
|
do
|
|
|
|
{
|
2004-01-06 18:26:23 +01:00
|
|
|
#ifdef WAL_DEBUG
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
if (XLOG_DEBUG ||
|
2010-02-26 03:01:40 +01:00
|
|
|
(rmid == RM_XACT_ID && trace_recovery_messages <= DEBUG2) ||
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
(rmid != RM_XACT_ID && trace_recovery_messages <= DEBUG3))
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
StringInfoData buf;
|
2000-10-21 17:43:36 +02:00
|
|
|
|
2006-03-24 05:32:13 +01:00
|
|
|
initStringInfo(&buf);
|
|
|
|
appendStringInfo(&buf, "REDO @ %X/%X; LSN %X/%X: ",
|
2006-10-04 02:30:14 +02:00
|
|
|
ReadRecPtr.xlogid, ReadRecPtr.xrecoff,
|
|
|
|
EndRecPtr.xlogid, EndRecPtr.xrecoff);
|
2006-03-24 05:32:13 +01:00
|
|
|
xlog_outrec(&buf, record);
|
|
|
|
appendStringInfo(&buf, " - ");
|
|
|
|
RmgrTable[record->xl_rmid].rm_desc(&buf,
|
|
|
|
record->xl_info,
|
2006-10-04 02:30:14 +02:00
|
|
|
XLogRecGetData(record));
|
2006-03-24 05:32:13 +01:00
|
|
|
elog(LOG, "%s", buf.data);
|
|
|
|
pfree(buf.data);
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
2004-01-06 18:26:23 +01:00
|
|
|
#endif
|
2000-10-21 17:43:36 +02:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/* Handle interrupt signals of startup process */
|
|
|
|
HandleStartupProcInterrupts();
|
2009-03-04 14:56:40 +01:00
|
|
|
|
2010-04-13 16:17:46 +02:00
|
|
|
/* Allow read-only connections if we're consistent now */
|
|
|
|
CheckRecoveryConsistency();
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* Have we reached our recovery target?
|
|
|
|
*/
|
|
|
|
if (recoveryStopsHere(record, &recoveryApply))
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
reachedStopPoint = true; /* see below */
|
2004-07-19 04:47:16 +02:00
|
|
|
recoveryContinue = false;
|
|
|
|
if (!recoveryApply)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2006-03-24 05:32:13 +01:00
|
|
|
/* Setup error traceback support for ereport() */
|
|
|
|
errcontext.callback = rm_redo_error_callback;
|
|
|
|
errcontext.arg = (void *) record;
|
|
|
|
errcontext.previous = error_context_stack;
|
|
|
|
error_context_stack = &errcontext;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/* nextXid must be beyond record's xid */
|
|
|
|
if (TransactionIdFollowsOrEquals(record->xl_xid,
|
2005-10-15 04:49:52 +02:00
|
|
|
ShmemVariableCache->nextXid))
|
2004-07-19 04:47:16 +02:00
|
|
|
{
|
|
|
|
ShmemVariableCache->nextXid = record->xl_xid;
|
|
|
|
TransactionIdAdvance(ShmemVariableCache->nextXid);
|
|
|
|
}
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Update shared replayEndRecPtr before replaying this record,
|
|
|
|
* so that XLogFlush will update minRecoveryPoint correctly.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->replayEndRecPtr = EndRecPtr;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
/*
|
|
|
|
* If we are attempting to enter Hot Standby mode, process
|
|
|
|
* XIDs we see
|
|
|
|
*/
|
2010-05-13 13:15:38 +02:00
|
|
|
if (standbyState >= STANDBY_INITIALIZED &&
|
|
|
|
TransactionIdIsValid(record->xl_xid))
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
RecordKnownAssignedTransactionIds(record->xl_xid);
|
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
RmgrTable[record->xl_rmid].rm_redo(EndRecPtr, record);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2006-03-24 05:32:13 +01:00
|
|
|
/* Pop the error context stack */
|
|
|
|
error_context_stack = errcontext.previous;
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Update shared recoveryLastRecPtr after this record has been
|
|
|
|
* replayed.
|
|
|
|
*/
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->recoveryLastRecPtr = EndRecPtr;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
LastRec = ReadRecPtr;
|
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
record = ReadRecord(NULL, LOG, false);
|
2004-07-19 04:47:16 +02:00
|
|
|
} while (record != NULL && recoveryContinue);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
|
|
|
* end of main redo apply loop
|
|
|
|
*/
|
|
|
|
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("redo done at %X/%X",
|
|
|
|
ReadRecPtr.xlogid, ReadRecPtr.xrecoff)));
|
2010-07-04 00:15:45 +02:00
|
|
|
xtime = GetLatestXTime();
|
|
|
|
if (xtime)
|
2007-09-27 00:36:30 +02:00
|
|
|
ereport(LOG,
|
2007-11-15 22:14:46 +01:00
|
|
|
(errmsg("last completed transaction was at log time %s",
|
2010-07-04 00:15:45 +02:00
|
|
|
timestamptz_to_str(xtime))));
|
2000-10-28 18:21:00 +02:00
|
|
|
InRedo = false;
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
else
|
2004-07-22 22:18:40 +02:00
|
|
|
{
|
|
|
|
/* there are no WAL records following the checkpoint */
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("redo is not required")));
|
2004-07-22 22:18:40 +02:00
|
|
|
}
|
2000-10-28 18:21:00 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* If we launched a WAL receiver, it should be gone by now. It will trump
|
|
|
|
* over the startup checkpoint and subsequent records if it's still alive,
|
|
|
|
* so be extra sure that it's gone.
|
|
|
|
*/
|
|
|
|
if (WalRcvInProgress())
|
|
|
|
elog(PANIC, "wal receiver still active");
|
|
|
|
|
2010-09-15 12:35:05 +02:00
|
|
|
/*
|
|
|
|
* We don't need the latch anymore. It's not strictly necessary to disown
|
2010-09-15 15:58:22 +02:00
|
|
|
* it, but let's do it for the sake of tidiness.
|
2010-09-15 12:35:05 +02:00
|
|
|
*/
|
|
|
|
if (StandbyMode)
|
|
|
|
DisownLatch(&XLogCtl->recoveryWakeupLatch);
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* We are now done reading the xlog from stream. Turn off streaming
|
2010-02-26 03:01:40 +01:00
|
|
|
* recovery to force fetching the files (which would be required at end of
|
|
|
|
* recovery, e.g., timeline history file) from archive or pg_xlog.
|
2010-01-15 10:19:10 +01:00
|
|
|
*/
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
StandbyMode = false;
|
2010-01-15 10:19:10 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Re-fetch the last valid or last applied record, so we can identify the
|
|
|
|
* exact endpoint of what we consider the valid portion of WAL.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
record = ReadRecord(&LastRec, PANIC, false);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
EndOfLog = EndRecPtr;
|
2004-07-19 04:47:16 +02:00
|
|
|
XLByteToPrevSeg(EndOfLog, endLogId, endLogSeg);
|
|
|
|
|
2004-08-04 18:25:02 +02:00
|
|
|
/*
|
|
|
|
* Complain if we did not roll forward far enough to render the backup
|
2009-06-26 22:29:04 +02:00
|
|
|
* dump consistent. Note: it is indeed okay to look at the local variable
|
|
|
|
* minRecoveryPoint here, even though ControlFile->minRecoveryPoint might
|
|
|
|
* be further ahead --- ControlFile->minRecoveryPoint cannot have been
|
|
|
|
* advanced beyond the WAL we processed.
|
2004-08-04 18:25:02 +02:00
|
|
|
*/
|
2010-01-04 13:50:50 +01:00
|
|
|
if (InArchiveRecovery &&
|
|
|
|
(XLByteLT(EndOfLog, minRecoveryPoint) ||
|
|
|
|
!XLogRecPtrIsInvalid(ControlFile->backupStartPoint)))
|
2004-08-04 18:25:02 +02:00
|
|
|
{
|
2007-09-29 03:36:10 +02:00
|
|
|
if (reachedStopPoint) /* stopped because of stop request */
|
2004-08-04 18:25:02 +02:00
|
|
|
ereport(FATAL,
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
(errmsg("requested recovery stop point is before consistent recovery point")));
|
2007-11-15 22:14:46 +01:00
|
|
|
else /* ran off end of WAL */
|
2004-08-04 18:25:02 +02:00
|
|
|
ereport(FATAL,
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
(errmsg("WAL ends before consistent recovery point")));
|
2004-08-04 18:25:02 +02:00
|
|
|
}
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
|
|
|
* Consider whether we need to assign a new timeline ID.
|
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* If we are doing an archive recovery, we always assign a new ID. This
|
|
|
|
* handles a couple of issues. If we stopped short of the end of WAL
|
2007-09-29 03:36:10 +02:00
|
|
|
* during recovery, then we are clearly generating a new timeline and must
|
|
|
|
* assign it a unique new ID. Even if we ran to the end, modifying the
|
2007-11-15 22:14:46 +01:00
|
|
|
* current last segment is problematic because it may result in trying to
|
|
|
|
* overwrite an already-archived copy of that segment, and we encourage
|
2007-09-29 03:36:10 +02:00
|
|
|
* DBAs to make their archive_commands reject that. We can dodge the
|
|
|
|
* problem by making the new active segment have a new timeline ID.
|
|
|
|
*
|
|
|
|
* In a normal crash recovery, we can just extend the timeline we were in.
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
2007-09-29 03:36:10 +02:00
|
|
|
if (InArchiveRecovery)
|
2004-07-22 00:31:26 +02:00
|
|
|
{
|
|
|
|
ThisTimeLineID = findNewestTimeLine(recoveryTargetTLI) + 1;
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("selected new timeline ID: %u", ThisTimeLineID)));
|
|
|
|
writeTimeLineHistory(ThisTimeLineID, recoveryTargetTLI,
|
|
|
|
curFileTLI, endLogId, endLogSeg);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Save the selected TimeLineID in shared memory, too */
|
|
|
|
XLogCtl->ThisTimeLineID = ThisTimeLineID;
|
|
|
|
|
2004-07-19 04:47:16 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We are now done reading the old WAL. Turn off archive fetching if it
|
|
|
|
* was active, and make a writable copy of the last WAL segment. (Note
|
|
|
|
* that we also have a copy of the last block of the old WAL in readBuf;
|
|
|
|
* we will use that below.)
|
2004-07-19 04:47:16 +02:00
|
|
|
*/
|
|
|
|
if (InArchiveRecovery)
|
2004-07-22 00:31:26 +02:00
|
|
|
exitArchiveRecovery(curFileTLI, endLogId, endLogSeg);
|
2004-07-19 04:47:16 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare to write WAL starting at EndOfLog position, and init xlog
|
|
|
|
* buffer cache using the block containing the last record from the
|
|
|
|
* previous incarnation.
|
|
|
|
*/
|
|
|
|
openLogId = endLogId;
|
|
|
|
openLogSeg = endLogSeg;
|
2004-07-22 00:31:26 +02:00
|
|
|
openLogFile = XLogFileOpen(openLogId, openLogSeg);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
openLogOff = 0;
|
2000-10-28 18:21:00 +02:00
|
|
|
Insert = &XLogCtl->Insert;
|
2001-04-05 11:34:32 +02:00
|
|
|
Insert->PrevRecord = LastRec;
|
2003-05-22 16:39:28 +02:00
|
|
|
XLogCtl->xlblocks[0].xlogid = openLogId;
|
|
|
|
XLogCtl->xlblocks[0].xrecoff =
|
2006-04-04 01:35:05 +02:00
|
|
|
((EndOfLog.xrecoff - 1) / XLOG_BLCKSZ + 1) * XLOG_BLCKSZ;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Tricky point here: readBuf contains the *last* block that the LastRec
|
|
|
|
* record spans, not the one it starts in. The last block is indeed the
|
|
|
|
* one we want to use.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2006-04-04 01:35:05 +02:00
|
|
|
Assert(readOff == (XLogCtl->xlblocks[0].xrecoff - XLOG_BLCKSZ) % XLogSegSize);
|
|
|
|
memcpy((char *) Insert->currpage, readBuf, XLOG_BLCKSZ);
|
2003-05-22 16:39:28 +02:00
|
|
|
Insert->currpos = (char *) Insert->currpage +
|
2006-04-04 01:35:05 +02:00
|
|
|
(EndOfLog.xrecoff + XLOG_BLCKSZ - XLogCtl->xlblocks[0].xrecoff);
|
2000-10-28 18:21:00 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
LogwrtResult.Write = LogwrtResult.Flush = EndOfLog;
|
2000-10-28 18:21:00 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLogCtl->Write.LogwrtResult = LogwrtResult;
|
|
|
|
Insert->LogwrtResult = LogwrtResult;
|
|
|
|
XLogCtl->LogwrtResult = LogwrtResult;
|
2000-10-28 18:21:00 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
XLogCtl->LogwrtRqst.Write = EndOfLog;
|
|
|
|
XLogCtl->LogwrtRqst.Flush = EndOfLog;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2003-07-17 18:45:04 +02:00
|
|
|
freespace = INSERT_FREESPACE(Insert);
|
|
|
|
if (freespace > 0)
|
|
|
|
{
|
|
|
|
/* Make sure rest of page is zero */
|
|
|
|
MemSet(Insert->currpos, 0, freespace);
|
|
|
|
XLogCtl->Write.curridx = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Whenever Write.LogwrtResult points to exactly the end of a page,
|
|
|
|
* Write.curridx must point to the *next* page (see XLogWrite()).
|
2003-07-17 18:45:04 +02:00
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Note: it might seem we should do AdvanceXLInsertBuffer() here, but
|
2004-08-29 07:07:03 +02:00
|
|
|
* this is sufficient. The first actual attempt to insert a log
|
2004-07-22 00:31:26 +02:00
|
|
|
* record will advance the insert state.
|
2003-07-17 18:45:04 +02:00
|
|
|
*/
|
|
|
|
XLogCtl->Write.curridx = NextBufIdx(0);
|
|
|
|
}
|
|
|
|
|
2005-06-18 00:32:51 +02:00
|
|
|
/* Pre-scan prepared transactions to find out the range of XIDs present */
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
oldestActiveXID = PrescanPreparedTransactions(NULL, NULL);
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2000-10-28 18:21:00 +02:00
|
|
|
if (InRecovery)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2003-08-04 02:43:34 +02:00
|
|
|
int rmid;
|
2003-02-21 01:06:22 +01:00
|
|
|
|
2009-08-07 21:29:49 +02:00
|
|
|
/*
|
|
|
|
* Resource managers might need to write WAL records, eg, to record
|
|
|
|
* index cleanup actions. So temporarily enable XLogInsertAllowed in
|
|
|
|
* this process only.
|
|
|
|
*/
|
|
|
|
LocalSetXLogInsertAllowed();
|
|
|
|
|
2003-02-21 01:06:22 +01:00
|
|
|
/*
|
|
|
|
* Allow resource managers to do any required cleanup.
|
|
|
|
*/
|
|
|
|
for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
|
|
|
|
{
|
|
|
|
if (RmgrTable[rmid].rm_cleanup != NULL)
|
|
|
|
RmgrTable[rmid].rm_cleanup();
|
|
|
|
}
|
|
|
|
|
2009-08-07 21:29:49 +02:00
|
|
|
/* Disallow XLogInsert again */
|
|
|
|
LocalXLogInsertAllowed = -1;
|
|
|
|
|
2006-04-14 22:27:24 +02:00
|
|
|
/*
|
|
|
|
* Check to see if the XLOG sequence contained any unresolved
|
|
|
|
* references to uninitialized pages.
|
|
|
|
*/
|
|
|
|
XLogCheckInvalidPages();
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2006-08-07 18:57:57 +02:00
|
|
|
* Perform a checkpoint to update all our recovery activity to disk.
|
2003-02-21 01:06:22 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* Note that we write a shutdown checkpoint rather than an on-line
|
|
|
|
* one. This is not particularly critical, but since we may be
|
|
|
|
* assigning a new TLI, using a shutdown checkpoint allows us to have
|
|
|
|
* the rule that TLI only changes in shutdown checkpoints, which
|
|
|
|
* allows some extra error checking in xlog_redo.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2009-06-25 23:36:00 +02:00
|
|
|
if (bgwriterLaunched)
|
|
|
|
RequestCheckpoint(CHECKPOINT_END_OF_RECOVERY |
|
|
|
|
CHECKPOINT_IMMEDIATE |
|
|
|
|
CHECKPOINT_WAIT);
|
|
|
|
else
|
|
|
|
CreateCheckPoint(CHECKPOINT_END_OF_RECOVERY | CHECKPOINT_IMMEDIATE);
|
2009-05-14 22:31:09 +02:00
|
|
|
|
2009-05-14 23:28:35 +02:00
|
|
|
/*
|
|
|
|
* And finally, execute the recovery_end_command, if any.
|
|
|
|
*/
|
2009-05-14 22:31:09 +02:00
|
|
|
if (recoveryEndCommand)
|
2010-03-18 10:17:18 +01:00
|
|
|
ExecuteRecoveryCommand(recoveryEndCommand,
|
|
|
|
"recovery_end_command",
|
|
|
|
true);
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
2001-01-09 07:24:33 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Preallocate additional log files, if wanted.
|
|
|
|
*/
|
2007-06-30 21:12:02 +02:00
|
|
|
PreallocXlogFiles(EndOfLog);
|
2001-01-09 07:24:33 +01:00
|
|
|
|
2003-05-22 16:39:28 +02:00
|
|
|
/*
|
|
|
|
* Okay, we're officially UP.
|
|
|
|
*/
|
2000-10-28 18:21:00 +02:00
|
|
|
InRecovery = false;
|
1999-10-06 23:58:18 +02:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
1999-10-06 23:58:18 +02:00
|
|
|
ControlFile->state = DB_IN_PRODUCTION;
|
2008-02-17 03:09:32 +01:00
|
|
|
ControlFile->time = (pg_time_t) time(NULL);
|
1999-10-06 23:58:18 +02:00
|
|
|
UpdateControlFile();
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockRelease(ControlFileLock);
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
/* start the archive_timeout timer running */
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
XLogCtl->Write.lastSegSwitchTime = (pg_time_t) time(NULL);
|
2006-08-18 01:04:10 +02:00
|
|
|
|
2006-08-21 18:16:31 +02:00
|
|
|
/* initialize shared-memory copy of latest checkpoint XID/epoch */
|
|
|
|
XLogCtl->ckptXidEpoch = ControlFile->checkPointCopy.nextXidEpoch;
|
|
|
|
XLogCtl->ckptXid = ControlFile->checkPointCopy.nextXid;
|
|
|
|
|
2007-09-08 22:31:15 +02:00
|
|
|
/* also initialize latestCompletedXid, to nextXid - 1 */
|
|
|
|
ShmemVariableCache->latestCompletedXid = ShmemVariableCache->nextXid;
|
|
|
|
TransactionIdRetreat(ShmemVariableCache->latestCompletedXid);
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* Start up the commit log and related stuff, too. In hot standby mode we
|
|
|
|
* did this already before WAL replay.
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
*/
|
|
|
|
if (standbyState == STANDBY_DISABLED)
|
|
|
|
{
|
|
|
|
StartupCLOG();
|
|
|
|
StartupSUBTRANS(oldestActiveXID);
|
|
|
|
StartupMultiXact();
|
|
|
|
}
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2005-06-18 00:32:51 +02:00
|
|
|
/* Reload shared-memory state for prepared transactions */
|
|
|
|
RecoverPreparedTransactions();
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
|
|
|
* Shutdown the recovery environment. This must occur after
|
|
|
|
* RecoverPreparedTransactions(), see notes for lock_twophase_recover()
|
|
|
|
*/
|
|
|
|
if (standbyState != STANDBY_DISABLED)
|
|
|
|
ShutdownRecoveryTransactionEnvironment();
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* Shut down readFile facility, free space */
|
|
|
|
if (readFile >= 0)
|
|
|
|
{
|
|
|
|
close(readFile);
|
|
|
|
readFile = -1;
|
|
|
|
}
|
|
|
|
if (readBuf)
|
|
|
|
{
|
|
|
|
free(readBuf);
|
|
|
|
readBuf = NULL;
|
|
|
|
}
|
2004-08-29 18:34:48 +02:00
|
|
|
if (readRecordBuf)
|
|
|
|
{
|
|
|
|
free(readRecordBuf);
|
|
|
|
readRecordBuf = NULL;
|
|
|
|
readRecordBufSize = 0;
|
|
|
|
}
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
/*
|
|
|
|
* If any of the critical GUCs have changed, log them before we allow
|
|
|
|
* backends to write WAL.
|
|
|
|
*/
|
|
|
|
LocalSetXLogInsertAllowed();
|
|
|
|
XLogReportParameters();
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* All done. Allow backends to write WAL. (Although the bool flag is
|
2009-06-26 22:29:04 +02:00
|
|
|
* probably atomic in itself, we use the info_lck here to ensure that
|
|
|
|
* there are no race conditions concerning visibility of other recent
|
|
|
|
* updates to shared memory.)
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
2009-06-26 22:29:04 +02:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->SharedRecoveryInProgress = false;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
}
|
|
|
|
|
2010-04-13 16:17:46 +02:00
|
|
|
/*
|
|
|
|
* Checks if recovery has reached a consistent state. When consistency is
|
|
|
|
* reached and we have a valid starting standby snapshot, tell postmaster
|
|
|
|
* that it can start accepting read-only connections.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
CheckRecoveryConsistency(void)
|
|
|
|
{
|
2010-07-06 21:19:02 +02:00
|
|
|
static bool backendsAllowed = false;
|
2010-04-13 16:17:46 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Have we passed our safe starting point?
|
|
|
|
*/
|
|
|
|
if (!reachedMinRecoveryPoint &&
|
|
|
|
XLByteLE(minRecoveryPoint, EndRecPtr) &&
|
|
|
|
XLogRecPtrIsInvalid(ControlFile->backupStartPoint))
|
|
|
|
{
|
|
|
|
reachedMinRecoveryPoint = true;
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("consistent recovery state reached at %X/%X",
|
|
|
|
EndRecPtr.xlogid, EndRecPtr.xrecoff)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Have we got a valid starting snapshot that will allow queries to be
|
|
|
|
* run? If so, we can tell postmaster that the database is consistent now,
|
|
|
|
* enabling connections.
|
2010-04-13 16:17:46 +02:00
|
|
|
*/
|
|
|
|
if (standbyState == STANDBY_SNAPSHOT_READY &&
|
|
|
|
!backendsAllowed &&
|
|
|
|
reachedMinRecoveryPoint &&
|
|
|
|
IsUnderPostmaster)
|
|
|
|
{
|
|
|
|
backendsAllowed = true;
|
2010-05-15 22:01:32 +02:00
|
|
|
SendPostmasterSignal(PMSIGNAL_BEGIN_HOT_STANDBY);
|
2010-04-13 16:17:46 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
|
|
|
* Is the system still in recovery?
|
|
|
|
*
|
2009-06-26 22:29:04 +02:00
|
|
|
* Unlike testing InRecovery, this works in any process that's connected to
|
|
|
|
* shared memory.
|
|
|
|
*
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* As a side-effect, we initialize the local TimeLineID and RedoRecPtr
|
|
|
|
* variables the first time we see that recovery is finished.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
RecoveryInProgress(void)
|
|
|
|
{
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* We check shared state each time only until we leave recovery mode. We
|
|
|
|
* can't re-enter recovery, so there's no need to keep checking after the
|
|
|
|
* shared variable has once been seen false.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
if (!LocalRecoveryInProgress)
|
|
|
|
return false;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
/* spinlock is essential on machines with weak memory ordering! */
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LocalRecoveryInProgress = xlogctl->SharedRecoveryInProgress;
|
2009-06-26 22:29:04 +02:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
/*
|
2009-06-26 22:29:04 +02:00
|
|
|
* Initialize TimeLineID and RedoRecPtr when we discover that recovery
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
* is finished. InitPostgres() relies upon this behaviour to ensure
|
2010-02-26 03:01:40 +01:00
|
|
|
* that InitXLOGAccess() is called at backend startup. (If you change
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
* this, see also LocalSetXLogInsertAllowed.)
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
if (!LocalRecoveryInProgress)
|
|
|
|
InitXLOGAccess();
|
|
|
|
|
|
|
|
return LocalRecoveryInProgress;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
/*
|
|
|
|
* Is this process allowed to insert new WAL records?
|
|
|
|
*
|
|
|
|
* Ordinarily this is essentially equivalent to !RecoveryInProgress().
|
|
|
|
* But we also have provisions for forcing the result "true" or "false"
|
|
|
|
* within specific processes regardless of the global state.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
XLogInsertAllowed(void)
|
|
|
|
{
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* If value is "unconditionally true" or "unconditionally false", just
|
|
|
|
* return it. This provides the normal fast path once recovery is known
|
|
|
|
* done.
|
2009-06-26 22:29:04 +02:00
|
|
|
*/
|
|
|
|
if (LocalXLogInsertAllowed >= 0)
|
|
|
|
return (bool) LocalXLogInsertAllowed;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Else, must check to see if we're still in recovery.
|
|
|
|
*/
|
|
|
|
if (RecoveryInProgress())
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* On exit from recovery, reset to "unconditionally true", since there is
|
|
|
|
* no need to keep checking.
|
2009-06-26 22:29:04 +02:00
|
|
|
*/
|
|
|
|
LocalXLogInsertAllowed = 1;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make XLogInsertAllowed() return true in the current process only.
|
2009-08-08 18:39:17 +02:00
|
|
|
*
|
|
|
|
* Note: it is allowed to switch LocalXLogInsertAllowed back to -1 later,
|
|
|
|
* and even call LocalSetXLogInsertAllowed() again after that.
|
2009-06-26 22:29:04 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
LocalSetXLogInsertAllowed(void)
|
|
|
|
{
|
|
|
|
Assert(LocalXLogInsertAllowed == -1);
|
|
|
|
LocalXLogInsertAllowed = 1;
|
|
|
|
|
|
|
|
/* Initialize as RecoveryInProgress() would do when switching state */
|
|
|
|
InitXLOGAccess();
|
|
|
|
}
|
|
|
|
|
2001-06-03 16:53:56 +02:00
|
|
|
/*
|
|
|
|
* Subroutine to try to fetch and validate a prior checkpoint record.
|
2004-08-04 18:25:02 +02:00
|
|
|
*
|
|
|
|
* whichChkpt identifies the checkpoint (merely for reporting purposes).
|
|
|
|
* 1 for "primary", 2 for "secondary", 0 for "other" (backup_label)
|
2001-06-03 16:53:56 +02:00
|
|
|
*/
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
static XLogRecord *
|
2004-08-29 18:34:48 +02:00
|
|
|
ReadCheckpointRecord(XLogRecPtr RecPtr, int whichChkpt)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
|
|
|
XLogRecord *record;
|
|
|
|
|
|
|
|
if (!XRecOffIsValid(RecPtr.xrecoff))
|
|
|
|
{
|
2004-08-04 18:25:02 +02:00
|
|
|
switch (whichChkpt)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("invalid primary checkpoint link in control file")));
|
2004-08-04 18:25:02 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid secondary checkpoint link in control file")));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("invalid checkpoint link in backup_label file")));
|
2004-08-04 18:25:02 +02:00
|
|
|
break;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
record = ReadRecord(&RecPtr, LOG, true);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
if (record == NULL)
|
|
|
|
{
|
2004-08-04 18:25:02 +02:00
|
|
|
switch (whichChkpt)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid primary checkpoint record")));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid secondary checkpoint record")));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid checkpoint record")));
|
|
|
|
break;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (record->xl_rmid != RM_XLOG_ID)
|
|
|
|
{
|
2004-08-04 18:25:02 +02:00
|
|
|
switch (whichChkpt)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid resource manager ID in primary checkpoint record")));
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid resource manager ID in secondary checkpoint record")));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("invalid resource manager ID in checkpoint record")));
|
2004-08-04 18:25:02 +02:00
|
|
|
break;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (record->xl_info != XLOG_CHECKPOINT_SHUTDOWN &&
|
|
|
|
record->xl_info != XLOG_CHECKPOINT_ONLINE)
|
|
|
|
{
|
2004-08-04 18:25:02 +02:00
|
|
|
switch (whichChkpt)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("invalid xl_info in primary checkpoint record")));
|
2004-08-04 18:25:02 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("invalid xl_info in secondary checkpoint record")));
|
2004-08-04 18:25:02 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid xl_info in checkpoint record")));
|
|
|
|
break;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-06-02 07:55:29 +02:00
|
|
|
if (record->xl_len != sizeof(CheckPoint) ||
|
|
|
|
record->xl_tot_len != SizeOfXLogRecord + sizeof(CheckPoint))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2004-08-04 18:25:02 +02:00
|
|
|
switch (whichChkpt)
|
|
|
|
{
|
|
|
|
case 1:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("invalid length of primary checkpoint record")));
|
2004-08-04 18:25:02 +02:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
ereport(LOG,
|
2005-10-15 04:49:52 +02:00
|
|
|
(errmsg("invalid length of secondary checkpoint record")));
|
2004-08-04 18:25:02 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("invalid length of checkpoint record")));
|
|
|
|
break;
|
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return record;
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
/*
|
2004-05-27 19:12:57 +02:00
|
|
|
* This must be called during startup of a backend process, except that
|
|
|
|
* it need not be called in a standalone backend (which does StartupXLOG
|
2004-07-22 00:31:26 +02:00
|
|
|
* instead). We need to initialize the local copies of ThisTimeLineID and
|
2004-05-27 19:12:57 +02:00
|
|
|
* RedoRecPtr.
|
|
|
|
*
|
2004-08-04 23:34:35 +02:00
|
|
|
* Note: before Postgres 8.0, we went to some effort to keep the postmaster
|
2004-07-22 00:31:26 +02:00
|
|
|
* process's copies of ThisTimeLineID and RedoRecPtr valid too. This was
|
2004-05-27 19:12:57 +02:00
|
|
|
* unnecessary however, since the postmaster itself never touches XLOG anyway.
|
2000-10-21 17:43:36 +02:00
|
|
|
*/
|
|
|
|
void
|
2004-05-27 19:12:57 +02:00
|
|
|
InitXLOGAccess(void)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2004-07-22 00:31:26 +02:00
|
|
|
/* ThisTimeLineID doesn't change so we need no lock to copy it */
|
|
|
|
ThisTimeLineID = XLogCtl->ThisTimeLineID;
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
Assert(ThisTimeLineID != 0 || IsBootstrapProcessingMode());
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2004-05-27 19:12:57 +02:00
|
|
|
/* Use GetRedoRecPtr to copy the RedoRecPtr safely */
|
|
|
|
(void) GetRedoRecPtr();
|
2002-03-15 20:20:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Once spawned, a backend may update its local RedoRecPtr from
|
|
|
|
* XLogCtl->Insert.RedoRecPtr; it must hold the insert lock or info_lck
|
|
|
|
* to do so. This is done in XLogInsert() or GetRedoRecPtr().
|
|
|
|
*/
|
|
|
|
XLogRecPtr
|
2000-12-28 14:00:29 +01:00
|
|
|
GetRedoRecPtr(void)
|
|
|
|
{
|
2002-03-15 20:20:36 +01:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2002-03-15 20:20:36 +01:00
|
|
|
Assert(XLByteLE(RedoRecPtr, xlogctl->Insert.RedoRecPtr));
|
|
|
|
RedoRecPtr = xlogctl->Insert.RedoRecPtr;
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
2002-03-15 20:20:36 +01:00
|
|
|
|
|
|
|
return RedoRecPtr;
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
/*
|
|
|
|
* GetInsertRecPtr -- Returns the current insert position.
|
|
|
|
*
|
|
|
|
* NOTE: The value *actually* returned is the position of the last full
|
|
|
|
* xlog page. It lags behind the real insert position by at most 1 page.
|
|
|
|
* For that, we don't need to acquire WALInsertLock which can be quite
|
|
|
|
* heavily contended, and an approximation is enough for the current
|
|
|
|
* usage of this function.
|
|
|
|
*/
|
|
|
|
XLogRecPtr
|
|
|
|
GetInsertRecPtr(void)
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
2007-11-15 22:14:46 +01:00
|
|
|
XLogRecPtr recptr;
|
2007-06-28 02:02:40 +02:00
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
recptr = xlogctl->LogwrtRqst.Write;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
|
|
|
return recptr;
|
|
|
|
}
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
2010-06-17 18:41:25 +02:00
|
|
|
* GetFlushRecPtr -- Returns the current flush position, ie, the last WAL
|
|
|
|
* position known to be fsync'd to disk.
|
2010-01-15 10:19:10 +01:00
|
|
|
*/
|
|
|
|
XLogRecPtr
|
2010-06-17 18:41:25 +02:00
|
|
|
GetFlushRecPtr(void)
|
2010-01-15 10:19:10 +01:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2010-06-17 18:41:25 +02:00
|
|
|
recptr = xlogctl->LogwrtResult.Flush;
|
2010-01-15 10:19:10 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
|
|
|
return recptr;
|
|
|
|
}
|
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
/*
|
|
|
|
* Get the time of the last xlog segment switch
|
|
|
|
*/
|
2008-02-17 03:09:32 +01:00
|
|
|
pg_time_t
|
2006-08-18 01:04:10 +02:00
|
|
|
GetLastSegSwitchTime(void)
|
|
|
|
{
|
2008-02-17 03:09:32 +01:00
|
|
|
pg_time_t result;
|
2006-08-18 01:04:10 +02:00
|
|
|
|
|
|
|
/* Need WALWriteLock, but shared lock is sufficient */
|
|
|
|
LWLockAcquire(WALWriteLock, LW_SHARED);
|
|
|
|
result = XLogCtl->Write.lastSegSwitchTime;
|
|
|
|
LWLockRelease(WALWriteLock);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2006-08-21 18:16:31 +02:00
|
|
|
/*
|
|
|
|
* GetNextXidAndEpoch - get the current nextXid value and associated epoch
|
|
|
|
*
|
|
|
|
* This is exported for use by code that would like to have 64-bit XIDs.
|
|
|
|
* We don't really support such things, but all XIDs within the system
|
|
|
|
* can be presumed "close to" the result, and thus the epoch associated
|
|
|
|
* with them can be determined.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
GetNextXidAndEpoch(TransactionId *xid, uint32 *epoch)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
uint32 ckptXidEpoch;
|
|
|
|
TransactionId ckptXid;
|
|
|
|
TransactionId nextXid;
|
2006-08-21 18:16:31 +02:00
|
|
|
|
|
|
|
/* Must read checkpoint info first, else have race condition */
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
ckptXidEpoch = xlogctl->ckptXidEpoch;
|
|
|
|
ckptXid = xlogctl->ckptXid;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Now fetch current nextXid */
|
|
|
|
nextXid = ReadNewTransactionId();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nextXid is certainly logically later than ckptXid. So if it's
|
|
|
|
* numerically less, it must have wrapped into the next epoch.
|
|
|
|
*/
|
|
|
|
if (nextXid < ckptXid)
|
|
|
|
ckptXidEpoch++;
|
|
|
|
|
|
|
|
*xid = nextXid;
|
|
|
|
*epoch = ckptXidEpoch;
|
|
|
|
}
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* GetRecoveryTargetTLI - get the recovery target timeline ID
|
|
|
|
*/
|
|
|
|
TimeLineID
|
|
|
|
GetRecoveryTargetTLI(void)
|
|
|
|
{
|
|
|
|
/* RecoveryTargetTLI doesn't change so we need no lock to copy it */
|
|
|
|
return XLogCtl->RecoveryTargetTLI;
|
|
|
|
}
|
|
|
|
|
1999-10-06 23:58:18 +02:00
|
|
|
/*
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
* This must be called ONCE during postmaster or standalone-backend shutdown
|
1999-10-06 23:58:18 +02:00
|
|
|
*/
|
|
|
|
void
|
2003-12-12 19:45:10 +01:00
|
|
|
ShutdownXLOG(int code, Datum arg)
|
1999-10-06 23:58:18 +02:00
|
|
|
{
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("shutting down")));
|
1999-10-06 23:58:18 +02:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
CreateRestartPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE);
|
|
|
|
else
|
2009-05-28 13:02:16 +02:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If archiving is enabled, rotate the last XLOG file so that all the
|
|
|
|
* remaining records are archived (postmaster wakes up the archiver
|
|
|
|
* process one more time at the end of shutdown). The checkpoint
|
|
|
|
* record will go to the next XLOG file and won't be archived (yet).
|
|
|
|
*/
|
|
|
|
if (XLogArchivingActive() && XLogArchiveCommandSet())
|
|
|
|
RequestXLogSwitch();
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
CreateCheckPoint(CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_IMMEDIATE);
|
2009-05-28 13:02:16 +02:00
|
|
|
}
|
2001-08-25 20:52:43 +02:00
|
|
|
ShutdownCLOG();
|
2004-07-01 02:52:04 +02:00
|
|
|
ShutdownSUBTRANS();
|
2005-04-28 23:47:18 +02:00
|
|
|
ShutdownMultiXact();
|
1999-10-06 23:58:18 +02:00
|
|
|
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("database system is shut down")));
|
1999-10-06 23:58:18 +02:00
|
|
|
}
|
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
/*
|
2007-06-30 21:12:02 +02:00
|
|
|
* Log start of a checkpoint.
|
|
|
|
*/
|
|
|
|
static void
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LogCheckpointStart(int flags, bool restartpoint)
|
2007-06-30 21:12:02 +02:00
|
|
|
{
|
2009-06-26 22:29:04 +02:00
|
|
|
const char *msg;
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* XXX: This is hopelessly untranslatable. We could call gettext_noop for
|
|
|
|
* the main message, but what about all the flags?
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
if (restartpoint)
|
2009-06-25 23:36:00 +02:00
|
|
|
msg = "restartpoint starting:%s%s%s%s%s%s%s";
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
else
|
2009-06-25 23:36:00 +02:00
|
|
|
msg = "checkpoint starting:%s%s%s%s%s%s%s";
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
elog(LOG, msg,
|
2007-06-30 21:12:02 +02:00
|
|
|
(flags & CHECKPOINT_IS_SHUTDOWN) ? " shutdown" : "",
|
2009-06-25 23:36:00 +02:00
|
|
|
(flags & CHECKPOINT_END_OF_RECOVERY) ? " end-of-recovery" : "",
|
2007-06-30 21:12:02 +02:00
|
|
|
(flags & CHECKPOINT_IMMEDIATE) ? " immediate" : "",
|
|
|
|
(flags & CHECKPOINT_FORCE) ? " force" : "",
|
|
|
|
(flags & CHECKPOINT_WAIT) ? " wait" : "",
|
|
|
|
(flags & CHECKPOINT_CAUSE_XLOG) ? " xlog" : "",
|
|
|
|
(flags & CHECKPOINT_CAUSE_TIME) ? " time" : "");
|
|
|
|
}
|
|
|
|
|
2007-08-02 00:45:09 +02:00
|
|
|
/*
|
2007-06-30 21:12:02 +02:00
|
|
|
* Log end of a checkpoint.
|
|
|
|
*/
|
|
|
|
static void
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LogCheckpointEnd(bool restartpoint)
|
2007-06-30 21:12:02 +02:00
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
long write_secs,
|
|
|
|
sync_secs,
|
|
|
|
total_secs;
|
|
|
|
int write_usecs,
|
|
|
|
sync_usecs,
|
|
|
|
total_usecs;
|
2007-06-30 21:12:02 +02:00
|
|
|
|
|
|
|
CheckpointStats.ckpt_end_t = GetCurrentTimestamp();
|
|
|
|
|
|
|
|
TimestampDifference(CheckpointStats.ckpt_start_t,
|
|
|
|
CheckpointStats.ckpt_end_t,
|
|
|
|
&total_secs, &total_usecs);
|
|
|
|
|
|
|
|
TimestampDifference(CheckpointStats.ckpt_write_t,
|
|
|
|
CheckpointStats.ckpt_sync_t,
|
|
|
|
&write_secs, &write_usecs);
|
|
|
|
|
|
|
|
TimestampDifference(CheckpointStats.ckpt_sync_t,
|
|
|
|
CheckpointStats.ckpt_sync_end_t,
|
|
|
|
&sync_secs, &sync_usecs);
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
if (restartpoint)
|
|
|
|
elog(LOG, "restartpoint complete: wrote %d buffers (%.1f%%); "
|
|
|
|
"write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s",
|
|
|
|
CheckpointStats.ckpt_bufs_written,
|
|
|
|
(double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
|
|
|
|
write_secs, write_usecs / 1000,
|
|
|
|
sync_secs, sync_usecs / 1000,
|
|
|
|
total_secs, total_usecs / 1000);
|
|
|
|
else
|
|
|
|
elog(LOG, "checkpoint complete: wrote %d buffers (%.1f%%); "
|
|
|
|
"%d transaction log file(s) added, %d removed, %d recycled; "
|
|
|
|
"write=%ld.%03d s, sync=%ld.%03d s, total=%ld.%03d s",
|
|
|
|
CheckpointStats.ckpt_bufs_written,
|
|
|
|
(double) CheckpointStats.ckpt_bufs_written * 100 / NBuffers,
|
|
|
|
CheckpointStats.ckpt_segs_added,
|
|
|
|
CheckpointStats.ckpt_segs_removed,
|
|
|
|
CheckpointStats.ckpt_segs_recycled,
|
|
|
|
write_secs, write_usecs / 1000,
|
|
|
|
sync_secs, sync_usecs / 1000,
|
|
|
|
total_secs, total_usecs / 1000);
|
2007-06-30 21:12:02 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Perform a checkpoint --- either during shutdown, or on-the-fly
|
2002-09-27 00:58:34 +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.
|
2009-06-25 23:36:00 +02:00
|
|
|
* CHECKPOINT_END_OF_RECOVERY: checkpoint is for end of WAL recovery.
|
2007-06-28 02:02:40 +02:00
|
|
|
* CHECKPOINT_IMMEDIATE: finish the checkpoint ASAP,
|
2007-08-02 00:45:09 +02:00
|
|
|
* ignoring checkpoint_completion_target parameter.
|
2007-06-28 02:02:40 +02:00
|
|
|
* CHECKPOINT_FORCE: force a checkpoint even if no XLOG activity has occured
|
2009-06-26 22:29:04 +02:00
|
|
|
* since the last one (implied by CHECKPOINT_IS_SHUTDOWN or
|
2009-06-25 23:36:00 +02:00
|
|
|
* CHECKPOINT_END_OF_RECOVERY).
|
2007-06-28 02:02:40 +02:00
|
|
|
*
|
2007-06-30 21:12:02 +02:00
|
|
|
* Note: flags contains other bits, of interest here only for logging purposes.
|
2007-06-28 02:02:40 +02:00
|
|
|
* In particular note that this routine is synchronous and does not pay
|
|
|
|
* attention to CHECKPOINT_WAIT.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
1999-09-27 17:48:12 +02:00
|
|
|
void
|
2007-06-28 02:02:40 +02:00
|
|
|
CreateCheckPoint(int flags)
|
1999-09-27 17:48:12 +02:00
|
|
|
{
|
2009-06-25 23:36:00 +02:00
|
|
|
bool shutdown;
|
2000-04-12 19:17:23 +02:00
|
|
|
CheckPoint checkPoint;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
XLogCtlInsert *Insert = &XLogCtl->Insert;
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecData rdata;
|
2000-04-12 19:17:23 +02:00
|
|
|
uint32 freespace;
|
2000-11-05 23:50:21 +01:00
|
|
|
uint32 _logId;
|
|
|
|
uint32 _logSeg;
|
2007-04-03 18:34:36 +02:00
|
|
|
TransactionId *inCommitXids;
|
|
|
|
int nInCommit;
|
2000-11-05 23:50:21 +01:00
|
|
|
|
2009-06-25 23:36:00 +02:00
|
|
|
/*
|
|
|
|
* An end-of-recovery checkpoint is really a shutdown checkpoint, just
|
|
|
|
* issued at a different time.
|
|
|
|
*/
|
2009-06-26 22:29:04 +02:00
|
|
|
if (flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY))
|
2009-06-25 23:36:00 +02:00
|
|
|
shutdown = true;
|
|
|
|
else
|
|
|
|
shutdown = false;
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
/* sanity check */
|
|
|
|
if (RecoveryInProgress() && (flags & CHECKPOINT_END_OF_RECOVERY) == 0)
|
|
|
|
elog(ERROR, "can't create a checkpoint during recovery");
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2009-06-25 23:36:00 +02:00
|
|
|
/*
|
|
|
|
* Acquire CheckpointLock to ensure only one checkpoint happens at a time.
|
|
|
|
* (This is just pro forma, since in the present system structure there is
|
|
|
|
* only one process that is allowed to issue checkpoints at any given
|
|
|
|
* time.)
|
|
|
|
*/
|
|
|
|
LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
|
|
|
|
|
2007-06-30 21:12:02 +02:00
|
|
|
/*
|
|
|
|
* Prepare to accumulate statistics.
|
|
|
|
*
|
|
|
|
* Note: because it is possible for log_checkpoints to change while a
|
|
|
|
* checkpoint proceeds, we always accumulate stats, even if
|
|
|
|
* log_checkpoints is currently off.
|
|
|
|
*/
|
|
|
|
MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
|
|
|
|
CheckpointStats.ckpt_start_t = GetCurrentTimestamp();
|
|
|
|
|
2003-05-10 20:01:31 +02:00
|
|
|
/*
|
|
|
|
* Use a critical section to force system panic if we have trouble.
|
|
|
|
*/
|
2001-09-29 06:02:27 +02:00
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
1999-09-27 17:48:12 +02:00
|
|
|
if (shutdown)
|
|
|
|
{
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
1999-09-27 17:48:12 +02:00
|
|
|
ControlFile->state = DB_SHUTDOWNING;
|
2008-02-17 03:09:32 +01:00
|
|
|
ControlFile->time = (pg_time_t) time(NULL);
|
1999-09-27 17:48:12 +02:00
|
|
|
UpdateControlFile();
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockRelease(ControlFileLock);
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2007-11-15 21:36:40 +01:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* Let smgr prepare for checkpoint; this has to happen before we determine
|
|
|
|
* the REDO pointer. Note that smgr must not do anything that'd have to
|
|
|
|
* be undone if we decide no checkpoint is needed.
|
2007-11-15 21:36:40 +01:00
|
|
|
*/
|
|
|
|
smgrpreckpt();
|
|
|
|
|
|
|
|
/* Begin filling in the checkpoint WAL record */
|
2003-05-22 16:39:28 +02:00
|
|
|
MemSet(&checkPoint, 0, sizeof(checkPoint));
|
2008-02-17 03:09:32 +01:00
|
|
|
checkPoint.time = (pg_time_t) time(NULL);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2004-08-11 06:07:16 +02:00
|
|
|
/*
|
2007-04-03 18:34:36 +02:00
|
|
|
* We must hold WALInsertLock while examining insert state to determine
|
|
|
|
* the checkpoint REDO pointer.
|
2004-08-11 06:07:16 +02:00
|
|
|
*/
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* If this isn't a shutdown or forced checkpoint, and we have not inserted
|
|
|
|
* any XLOG records since the start of the last checkpoint, skip the
|
|
|
|
* checkpoint. The idea here is to avoid inserting duplicate checkpoints
|
|
|
|
* when the system is idle. That wastes log space, and more importantly it
|
|
|
|
* exposes us to possible loss of both current and previous checkpoint
|
|
|
|
* records if the machine crashes just as we're writing the update.
|
|
|
|
* (Perhaps it'd make even more sense to checkpoint only when the previous
|
|
|
|
* checkpoint record is in a different xlog page?)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* We have to make two tests to determine that nothing has happened since
|
|
|
|
* the start of the last checkpoint: current insertion point must match
|
|
|
|
* the end of the last checkpoint record, and its redo pointer must point
|
|
|
|
* to itself.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2009-06-25 23:36:00 +02:00
|
|
|
if ((flags & (CHECKPOINT_IS_SHUTDOWN | CHECKPOINT_END_OF_RECOVERY |
|
|
|
|
CHECKPOINT_FORCE)) == 0)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
|
|
|
XLogRecPtr curInsert;
|
|
|
|
|
|
|
|
INSERT_RECPTR(curInsert, Insert, Insert->curridx);
|
|
|
|
if (curInsert.xlogid == ControlFile->checkPoint.xlogid &&
|
|
|
|
curInsert.xrecoff == ControlFile->checkPoint.xrecoff +
|
|
|
|
MAXALIGN(SizeOfXLogRecord + sizeof(CheckPoint)) &&
|
|
|
|
ControlFile->checkPoint.xlogid ==
|
|
|
|
ControlFile->checkPointCopy.redo.xlogid &&
|
|
|
|
ControlFile->checkPoint.xrecoff ==
|
|
|
|
ControlFile->checkPointCopy.redo.xrecoff)
|
|
|
|
{
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
LWLockRelease(CheckpointLock);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
END_CRIT_SECTION();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-08-27 09:15:41 +02:00
|
|
|
/*
|
|
|
|
* An end-of-recovery checkpoint is created before anyone is allowed to
|
|
|
|
* write WAL. To allow us to write the checkpoint record, temporarily
|
|
|
|
* enable XLogInsertAllowed. (This also ensures ThisTimeLineID is
|
|
|
|
* initialized, which we need here and in AdvanceXLInsertBuffer.)
|
|
|
|
*/
|
|
|
|
if (flags & CHECKPOINT_END_OF_RECOVERY)
|
|
|
|
LocalSetXLogInsertAllowed();
|
|
|
|
|
|
|
|
checkPoint.ThisTimeLineID = ThisTimeLineID;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Compute new REDO record ptr = location of next XLOG record.
|
|
|
|
*
|
2005-10-15 04:49:52 +02:00
|
|
|
* NB: this is NOT necessarily where the checkpoint record itself will be,
|
|
|
|
* since other backends may insert more XLOG records while we're off doing
|
|
|
|
* the buffer flush work. Those XLOG records are logically after the
|
|
|
|
* checkpoint, even though physically before it. Got that?
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
freespace = INSERT_FREESPACE(Insert);
|
1999-09-27 17:48:12 +02:00
|
|
|
if (freespace < SizeOfXLogRecord)
|
|
|
|
{
|
2006-08-06 05:53:44 +02:00
|
|
|
(void) AdvanceXLInsertBuffer(false);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/* OK to ignore update return flag, since we will do flush anyway */
|
2004-02-11 23:55:26 +01:00
|
|
|
freespace = INSERT_FREESPACE(Insert);
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
INSERT_RECPTR(checkPoint.redo, Insert, Insert->curridx);
|
2001-03-22 05:01:46 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Here we update the shared RedoRecPtr for future XLogInsert calls; this
|
|
|
|
* must be done while holding the insert lock AND the info_lck.
|
2003-05-10 20:01:31 +02:00
|
|
|
*
|
2003-08-04 02:43:34 +02:00
|
|
|
* Note: if we fail to complete the checkpoint, RedoRecPtr will be left
|
2005-10-15 04:49:52 +02:00
|
|
|
* pointing past where it really needs to point. This is okay; the only
|
|
|
|
* consequence is that XLogInsert might back up whole buffers that it
|
|
|
|
* didn't really need to. We can't postpone advancing RedoRecPtr because
|
|
|
|
* XLogInserts that happen while we are dumping buffers must assume that
|
|
|
|
* their buffer changes are not included in the checkpoint.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2002-03-15 20:20:36 +01:00
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
2002-03-15 20:20:36 +01:00
|
|
|
RedoRecPtr = xlogctl->Insert.RedoRecPtr = checkPoint.redo;
|
2005-12-29 19:08:05 +01:00
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
2002-03-15 20:20:36 +01:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2007-04-03 18:34:36 +02:00
|
|
|
* Now we can release WAL insert lock, allowing other xacts to proceed
|
|
|
|
* while we are flushing disk buffers.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(WALInsertLock);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2007-06-30 21:12:02 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01:00
|
|
|
* If enabled, log checkpoint start. We postpone this until now so as not
|
|
|
|
* to log anything if we decided to skip the checkpoint.
|
2007-06-30 21:12:02 +02:00
|
|
|
*/
|
|
|
|
if (log_checkpoints)
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LogCheckpointStart(flags, false);
|
2006-08-07 18:57:57 +02:00
|
|
|
|
2008-12-17 02:39:04 +01:00
|
|
|
TRACE_POSTGRESQL_CHECKPOINT_START(flags);
|
|
|
|
|
2007-04-03 18:34:36 +02:00
|
|
|
/*
|
|
|
|
* Before flushing data, we must wait for any transactions that are
|
|
|
|
* currently in their commit critical sections. If an xact inserted its
|
|
|
|
* commit record into XLOG just before the REDO point, then a crash
|
|
|
|
* restart from the REDO point would not replay that record, which means
|
|
|
|
* that our flushing had better include the xact's update of pg_clog. So
|
|
|
|
* we wait till he's out of his commit critical section before proceeding.
|
|
|
|
* See notes in RecordTransactionCommit().
|
|
|
|
*
|
|
|
|
* Because we've already released WALInsertLock, this test is a bit fuzzy:
|
|
|
|
* it is possible that we will wait for xacts we didn't really need to
|
|
|
|
* wait for. But the delay should be short and it seems better to make
|
|
|
|
* checkpoint take a bit longer than to hold locks longer than necessary.
|
|
|
|
* (In fact, the whole reason we have this issue is that xact.c does
|
|
|
|
* commit record XLOG insertion and clog update as two separate steps
|
2007-11-15 22:14:46 +01:00
|
|
|
* protected by different locks, but again that seems best on grounds of
|
|
|
|
* minimizing lock contention.)
|
2007-04-03 18:34:36 +02:00
|
|
|
*
|
2007-11-15 22:14:46 +01:00
|
|
|
* A transaction that has not yet set inCommit when we look cannot be at
|
|
|
|
* risk, since he's not inserted his commit record yet; and one that's
|
2007-04-03 18:34:36 +02:00
|
|
|
* already cleared it is not at risk either, since he's done fixing clog
|
|
|
|
* and we will correctly flush the update below. So we cannot miss any
|
|
|
|
* xacts we need to wait for.
|
|
|
|
*/
|
|
|
|
nInCommit = GetTransactionsInCommit(&inCommitXids);
|
|
|
|
if (nInCommit > 0)
|
|
|
|
{
|
2007-11-15 22:14:46 +01:00
|
|
|
do
|
|
|
|
{
|
|
|
|
pg_usleep(10000L); /* wait for 10 msec */
|
2007-04-03 18:34:36 +02:00
|
|
|
} while (HaveTransactionsInCommit(inCommitXids, nInCommit));
|
|
|
|
}
|
|
|
|
pfree(inCommitXids);
|
|
|
|
|
2003-05-10 20:01:31 +02:00
|
|
|
/*
|
|
|
|
* Get the other info we need for the checkpoint record.
|
|
|
|
*/
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockAcquire(XidGenLock, LW_SHARED);
|
1999-09-27 17:48:12 +02:00
|
|
|
checkPoint.nextXid = ShmemVariableCache->nextXid;
|
2009-08-31 04:23:23 +02:00
|
|
|
checkPoint.oldestXid = ShmemVariableCache->oldestXid;
|
|
|
|
checkPoint.oldestXidDB = ShmemVariableCache->oldestXidDB;
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(XidGenLock);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2006-08-21 18:16:31 +02:00
|
|
|
/* Increase XID epoch if we've wrapped around since last checkpoint */
|
|
|
|
checkPoint.nextXidEpoch = ControlFile->checkPointCopy.nextXidEpoch;
|
|
|
|
if (checkPoint.nextXid < ControlFile->checkPointCopy.nextXid)
|
|
|
|
checkPoint.nextXidEpoch++;
|
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockAcquire(OidGenLock, LW_SHARED);
|
1999-09-27 17:48:12 +02:00
|
|
|
checkPoint.nextOid = ShmemVariableCache->nextOid;
|
2000-11-03 12:39:36 +01:00
|
|
|
if (!shutdown)
|
|
|
|
checkPoint.nextOid += ShmemVariableCache->oidCount;
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(OidGenLock);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2005-06-08 17:50:28 +02:00
|
|
|
MultiXactGetCheckptMulti(shutdown,
|
|
|
|
&checkPoint.nextMulti,
|
|
|
|
&checkPoint.nextMultiOffset);
|
2005-04-28 23:47:18 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Having constructed the checkpoint record, ensure all shmem disk buffers
|
|
|
|
* and commit-log buffers are flushed to disk.
|
2003-05-10 20:01:31 +02:00
|
|
|
*
|
2005-11-22 19:17:34 +01:00
|
|
|
* This I/O could fail for various reasons. If so, we will fail to
|
|
|
|
* complete the checkpoint, but there is no reason to force a system
|
2006-11-30 19:29:12 +01:00
|
|
|
* panic. Accordingly, exit critical section while doing it.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2003-05-10 20:01:31 +02:00
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
2007-06-28 02:02:40 +02:00
|
|
|
CheckPointGuts(checkPoint.redo, flags);
|
2001-08-25 20:52:43 +02:00
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* Take a snapshot of running transactions and write this to WAL. This
|
|
|
|
* allows us to reconstruct the state of running transactions during
|
|
|
|
* archive recovery, if required. Skip, if this info disabled.
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
*
|
|
|
|
* If we are shutting down, or Startup process is completing crash
|
|
|
|
* recovery we don't need to write running xact data.
|
|
|
|
*
|
|
|
|
* Update checkPoint.nextXid since we have a later value
|
|
|
|
*/
|
|
|
|
if (!shutdown && XLogStandbyInfoActive())
|
2010-02-26 03:01:40 +01:00
|
|
|
LogStandbySnapshot(&checkPoint.oldestActiveXid, &checkPoint.nextXid);
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
else
|
|
|
|
checkPoint.oldestActiveXid = InvalidTransactionId;
|
|
|
|
|
2003-05-10 20:01:31 +02:00
|
|
|
START_CRIT_SECTION();
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Now insert the checkpoint record into XLOG.
|
|
|
|
*/
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata.data = (char *) (&checkPoint);
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata.len = sizeof(checkPoint);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata.buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata.next = NULL;
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
recptr = XLogInsert(RM_XLOG_ID,
|
|
|
|
shutdown ? XLOG_CHECKPOINT_SHUTDOWN :
|
|
|
|
XLOG_CHECKPOINT_ONLINE,
|
|
|
|
&rdata);
|
|
|
|
|
|
|
|
XLogFlush(recptr);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* We mustn't write any new WAL after a shutdown checkpoint, or it will be
|
|
|
|
* overwritten at next startup. No-one should even try, this just allows
|
|
|
|
* sanity-checking. In the case of an end-of-recovery checkpoint, we want
|
|
|
|
* to just temporarily disable writing until the system has exited
|
2009-06-26 22:29:04 +02:00
|
|
|
* recovery.
|
|
|
|
*/
|
|
|
|
if (shutdown)
|
|
|
|
{
|
|
|
|
if (flags & CHECKPOINT_END_OF_RECOVERY)
|
2010-02-26 03:01:40 +01:00
|
|
|
LocalXLogInsertAllowed = -1; /* return to "check" state */
|
2009-06-26 22:29:04 +02:00
|
|
|
else
|
2010-02-26 03:01:40 +01:00
|
|
|
LocalXLogInsertAllowed = 0; /* never again write WAL */
|
2009-06-26 22:29:04 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We now have ProcLastRecPtr = start of actual checkpoint record, recptr
|
|
|
|
* = end of actual checkpoint record.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
if (shutdown && !XLByteEQ(checkPoint.redo, ProcLastRecPtr))
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errmsg("concurrent transaction log activity while database system is shutting down")));
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2001-06-06 19:07:46 +02:00
|
|
|
* Select point at which we can truncate the log, which we base on the
|
|
|
|
* prior checkpoint's earliest info.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2005-06-18 00:32:51 +02:00
|
|
|
XLByteToSeg(ControlFile->checkPointCopy.redo, _logId, _logSeg);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Update the control file.
|
|
|
|
*/
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
1999-09-27 17:48:12 +02:00
|
|
|
if (shutdown)
|
|
|
|
ControlFile->state = DB_SHUTDOWNED;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ControlFile->prevCheckPoint = ControlFile->checkPoint;
|
|
|
|
ControlFile->checkPoint = ProcLastRecPtr;
|
|
|
|
ControlFile->checkPointCopy = checkPoint;
|
2008-02-17 03:09:32 +01:00
|
|
|
ControlFile->time = (pg_time_t) time(NULL);
|
2009-12-30 09:37:21 +01:00
|
|
|
/* crash recovery should always recover to the end of WAL */
|
|
|
|
MemSet(&ControlFile->minRecoveryPoint, 0, sizeof(XLogRecPtr));
|
1999-09-27 17:48:12 +02:00
|
|
|
UpdateControlFile();
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(ControlFileLock);
|
1999-09-27 17:48:12 +02:00
|
|
|
|
2006-08-21 18:16:31 +02:00
|
|
|
/* Update shared-memory copy of checkpoint XID/epoch */
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->ckptXidEpoch = checkPoint.nextXidEpoch;
|
|
|
|
xlogctl->ckptXid = checkPoint.nextXid;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
2003-05-10 20:01:31 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* We are now done with critical updates; no need for system panic if we
|
2007-06-28 02:02:40 +02:00
|
|
|
* have trouble while fooling with old log segments.
|
2003-05-10 20:01:31 +02:00
|
|
|
*/
|
|
|
|
END_CRIT_SECTION();
|
|
|
|
|
2007-11-15 21:36:40 +01:00
|
|
|
/*
|
|
|
|
* Let smgr do post-checkpoint cleanup (eg, deleting old files).
|
|
|
|
*/
|
|
|
|
smgrpostckpt();
|
|
|
|
|
2000-11-05 23:50:21 +01:00
|
|
|
/*
|
2010-04-12 11:52:29 +02:00
|
|
|
* Delete old log files (those no longer needed even for previous
|
|
|
|
* checkpoint or the standbys in XLOG streaming).
|
2010-01-15 10:19:10 +01:00
|
|
|
*/
|
2010-04-12 11:52:29 +02:00
|
|
|
if (_logId || _logSeg)
|
2010-01-15 10:19:10 +01:00
|
|
|
{
|
2010-04-12 11:52:29 +02:00
|
|
|
/*
|
|
|
|
* Calculate the last segment that we need to retain because of
|
2010-07-06 21:19:02 +02:00
|
|
|
* wal_keep_segments, by subtracting wal_keep_segments from the new
|
|
|
|
* checkpoint location.
|
2010-04-12 11:52:29 +02:00
|
|
|
*/
|
2010-04-20 13:15:06 +02:00
|
|
|
if (wal_keep_segments > 0)
|
2010-01-15 10:19:10 +01:00
|
|
|
{
|
2010-04-12 11:52:29 +02:00
|
|
|
uint32 log;
|
|
|
|
uint32 seg;
|
|
|
|
int d_log;
|
|
|
|
int d_seg;
|
|
|
|
|
|
|
|
XLByteToSeg(recptr, log, seg);
|
|
|
|
|
2010-04-20 13:15:06 +02:00
|
|
|
d_seg = wal_keep_segments % XLogSegsPerFile;
|
|
|
|
d_log = wal_keep_segments / XLogSegsPerFile;
|
2010-04-12 11:52:29 +02:00
|
|
|
if (seg < d_seg)
|
|
|
|
{
|
|
|
|
d_log += 1;
|
|
|
|
seg = seg - d_seg + XLogSegsPerFile;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
seg = seg - d_seg;
|
|
|
|
/* avoid underflow, don't go below (0,1) */
|
|
|
|
if (log < d_log || (log == d_log && seg == 0))
|
|
|
|
{
|
|
|
|
log = 0;
|
|
|
|
seg = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log = log - d_log;
|
|
|
|
|
|
|
|
/* don't delete WAL segments newer than the calculated segment */
|
2010-01-15 10:19:10 +01:00
|
|
|
if (log < _logId || (log == _logId && seg < _logSeg))
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
_logId = log;
|
|
|
|
_logSeg = seg;
|
2010-01-15 10:19:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
PrevLogSeg(_logId, _logSeg);
|
2007-06-30 21:12:02 +02:00
|
|
|
RemoveOldXlogFiles(_logId, _logSeg, recptr);
|
2000-11-05 23:50:21 +01:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
2007-06-28 02:02:40 +02:00
|
|
|
* Make more log segments if needed. (Do this after recycling old log
|
|
|
|
* segments, since that may supply some of the needed files.)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
|
|
|
if (!shutdown)
|
2007-06-30 21:12:02 +02:00
|
|
|
PreallocXlogFiles(recptr);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
|
2004-08-24 01:22:45 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Truncate pg_subtrans if possible. We can throw away all data before
|
|
|
|
* the oldest XMIN of any running transaction. No future transaction will
|
|
|
|
* attempt to reference any pg_subtrans entry older than that (see Asserts
|
|
|
|
* in subtrans.c). During recovery, though, we mustn't do this because
|
|
|
|
* StartupSUBTRANS hasn't been called yet.
|
2004-08-24 01:22:45 +02:00
|
|
|
*/
|
2009-06-26 22:29:04 +02:00
|
|
|
if (!RecoveryInProgress())
|
2006-07-30 04:07:18 +02:00
|
|
|
TruncateSUBTRANS(GetOldestXmin(true, false));
|
2004-08-24 01:22:45 +02:00
|
|
|
|
2007-06-30 21:12:02 +02:00
|
|
|
/* All real work is done, but log before releasing lock. */
|
|
|
|
if (log_checkpoints)
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LogCheckpointEnd(false);
|
2004-10-29 02:16:08 +02:00
|
|
|
|
2009-03-12 00:19:25 +01:00
|
|
|
TRACE_POSTGRESQL_CHECKPOINT_DONE(CheckpointStats.ckpt_bufs_written,
|
|
|
|
NBuffers,
|
|
|
|
CheckpointStats.ckpt_segs_added,
|
|
|
|
CheckpointStats.ckpt_segs_removed,
|
|
|
|
CheckpointStats.ckpt_segs_recycled);
|
2008-12-17 02:39:04 +01:00
|
|
|
|
2001-09-29 06:02:27 +02:00
|
|
|
LWLockRelease(CheckpointLock);
|
1999-09-27 17:48:12 +02:00
|
|
|
}
|
2000-10-21 17:43:36 +02:00
|
|
|
|
2006-08-07 18:57:57 +02:00
|
|
|
/*
|
|
|
|
* Flush all data in shared memory to disk, and fsync
|
|
|
|
*
|
|
|
|
* This is the common code shared between regular checkpoints and
|
|
|
|
* recovery restartpoints.
|
|
|
|
*/
|
|
|
|
static void
|
2007-06-28 02:02:40 +02:00
|
|
|
CheckPointGuts(XLogRecPtr checkPointRedo, int flags)
|
2006-08-07 18:57:57 +02:00
|
|
|
{
|
|
|
|
CheckPointCLOG();
|
|
|
|
CheckPointSUBTRANS();
|
|
|
|
CheckPointMultiXact();
|
2010-02-07 21:48:13 +01:00
|
|
|
CheckPointRelationMap();
|
2007-11-15 22:14:46 +01:00
|
|
|
CheckPointBuffers(flags); /* performs all required fsyncs */
|
2006-08-07 18:57:57 +02:00
|
|
|
/* We deliberately delay 2PC checkpointing as long as possible */
|
|
|
|
CheckPointTwoPhase(checkPointRedo);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-26 22:29:04 +02:00
|
|
|
* Save a checkpoint for recovery restart if appropriate
|
|
|
|
*
|
|
|
|
* This function is called each time a checkpoint record is read from XLOG.
|
|
|
|
* It must determine whether the checkpoint represents a safe restartpoint or
|
|
|
|
* not. If so, the checkpoint record is stashed in shared memory so that
|
|
|
|
* CreateRestartPoint can consult it. (Note that the latter function is
|
|
|
|
* executed by the bgwriter, while this one will be executed by the startup
|
|
|
|
* process.)
|
2006-08-07 18:57:57 +02:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
RecoveryRestartPoint(const CheckPoint *checkPoint)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
int rmid;
|
2009-06-11 16:49:15 +02:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
2006-08-07 18:57:57 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Is it safe to checkpoint? We must ask each of the resource managers
|
|
|
|
* whether they have any partial state information that might prevent a
|
|
|
|
* correct restart from this point. If so, we skip this opportunity, but
|
|
|
|
* return at the next checkpoint record for another try.
|
|
|
|
*/
|
|
|
|
for (rmid = 0; rmid <= RM_MAX_ID; rmid++)
|
|
|
|
{
|
|
|
|
if (RmgrTable[rmid].rm_safe_restartpoint != NULL)
|
|
|
|
if (!(RmgrTable[rmid].rm_safe_restartpoint()))
|
2007-08-29 01:17:47 +02:00
|
|
|
{
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
elog(trace_recovery(DEBUG2), "RM %d not safe to record restart point at %X/%X",
|
2007-08-29 01:17:47 +02:00
|
|
|
rmid,
|
|
|
|
checkPoint->redo.xlogid,
|
|
|
|
checkPoint->redo.xrecoff);
|
2006-08-07 18:57:57 +02:00
|
|
|
return;
|
2007-08-29 01:17:47 +02:00
|
|
|
}
|
2006-08-07 18:57:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Copy the checkpoint record to shared memory, so that bgwriter can use
|
|
|
|
* it the next time it wants to perform a restartpoint.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
XLogCtl->lastCheckPointRecPtr = ReadRecPtr;
|
|
|
|
memcpy(&XLogCtl->lastCheckPoint, checkPoint, sizeof(CheckPoint));
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-06-26 22:29:04 +02:00
|
|
|
* Establish a restartpoint if possible.
|
|
|
|
*
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* This is similar to CreateCheckPoint, but is used during WAL recovery
|
|
|
|
* to establish a point from which recovery can roll forward without
|
|
|
|
* replaying the entire recovery log.
|
|
|
|
*
|
|
|
|
* Returns true if a new restartpoint was established. We can only establish
|
2009-06-26 22:29:04 +02:00
|
|
|
* a restartpoint if we have replayed a safe checkpoint record since last
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* restartpoint.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
CreateRestartPoint(int flags)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
XLogRecPtr lastCheckPointRecPtr;
|
|
|
|
CheckPoint lastCheckPoint;
|
2010-01-15 10:19:10 +01:00
|
|
|
uint32 _logId;
|
|
|
|
uint32 _logSeg;
|
2010-07-06 21:19:02 +02:00
|
|
|
TimestampTz xtime;
|
2009-06-11 16:49:15 +02:00
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Acquire CheckpointLock to ensure only one restartpoint or checkpoint
|
|
|
|
* happens at a time.
|
|
|
|
*/
|
|
|
|
LWLockAcquire(CheckpointLock, LW_EXCLUSIVE);
|
|
|
|
|
2009-06-26 22:29:04 +02:00
|
|
|
/* Get a local copy of the last safe checkpoint record. */
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
lastCheckPointRecPtr = xlogctl->lastCheckPointRecPtr;
|
|
|
|
memcpy(&lastCheckPoint, &XLogCtl->lastCheckPoint, sizeof(CheckPoint));
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
/*
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* Check that we're still in recovery mode. It's ok if we exit recovery
|
|
|
|
* mode after this check, the restart point is valid anyway.
|
|
|
|
*/
|
|
|
|
if (!RecoveryInProgress())
|
|
|
|
{
|
|
|
|
ereport(DEBUG2,
|
2009-06-11 16:49:15 +02:00
|
|
|
(errmsg("skipping restartpoint, recovery has already ended")));
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockRelease(CheckpointLock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the last checkpoint record we've replayed is already our last
|
|
|
|
* restartpoint, we can't perform a new restart point. We still update
|
|
|
|
* minRecoveryPoint in that case, so that if this is a shutdown restart
|
|
|
|
* point, we won't start up earlier than before. That's not strictly
|
2010-07-06 21:19:02 +02:00
|
|
|
* necessary, but when hot standby is enabled, it would be rather weird if
|
|
|
|
* the database opened up for read-only connections at a point-in-time
|
|
|
|
* before the last shutdown. Such time travel is still possible in case of
|
|
|
|
* immediate shutdown, though.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*
|
|
|
|
* We don't explicitly advance minRecoveryPoint when we do create a
|
2009-06-11 16:49:15 +02:00
|
|
|
* restartpoint. It's assumed that flushing the buffers will do that as a
|
|
|
|
* side-effect.
|
2006-08-07 18:57:57 +02:00
|
|
|
*/
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
if (XLogRecPtrIsInvalid(lastCheckPointRecPtr) ||
|
|
|
|
XLByteLE(lastCheckPoint.redo, ControlFile->checkPointCopy.redo))
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
XLogRecPtr InvalidXLogRecPtr = {0, 0};
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
ereport(DEBUG2,
|
|
|
|
(errmsg("skipping restartpoint, already performed at %X/%X",
|
2009-06-11 16:49:15 +02:00
|
|
|
lastCheckPoint.redo.xlogid, lastCheckPoint.redo.xrecoff)));
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
UpdateMinRecoveryPoint(InvalidXLogRecPtr, true);
|
2010-06-03 05:20:00 +02:00
|
|
|
if (flags & CHECKPOINT_IS_SHUTDOWN)
|
|
|
|
{
|
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
|
|
|
ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
|
|
|
|
ControlFile->time = (pg_time_t) time(NULL);
|
|
|
|
UpdateControlFile();
|
|
|
|
LWLockRelease(ControlFileLock);
|
|
|
|
}
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockRelease(CheckpointLock);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-06-09 17:04:07 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Update the shared RedoRecPtr so that the startup process can calculate
|
|
|
|
* the number of segments replayed since last restartpoint, and request a
|
|
|
|
* restartpoint if it exceeds checkpoint_segments.
|
2010-06-09 17:04:07 +02:00
|
|
|
*
|
|
|
|
* You need to hold WALInsertLock and info_lck to update it, although
|
|
|
|
* during recovery acquiring WALInsertLock is just pro forma, because
|
|
|
|
* there is no other processes updating Insert.RedoRecPtr.
|
|
|
|
*/
|
|
|
|
LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
xlogctl->Insert.RedoRecPtr = lastCheckPoint.redo;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
if (log_checkpoints)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Prepare to accumulate statistics.
|
|
|
|
*/
|
|
|
|
MemSet(&CheckpointStats, 0, sizeof(CheckpointStats));
|
|
|
|
CheckpointStats.ckpt_start_t = GetCurrentTimestamp();
|
|
|
|
|
|
|
|
LogCheckpointStart(flags, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
CheckPointGuts(lastCheckPoint.redo, flags);
|
2006-08-07 18:57:57 +02:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Select point at which we can truncate the xlog, which we base on the
|
|
|
|
* prior checkpoint's earliest info.
|
|
|
|
*/
|
|
|
|
XLByteToSeg(ControlFile->checkPointCopy.redo, _logId, _logSeg);
|
|
|
|
|
2006-08-07 18:57:57 +02:00
|
|
|
/*
|
2009-06-26 22:29:04 +02:00
|
|
|
* Update pg_control, using current time. Check that it still shows
|
|
|
|
* IN_ARCHIVE_RECOVERY state and an older checkpoint, else do nothing;
|
2010-02-26 03:01:40 +01:00
|
|
|
* this is a quick hack to make sure nothing really bad happens if somehow
|
|
|
|
* we get here after the end-of-recovery checkpoint.
|
2006-08-07 18:57:57 +02:00
|
|
|
*/
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
2009-06-26 22:29:04 +02:00
|
|
|
if (ControlFile->state == DB_IN_ARCHIVE_RECOVERY &&
|
|
|
|
XLByteLT(ControlFile->checkPointCopy.redo, lastCheckPoint.redo))
|
|
|
|
{
|
|
|
|
ControlFile->prevCheckPoint = ControlFile->checkPoint;
|
|
|
|
ControlFile->checkPoint = lastCheckPointRecPtr;
|
|
|
|
ControlFile->checkPointCopy = lastCheckPoint;
|
|
|
|
ControlFile->time = (pg_time_t) time(NULL);
|
2010-06-03 05:20:00 +02:00
|
|
|
if (flags & CHECKPOINT_IS_SHUTDOWN)
|
|
|
|
ControlFile->state = DB_SHUTDOWNED_IN_RECOVERY;
|
2009-06-26 22:29:04 +02:00
|
|
|
UpdateControlFile();
|
|
|
|
}
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
LWLockRelease(ControlFileLock);
|
2006-08-07 18:57:57 +02:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Delete old log files (those no longer needed even for previous
|
|
|
|
* checkpoint/restartpoint) to prevent the disk holding the xlog from
|
|
|
|
* growing full. We don't need do this during normal recovery, but during
|
|
|
|
* streaming recovery we have to or the disk will eventually fill up from
|
|
|
|
* old log files streamed from master.
|
|
|
|
*/
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
if (WalRcvInProgress() && (_logId || _logSeg))
|
2010-01-15 10:19:10 +01:00
|
|
|
{
|
|
|
|
XLogRecPtr endptr;
|
|
|
|
|
|
|
|
/* Get the current (or recent) end of xlog */
|
2010-07-03 22:43:58 +02:00
|
|
|
endptr = GetWalRcvWriteRecPtr(NULL);
|
2010-01-15 10:19:10 +01:00
|
|
|
|
|
|
|
PrevLogSeg(_logId, _logSeg);
|
|
|
|
RemoveOldXlogFiles(_logId, _logSeg, endptr);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make more log segments if needed. (Do this after recycling old log
|
|
|
|
* segments, since that may supply some of the needed files.)
|
|
|
|
*/
|
|
|
|
PreallocXlogFiles(endptr);
|
|
|
|
}
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/*
|
2010-08-30 16:22:05 +02:00
|
|
|
* Truncate pg_subtrans if possible. We can throw away all data before
|
|
|
|
* the oldest XMIN of any running transaction. No future transaction will
|
|
|
|
* attempt to reference any pg_subtrans entry older than that (see Asserts
|
|
|
|
* in subtrans.c). When hot standby is disabled, though, we mustn't do
|
|
|
|
* this because StartupSUBTRANS hasn't been called yet.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
2010-08-30 16:22:05 +02:00
|
|
|
if (EnableHotStandby)
|
|
|
|
TruncateSUBTRANS(GetOldestXmin(true, false));
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
/* All real work is done, but log before releasing lock. */
|
|
|
|
if (log_checkpoints)
|
|
|
|
LogCheckpointEnd(true);
|
|
|
|
|
2010-07-04 00:15:45 +02:00
|
|
|
xtime = GetLatestXTime();
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
ereport((log_checkpoints ? LOG : DEBUG2),
|
2010-07-04 00:15:45 +02:00
|
|
|
(errmsg("recovery restart point at %X/%X",
|
|
|
|
lastCheckPoint.redo.xlogid, lastCheckPoint.redo.xrecoff),
|
2010-07-06 21:19:02 +02:00
|
|
|
xtime ? errdetail("last completed transaction was at log time %s",
|
|
|
|
timestamptz_to_str(xtime)) : 0));
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
|
|
|
LWLockRelease(CheckpointLock);
|
2010-03-18 10:17:18 +01:00
|
|
|
|
|
|
|
/*
|
2010-06-10 10:13:50 +02:00
|
|
|
* Finally, execute archive_cleanup_command, if any.
|
2010-03-18 10:17:18 +01:00
|
|
|
*/
|
2010-06-10 10:13:50 +02:00
|
|
|
if (XLogCtl->archiveCleanupCommand[0])
|
|
|
|
ExecuteRecoveryCommand(XLogCtl->archiveCleanupCommand,
|
|
|
|
"archive_cleanup_command",
|
2010-03-18 10:17:18 +01:00
|
|
|
false);
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
return true;
|
2006-08-07 18:57:57 +02:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* Write a NEXTOID log record
|
|
|
|
*/
|
2000-11-03 12:39:36 +01:00
|
|
|
void
|
|
|
|
XLogPutNextOid(Oid nextOid)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
XLogRecData rdata;
|
2000-11-03 12:39:36 +01:00
|
|
|
|
2001-03-22 05:01:46 +01:00
|
|
|
rdata.data = (char *) (&nextOid);
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata.len = sizeof(Oid);
|
2005-06-06 22:22:58 +02:00
|
|
|
rdata.buffer = InvalidBuffer;
|
2000-12-28 14:00:29 +01:00
|
|
|
rdata.next = NULL;
|
|
|
|
(void) XLogInsert(RM_XLOG_ID, XLOG_NEXTOID, &rdata);
|
2005-10-15 04:49:52 +02:00
|
|
|
|
2005-04-28 23:47:18 +02:00
|
|
|
/*
|
|
|
|
* We need not flush the NEXTOID record immediately, because any of the
|
2005-10-15 04:49:52 +02:00
|
|
|
* just-allocated OIDs could only reach disk as part of a tuple insert or
|
|
|
|
* update that would have its own XLOG record that must follow the NEXTOID
|
|
|
|
* record. Therefore, the standard buffer LSN interlock applied to those
|
|
|
|
* records will ensure no such OID reaches disk before the NEXTOID record
|
|
|
|
* does.
|
2006-10-19 00:44:12 +02:00
|
|
|
*
|
|
|
|
* Note, however, that the above statement only covers state "within" the
|
2007-11-15 22:14:46 +01:00
|
|
|
* database. When we use a generated OID as a file or directory name, we
|
|
|
|
* are in a sense violating the basic WAL rule, because that filesystem
|
2006-10-19 00:44:12 +02:00
|
|
|
* change may reach disk before the NEXTOID WAL record does. The impact
|
2007-11-15 22:14:46 +01:00
|
|
|
* of this is that if a database crash occurs immediately afterward, we
|
|
|
|
* might after restart re-generate the same OID and find that it conflicts
|
|
|
|
* with the leftover file or directory. But since for safety's sake we
|
|
|
|
* always loop until finding a nonconflicting filename, this poses no real
|
|
|
|
* problem in practice. See pgsql-hackers discussion 27-Sep-2006.
|
2005-04-28 23:47:18 +02:00
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/*
|
|
|
|
* Write an XLOG SWITCH record.
|
|
|
|
*
|
|
|
|
* Here we just blindly issue an XLogInsert request for the record.
|
|
|
|
* All the magic happens inside XLogInsert.
|
|
|
|
*
|
|
|
|
* The return value is either the end+1 address of the switch record,
|
|
|
|
* or the end+1 address of the prior segment if we did not need to
|
|
|
|
* write a switch record because we are already at segment start.
|
|
|
|
*/
|
2006-08-18 01:04:10 +02:00
|
|
|
XLogRecPtr
|
2006-08-06 05:53:44 +02:00
|
|
|
RequestXLogSwitch(void)
|
|
|
|
{
|
|
|
|
XLogRecPtr RecPtr;
|
|
|
|
XLogRecData rdata;
|
|
|
|
|
|
|
|
/* XLOG SWITCH, alone among xlog record types, has no data */
|
|
|
|
rdata.buffer = InvalidBuffer;
|
|
|
|
rdata.data = NULL;
|
|
|
|
rdata.len = 0;
|
|
|
|
rdata.next = NULL;
|
|
|
|
|
|
|
|
RecPtr = XLogInsert(RM_XLOG_ID, XLOG_SWITCH, &rdata);
|
|
|
|
|
|
|
|
return RecPtr;
|
|
|
|
}
|
|
|
|
|
2010-01-20 20:43:40 +01:00
|
|
|
/*
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
* Check if any of the GUC parameters that are critical for hot standby
|
|
|
|
* have changed, and update the value in pg_control file if necessary.
|
2010-01-20 20:43:40 +01:00
|
|
|
*/
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
static void
|
|
|
|
XLogReportParameters(void)
|
2010-01-20 20:43:40 +01:00
|
|
|
{
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
if (wal_level != ControlFile->wal_level ||
|
|
|
|
MaxConnections != ControlFile->MaxConnections ||
|
|
|
|
max_prepared_xacts != ControlFile->max_prepared_xacts ||
|
2010-06-12 11:14:52 +02:00
|
|
|
max_locks_per_xact != ControlFile->max_locks_per_xact)
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
{
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* The change in number of backend slots doesn't need to be WAL-logged
|
|
|
|
* if archiving is not enabled, as you can't start archive recovery
|
|
|
|
* with wal_level=minimal anyway. We don't really care about the
|
|
|
|
* values in pg_control either if wal_level=minimal, but seems better
|
|
|
|
* to keep them up-to-date to avoid confusion.
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
*/
|
|
|
|
if (wal_level != ControlFile->wal_level || XLogIsNeeded())
|
|
|
|
{
|
|
|
|
XLogRecData rdata;
|
|
|
|
xl_parameter_change xlrec;
|
2010-01-20 20:43:40 +01:00
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
xlrec.MaxConnections = MaxConnections;
|
|
|
|
xlrec.max_prepared_xacts = max_prepared_xacts;
|
|
|
|
xlrec.max_locks_per_xact = max_locks_per_xact;
|
|
|
|
xlrec.wal_level = wal_level;
|
|
|
|
|
|
|
|
rdata.buffer = InvalidBuffer;
|
|
|
|
rdata.data = (char *) &xlrec;
|
|
|
|
rdata.len = sizeof(xlrec);
|
|
|
|
rdata.next = NULL;
|
|
|
|
|
|
|
|
XLogInsert(RM_XLOG_ID, XLOG_PARAMETER_CHANGE, &rdata);
|
|
|
|
}
|
2010-01-20 20:43:40 +01:00
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
ControlFile->MaxConnections = MaxConnections;
|
|
|
|
ControlFile->max_prepared_xacts = max_prepared_xacts;
|
|
|
|
ControlFile->max_locks_per_xact = max_locks_per_xact;
|
|
|
|
ControlFile->wal_level = wal_level;
|
|
|
|
UpdateControlFile();
|
|
|
|
}
|
2010-01-20 20:43:40 +01:00
|
|
|
}
|
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
/*
|
|
|
|
* XLOG resource manager's routines
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*
|
|
|
|
* Definitions of info values are in include/catalog/pg_control.h, though
|
2009-06-26 22:29:04 +02:00
|
|
|
* not all record types are related to control file updates.
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
*/
|
2000-10-21 17:43:36 +02:00
|
|
|
void
|
|
|
|
xlog_redo(XLogRecPtr lsn, XLogRecord *record)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
uint8 info = record->xl_info & ~XLR_INFO_MASK;
|
2000-11-03 12:39:36 +01:00
|
|
|
|
2009-01-20 19:59:37 +01:00
|
|
|
/* Backup blocks are not used in xlog records */
|
|
|
|
Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
|
|
|
|
|
2001-03-18 21:18:59 +01:00
|
|
|
if (info == XLOG_NEXTOID)
|
2000-11-03 12:39:36 +01:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid nextOid;
|
2000-11-03 12:39:36 +01:00
|
|
|
|
|
|
|
memcpy(&nextOid, XLogRecGetData(record), sizeof(Oid));
|
|
|
|
if (ShmemVariableCache->nextOid < nextOid)
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
{
|
2000-11-03 12:39:36 +01:00
|
|
|
ShmemVariableCache->nextOid = nextOid;
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ShmemVariableCache->oidCount = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (info == XLOG_CHECKPOINT_SHUTDOWN)
|
|
|
|
{
|
|
|
|
CheckPoint checkPoint;
|
|
|
|
|
|
|
|
memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
|
|
|
|
/* In a SHUTDOWN checkpoint, believe the counters exactly */
|
|
|
|
ShmemVariableCache->nextXid = checkPoint.nextXid;
|
|
|
|
ShmemVariableCache->nextOid = checkPoint.nextOid;
|
|
|
|
ShmemVariableCache->oidCount = 0;
|
2005-06-08 17:50:28 +02:00
|
|
|
MultiXactSetNextMXact(checkPoint.nextMulti,
|
|
|
|
checkPoint.nextMultiOffset);
|
2010-02-17 04:10:33 +01:00
|
|
|
SetTransactionIdLimit(checkPoint.oldestXid, checkPoint.oldestXidDB);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2010-04-27 11:25:18 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* If we see a shutdown checkpoint while waiting for an end-of-backup
|
|
|
|
* record, the backup was cancelled and the end-of-backup record will
|
|
|
|
* never arrive.
|
2010-04-27 11:25:18 +02:00
|
|
|
*/
|
|
|
|
if (InArchiveRecovery &&
|
|
|
|
!XLogRecPtrIsInvalid(ControlFile->backupStartPoint))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errmsg("online backup was cancelled, recovery cannot continue")));
|
|
|
|
|
2010-04-13 16:17:46 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* If we see a shutdown checkpoint, we know that nothing was running
|
|
|
|
* on the master at this point. So fake-up an empty running-xacts
|
|
|
|
* record and use that here and now. Recover additional standby state
|
|
|
|
* for prepared transactions.
|
2010-04-13 16:17:46 +02:00
|
|
|
*/
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
if (standbyState >= STANDBY_INITIALIZED)
|
|
|
|
{
|
2010-04-13 16:17:46 +02:00
|
|
|
TransactionId *xids;
|
|
|
|
int nxids;
|
|
|
|
TransactionId oldestActiveXID;
|
2010-05-13 13:15:38 +02:00
|
|
|
TransactionId latestCompletedXid;
|
2010-04-13 16:17:46 +02:00
|
|
|
RunningTransactionsData running;
|
|
|
|
|
|
|
|
oldestActiveXID = PrescanPreparedTransactions(&xids, &nxids);
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
/*
|
2010-04-13 16:17:46 +02:00
|
|
|
* Construct a RunningTransactions snapshot representing a shut
|
2010-07-06 21:19:02 +02:00
|
|
|
* down server, with only prepared transactions still alive. We're
|
|
|
|
* never overflowed at this point because all subxids are listed
|
|
|
|
* with their parent prepared transactions.
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
*/
|
2010-04-13 16:17:46 +02:00
|
|
|
running.xcnt = nxids;
|
|
|
|
running.subxid_overflow = false;
|
|
|
|
running.nextXid = checkPoint.nextXid;
|
|
|
|
running.oldestRunningXid = oldestActiveXID;
|
2010-05-13 13:15:38 +02:00
|
|
|
latestCompletedXid = checkPoint.nextXid;
|
|
|
|
TransactionIdRetreat(latestCompletedXid);
|
2010-05-14 09:11:49 +02:00
|
|
|
Assert(TransactionIdIsNormal(latestCompletedXid));
|
2010-05-13 13:15:38 +02:00
|
|
|
running.latestCompletedXid = latestCompletedXid;
|
2010-04-13 16:17:46 +02:00
|
|
|
running.xids = xids;
|
|
|
|
|
|
|
|
ProcArrayApplyRecoveryInfo(&running);
|
|
|
|
|
|
|
|
StandbyRecoverPreparedTransactions(true);
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
}
|
|
|
|
|
2006-08-21 18:16:31 +02:00
|
|
|
/* ControlFile->checkPointCopy always tracks the latest ckpt XID */
|
|
|
|
ControlFile->checkPointCopy.nextXidEpoch = checkPoint.nextXidEpoch;
|
|
|
|
ControlFile->checkPointCopy.nextXid = checkPoint.nextXid;
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* TLI may change in a shutdown checkpoint, but it shouldn't decrease
|
2004-07-22 00:31:26 +02:00
|
|
|
*/
|
|
|
|
if (checkPoint.ThisTimeLineID != ThisTimeLineID)
|
|
|
|
{
|
|
|
|
if (checkPoint.ThisTimeLineID < ThisTimeLineID ||
|
|
|
|
!list_member_int(expectedTLIs,
|
|
|
|
(int) checkPoint.ThisTimeLineID))
|
|
|
|
ereport(PANIC,
|
|
|
|
(errmsg("unexpected timeline ID %u (after %u) in checkpoint record",
|
2009-06-11 16:49:15 +02:00
|
|
|
checkPoint.ThisTimeLineID, ThisTimeLineID)));
|
|
|
|
/* Following WAL records should be run with new TLI */
|
|
|
|
ThisTimeLineID = checkPoint.ThisTimeLineID;
|
2004-07-22 00:31:26 +02:00
|
|
|
}
|
2006-08-07 18:57:57 +02:00
|
|
|
|
|
|
|
RecoveryRestartPoint(&checkPoint);
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
|
|
|
else if (info == XLOG_CHECKPOINT_ONLINE)
|
|
|
|
{
|
|
|
|
CheckPoint checkPoint;
|
|
|
|
|
|
|
|
memcpy(&checkPoint, XLogRecGetData(record), sizeof(CheckPoint));
|
2001-03-18 21:18:59 +01:00
|
|
|
/* In an ONLINE checkpoint, treat the counters like NEXTOID */
|
2001-08-24 01:06:38 +02:00
|
|
|
if (TransactionIdPrecedes(ShmemVariableCache->nextXid,
|
|
|
|
checkPoint.nextXid))
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
ShmemVariableCache->nextXid = checkPoint.nextXid;
|
|
|
|
if (ShmemVariableCache->nextOid < checkPoint.nextOid)
|
|
|
|
{
|
|
|
|
ShmemVariableCache->nextOid = checkPoint.nextOid;
|
|
|
|
ShmemVariableCache->oidCount = 0;
|
|
|
|
}
|
2005-06-08 17:50:28 +02:00
|
|
|
MultiXactAdvanceNextMXact(checkPoint.nextMulti,
|
|
|
|
checkPoint.nextMultiOffset);
|
2009-08-31 04:23:23 +02:00
|
|
|
if (TransactionIdPrecedes(ShmemVariableCache->oldestXid,
|
|
|
|
checkPoint.oldestXid))
|
2010-02-17 04:10:33 +01:00
|
|
|
SetTransactionIdLimit(checkPoint.oldestXid,
|
|
|
|
checkPoint.oldestXidDB);
|
2006-08-21 18:16:31 +02:00
|
|
|
|
|
|
|
/* ControlFile->checkPointCopy always tracks the latest ckpt XID */
|
|
|
|
ControlFile->checkPointCopy.nextXidEpoch = checkPoint.nextXidEpoch;
|
|
|
|
ControlFile->checkPointCopy.nextXid = checkPoint.nextXid;
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
/* TLI should not change in an on-line checkpoint */
|
|
|
|
if (checkPoint.ThisTimeLineID != ThisTimeLineID)
|
2004-02-11 23:55:26 +01:00
|
|
|
ereport(PANIC,
|
2004-07-22 00:31:26 +02:00
|
|
|
(errmsg("unexpected timeline ID %u (should be %u) in checkpoint record",
|
|
|
|
checkPoint.ThisTimeLineID, ThisTimeLineID)));
|
2006-08-07 18:57:57 +02:00
|
|
|
|
|
|
|
RecoveryRestartPoint(&checkPoint);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
2007-05-20 23:08:19 +02:00
|
|
|
else if (info == XLOG_NOOP)
|
|
|
|
{
|
|
|
|
/* nothing to do here */
|
|
|
|
}
|
2006-08-06 05:53:44 +02:00
|
|
|
else if (info == XLOG_SWITCH)
|
|
|
|
{
|
|
|
|
/* nothing to do here */
|
|
|
|
}
|
2010-01-04 13:50:50 +01:00
|
|
|
else if (info == XLOG_BACKUP_END)
|
|
|
|
{
|
|
|
|
XLogRecPtr startpoint;
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2010-01-04 13:50:50 +01:00
|
|
|
memcpy(&startpoint, XLogRecGetData(record), sizeof(startpoint));
|
|
|
|
|
|
|
|
if (XLByteEQ(ControlFile->backupStartPoint, startpoint))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We have reached the end of base backup, the point where
|
|
|
|
* pg_stop_backup() was done. The data on disk is now consistent.
|
|
|
|
* Reset backupStartPoint, and update minRecoveryPoint to make
|
|
|
|
* sure we don't allow starting up at an earlier point even if
|
|
|
|
* recovery is stopped and restarted soon after this.
|
|
|
|
*/
|
|
|
|
elog(DEBUG1, "end of backup reached");
|
|
|
|
|
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
|
|
|
|
|
|
|
if (XLByteLT(ControlFile->minRecoveryPoint, lsn))
|
|
|
|
ControlFile->minRecoveryPoint = lsn;
|
|
|
|
MemSet(&ControlFile->backupStartPoint, 0, sizeof(XLogRecPtr));
|
|
|
|
UpdateControlFile();
|
|
|
|
|
|
|
|
LWLockRelease(ControlFileLock);
|
|
|
|
}
|
|
|
|
}
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
else if (info == XLOG_PARAMETER_CHANGE)
|
2010-01-20 20:43:40 +01:00
|
|
|
{
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
xl_parameter_change xlrec;
|
|
|
|
|
|
|
|
/* Update our copy of the parameters in pg_control */
|
|
|
|
memcpy(&xlrec, XLogRecGetData(record), sizeof(xl_parameter_change));
|
|
|
|
|
2010-05-03 13:17:52 +02:00
|
|
|
LWLockAcquire(ControlFileLock, LW_EXCLUSIVE);
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
ControlFile->MaxConnections = xlrec.MaxConnections;
|
|
|
|
ControlFile->max_prepared_xacts = xlrec.max_prepared_xacts;
|
|
|
|
ControlFile->max_locks_per_xact = xlrec.max_locks_per_xact;
|
|
|
|
ControlFile->wal_level = xlrec.wal_level;
|
2010-07-06 21:19:02 +02:00
|
|
|
|
2010-05-03 13:17:52 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Update minRecoveryPoint to ensure that if recovery is aborted, we
|
|
|
|
* recover back up to this point before allowing hot standby again.
|
|
|
|
* This is particularly important if wal_level was set to 'archive'
|
|
|
|
* before, and is now 'hot_standby', to ensure you don't run queries
|
|
|
|
* against the WAL preceding the wal_level change. Same applies to
|
|
|
|
* decreasing max_* settings.
|
2010-05-03 13:17:52 +02:00
|
|
|
*/
|
|
|
|
minRecoveryPoint = ControlFile->minRecoveryPoint;
|
|
|
|
if ((minRecoveryPoint.xlogid != 0 || minRecoveryPoint.xrecoff != 0)
|
|
|
|
&& XLByteLT(minRecoveryPoint, lsn))
|
|
|
|
{
|
|
|
|
ControlFile->minRecoveryPoint = lsn;
|
|
|
|
}
|
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
UpdateControlFile();
|
2010-05-03 13:17:52 +02:00
|
|
|
LWLockRelease(ControlFileLock);
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
|
|
|
|
/* Check to see if any changes to max_connections give problems */
|
|
|
|
CheckRequiredParameterValues();
|
2010-01-20 20:43:40 +01:00
|
|
|
}
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
void
|
2006-03-24 05:32:13 +01:00
|
|
|
xlog_desc(StringInfo buf, uint8 xl_info, char *rec)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
uint8 info = xl_info & ~XLR_INFO_MASK;
|
2000-10-21 17:43:36 +02:00
|
|
|
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
if (info == XLOG_CHECKPOINT_SHUTDOWN ||
|
|
|
|
info == XLOG_CHECKPOINT_ONLINE)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
CheckPoint *checkpoint = (CheckPoint *) rec;
|
|
|
|
|
2007-03-03 21:02:27 +01:00
|
|
|
appendStringInfo(buf, "checkpoint: redo %X/%X; "
|
2009-08-31 04:23:23 +02:00
|
|
|
"tli %u; xid %u/%u; oid %u; multi %u; offset %u; "
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
"oldest xid %u in DB %u; oldest running xid %u; %s",
|
2006-10-04 02:30:14 +02:00
|
|
|
checkpoint->redo.xlogid, checkpoint->redo.xrecoff,
|
|
|
|
checkpoint->ThisTimeLineID,
|
|
|
|
checkpoint->nextXidEpoch, checkpoint->nextXid,
|
|
|
|
checkpoint->nextOid,
|
|
|
|
checkpoint->nextMulti,
|
|
|
|
checkpoint->nextMultiOffset,
|
2009-08-31 04:23:23 +02:00
|
|
|
checkpoint->oldestXid,
|
|
|
|
checkpoint->oldestXidDB,
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
checkpoint->oldestActiveXid,
|
2006-10-04 02:30:14 +02:00
|
|
|
(info == XLOG_CHECKPOINT_SHUTDOWN) ? "shutdown" : "online");
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now grabs lockfile in target directory, as added
insurance against running it in parallel with live postmaster.
2001-03-13 02:17:06 +01:00
|
|
|
}
|
2007-05-20 23:08:19 +02:00
|
|
|
else if (info == XLOG_NOOP)
|
|
|
|
{
|
|
|
|
appendStringInfo(buf, "xlog no-op");
|
|
|
|
}
|
2000-11-03 12:39:36 +01:00
|
|
|
else if (info == XLOG_NEXTOID)
|
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
Oid nextOid;
|
2000-11-03 12:39:36 +01:00
|
|
|
|
|
|
|
memcpy(&nextOid, rec, sizeof(Oid));
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(buf, "nextOid: %u", nextOid);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
2006-08-06 05:53:44 +02:00
|
|
|
else if (info == XLOG_SWITCH)
|
|
|
|
{
|
|
|
|
appendStringInfo(buf, "xlog switch");
|
|
|
|
}
|
2010-01-04 13:50:50 +01:00
|
|
|
else if (info == XLOG_BACKUP_END)
|
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
XLogRecPtr startpoint;
|
2010-01-04 13:50:50 +01:00
|
|
|
|
|
|
|
memcpy(&startpoint, rec, sizeof(XLogRecPtr));
|
|
|
|
appendStringInfo(buf, "backup end: %X/%X",
|
|
|
|
startpoint.xlogid, startpoint.xrecoff);
|
|
|
|
}
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
else if (info == XLOG_PARAMETER_CHANGE)
|
2010-01-20 20:43:40 +01:00
|
|
|
{
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
xl_parameter_change xlrec;
|
|
|
|
const char *wal_level_str;
|
|
|
|
const struct config_enum_entry *entry;
|
|
|
|
|
|
|
|
memcpy(&xlrec, rec, sizeof(xl_parameter_change));
|
|
|
|
|
|
|
|
/* Find a string representation for wal_level */
|
|
|
|
wal_level_str = "?";
|
|
|
|
for (entry = wal_level_options; entry->name; entry++)
|
|
|
|
{
|
|
|
|
if (entry->val == xlrec.wal_level)
|
|
|
|
{
|
|
|
|
wal_level_str = entry->name;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-20 20:43:40 +01:00
|
|
|
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
appendStringInfo(buf, "parameter change: max_connections=%d max_prepared_xacts=%d max_locks_per_xact=%d wal_level=%s",
|
|
|
|
xlrec.MaxConnections,
|
|
|
|
xlrec.max_prepared_xacts,
|
|
|
|
xlrec.max_locks_per_xact,
|
|
|
|
wal_level_str);
|
2010-01-20 20:43:40 +01:00
|
|
|
}
|
2000-10-21 17:43:36 +02:00
|
|
|
else
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(buf, "UNKNOWN");
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
|
|
|
|
2004-01-06 23:22:37 +01:00
|
|
|
#ifdef WAL_DEBUG
|
2004-07-19 04:47:16 +02:00
|
|
|
|
2000-10-21 17:43:36 +02:00
|
|
|
static void
|
2006-03-24 05:32:13 +01:00
|
|
|
xlog_outrec(StringInfo buf, XLogRecord *record)
|
2000-10-21 17:43:36 +02:00
|
|
|
{
|
2001-03-22 05:01:46 +01:00
|
|
|
int i;
|
2000-12-30 07:52:34 +01:00
|
|
|
|
2006-03-24 05:32:13 +01:00
|
|
|
appendStringInfo(buf, "prev %X/%X; xid %u",
|
2006-03-29 23:17:39 +02:00
|
|
|
record->xl_prev.xlogid, record->xl_prev.xrecoff,
|
|
|
|
record->xl_xid);
|
2000-12-30 07:52:34 +01:00
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
appendStringInfo(buf, "; len %u",
|
|
|
|
record->xl_len);
|
|
|
|
|
2006-03-29 23:17:39 +02:00
|
|
|
for (i = 0; i < XLR_MAX_BKP_BLOCKS; i++)
|
2000-12-30 07:52:34 +01:00
|
|
|
{
|
2006-03-29 23:17:39 +02:00
|
|
|
if (record->xl_info & XLR_SET_BKP_BLOCK(i))
|
2006-10-04 02:30:14 +02:00
|
|
|
appendStringInfo(buf, "; bkpb%d", i + 1);
|
2000-12-30 07:52:34 +01:00
|
|
|
}
|
|
|
|
|
2006-03-29 23:17:39 +02:00
|
|
|
appendStringInfo(buf, ": %s", RmgrTable[record->xl_rmid].rm_name);
|
2000-10-21 17:43:36 +02:00
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
#endif /* WAL_DEBUG */
|
2001-03-16 06:44:33 +01:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
2008-05-14 16:02:57 +02:00
|
|
|
* Return the (possible) sync flag used for opening a file, depending on the
|
|
|
|
* value of the GUC wal_sync_method.
|
2001-03-16 06:44:33 +01:00
|
|
|
*/
|
2008-05-14 16:02:57 +02:00
|
|
|
static int
|
|
|
|
get_sync_bit(int method)
|
2001-03-16 06:44:33 +01:00
|
|
|
{
|
2010-02-26 03:01:40 +01:00
|
|
|
int o_direct_flag = 0;
|
2010-02-19 11:51:04 +01:00
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
/* If fsync is disabled, never open in sync mode */
|
|
|
|
if (!enableFsync)
|
|
|
|
return 0;
|
2001-03-16 06:44:33 +01:00
|
|
|
|
2010-02-19 11:51:04 +01:00
|
|
|
/*
|
|
|
|
* Optimize writes by bypassing kernel cache with O_DIRECT when using
|
|
|
|
* O_SYNC, O_DSYNC or O_FSYNC. But only if archiving and streaming are
|
2010-02-26 03:01:40 +01:00
|
|
|
* disabled, otherwise the archive command or walsender process will read
|
|
|
|
* the WAL soon after writing it, which is guaranteed to cause a physical
|
|
|
|
* read if we bypassed the kernel cache. We also skip the
|
|
|
|
* posix_fadvise(POSIX_FADV_DONTNEED) call in XLogFileClose() for the same
|
|
|
|
* reason.
|
2010-02-19 11:51:04 +01:00
|
|
|
*
|
|
|
|
* Never use O_DIRECT in walreceiver process for similar reasons; the WAL
|
|
|
|
* written by walreceiver is normally read by the startup process soon
|
|
|
|
* after its written. Also, walreceiver performs unaligned writes, which
|
|
|
|
* don't work with O_DIRECT, so it is required for correctness too.
|
|
|
|
*/
|
|
|
|
if (!XLogIsNeeded() && !am_walreceiver)
|
|
|
|
o_direct_flag = PG_O_DIRECT;
|
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
switch (method)
|
2001-03-16 06:44:33 +01:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
/*
|
|
|
|
* enum values for all sync options are defined even if they are
|
|
|
|
* not supported on the current platform. But if not, they are
|
|
|
|
* not included in the enum option array, and therefore will never
|
|
|
|
* be seen here.
|
|
|
|
*/
|
2008-05-12 10:35:05 +02:00
|
|
|
case SYNC_METHOD_FSYNC:
|
|
|
|
case SYNC_METHOD_FSYNC_WRITETHROUGH:
|
|
|
|
case SYNC_METHOD_FDATASYNC:
|
2008-05-14 16:02:57 +02:00
|
|
|
return 0;
|
2001-03-16 06:44:33 +01:00
|
|
|
#ifdef OPEN_SYNC_FLAG
|
2008-05-12 10:35:05 +02:00
|
|
|
case SYNC_METHOD_OPEN:
|
2010-02-19 11:51:04 +01:00
|
|
|
return OPEN_SYNC_FLAG | o_direct_flag;
|
2001-03-16 06:44:33 +01:00
|
|
|
#endif
|
|
|
|
#ifdef OPEN_DATASYNC_FLAG
|
2008-05-12 10:35:05 +02:00
|
|
|
case SYNC_METHOD_OPEN_DSYNC:
|
2010-02-19 11:51:04 +01:00
|
|
|
return OPEN_DATASYNC_FLAG | o_direct_flag;
|
2001-03-16 06:44:33 +01:00
|
|
|
#endif
|
2008-05-12 10:35:05 +02:00
|
|
|
default:
|
2008-05-16 21:15:05 +02:00
|
|
|
/* can't happen (unless we are out of sync with option array) */
|
|
|
|
elog(ERROR, "unrecognized wal_sync_method: %d", method);
|
2009-06-11 16:49:15 +02:00
|
|
|
return 0; /* silence warning */
|
2008-05-12 10:35:05 +02:00
|
|
|
}
|
2008-05-14 16:02:57 +02:00
|
|
|
}
|
2001-03-16 06:44:33 +01:00
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
/*
|
|
|
|
* GUC support
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
assign_xlog_sync_method(int new_sync_method, bool doit, GucSource source)
|
|
|
|
{
|
2002-05-17 03:19:19 +02:00
|
|
|
if (!doit)
|
2008-05-12 10:35:05 +02:00
|
|
|
return true;
|
2002-05-17 03:19:19 +02:00
|
|
|
|
2008-05-14 16:02:57 +02:00
|
|
|
if (sync_method != new_sync_method)
|
2001-03-16 06:44:33 +01:00
|
|
|
{
|
|
|
|
/*
|
2001-03-22 05:01:46 +01:00
|
|
|
* To ensure that no blocks escape unsynced, force an fsync on the
|
|
|
|
* currently open log segment (if any). Also, if the open flag is
|
2005-10-15 04:49:52 +02:00
|
|
|
* changing, close the log file so it will be reopened (with new flag
|
|
|
|
* bit) at next use.
|
2001-03-16 06:44:33 +01:00
|
|
|
*/
|
|
|
|
if (openLogFile >= 0)
|
|
|
|
{
|
|
|
|
if (pg_fsync(openLogFile) != 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not fsync log file %u, segment %u: %m",
|
|
|
|
openLogId, openLogSeg)));
|
2008-05-16 21:15:05 +02:00
|
|
|
if (get_sync_bit(sync_method) != get_sync_bit(new_sync_method))
|
2006-06-15 21:15:00 +02:00
|
|
|
XLogFileClose();
|
2001-03-16 06:44:33 +01:00
|
|
|
}
|
|
|
|
}
|
2002-05-17 03:19:19 +02:00
|
|
|
|
2008-05-12 10:35:05 +02:00
|
|
|
return true;
|
2001-03-16 06:44:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2010-01-15 10:19:10 +01:00
|
|
|
* Issue appropriate kind of fsync (if any) for an XLOG output file.
|
|
|
|
*
|
|
|
|
* 'fd' is a file descriptor for the XLOG file to be fsync'd.
|
|
|
|
* 'log' and 'seg' are for error reporting purposes.
|
2001-03-16 06:44:33 +01:00
|
|
|
*/
|
2010-01-15 10:19:10 +01:00
|
|
|
void
|
|
|
|
issue_xlog_fsync(int fd, uint32 log, uint32 seg)
|
2001-03-16 06:44:33 +01:00
|
|
|
{
|
|
|
|
switch (sync_method)
|
|
|
|
{
|
2001-10-25 07:50:21 +02:00
|
|
|
case SYNC_METHOD_FSYNC:
|
2010-01-15 10:19:10 +01:00
|
|
|
if (pg_fsync_no_writethrough(fd) != 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not fsync log file %u, segment %u: %m",
|
2010-01-15 10:19:10 +01:00
|
|
|
log, seg)));
|
2001-03-16 06:44:33 +01:00
|
|
|
break;
|
2005-05-20 16:53:26 +02:00
|
|
|
#ifdef HAVE_FSYNC_WRITETHROUGH
|
|
|
|
case SYNC_METHOD_FSYNC_WRITETHROUGH:
|
2010-01-15 10:19:10 +01:00
|
|
|
if (pg_fsync_writethrough(fd) != 0)
|
2005-05-20 16:53:26 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not fsync write-through log file %u, segment %u: %m",
|
2010-01-15 10:19:10 +01:00
|
|
|
log, seg)));
|
2005-05-20 16:53:26 +02:00
|
|
|
break;
|
|
|
|
#endif
|
2001-03-16 06:44:33 +01:00
|
|
|
#ifdef HAVE_FDATASYNC
|
|
|
|
case SYNC_METHOD_FDATASYNC:
|
2010-01-15 10:19:10 +01:00
|
|
|
if (pg_fdatasync(fd) != 0)
|
2003-07-21 22:29:40 +02:00
|
|
|
ereport(PANIC,
|
|
|
|
(errcode_for_file_access(),
|
2005-10-15 04:49:52 +02:00
|
|
|
errmsg("could not fdatasync log file %u, segment %u: %m",
|
2010-01-15 10:19:10 +01:00
|
|
|
log, seg)));
|
2001-03-16 06:44:33 +01:00
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
case SYNC_METHOD_OPEN:
|
2008-05-12 21:45:23 +02:00
|
|
|
case SYNC_METHOD_OPEN_DSYNC:
|
2001-03-16 06:44:33 +01:00
|
|
|
/* write synced it already */
|
|
|
|
break;
|
|
|
|
default:
|
2003-07-21 22:29:40 +02:00
|
|
|
elog(PANIC, "unrecognized wal_sync_method: %d", sync_method);
|
2001-03-16 06:44:33 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2004-08-03 22:32:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pg_start_backup: set up for taking an on-line backup dump
|
|
|
|
*
|
|
|
|
* Essentially what this does is to create a backup label file in $PGDATA,
|
|
|
|
* where it will be archived as part of the backup dump. The label file
|
|
|
|
* contains the user-supplied label string (typically this would be used
|
|
|
|
* to tell where the backup dump will be stored) and the starting time and
|
2006-08-06 05:53:44 +02:00
|
|
|
* starting WAL location for the dump.
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_start_backup(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *backupid = PG_GETARG_TEXT_P(0);
|
2009-04-07 02:31:26 +02:00
|
|
|
bool fast = PG_GETARG_BOOL(1);
|
2004-08-03 22:32:36 +02:00
|
|
|
char *backupidstr;
|
2004-08-04 18:25:02 +02:00
|
|
|
XLogRecPtr checkpointloc;
|
2004-08-03 22:32:36 +02:00
|
|
|
XLogRecPtr startpoint;
|
2007-08-04 03:26:54 +02:00
|
|
|
pg_time_t stamp_time;
|
2004-08-03 22:32:36 +02:00
|
|
|
char strfbuf[128];
|
|
|
|
char xlogfilename[MAXFNAMELEN];
|
|
|
|
uint32 _logId;
|
|
|
|
uint32 _logSeg;
|
|
|
|
struct stat stat_buf;
|
|
|
|
FILE *fp;
|
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (!superuser())
|
2004-08-03 22:32:36 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2007-09-27 00:36:30 +02:00
|
|
|
errmsg("must be superuser to run a backup")));
|
2004-11-17 03:22:54 +01:00
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("recovery is in progress"),
|
|
|
|
errhint("WAL control functions cannot be executed during recovery.")));
|
|
|
|
|
2010-04-29 23:49:03 +02:00
|
|
|
if (!XLogIsNeeded())
|
2007-09-27 00:36:30 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2010-07-06 21:19:02 +02:00
|
|
|
errmsg("WAL level not sufficient for making an online backup"),
|
2010-04-29 23:49:03 +02:00
|
|
|
errhint("wal_level must be set to \"archive\" or \"hot_standby\" at server start.")));
|
2004-11-17 03:22:54 +01:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
backupidstr = text_to_cstring(backupid);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2006-04-17 20:55:05 +02:00
|
|
|
* Mark backup active in shared memory. We must do full-page WAL writes
|
|
|
|
* during an on-line backup even if not doing so at other times, because
|
|
|
|
* it's quite possible for the backup dump to obtain a "torn" (partially
|
2006-10-04 02:30:14 +02:00
|
|
|
* written) copy of a database page if it reads the page concurrently with
|
|
|
|
* our write to the same page. This can be fixed as long as the first
|
|
|
|
* write to the page in the WAL sequence is a full-page write. Hence, we
|
|
|
|
* turn on forcePageWrites and then force a CHECKPOINT, to ensure there
|
|
|
|
* are no dirty pages in shared memory that might get dumped while the
|
|
|
|
* backup is in progress without having a corresponding WAL record. (Once
|
|
|
|
* the backup is complete, we need not force full-page writes anymore,
|
|
|
|
* since we expect that any pages not modified during the backup interval
|
|
|
|
* must have been correctly captured by the backup.)
|
2006-04-17 20:55:05 +02:00
|
|
|
*
|
2006-10-04 02:30:14 +02:00
|
|
|
* We must hold WALInsertLock to change the value of forcePageWrites, to
|
|
|
|
* ensure adequate interlocking against XLogInsert().
|
2004-08-04 18:25:02 +02:00
|
|
|
*/
|
2006-04-17 20:55:05 +02:00
|
|
|
LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
|
|
|
|
if (XLogCtl->Insert.forcePageWrites)
|
|
|
|
{
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("a backup is already in progress"),
|
|
|
|
errhint("Run pg_stop_backup() and try again.")));
|
|
|
|
}
|
|
|
|
XLogCtl->Insert.forcePageWrites = true;
|
|
|
|
LWLockRelease(WALInsertLock);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2009-05-07 13:25:25 +02:00
|
|
|
/*
|
|
|
|
* Force an XLOG file switch before the checkpoint, to ensure that the WAL
|
|
|
|
* segment the checkpoint is written to doesn't contain pages with old
|
|
|
|
* timeline IDs. That would otherwise happen if you called
|
|
|
|
* pg_start_backup() right after restoring from a PITR archive: the first
|
|
|
|
* WAL segment containing the startup checkpoint has pages in the
|
|
|
|
* beginning with the old timeline ID. That can cause trouble at recovery:
|
|
|
|
* we won't have a history file covering the old timeline if pg_xlog
|
|
|
|
* directory was not included in the base backup and the WAL archive was
|
|
|
|
* cleared too before starting the backup.
|
|
|
|
*/
|
|
|
|
RequestXLogSwitch();
|
|
|
|
|
2008-04-17 01:59:40 +02:00
|
|
|
/* Ensure we release forcePageWrites if fail below */
|
|
|
|
PG_ENSURE_ERROR_CLEANUP(pg_start_backup_callback, (Datum) 0);
|
2006-04-17 20:55:05 +02:00
|
|
|
{
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Force a CHECKPOINT. Aside from being necessary to prevent torn
|
2006-04-17 20:55:05 +02:00
|
|
|
* page problems, this guarantees that two successive backup runs will
|
|
|
|
* have different checkpoint positions and hence different history
|
|
|
|
* file names, even if nothing happened in between.
|
2007-06-28 02:02:40 +02:00
|
|
|
*
|
2009-06-11 16:49:15 +02:00
|
|
|
* We use CHECKPOINT_IMMEDIATE only if requested by user (via passing
|
|
|
|
* fast = true). Otherwise this can take awhile.
|
2006-04-17 20:55:05 +02:00
|
|
|
*/
|
2009-04-07 02:31:26 +02:00
|
|
|
RequestCheckpoint(CHECKPOINT_FORCE | CHECKPOINT_WAIT |
|
|
|
|
(fast ? CHECKPOINT_IMMEDIATE : 0));
|
2004-08-04 18:25:02 +02:00
|
|
|
|
2006-04-17 20:55:05 +02:00
|
|
|
/*
|
|
|
|
* Now we need to fetch the checkpoint record location, and also its
|
|
|
|
* REDO pointer. The oldest point in WAL that would be needed to
|
|
|
|
* restore starting from the checkpoint is precisely the REDO pointer.
|
|
|
|
*/
|
2010-03-10 03:04:48 +01:00
|
|
|
LWLockAcquire(ControlFileLock, LW_SHARED);
|
2006-04-17 20:55:05 +02:00
|
|
|
checkpointloc = ControlFile->checkPoint;
|
|
|
|
startpoint = ControlFile->checkPointCopy.redo;
|
|
|
|
LWLockRelease(ControlFileLock);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2006-04-17 20:55:05 +02:00
|
|
|
XLByteToSeg(startpoint, _logId, _logSeg);
|
|
|
|
XLogFileName(xlogfilename, ThisTimeLineID, _logId, _logSeg);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2007-08-04 03:26:54 +02:00
|
|
|
/* Use the log timezone here, not the session timezone */
|
|
|
|
stamp_time = (pg_time_t) time(NULL);
|
|
|
|
pg_strftime(strfbuf, sizeof(strfbuf),
|
|
|
|
"%Y-%m-%d %H:%M:%S %Z",
|
|
|
|
pg_localtime(&stamp_time, log_timezone));
|
2006-04-17 20:55:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for existing backup label --- implies a backup is already
|
|
|
|
* running. (XXX given that we checked forcePageWrites above, maybe
|
|
|
|
* it would be OK to just unlink any such label file?)
|
|
|
|
*/
|
|
|
|
if (stat(BACKUP_LABEL_FILE, &stat_buf) != 0)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not stat file \"%s\": %m",
|
|
|
|
BACKUP_LABEL_FILE)));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("a backup is already in progress"),
|
|
|
|
errhint("If you're sure there is no backup in progress, remove file \"%s\" and try again.",
|
|
|
|
BACKUP_LABEL_FILE)));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Okay, write the file
|
|
|
|
*/
|
|
|
|
fp = AllocateFile(BACKUP_LABEL_FILE, "w");
|
|
|
|
if (!fp)
|
2004-08-03 22:32:36 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
2006-04-17 20:55:05 +02:00
|
|
|
errmsg("could not create file \"%s\": %m",
|
|
|
|
BACKUP_LABEL_FILE)));
|
|
|
|
fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
|
|
|
|
startpoint.xlogid, startpoint.xrecoff, xlogfilename);
|
|
|
|
fprintf(fp, "CHECKPOINT LOCATION: %X/%X\n",
|
|
|
|
checkpointloc.xlogid, checkpointloc.xrecoff);
|
|
|
|
fprintf(fp, "START TIME: %s\n", strfbuf);
|
|
|
|
fprintf(fp, "LABEL: %s\n", backupidstr);
|
|
|
|
if (fflush(fp) || ferror(fp) || FreeFile(fp))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not write file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
BACKUP_LABEL_FILE)));
|
2004-08-03 22:32:36 +02:00
|
|
|
}
|
2008-04-17 01:59:40 +02:00
|
|
|
PG_END_ENSURE_ERROR_CLEANUP(pg_start_backup_callback, (Datum) 0);
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2006-08-06 05:53:44 +02:00
|
|
|
* We're done. As a convenience, return the starting WAL location.
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
|
|
|
snprintf(xlogfilename, sizeof(xlogfilename), "%X/%X",
|
|
|
|
startpoint.xlogid, startpoint.xrecoff);
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
|
2004-08-03 22:32:36 +02:00
|
|
|
}
|
|
|
|
|
2008-04-17 01:59:40 +02:00
|
|
|
/* Error cleanup callback for pg_start_backup */
|
|
|
|
static void
|
|
|
|
pg_start_backup_callback(int code, Datum arg)
|
|
|
|
{
|
|
|
|
/* Turn off forcePageWrites on failure */
|
|
|
|
LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
|
|
|
|
XLogCtl->Insert.forcePageWrites = false;
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
}
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
|
|
|
* pg_stop_backup: finish taking an on-line backup dump
|
|
|
|
*
|
2010-01-04 13:50:50 +01:00
|
|
|
* We write an end-of-backup WAL record, and remove the backup label file
|
|
|
|
* created by pg_start_backup, creating a backup history file in pg_xlog
|
|
|
|
* instead (whence it will immediately be archived). The backup history file
|
|
|
|
* contains the same info found in the label file, plus the backup-end time
|
2010-02-17 05:19:41 +01:00
|
|
|
* and WAL location. Before 9.0, the backup-end time was read from the backup
|
2010-01-04 13:50:50 +01:00
|
|
|
* history file at the beginning of archive recovery, but we now use the WAL
|
|
|
|
* record for that and the file is for informational and debug purposes only.
|
|
|
|
*
|
2008-04-23 15:44:59 +02:00
|
|
|
* Note: different from CancelBackup which just cancels online backup mode.
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_stop_backup(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
XLogRecPtr startpoint;
|
|
|
|
XLogRecPtr stoppoint;
|
2010-02-26 03:01:40 +01:00
|
|
|
XLogRecData rdata;
|
2007-08-04 03:26:54 +02:00
|
|
|
pg_time_t stamp_time;
|
2004-08-03 22:32:36 +02:00
|
|
|
char strfbuf[128];
|
2004-08-04 18:25:02 +02:00
|
|
|
char histfilepath[MAXPGPATH];
|
2004-08-03 22:32:36 +02:00
|
|
|
char startxlogfilename[MAXFNAMELEN];
|
|
|
|
char stopxlogfilename[MAXFNAMELEN];
|
2008-12-03 09:20:11 +01:00
|
|
|
char lastxlogfilename[MAXFNAMELEN];
|
|
|
|
char histfilename[MAXFNAMELEN];
|
2004-08-03 22:32:36 +02:00
|
|
|
uint32 _logId;
|
|
|
|
uint32 _logSeg;
|
|
|
|
FILE *lfp;
|
|
|
|
FILE *fp;
|
|
|
|
char ch;
|
|
|
|
int ich;
|
2008-04-05 03:34:06 +02:00
|
|
|
int seconds_before_warning;
|
|
|
|
int waits = 0;
|
2010-04-18 20:44:53 +02:00
|
|
|
bool reported_waiting = false;
|
2004-08-03 22:32:36 +02:00
|
|
|
|
2004-08-29 07:07:03 +02:00
|
|
|
if (!superuser())
|
2004-08-03 22:32:36 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to run a backup"))));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("recovery is in progress"),
|
|
|
|
errhint("WAL control functions cannot be executed during recovery.")));
|
|
|
|
|
2010-04-29 23:49:03 +02:00
|
|
|
if (!XLogIsNeeded())
|
2008-09-08 18:42:15 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2010-07-06 21:19:02 +02:00
|
|
|
errmsg("WAL level not sufficient for making an online backup"),
|
2010-04-29 23:49:03 +02:00
|
|
|
errhint("wal_level must be set to \"archive\" or \"hot_standby\" at server start.")));
|
2008-09-08 18:42:15 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2006-08-06 05:53:44 +02:00
|
|
|
* OK to clear forcePageWrites
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
|
|
|
LWLockAcquire(WALInsertLock, LW_EXCLUSIVE);
|
2006-04-17 20:55:05 +02:00
|
|
|
XLogCtl->Insert.forcePageWrites = false;
|
2004-08-03 22:32:36 +02:00
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Open the existing label file
|
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
|
2004-08-03 22:32:36 +02:00
|
|
|
if (!lfp)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
BACKUP_LABEL_FILE)));
|
2004-08-03 22:32:36 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("a backup is not in progress")));
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Read and parse the START WAL LOCATION line (this code is pretty crude,
|
|
|
|
* but we are not expecting any variability in the file format).
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
|
|
|
if (fscanf(lfp, "START WAL LOCATION: %X/%X (file %24s)%c",
|
|
|
|
&startpoint.xlogid, &startpoint.xrecoff, startxlogfilename,
|
|
|
|
&ch) != 4 || ch != '\n')
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2005-07-04 06:51:52 +02:00
|
|
|
errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2010-01-04 13:50:50 +01:00
|
|
|
/*
|
|
|
|
* Write the backup-end xlog record
|
|
|
|
*/
|
|
|
|
rdata.data = (char *) (&startpoint);
|
|
|
|
rdata.len = sizeof(startpoint);
|
|
|
|
rdata.buffer = InvalidBuffer;
|
|
|
|
rdata.next = NULL;
|
|
|
|
stoppoint = XLogInsert(RM_XLOG_ID, XLOG_BACKUP_END, &rdata);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Force a switch to a new xlog segment file, so that the backup is valid
|
|
|
|
* as soon as archiver moves out the current segment file.
|
|
|
|
*/
|
|
|
|
RequestXLogSwitch();
|
|
|
|
|
2010-02-19 02:04:03 +01:00
|
|
|
XLByteToPrevSeg(stoppoint, _logId, _logSeg);
|
2010-01-04 13:50:50 +01:00
|
|
|
XLogFileName(stopxlogfilename, ThisTimeLineID, _logId, _logSeg);
|
|
|
|
|
|
|
|
/* Use the log timezone here, not the session timezone */
|
|
|
|
stamp_time = (pg_time_t) time(NULL);
|
|
|
|
pg_strftime(strfbuf, sizeof(strfbuf),
|
|
|
|
"%Y-%m-%d %H:%M:%S %Z",
|
|
|
|
pg_localtime(&stamp_time, log_timezone));
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
|
|
|
* Write the backup history file
|
|
|
|
*/
|
|
|
|
XLByteToSeg(startpoint, _logId, _logSeg);
|
2004-08-04 18:25:02 +02:00
|
|
|
BackupHistoryFilePath(histfilepath, ThisTimeLineID, _logId, _logSeg,
|
2004-08-03 22:32:36 +02:00
|
|
|
startpoint.xrecoff % XLogSegSize);
|
2004-08-04 18:25:02 +02:00
|
|
|
fp = AllocateFile(histfilepath, "w");
|
2004-08-03 22:32:36 +02:00
|
|
|
if (!fp)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not create file \"%s\": %m",
|
2004-08-04 18:25:02 +02:00
|
|
|
histfilepath)));
|
2004-08-03 22:32:36 +02:00
|
|
|
fprintf(fp, "START WAL LOCATION: %X/%X (file %s)\n",
|
|
|
|
startpoint.xlogid, startpoint.xrecoff, startxlogfilename);
|
|
|
|
fprintf(fp, "STOP WAL LOCATION: %X/%X (file %s)\n",
|
|
|
|
stoppoint.xlogid, stoppoint.xrecoff, stopxlogfilename);
|
2004-08-04 18:25:02 +02:00
|
|
|
/* transfer remaining lines from label to history file */
|
2004-08-03 22:32:36 +02:00
|
|
|
while ((ich = fgetc(lfp)) != EOF)
|
|
|
|
fputc(ich, fp);
|
|
|
|
fprintf(fp, "STOP TIME: %s\n", strfbuf);
|
|
|
|
if (fflush(fp) || ferror(fp) || FreeFile(fp))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not write file \"%s\": %m",
|
2004-08-04 18:25:02 +02:00
|
|
|
histfilepath)));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
|
|
|
* Close and remove the backup label file
|
|
|
|
*/
|
|
|
|
if (ferror(lfp) || FreeFile(lfp))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
BACKUP_LABEL_FILE)));
|
|
|
|
if (unlink(BACKUP_LABEL_FILE) != 0)
|
2004-08-03 22:32:36 +02:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not remove file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
BACKUP_LABEL_FILE)));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Clean out any no-longer-needed history files. As a side effect, this
|
|
|
|
* will post a .ready file for the newly created history file, notifying
|
|
|
|
* the archiver that history file may be archived immediately.
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
2006-06-22 22:42:57 +02:00
|
|
|
CleanupBackupHistory();
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2008-04-05 03:34:06 +02:00
|
|
|
/*
|
2010-04-29 23:49:03 +02:00
|
|
|
* If archiving is enabled, wait for all the required WAL files to be
|
2010-07-06 21:19:02 +02:00
|
|
|
* archived before returning. If archiving isn't enabled, the required WAL
|
|
|
|
* needs to be transported via streaming replication (hopefully with
|
|
|
|
* wal_keep_segments set high enough), or some more exotic mechanism like
|
|
|
|
* polling and copying files from pg_xlog with script. We have no
|
|
|
|
* knowledge of those mechanisms, so it's up to the user to ensure that he
|
|
|
|
* gets all the required WAL.
|
2010-04-29 23:49:03 +02:00
|
|
|
*
|
|
|
|
* We wait until both the last WAL file filled during backup and the
|
2010-07-06 21:19:02 +02:00
|
|
|
* history file have been archived, and assume that the alphabetic sorting
|
|
|
|
* property of the WAL files ensures any earlier WAL files are safely
|
|
|
|
* archived as well.
|
2008-04-05 03:34:06 +02:00
|
|
|
*
|
2009-06-11 16:49:15 +02:00
|
|
|
* We wait forever, since archive_command is supposed to work and we
|
|
|
|
* assume the admin wanted his backup to work completely. If you don't
|
2010-02-26 03:01:40 +01:00
|
|
|
* wish to wait, you can set statement_timeout. Also, some notices are
|
|
|
|
* issued to clue in anyone who might be doing this interactively.
|
2008-04-05 03:34:06 +02:00
|
|
|
*/
|
2010-04-29 23:49:03 +02:00
|
|
|
if (XLogArchivingActive())
|
|
|
|
{
|
2010-07-06 21:19:02 +02:00
|
|
|
XLByteToPrevSeg(stoppoint, _logId, _logSeg);
|
|
|
|
XLogFileName(lastxlogfilename, ThisTimeLineID, _logId, _logSeg);
|
2008-04-05 03:34:06 +02:00
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
XLByteToSeg(startpoint, _logId, _logSeg);
|
|
|
|
BackupHistoryFileName(histfilename, ThisTimeLineID, _logId, _logSeg,
|
|
|
|
startpoint.xrecoff % XLogSegSize);
|
2008-04-05 03:34:06 +02:00
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
seconds_before_warning = 60;
|
|
|
|
waits = 0;
|
2008-04-05 03:34:06 +02:00
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
while (XLogArchiveIsBusy(lastxlogfilename) ||
|
|
|
|
XLogArchiveIsBusy(histfilename))
|
2010-04-18 20:44:53 +02:00
|
|
|
{
|
2010-07-06 21:19:02 +02:00
|
|
|
CHECK_FOR_INTERRUPTS();
|
2010-04-18 20:44:53 +02:00
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
if (!reported_waiting && waits > 5)
|
|
|
|
{
|
|
|
|
ereport(NOTICE,
|
|
|
|
(errmsg("pg_stop_backup cleanup done, waiting for required WAL segments to be archived")));
|
|
|
|
reported_waiting = true;
|
|
|
|
}
|
2008-04-05 03:34:06 +02:00
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
pg_usleep(1000000L);
|
|
|
|
|
|
|
|
if (++waits >= seconds_before_warning)
|
|
|
|
{
|
|
|
|
seconds_before_warning *= 2; /* This wraps in >10 years... */
|
|
|
|
ereport(WARNING,
|
|
|
|
(errmsg("pg_stop_backup still waiting for all required WAL segments to be archived (%d seconds elapsed)",
|
|
|
|
waits),
|
|
|
|
errhint("Check that your archive_command is executing properly. "
|
|
|
|
"pg_stop_backup can be cancelled safely, "
|
|
|
|
"but the database backup will not be usable without all the WAL segments.")));
|
|
|
|
}
|
2008-04-05 03:34:06 +02:00
|
|
|
}
|
|
|
|
|
2010-07-06 21:19:02 +02:00
|
|
|
ereport(NOTICE,
|
|
|
|
(errmsg("pg_stop_backup complete, all required WAL segments have been archived")));
|
2010-04-29 23:49:03 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ereport(NOTICE,
|
|
|
|
(errmsg("WAL archiving is not enabled; you must ensure that all required WAL segments are copied through other means to complete the backup")));
|
2010-04-18 20:44:53 +02:00
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
/*
|
2006-08-06 05:53:44 +02:00
|
|
|
* We're done. As a convenience, return the ending WAL location.
|
2004-08-03 22:32:36 +02:00
|
|
|
*/
|
|
|
|
snprintf(stopxlogfilename, sizeof(stopxlogfilename), "%X/%X",
|
|
|
|
stoppoint.xlogid, stoppoint.xrecoff);
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(stopxlogfilename));
|
2004-08-03 22:32:36 +02:00
|
|
|
}
|
2004-08-04 18:25:02 +02:00
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/*
|
|
|
|
* pg_switch_xlog: switch to next xlog file
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_switch_xlog(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
XLogRecPtr switchpoint;
|
2006-08-06 05:53:44 +02:00
|
|
|
char location[MAXFNAMELEN];
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2007-11-15 22:14:46 +01:00
|
|
|
(errmsg("must be superuser to switch transaction log files"))));
|
2006-08-06 05:53:44 +02:00
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("recovery is in progress"),
|
|
|
|
errhint("WAL control functions cannot be executed during recovery.")));
|
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
switchpoint = RequestXLogSwitch();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* As a convenience, return the WAL location of the switch record
|
|
|
|
*/
|
|
|
|
snprintf(location, sizeof(location), "%X/%X",
|
|
|
|
switchpoint.xlogid, switchpoint.xrecoff);
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(location));
|
2006-08-06 05:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-08-18 01:04:10 +02:00
|
|
|
* Report the current WAL write location (same format as pg_start_backup etc)
|
|
|
|
*
|
|
|
|
* This is useful for determining how much of WAL is visible to an external
|
|
|
|
* archiving process. Note that the data before this point is written out
|
|
|
|
* to the kernel, but is not necessarily synced to disk.
|
2006-08-06 05:53:44 +02:00
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_current_xlog_location(PG_FUNCTION_ARGS)
|
2006-08-18 01:04:10 +02:00
|
|
|
{
|
|
|
|
char location[MAXFNAMELEN];
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("recovery is in progress"),
|
|
|
|
errhint("WAL control functions cannot be executed during recovery.")));
|
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
/* Make sure we have an up-to-date local LogwrtResult */
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
LogwrtResult = xlogctl->LogwrtResult;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(location, sizeof(location), "%X/%X",
|
|
|
|
LogwrtResult.Write.xlogid, LogwrtResult.Write.xrecoff);
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(location));
|
2006-08-18 01:04:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report the current WAL insert location (same format as pg_start_backup etc)
|
|
|
|
*
|
|
|
|
* This function is mostly for debugging purposes.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_current_xlog_insert_location(PG_FUNCTION_ARGS)
|
2006-08-06 05:53:44 +02:00
|
|
|
{
|
|
|
|
XLogCtlInsert *Insert = &XLogCtl->Insert;
|
|
|
|
XLogRecPtr current_recptr;
|
|
|
|
char location[MAXFNAMELEN];
|
|
|
|
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("recovery is in progress"),
|
|
|
|
errhint("WAL control functions cannot be executed during recovery.")));
|
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/*
|
|
|
|
* Get the current end-of-WAL position ... shared lock is sufficient
|
|
|
|
*/
|
|
|
|
LWLockAcquire(WALInsertLock, LW_SHARED);
|
|
|
|
INSERT_RECPTR(current_recptr, Insert, Insert->curridx);
|
|
|
|
LWLockRelease(WALInsertLock);
|
|
|
|
|
|
|
|
snprintf(location, sizeof(location), "%X/%X",
|
|
|
|
current_recptr.xlogid, current_recptr.xrecoff);
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(location));
|
2006-08-06 05:53:44 +02:00
|
|
|
}
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Report the last WAL receive location (same format as pg_start_backup etc)
|
|
|
|
*
|
|
|
|
* This is useful for determining how much of WAL is guaranteed to be received
|
|
|
|
* and synced to disk by walreceiver.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_last_xlog_receive_location(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
char location[MAXFNAMELEN];
|
|
|
|
|
2010-07-03 22:43:58 +02:00
|
|
|
recptr = GetWalRcvWriteRecPtr(NULL);
|
2010-01-15 10:19:10 +01:00
|
|
|
|
2010-06-10 09:00:27 +02:00
|
|
|
if (recptr.xlogid == 0 && recptr.xrecoff == 0)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
snprintf(location, sizeof(location), "%X/%X",
|
|
|
|
recptr.xlogid, recptr.xrecoff);
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(location));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report the last WAL replay location (same format as pg_start_backup etc)
|
|
|
|
*
|
|
|
|
* This is useful for determining how much of WAL is visible to read-only
|
|
|
|
* connections during recovery.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_last_xlog_replay_location(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
/* use volatile pointer to prevent code rearrangement */
|
|
|
|
volatile XLogCtlData *xlogctl = XLogCtl;
|
|
|
|
XLogRecPtr recptr;
|
|
|
|
char location[MAXFNAMELEN];
|
|
|
|
|
|
|
|
SpinLockAcquire(&xlogctl->info_lck);
|
|
|
|
recptr = xlogctl->recoveryLastRecPtr;
|
|
|
|
SpinLockRelease(&xlogctl->info_lck);
|
|
|
|
|
2010-06-10 09:00:27 +02:00
|
|
|
if (recptr.xlogid == 0 && recptr.xrecoff == 0)
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
snprintf(location, sizeof(location), "%X/%X",
|
|
|
|
recptr.xlogid, recptr.xrecoff);
|
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(location));
|
|
|
|
}
|
|
|
|
|
2006-08-06 05:53:44 +02:00
|
|
|
/*
|
|
|
|
* Compute an xlog file name and decimal byte offset given a WAL location,
|
|
|
|
* such as is returned by pg_stop_backup() or pg_xlog_switch().
|
|
|
|
*
|
|
|
|
* Note that a location exactly at a segment boundary is taken to be in
|
|
|
|
* the previous segment. This is usually the right thing, since the
|
|
|
|
* expected usage is to determine which xlog file(s) are ready to archive.
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_xlogfile_name_offset(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *location = PG_GETARG_TEXT_P(0);
|
|
|
|
char *locationstr;
|
|
|
|
unsigned int uxlogid;
|
|
|
|
unsigned int uxrecoff;
|
|
|
|
uint32 xlogid;
|
|
|
|
uint32 xlogseg;
|
|
|
|
uint32 xrecoff;
|
|
|
|
XLogRecPtr locationpoint;
|
|
|
|
char xlogfilename[MAXFNAMELEN];
|
2006-10-04 02:30:14 +02:00
|
|
|
Datum values[2];
|
|
|
|
bool isnull[2];
|
|
|
|
TupleDesc resultTupleDesc;
|
|
|
|
HeapTuple resultHeapTuple;
|
|
|
|
Datum result;
|
2006-08-06 05:53:44 +02:00
|
|
|
|
2010-04-07 08:12:52 +02:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("recovery is in progress"),
|
|
|
|
errhint("pg_xlogfile_name_offset() cannot be executed during recovery.")));
|
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
/*
|
|
|
|
* Read input and parse
|
|
|
|
*/
|
2008-03-25 23:42:46 +01:00
|
|
|
locationstr = text_to_cstring(location);
|
2006-08-06 05:53:44 +02:00
|
|
|
|
|
|
|
if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2006-10-06 19:14:01 +02:00
|
|
|
errmsg("could not parse transaction log location \"%s\"",
|
2006-08-06 05:53:44 +02:00
|
|
|
locationstr)));
|
|
|
|
|
|
|
|
locationpoint.xlogid = uxlogid;
|
|
|
|
locationpoint.xrecoff = uxrecoff;
|
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Construct a tuple descriptor for the result row. This must match this
|
|
|
|
* function's pg_proc entry!
|
2006-08-18 01:04:10 +02:00
|
|
|
*/
|
|
|
|
resultTupleDesc = CreateTemplateTupleDesc(2, false);
|
|
|
|
TupleDescInitEntry(resultTupleDesc, (AttrNumber) 1, "file_name",
|
|
|
|
TEXTOID, -1, 0);
|
|
|
|
TupleDescInitEntry(resultTupleDesc, (AttrNumber) 2, "file_offset",
|
|
|
|
INT4OID, -1, 0);
|
|
|
|
|
|
|
|
resultTupleDesc = BlessTupleDesc(resultTupleDesc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* xlogfilename
|
|
|
|
*/
|
2006-08-06 05:53:44 +02:00
|
|
|
XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
|
|
|
|
XLogFileName(xlogfilename, ThisTimeLineID, xlogid, xlogseg);
|
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
values[0] = CStringGetTextDatum(xlogfilename);
|
2006-08-18 01:04:10 +02:00
|
|
|
isnull[0] = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* offset
|
|
|
|
*/
|
2006-08-06 05:53:44 +02:00
|
|
|
xrecoff = locationpoint.xrecoff - xlogseg * XLogSegSize;
|
|
|
|
|
2006-08-18 01:04:10 +02:00
|
|
|
values[1] = UInt32GetDatum(xrecoff);
|
|
|
|
isnull[1] = false;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Tuple jam: Having first prepared your Datums, then squash together
|
|
|
|
*/
|
|
|
|
resultHeapTuple = heap_form_tuple(resultTupleDesc, values, isnull);
|
|
|
|
|
|
|
|
result = HeapTupleGetDatum(resultHeapTuple);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
2006-08-06 05:53:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Compute an xlog file name given a WAL location,
|
|
|
|
* such as is returned by pg_stop_backup() or pg_xlog_switch().
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
pg_xlogfile_name(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *location = PG_GETARG_TEXT_P(0);
|
|
|
|
char *locationstr;
|
|
|
|
unsigned int uxlogid;
|
|
|
|
unsigned int uxrecoff;
|
|
|
|
uint32 xlogid;
|
|
|
|
uint32 xlogseg;
|
|
|
|
XLogRecPtr locationpoint;
|
|
|
|
char xlogfilename[MAXFNAMELEN];
|
|
|
|
|
2010-04-07 08:12:52 +02:00
|
|
|
if (RecoveryInProgress())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
|
|
|
errmsg("recovery is in progress"),
|
2010-07-06 21:19:02 +02:00
|
|
|
errhint("pg_xlogfile_name() cannot be executed during recovery.")));
|
2010-04-07 08:12:52 +02:00
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
locationstr = text_to_cstring(location);
|
2006-08-06 05:53:44 +02:00
|
|
|
|
|
|
|
if (sscanf(locationstr, "%X/%X", &uxlogid, &uxrecoff) != 2)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
2006-11-16 15:28:41 +01:00
|
|
|
errmsg("could not parse transaction log location \"%s\"",
|
2006-08-06 05:53:44 +02:00
|
|
|
locationstr)));
|
|
|
|
|
|
|
|
locationpoint.xlogid = uxlogid;
|
|
|
|
locationpoint.xrecoff = uxrecoff;
|
|
|
|
|
|
|
|
XLByteToPrevSeg(locationpoint, xlogid, xlogseg);
|
|
|
|
XLogFileName(xlogfilename, ThisTimeLineID, xlogid, xlogseg);
|
|
|
|
|
2008-03-25 23:42:46 +01:00
|
|
|
PG_RETURN_TEXT_P(cstring_to_text(xlogfilename));
|
2006-08-06 05:53:44 +02:00
|
|
|
}
|
|
|
|
|
2004-08-04 18:25:02 +02:00
|
|
|
/*
|
|
|
|
* read_backup_label: check to see if a backup_label file is present
|
|
|
|
*
|
|
|
|
* If we see a backup_label during recovery, we assume that we are recovering
|
|
|
|
* from a backup dump file, and we therefore roll forward from the checkpoint
|
2004-08-29 07:07:03 +02:00
|
|
|
* identified by the label file, NOT what pg_control says. This avoids the
|
2004-08-04 18:25:02 +02:00
|
|
|
* problem that pg_control might have been archived one or more checkpoints
|
|
|
|
* later than the start of the dump, and so if we rely on it as the start
|
|
|
|
* point, we will fail to restore a consistent database state.
|
|
|
|
*
|
|
|
|
* Returns TRUE if a backup_label was found (and fills the checkpoint
|
2010-01-15 10:19:10 +01:00
|
|
|
* location and its REDO location into *checkPointLoc and RedoStartLSN,
|
|
|
|
* respectively); returns FALSE if not.
|
2004-08-04 18:25:02 +02:00
|
|
|
*/
|
|
|
|
static bool
|
2010-01-04 13:50:50 +01:00
|
|
|
read_backup_label(XLogRecPtr *checkPointLoc)
|
2004-08-04 18:25:02 +02:00
|
|
|
{
|
|
|
|
char startxlogfilename[MAXFNAMELEN];
|
|
|
|
TimeLineID tli;
|
|
|
|
FILE *lfp;
|
|
|
|
char ch;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if label file is present
|
|
|
|
*/
|
2005-07-04 06:51:52 +02:00
|
|
|
lfp = AllocateFile(BACKUP_LABEL_FILE, "r");
|
2004-08-04 18:25:02 +02:00
|
|
|
if (!lfp)
|
|
|
|
{
|
|
|
|
if (errno != ENOENT)
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
BACKUP_LABEL_FILE)));
|
2004-08-04 18:25:02 +02:00
|
|
|
return false; /* it's not there, all is fine */
|
|
|
|
}
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-04 18:25:02 +02:00
|
|
|
/*
|
2005-10-15 04:49:52 +02:00
|
|
|
* Read and parse the START WAL LOCATION and CHECKPOINT lines (this code
|
|
|
|
* is pretty crude, but we are not expecting any variability in the file
|
|
|
|
* format).
|
2004-08-04 18:25:02 +02:00
|
|
|
*/
|
|
|
|
if (fscanf(lfp, "START WAL LOCATION: %X/%X (file %08X%16s)%c",
|
2010-01-15 10:19:10 +01:00
|
|
|
&RedoStartLSN.xlogid, &RedoStartLSN.xrecoff, &tli,
|
2004-08-04 18:25:02 +02:00
|
|
|
startxlogfilename, &ch) != 5 || ch != '\n')
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2005-07-04 06:51:52 +02:00
|
|
|
errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
|
2004-08-04 18:25:02 +02:00
|
|
|
if (fscanf(lfp, "CHECKPOINT LOCATION: %X/%X%c",
|
|
|
|
&checkPointLoc->xlogid, &checkPointLoc->xrecoff,
|
|
|
|
&ch) != 3 || ch != '\n')
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
|
2005-07-04 06:51:52 +02:00
|
|
|
errmsg("invalid data in file \"%s\"", BACKUP_LABEL_FILE)));
|
2004-08-04 18:25:02 +02:00
|
|
|
if (ferror(lfp) || FreeFile(lfp))
|
|
|
|
ereport(FATAL,
|
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read file \"%s\": %m",
|
2005-07-04 06:51:52 +02:00
|
|
|
BACKUP_LABEL_FILE)));
|
2004-08-29 07:07:03 +02:00
|
|
|
|
2004-08-04 18:25:02 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-03-24 05:32:13 +01:00
|
|
|
/*
|
|
|
|
* Error context callback for errors occurring during rm_redo().
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
rm_redo_error_callback(void *arg)
|
|
|
|
{
|
2006-10-04 02:30:14 +02:00
|
|
|
XLogRecord *record = (XLogRecord *) arg;
|
|
|
|
StringInfoData buf;
|
2006-03-24 05:32:13 +01:00
|
|
|
|
|
|
|
initStringInfo(&buf);
|
2006-08-06 05:53:44 +02:00
|
|
|
RmgrTable[record->xl_rmid].rm_desc(&buf,
|
|
|
|
record->xl_info,
|
2006-03-24 05:32:13 +01:00
|
|
|
XLogRecGetData(record));
|
|
|
|
|
|
|
|
/* don't bother emitting empty description */
|
|
|
|
if (buf.len > 0)
|
|
|
|
errcontext("xlog redo %s", buf.data);
|
|
|
|
|
|
|
|
pfree(buf.data);
|
|
|
|
}
|
2008-04-23 15:44:59 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* BackupInProgress: check if online backup mode is active
|
|
|
|
*
|
|
|
|
* This is done by checking for existence of the "backup_label" file.
|
|
|
|
*/
|
|
|
|
bool
|
|
|
|
BackupInProgress(void)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
return (stat(BACKUP_LABEL_FILE, &stat_buf) == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* CancelBackup: rename the "backup_label" file to cancel backup mode
|
|
|
|
*
|
|
|
|
* If the "backup_label" file exists, it will be renamed to "backup_label.old".
|
|
|
|
* Note that this will render an online backup in progress useless.
|
|
|
|
* To correctly finish an online backup, pg_stop_backup must be called.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
CancelBackup(void)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
/* if the file is not there, return */
|
|
|
|
if (stat(BACKUP_LABEL_FILE, &stat_buf) < 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* remove leftover file from previously cancelled backup if it exists */
|
|
|
|
unlink(BACKUP_LABEL_OLD);
|
|
|
|
|
|
|
|
if (rename(BACKUP_LABEL_FILE, BACKUP_LABEL_OLD) == 0)
|
|
|
|
{
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("online backup mode cancelled"),
|
2008-04-24 16:23:43 +02:00
|
|
|
errdetail("\"%s\" was renamed to \"%s\".",
|
2009-06-11 16:49:15 +02:00
|
|
|
BACKUP_LABEL_FILE, BACKUP_LABEL_OLD)));
|
2008-04-23 15:44:59 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ereport(WARNING,
|
|
|
|
(errcode_for_file_access(),
|
2008-04-24 16:23:43 +02:00
|
|
|
errmsg("online backup mode was not cancelled"),
|
|
|
|
errdetail("Could not rename \"%s\" to \"%s\": %m.",
|
2009-06-11 16:49:15 +02:00
|
|
|
BACKUP_LABEL_FILE, BACKUP_LABEL_OLD)));
|
2008-04-23 15:44:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* ------------------------------------------------------
|
2009-06-11 16:49:15 +02:00
|
|
|
* Startup Process main entry point and signal handlers
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* ------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* startupproc_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
|
|
|
|
startupproc_quickdie(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
PG_SETMASK(&BlockSig);
|
|
|
|
|
|
|
|
/*
|
2009-05-15 17:56:39 +02:00
|
|
|
* We DO NOT want to run proc_exit() callbacks -- we're here because
|
|
|
|
* shared memory may be corrupted, so we don't want to try to clean up our
|
|
|
|
* transaction. Just nail the windows shut and get out of town. Now that
|
|
|
|
* there's an atexit callback to prevent third-party code from breaking
|
|
|
|
* things by calling exit() directly, we have to reset the callbacks
|
|
|
|
* explicitly to make this work as intended.
|
|
|
|
*/
|
|
|
|
on_exit_reset();
|
|
|
|
|
|
|
|
/*
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
* Note we do exit(2) not exit(0). This is to force the postmaster into a
|
|
|
|
* 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
|
2009-05-15 17:56:39 +02:00
|
|
|
* shared memory state. (The "dead man switch" mechanism in pmsignal.c
|
2009-06-11 16:49:15 +02:00
|
|
|
* should ensure the postmaster sees this as a crash, too, but no harm in
|
|
|
|
* being doubly sure.)
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
|
|
|
exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-09-15 12:35:05 +02:00
|
|
|
/* SIGUSR1: let latch facility handle the signal */
|
|
|
|
static void
|
|
|
|
StartupProcSigUsr1Handler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
latch_sigusr1_handler();
|
|
|
|
}
|
|
|
|
|
2009-03-04 14:56:40 +01:00
|
|
|
/* SIGHUP: set flag to re-read config file at next convenient time */
|
|
|
|
static void
|
|
|
|
StartupProcSigHupHandler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
got_SIGHUP = true;
|
2010-10-14 20:15:26 +02:00
|
|
|
WakeupRecovery();
|
2009-03-04 14:56:40 +01:00
|
|
|
}
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* SIGTERM: set flag to abort redo and exit */
|
|
|
|
static void
|
|
|
|
StartupProcShutdownHandler(SIGNAL_ARGS)
|
|
|
|
{
|
|
|
|
if (in_restore_command)
|
2009-02-23 10:28:50 +01:00
|
|
|
proc_exit(1);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
else
|
|
|
|
shutdown_requested = true;
|
2010-10-14 20:15:26 +02:00
|
|
|
WakeupRecovery();
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
}
|
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/* Handle SIGHUP and SIGTERM signals of startup process */
|
|
|
|
void
|
|
|
|
HandleStartupProcInterrupts(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Check if we were requested to re-read config file.
|
|
|
|
*/
|
|
|
|
if (got_SIGHUP)
|
|
|
|
{
|
|
|
|
got_SIGHUP = false;
|
|
|
|
ProcessConfigFile(PGC_SIGHUP);
|
|
|
|
}
|
2010-02-26 03:01:40 +01:00
|
|
|
|
2010-01-15 10:19:10 +01:00
|
|
|
/*
|
|
|
|
* Check if we were requested to exit without finishing recovery.
|
|
|
|
*/
|
|
|
|
if (shutdown_requested)
|
|
|
|
proc_exit(1);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Emergency bailout if postmaster has died. This is to avoid the
|
|
|
|
* necessity for manual cleanup of all postmaster children.
|
|
|
|
*/
|
|
|
|
if (IsUnderPostmaster && !PostmasterIsAlive(true))
|
|
|
|
exit(1);
|
2010-01-15 10:19:10 +01:00
|
|
|
}
|
|
|
|
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
/* Main entry point for startup process */
|
|
|
|
void
|
|
|
|
StartupProcessMain(void)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If possible, make this process a group leader, so that the postmaster
|
|
|
|
* can signal any child processes too.
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_SETSID
|
|
|
|
if (setsid() < 0)
|
|
|
|
elog(FATAL, "setsid() failed: %m");
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
2010-04-29 23:36:19 +02:00
|
|
|
* Properly accept or ignore signals the postmaster might send us.
|
|
|
|
*
|
|
|
|
* Note: ideally we'd not enable handle_standby_sig_alarm unless actually
|
|
|
|
* doing hot standby, but we don't know that yet. Rely on it to not do
|
|
|
|
* anything if it shouldn't.
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
*/
|
2009-06-11 16:49:15 +02:00
|
|
|
pqsignal(SIGHUP, StartupProcSigHupHandler); /* reload config file */
|
|
|
|
pqsignal(SIGINT, SIG_IGN); /* ignore query cancel */
|
2010-02-26 03:01:40 +01:00
|
|
|
pqsignal(SIGTERM, StartupProcShutdownHandler); /* request shutdown */
|
|
|
|
pqsignal(SIGQUIT, startupproc_quickdie); /* hard crash time */
|
2010-04-29 23:36:19 +02:00
|
|
|
if (EnableHotStandby)
|
2010-02-26 03:01:40 +01:00
|
|
|
pqsignal(SIGALRM, handle_standby_sig_alarm); /* ignored unless
|
|
|
|
* InHotStandby */
|
2010-01-23 17:37:12 +01:00
|
|
|
else
|
|
|
|
pqsignal(SIGALRM, SIG_IGN);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
pqsignal(SIGPIPE, SIG_IGN);
|
2010-09-15 12:35:05 +02:00
|
|
|
pqsignal(SIGUSR1, StartupProcSigUsr1Handler);
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
pqsignal(SIGUSR2, SIG_IGN);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unblock signals (they were blocked when the postmaster forked us)
|
|
|
|
*/
|
|
|
|
PG_SETMASK(&UnBlockSig);
|
|
|
|
|
2009-06-11 16:49:15 +02:00
|
|
|
StartupXLOG();
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
|
2009-02-23 10:28:50 +01:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Exit normally. Exit code 0 tells postmaster that we completed recovery
|
|
|
|
* successfully.
|
2009-02-23 10:28:50 +01:00
|
|
|
*/
|
Start background writer during archive recovery. Background writer now performs
its usual buffer cleaning duties during archive recovery, and it's responsible
for performing restartpoints.
This requires some changes in postmaster. When the startup process has done
all the initialization and is ready to start WAL redo, it signals the
postmaster to launch the background writer. The postmaster is signaled again
when the point in recovery is reached where we know that the database is in
consistent state. Postmaster isn't interested in that at the moment, but
that's the point where we could let other backends in to perform read-only
queries. The postmaster is signaled third time when the recovery has ended,
so that postmaster knows that it's safe to start accepting connections.
The startup process now traps SIGTERM, and performs a "clean" shutdown. If
you do a fast shutdown during recovery, a shutdown restartpoint is performed,
like a shutdown checkpoint, and postmaster kills the processes cleanly. You
still have to continue the recovery at next startup, though.
Currently, the background writer is only launched during archive recovery.
We could launch it during crash recovery as well, but it seems better to keep
that codepath as simple as possible, for the sake of robustness. And it
couldn't do any restartpoints during crash recovery anyway, so it wouldn't be
that useful.
log_restartpoints is gone. Use log_checkpoints instead. This is yet to be
documented.
This whole operation is a pre-requisite for Hot Standby, but has some value of
its own whether the hot standby patch makes 8.4 or not.
Simon Riggs, with lots of modifications by me.
2009-02-18 16:58:41 +01:00
|
|
|
proc_exit(0);
|
|
|
|
}
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read the XLOG page containing RecPtr into readBuf (if not read already).
|
2010-03-30 18:23:57 +02:00
|
|
|
* Returns true if the page is read successfully.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*
|
|
|
|
* This is responsible for restoring files from archive as needed, as well
|
|
|
|
* as for waiting for the requested WAL record to arrive in standby mode.
|
2010-03-30 18:23:57 +02:00
|
|
|
*
|
|
|
|
* 'emode' specifies the log level used for reporting "file not found" or
|
|
|
|
* "end of WAL" situations in archive recovery, or in standby mode when a
|
|
|
|
* trigger file is found. If set to WARNING or below, XLogPageRead() returns
|
|
|
|
* false in those situations, on higher log levels the ereport() won't
|
|
|
|
* return.
|
|
|
|
*
|
|
|
|
* In standby mode, if after a successful return of XLogPageRead() the
|
|
|
|
* caller finds the record it's interested in to be broken, it should
|
|
|
|
* ereport the error with the level determined by
|
|
|
|
* emode_for_corrupt_record(), and then set "failedSources |= readSource"
|
|
|
|
* and call XLogPageRead() again with the same arguments. This lets
|
|
|
|
* XLogPageRead() to try fetching the record from another source, or to
|
|
|
|
* sleep and retry.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
XLogPageRead(XLogRecPtr *RecPtr, int emode, bool fetching_ckpt,
|
|
|
|
bool randAccess)
|
|
|
|
{
|
|
|
|
static XLogRecPtr receivedUpto = {0, 0};
|
|
|
|
bool switched_segment = false;
|
|
|
|
uint32 targetPageOff;
|
|
|
|
uint32 targetRecOff;
|
|
|
|
uint32 targetId;
|
|
|
|
uint32 targetSeg;
|
2010-03-30 18:23:57 +02:00
|
|
|
static pg_time_t last_fail_time = 0;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
|
|
|
|
XLByteToSeg(*RecPtr, targetId, targetSeg);
|
|
|
|
targetPageOff = ((RecPtr->xrecoff % XLogSegSize) / XLOG_BLCKSZ) * XLOG_BLCKSZ;
|
|
|
|
targetRecOff = RecPtr->xrecoff % XLOG_BLCKSZ;
|
|
|
|
|
|
|
|
/* Fast exit if we have read the record in the current buffer already */
|
2010-03-30 18:23:57 +02:00
|
|
|
if (failedSources == 0 && targetId == readId && targetSeg == readSeg &&
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
targetPageOff == readOff && targetRecOff < readLen)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if we need to switch to a new segment because the requested record
|
|
|
|
* is not in the currently open one.
|
|
|
|
*/
|
|
|
|
if (readFile >= 0 && !XLByteInSeg(*RecPtr, readId, readSeg))
|
|
|
|
{
|
2010-06-09 17:04:07 +02:00
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Signal bgwriter to start a restartpoint if we've replayed too much
|
|
|
|
* xlog since the last one.
|
2010-06-09 17:04:07 +02:00
|
|
|
*/
|
|
|
|
if (StandbyMode && bgwriterLaunched)
|
|
|
|
{
|
|
|
|
if (XLogCheckpointNeeded(readId, readSeg))
|
|
|
|
{
|
|
|
|
(void) GetRedoRecPtr();
|
|
|
|
if (XLogCheckpointNeeded(readId, readSeg))
|
|
|
|
RequestCheckpoint(CHECKPOINT_CAUSE_XLOG);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
close(readFile);
|
|
|
|
readFile = -1;
|
2010-03-30 18:23:57 +02:00
|
|
|
readSource = 0;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
XLByteToSeg(*RecPtr, readId, readSeg);
|
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
retry:
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* See if we need to retrieve more data */
|
|
|
|
if (readFile < 0 ||
|
2010-03-30 18:23:57 +02:00
|
|
|
(readSource == XLOG_FROM_STREAM && !XLByteLT(*RecPtr, receivedUpto)))
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
{
|
|
|
|
if (StandbyMode)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* In standby mode, wait for the requested record to become
|
2010-02-26 03:01:40 +01:00
|
|
|
* available, either via restore_command succeeding to restore the
|
|
|
|
* segment, or via walreceiver having streamed the record.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
if (WalRcvInProgress())
|
|
|
|
{
|
2010-07-06 21:19:02 +02:00
|
|
|
bool havedata;
|
2010-07-03 22:43:58 +02:00
|
|
|
|
2010-06-14 08:04:21 +02:00
|
|
|
/*
|
|
|
|
* If we find an invalid record in the WAL streamed from
|
|
|
|
* master, something is seriously wrong. There's little
|
2010-07-06 21:19:02 +02:00
|
|
|
* chance that the problem will just go away, but PANIC is
|
|
|
|
* not good for availability either, especially in hot
|
|
|
|
* standby mode. Disconnect, and retry from
|
|
|
|
* archive/pg_xlog again. The WAL in the archive should be
|
|
|
|
* identical to what was streamed, so it's unlikely that
|
|
|
|
* it helps, but one can hope...
|
2010-06-14 08:04:21 +02:00
|
|
|
*/
|
|
|
|
if (failedSources & XLOG_FROM_STREAM)
|
|
|
|
{
|
|
|
|
ShutdownWalRcv();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/*
|
2010-07-03 22:43:58 +02:00
|
|
|
* Walreceiver is active, so see if new data has arrived.
|
|
|
|
*
|
|
|
|
* We only advance XLogReceiptTime when we obtain fresh
|
|
|
|
* WAL from walreceiver and observe that we had already
|
|
|
|
* processed everything before the most recent "chunk"
|
|
|
|
* that it flushed to disk. In steady state where we are
|
2010-07-06 21:19:02 +02:00
|
|
|
* keeping up with the incoming data, XLogReceiptTime will
|
|
|
|
* be updated on each cycle. When we are behind,
|
2010-07-03 22:43:58 +02:00
|
|
|
* XLogReceiptTime will not advance, so the grace time
|
|
|
|
* alloted to conflicting queries will decrease.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
|
|
|
if (XLByteLT(*RecPtr, receivedUpto))
|
2010-07-03 22:43:58 +02:00
|
|
|
havedata = true;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
XLogRecPtr latestChunkStart;
|
|
|
|
|
|
|
|
receivedUpto = GetWalRcvWriteRecPtr(&latestChunkStart);
|
|
|
|
if (XLByteLT(*RecPtr, receivedUpto))
|
|
|
|
{
|
|
|
|
havedata = true;
|
|
|
|
if (!XLByteLT(*RecPtr, latestChunkStart))
|
|
|
|
XLogReceiptTime = GetCurrentTimestamp();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
havedata = false;
|
|
|
|
}
|
|
|
|
if (havedata)
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Great, streamed far enough. Open the file if it's
|
2010-07-03 22:43:58 +02:00
|
|
|
* not open already. Use XLOG_FROM_STREAM so that
|
|
|
|
* source info is set correctly and XLogReceiptTime
|
|
|
|
* isn't changed.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
|
|
|
if (readFile < 0)
|
|
|
|
{
|
|
|
|
readFile =
|
|
|
|
XLogFileRead(readId, readSeg, PANIC,
|
2010-03-30 18:23:57 +02:00
|
|
|
recoveryTargetTLI,
|
2010-07-03 22:43:58 +02:00
|
|
|
XLOG_FROM_STREAM, false);
|
|
|
|
Assert(readFile >= 0);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
switched_segment = true;
|
2010-07-03 22:43:58 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* just make sure source info is correct... */
|
2010-03-30 18:23:57 +02:00
|
|
|
readSource = XLOG_FROM_STREAM;
|
2010-07-03 22:43:58 +02:00
|
|
|
XLogReceiptSource = XLOG_FROM_STREAM;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2010-07-03 22:43:58 +02:00
|
|
|
/*
|
2010-09-15 12:35:05 +02:00
|
|
|
* Data not here yet, so check for trigger then sleep for
|
|
|
|
* five seconds like in the WAL file polling case below.
|
2010-07-03 22:43:58 +02:00
|
|
|
*/
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
if (CheckForStandbyTrigger())
|
2010-03-30 18:23:57 +02:00
|
|
|
goto triggered;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
|
|
|
|
/*
|
2010-09-15 12:35:05 +02:00
|
|
|
* Wait for more WAL to arrive, or timeout to be reached
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
2010-09-15 12:35:05 +02:00
|
|
|
WaitLatch(&XLogCtl->recoveryWakeupLatch, 5000000L);
|
|
|
|
ResetLatch(&XLogCtl->recoveryWakeupLatch);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2010-07-06 21:19:02 +02:00
|
|
|
int sources;
|
|
|
|
pg_time_t now;
|
2010-03-30 18:23:57 +02:00
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/*
|
|
|
|
* Until walreceiver manages to reconnect, poll the
|
|
|
|
* archive.
|
|
|
|
*/
|
|
|
|
if (readFile >= 0)
|
|
|
|
{
|
|
|
|
close(readFile);
|
|
|
|
readFile = -1;
|
|
|
|
}
|
|
|
|
/* Reset curFileTLI if random fetch. */
|
|
|
|
if (randAccess)
|
|
|
|
curFileTLI = 0;
|
|
|
|
|
|
|
|
/*
|
2010-03-30 18:23:57 +02:00
|
|
|
* Try to restore the file from archive, or read an
|
|
|
|
* existing file from pg_xlog.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
2010-03-30 18:23:57 +02:00
|
|
|
sources = XLOG_FROM_ARCHIVE | XLOG_FROM_PG_XLOG;
|
|
|
|
if (!(sources & ~failedSources))
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
{
|
|
|
|
/*
|
2010-03-30 18:23:57 +02:00
|
|
|
* We've exhausted all options for retrieving the
|
|
|
|
* file. Retry ...
|
|
|
|
*/
|
|
|
|
failedSources = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ... but sleep first if it hasn't been long since
|
|
|
|
* last attempt.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
2010-03-30 18:23:57 +02:00
|
|
|
now = (pg_time_t) time(NULL);
|
|
|
|
if ((now - last_fail_time) < 5)
|
|
|
|
{
|
|
|
|
pg_usleep(1000000L * (5 - (now - last_fail_time)));
|
|
|
|
now = (pg_time_t) time(NULL);
|
|
|
|
}
|
|
|
|
last_fail_time = now;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If primary_conninfo is set, launch walreceiver to
|
2010-07-06 21:19:02 +02:00
|
|
|
* try to stream the missing WAL, before retrying to
|
|
|
|
* restore from archive/pg_xlog.
|
2010-03-30 18:23:57 +02:00
|
|
|
*
|
|
|
|
* If fetching_ckpt is TRUE, RecPtr points to the
|
|
|
|
* initial checkpoint location. In that case, we use
|
2010-07-06 21:19:02 +02:00
|
|
|
* RedoStartLSN as the streaming start position
|
|
|
|
* instead of RecPtr, so that when we later jump
|
|
|
|
* backwards to start redo at RedoStartLSN, we will
|
|
|
|
* have the logs streamed already.
|
2010-03-30 18:23:57 +02:00
|
|
|
*/
|
|
|
|
if (PrimaryConnInfo)
|
|
|
|
{
|
|
|
|
RequestXLogStreaming(
|
2010-07-06 21:19:02 +02:00
|
|
|
fetching_ckpt ? RedoStartLSN : *RecPtr,
|
|
|
|
PrimaryConnInfo);
|
2010-03-30 18:23:57 +02:00
|
|
|
continue;
|
|
|
|
}
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
}
|
2010-03-30 18:23:57 +02:00
|
|
|
/* Don't try to read from a source that just failed */
|
|
|
|
sources &= ~failedSources;
|
|
|
|
readFile = XLogFileReadAnyTLI(readId, readSeg, DEBUG2,
|
|
|
|
sources);
|
|
|
|
switched_segment = true;
|
2010-07-03 22:43:58 +02:00
|
|
|
if (readFile >= 0)
|
2010-03-30 18:23:57 +02:00
|
|
|
break;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
|
|
|
|
/*
|
2010-03-30 18:23:57 +02:00
|
|
|
* Nope, not found in archive and/or pg_xlog.
|
|
|
|
*/
|
|
|
|
failedSources |= sources;
|
|
|
|
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* Check to see if the trigger file exists. Note that we
|
|
|
|
* do this only after failure, so when you create the
|
|
|
|
* trigger file, we still finish replaying as much as we
|
|
|
|
* can from archive and pg_xlog before failover.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
2010-03-30 18:23:57 +02:00
|
|
|
if (CheckForStandbyTrigger())
|
|
|
|
goto triggered;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* This possibly-long loop needs to handle interrupts of
|
|
|
|
* startup process.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
|
|
|
HandleStartupProcInterrupts();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* In archive or crash recovery. */
|
|
|
|
if (readFile < 0)
|
|
|
|
{
|
2010-07-06 21:19:02 +02:00
|
|
|
int sources;
|
2010-03-30 18:23:57 +02:00
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/* Reset curFileTLI if random fetch. */
|
|
|
|
if (randAccess)
|
|
|
|
curFileTLI = 0;
|
2010-03-30 18:23:57 +02:00
|
|
|
|
|
|
|
sources = XLOG_FROM_PG_XLOG;
|
|
|
|
if (InArchiveRecovery)
|
|
|
|
sources |= XLOG_FROM_ARCHIVE;
|
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
readFile = XLogFileReadAnyTLI(readId, readSeg, emode,
|
2010-03-30 18:23:57 +02:00
|
|
|
sources);
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
switched_segment = true;
|
|
|
|
if (readFile < 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2010-07-06 21:19:02 +02:00
|
|
|
* At this point, we have the right segment open and if we're streaming we
|
|
|
|
* know the requested record is in it.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
|
|
|
Assert(readFile != -1);
|
|
|
|
|
|
|
|
/*
|
2010-02-26 03:01:40 +01:00
|
|
|
* If the current segment is being streamed from master, calculate how
|
|
|
|
* much of the current page we have received already. We know the
|
|
|
|
* requested record has been received, but this is for the benefit of
|
|
|
|
* future calls, to allow quick exit at the top of this function.
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
*/
|
2010-03-30 18:23:57 +02:00
|
|
|
if (readSource == XLOG_FROM_STREAM)
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
{
|
|
|
|
if (RecPtr->xlogid != receivedUpto.xlogid ||
|
|
|
|
(RecPtr->xrecoff / XLOG_BLCKSZ) != (receivedUpto.xrecoff / XLOG_BLCKSZ))
|
|
|
|
{
|
|
|
|
readLen = XLOG_BLCKSZ;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
readLen = receivedUpto.xrecoff % XLogSegSize - targetPageOff;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
readLen = XLOG_BLCKSZ;
|
|
|
|
|
|
|
|
if (switched_segment && targetPageOff != 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Whenever switching to a new WAL segment, we read the first page of
|
|
|
|
* the file and validate its header, even if that's not where the
|
|
|
|
* target record is. This is so that we can check the additional
|
|
|
|
* identification info that is present in the first page's "long"
|
|
|
|
* header.
|
|
|
|
*/
|
|
|
|
readOff = 0;
|
|
|
|
if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
(errcode_for_file_access(),
|
|
|
|
errmsg("could not read from log file %u, segment %u, offset %u: %m",
|
|
|
|
readId, readSeg, readOff)));
|
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2010-04-16 10:58:16 +02:00
|
|
|
if (!ValidXLOGHeader((XLogPageHeader) readBuf, emode))
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the requested page */
|
|
|
|
readOff = targetPageOff;
|
|
|
|
if (lseek(readFile, (off_t) readOff, SEEK_SET) < 0)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
(errcode_for_file_access(),
|
2010-02-26 03:01:40 +01:00
|
|
|
errmsg("could not seek in log file %u, segment %u to offset %u: %m",
|
|
|
|
readId, readSeg, readOff)));
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
|
|
|
if (read(readFile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
|
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
ereport(emode_for_corrupt_record(emode, *RecPtr),
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
(errcode_for_file_access(),
|
2010-02-26 03:01:40 +01:00
|
|
|
errmsg("could not read from log file %u, segment %u, offset %u: %m",
|
|
|
|
readId, readSeg, readOff)));
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
}
|
2010-04-16 10:58:16 +02:00
|
|
|
if (!ValidXLOGHeader((XLogPageHeader) readBuf, emode))
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
goto next_record_is_invalid;
|
|
|
|
|
|
|
|
Assert(targetId == readId);
|
|
|
|
Assert(targetSeg == readSeg);
|
|
|
|
Assert(targetPageOff == readOff);
|
|
|
|
Assert(targetRecOff < readLen);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
|
|
next_record_is_invalid:
|
2010-03-30 18:23:57 +02:00
|
|
|
failedSources |= readSource;
|
|
|
|
|
|
|
|
if (readFile >= 0)
|
|
|
|
close(readFile);
|
|
|
|
readFile = -1;
|
|
|
|
readLen = 0;
|
|
|
|
readSource = 0;
|
|
|
|
|
|
|
|
/* In standby-mode, keep trying */
|
|
|
|
if (StandbyMode)
|
|
|
|
goto retry;
|
|
|
|
else
|
|
|
|
return false;
|
|
|
|
|
|
|
|
triggered:
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
if (readFile >= 0)
|
|
|
|
close(readFile);
|
|
|
|
readFile = -1;
|
|
|
|
readLen = 0;
|
2010-03-30 18:23:57 +02:00
|
|
|
readSource = 0;
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-03-30 18:23:57 +02:00
|
|
|
/*
|
|
|
|
* Determine what log level should be used to report a corrupt WAL record
|
|
|
|
* in the current WAL page, previously read by XLogPageRead().
|
|
|
|
*
|
|
|
|
* 'emode' is the error mode that would be used to report a file-not-found
|
2010-07-06 21:19:02 +02:00
|
|
|
* or legitimate end-of-WAL situation. Generally, we use it as-is, but if
|
2010-06-28 21:46:19 +02:00
|
|
|
* we're retrying the exact same record that we've tried previously, only
|
2010-07-06 21:19:02 +02:00
|
|
|
* complain the first time to keep the noise down. However, we only do when
|
2010-06-28 21:46:19 +02:00
|
|
|
* reading from pg_xlog, because we don't expect any invalid records in archive
|
|
|
|
* or in records streamed from master. Files in the archive should be complete,
|
|
|
|
* and we should never hit the end of WAL because we stop and wait for more WAL
|
2010-07-06 21:19:02 +02:00
|
|
|
* to arrive before replaying it.
|
2010-04-16 10:58:16 +02:00
|
|
|
*
|
|
|
|
* NOTE: This function remembers the RecPtr value it was last called with,
|
|
|
|
* to suppress repeated messages about the same record. Only call this when
|
|
|
|
* you are about to ereport(), or you might cause a later message to be
|
|
|
|
* erroneously suppressed.
|
2010-03-30 18:23:57 +02:00
|
|
|
*/
|
|
|
|
static int
|
2010-04-16 10:58:16 +02:00
|
|
|
emode_for_corrupt_record(int emode, XLogRecPtr RecPtr)
|
2010-03-30 18:23:57 +02:00
|
|
|
{
|
2010-04-16 10:58:16 +02:00
|
|
|
static XLogRecPtr lastComplaint = {0, 0};
|
|
|
|
|
2010-06-28 21:46:19 +02:00
|
|
|
if (readSource == XLOG_FROM_PG_XLOG && emode == LOG)
|
2010-04-16 10:58:16 +02:00
|
|
|
{
|
|
|
|
if (XLByteEQ(RecPtr, lastComplaint))
|
|
|
|
emode = DEBUG1;
|
|
|
|
else
|
|
|
|
lastComplaint = RecPtr;
|
|
|
|
}
|
2010-03-30 18:23:57 +02:00
|
|
|
return emode;
|
|
|
|
}
|
|
|
|
|
Make standby server continuously retry restoring the next WAL segment with
restore_command, if the connection to the primary server is lost. This
ensures that the standby can recover automatically, if the connection is
lost for a long time and standby falls behind so much that the required
WAL segments have been archived and deleted in the master.
This also makes standby_mode useful without streaming replication; the
server will keep retrying restore_command every few seconds until the
trigger file is found. That's the same basic functionality pg_standby
offers, but without the bells and whistles.
To implement that, refactor the ReadRecord/FetchRecord functions. The
FetchRecord() function introduced in the original streaming replication
patch is removed, and all the retry logic is now in a new function called
XLogReadPage(). XLogReadPage() is now responsible for executing
restore_command, launching walreceiver, and waiting for new WAL to arrive
from primary, as required.
This also changes the life cycle of walreceiver. When launched, it now only
tries to connect to the master once, and exits if the connection fails, or
is lost during streaming for any reason. The startup process detects the
death, and re-launches walreceiver if necessary.
2010-01-27 16:27:51 +01:00
|
|
|
/*
|
|
|
|
* Check to see if the trigger file exists. If it does, request postmaster
|
|
|
|
* to shut down walreceiver, wait for it to exit, remove the trigger
|
|
|
|
* file, and return true.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
CheckForStandbyTrigger(void)
|
|
|
|
{
|
|
|
|
struct stat stat_buf;
|
|
|
|
|
|
|
|
if (TriggerFile == NULL)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (stat(TriggerFile, &stat_buf) == 0)
|
|
|
|
{
|
|
|
|
ereport(LOG,
|
|
|
|
(errmsg("trigger file found: %s", TriggerFile)));
|
|
|
|
ShutdownWalRcv();
|
|
|
|
unlink(TriggerFile);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2010-09-15 12:35:05 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Wake up startup process to replay newly arrived WAL, or to notice that
|
|
|
|
* failover has been requested.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
WakeupRecovery(void)
|
|
|
|
{
|
|
|
|
SetLatch(&XLogCtl->recoveryWakeupLatch);
|
|
|
|
}
|