2000-10-13 14:06:40 +02:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
2000-10-20 13:01:21 +02:00
|
|
|
* xlogutils.c
|
2000-10-13 14:06:40 +02:00
|
|
|
*
|
2004-07-22 00:31:26 +02:00
|
|
|
* PostgreSQL transaction log manager utility routines
|
|
|
|
*
|
|
|
|
* This file contains support routines that are used by XLOG replay functions.
|
|
|
|
* None of this code is used during normal system operation.
|
|
|
|
*
|
2000-10-13 14:06:40 +02:00
|
|
|
*
|
2013-01-01 23:15:01 +01:00
|
|
|
* Portions Copyright (c) 1996-2013, PostgreSQL Global Development Group
|
2000-10-13 14:06:40 +02:00
|
|
|
* Portions Copyright (c) 1994, Regents of the University of California
|
|
|
|
*
|
2010-09-20 22:08:53 +02:00
|
|
|
* src/backend/access/transam/xlogutils.c
|
XLOG (and related) changes:
* Store two past checkpoint locations, not just one, in pg_control.
On startup, we fall back to the older checkpoint if the newer one
is unreadable. Also, a physical copy of the newest checkpoint record
is kept in pg_control for possible use in disaster recovery (ie,
complete loss of pg_xlog). Also add a version number for pg_control
itself. Remove archdir from pg_control; it ought to be a GUC
parameter, not a special case (not that it's implemented yet anyway).
* Suppress successive checkpoint records when nothing has been entered
in the WAL log since the last one. This is not so much to avoid I/O
as to make it actually useful to keep track of the last two
checkpoints. If the things are right next to each other then there's
not a lot of redundancy gained...
* Change CRC scheme to a true 64-bit CRC, not a pair of 32-bit CRCs
on alternate bytes. Polynomial borrowed from ECMA DLT1 standard.
* Fix XLOG record length handling so that it will work at BLCKSZ = 32k.
* Change XID allocation to work more like OID allocation. (This is of
dubious necessity, but I think it's a good idea anyway.)
* Fix a number of minor bugs, such as off-by-one logic for XLOG file
wraparound at the 4 gig mark.
* Add documentation and clean up some coding infelicities; move file
format declarations out to include files where planned contrib
utilities can get at them.
* Checkpoint will now occur every CHECKPOINT_SEGMENTS log segments or
every CHECKPOINT_TIMEOUT seconds, whichever comes first. It is also
possible to force a checkpoint by sending SIGUSR1 to the postmaster
(undocumented feature...)
* Defend against kill -9 postmaster by storing shmem block's key and ID
in postmaster.pid lockfile, and checking at startup to ensure that no
processes are still connected to old shmem block (if it still exists).
* Switch backends to accept SIGQUIT rather than SIGUSR1 for emergency
stop, for symmetry with postmaster and xlog utilities. Clean up signal
handling in bootstrap.c so that xlog utilities launched by postmaster
will react to signals better.
* Standalone bootstrap now 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-13 14:06:40 +02:00
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
#include "postgres.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
|
|
|
|
2011-08-26 22:52:16 +02:00
|
|
|
#include "access/xlog.h"
|
2000-10-20 13:01:21 +02:00
|
|
|
#include "access/xlogutils.h"
|
2008-11-11 14:19:16 +01:00
|
|
|
#include "catalog/catalog.h"
|
2013-02-22 02:46:17 +01:00
|
|
|
#include "common/relpath.h"
|
2001-08-25 20:52:43 +02:00
|
|
|
#include "storage/smgr.h"
|
2011-09-04 07:13:16 +02:00
|
|
|
#include "utils/guc.h"
|
|
|
|
#include "utils/hsearch.h"
|
2008-06-19 02:46:06 +02:00
|
|
|
#include "utils/rel.h"
|
2000-10-13 14:06:40 +02:00
|
|
|
|
2001-08-25 20:52:43 +02:00
|
|
|
|
2006-04-14 22:27:24 +02:00
|
|
|
/*
|
|
|
|
* During XLOG replay, we may see XLOG records for incremental updates of
|
|
|
|
* pages that no longer exist, because their relation was later dropped or
|
|
|
|
* truncated. (Note: this is only possible when full_page_writes = OFF,
|
|
|
|
* since when it's ON, the first reference we see to a page should always
|
|
|
|
* be a full-page rewrite not an incremental update.) Rather than simply
|
|
|
|
* ignoring such records, we make a note of the referenced page, and then
|
|
|
|
* complain if we don't actually see a drop or truncate covering the page
|
|
|
|
* later in replay.
|
|
|
|
*/
|
|
|
|
typedef struct xl_invalid_page_key
|
|
|
|
{
|
|
|
|
RelFileNode node; /* the relation */
|
2008-08-11 13:05:11 +02:00
|
|
|
ForkNumber forkno; /* the fork number */
|
2006-04-14 22:27:24 +02:00
|
|
|
BlockNumber blkno; /* the page */
|
|
|
|
} xl_invalid_page_key;
|
|
|
|
|
|
|
|
typedef struct xl_invalid_page
|
|
|
|
{
|
|
|
|
xl_invalid_page_key key; /* hash key ... must be first */
|
|
|
|
bool present; /* page existed but contained zeroes */
|
|
|
|
} xl_invalid_page;
|
|
|
|
|
|
|
|
static HTAB *invalid_page_tab = NULL;
|
|
|
|
|
|
|
|
|
2011-12-02 09:49:54 +01:00
|
|
|
/* Report a reference to an invalid page */
|
|
|
|
static void
|
|
|
|
report_invalid_page(int elevel, RelFileNode node, ForkNumber forkno,
|
|
|
|
BlockNumber blkno, bool present)
|
|
|
|
{
|
|
|
|
char *path = relpathperm(node, forkno);
|
|
|
|
|
|
|
|
if (present)
|
|
|
|
elog(elevel, "page %u of relation %s is uninitialized",
|
|
|
|
blkno, path);
|
|
|
|
else
|
|
|
|
elog(elevel, "page %u of relation %s does not exist",
|
|
|
|
blkno, path);
|
|
|
|
pfree(path);
|
|
|
|
}
|
|
|
|
|
2006-04-14 22:27:24 +02:00
|
|
|
/* Log a reference to an invalid page */
|
|
|
|
static void
|
2008-08-11 13:05:11 +02:00
|
|
|
log_invalid_page(RelFileNode node, ForkNumber forkno, BlockNumber blkno,
|
|
|
|
bool present)
|
2006-04-14 22:27:24 +02:00
|
|
|
{
|
|
|
|
xl_invalid_page_key key;
|
|
|
|
xl_invalid_page *hentry;
|
|
|
|
bool found;
|
|
|
|
|
2011-12-02 09:49:54 +01:00
|
|
|
/*
|
|
|
|
* Once recovery has reached a consistent state, the invalid-page table
|
|
|
|
* should be empty and remain so. If a reference to an invalid page is
|
2012-06-10 21:20:04 +02:00
|
|
|
* found after consistency is reached, PANIC immediately. This might seem
|
|
|
|
* aggressive, but it's better than letting the invalid reference linger
|
|
|
|
* in the hash table until the end of recovery and PANIC there, which
|
|
|
|
* might come only much later if this is a standby server.
|
2011-12-02 09:49:54 +01:00
|
|
|
*/
|
2011-12-09 13:32:42 +01:00
|
|
|
if (reachedConsistency)
|
2011-12-02 09:49:54 +01:00
|
|
|
{
|
|
|
|
report_invalid_page(WARNING, node, forkno, blkno, present);
|
|
|
|
elog(PANIC, "WAL contains references to invalid pages");
|
|
|
|
}
|
|
|
|
|
2006-04-14 22:27:24 +02:00
|
|
|
/*
|
|
|
|
* Log references to invalid pages at DEBUG1 level. This allows some
|
|
|
|
* tracing of the cause (note the elog context mechanism will tell us
|
|
|
|
* something about the XLOG record that generated the reference).
|
|
|
|
*/
|
2008-11-11 14:19:16 +01:00
|
|
|
if (log_min_messages <= DEBUG1 || client_min_messages <= DEBUG1)
|
2011-12-02 09:49:54 +01:00
|
|
|
report_invalid_page(DEBUG1, node, forkno, blkno, present);
|
2006-04-14 22:27:24 +02:00
|
|
|
|
|
|
|
if (invalid_page_tab == NULL)
|
|
|
|
{
|
|
|
|
/* create hash table when first needed */
|
|
|
|
HASHCTL ctl;
|
|
|
|
|
|
|
|
memset(&ctl, 0, sizeof(ctl));
|
|
|
|
ctl.keysize = sizeof(xl_invalid_page_key);
|
|
|
|
ctl.entrysize = sizeof(xl_invalid_page);
|
|
|
|
ctl.hash = tag_hash;
|
|
|
|
|
|
|
|
invalid_page_tab = hash_create("XLOG invalid-page table",
|
|
|
|
100,
|
|
|
|
&ctl,
|
|
|
|
HASH_ELEM | HASH_FUNCTION);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we currently assume xl_invalid_page_key contains no padding */
|
|
|
|
key.node = node;
|
2008-08-11 13:05:11 +02:00
|
|
|
key.forkno = forkno;
|
2006-04-14 22:27:24 +02:00
|
|
|
key.blkno = blkno;
|
|
|
|
hentry = (xl_invalid_page *)
|
|
|
|
hash_search(invalid_page_tab, (void *) &key, HASH_ENTER, &found);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
|
|
|
/* hash_search already filled in the key */
|
|
|
|
hentry->present = present;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* repeat reference ... leave "present" as it was */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forget any invalid pages >= minblkno, because they've been dropped */
|
|
|
|
static void
|
2008-08-11 13:05:11 +02:00
|
|
|
forget_invalid_pages(RelFileNode node, ForkNumber forkno, BlockNumber minblkno)
|
2006-04-14 22:27:24 +02:00
|
|
|
{
|
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
xl_invalid_page *hentry;
|
|
|
|
|
|
|
|
if (invalid_page_tab == NULL)
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
hash_seq_init(&status, invalid_page_tab);
|
|
|
|
|
|
|
|
while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
if (RelFileNodeEquals(hentry->key.node, node) &&
|
2008-08-11 13:05:11 +02:00
|
|
|
hentry->key.forkno == forkno &&
|
2006-04-14 22:27:24 +02:00
|
|
|
hentry->key.blkno >= minblkno)
|
|
|
|
{
|
2008-11-11 14:19:16 +01:00
|
|
|
if (log_min_messages <= DEBUG2 || client_min_messages <= DEBUG2)
|
|
|
|
{
|
2010-08-13 22:10:54 +02:00
|
|
|
char *path = relpathperm(hentry->key.node, forkno);
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2008-11-11 14:19:16 +01:00
|
|
|
elog(DEBUG2, "page %u of relation %s has been dropped",
|
|
|
|
hentry->key.blkno, path);
|
|
|
|
pfree(path);
|
|
|
|
}
|
2006-04-14 22:27:24 +02:00
|
|
|
|
|
|
|
if (hash_search(invalid_page_tab,
|
|
|
|
(void *) &hentry->key,
|
|
|
|
HASH_REMOVE, NULL) == NULL)
|
|
|
|
elog(ERROR, "hash table corrupted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Forget any invalid pages in a whole database */
|
|
|
|
static void
|
|
|
|
forget_invalid_pages_db(Oid dbid)
|
|
|
|
{
|
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
xl_invalid_page *hentry;
|
|
|
|
|
|
|
|
if (invalid_page_tab == NULL)
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
hash_seq_init(&status, invalid_page_tab);
|
|
|
|
|
|
|
|
while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
|
|
|
if (hentry->key.node.dbNode == dbid)
|
|
|
|
{
|
2008-11-11 14:19:16 +01:00
|
|
|
if (log_min_messages <= DEBUG2 || client_min_messages <= DEBUG2)
|
|
|
|
{
|
2010-08-13 22:10:54 +02:00
|
|
|
char *path = relpathperm(hentry->key.node, hentry->key.forkno);
|
2009-06-11 16:49:15 +02:00
|
|
|
|
2008-11-11 14:19:16 +01:00
|
|
|
elog(DEBUG2, "page %u of relation %s has been dropped",
|
|
|
|
hentry->key.blkno, path);
|
|
|
|
pfree(path);
|
|
|
|
}
|
2006-04-14 22:27:24 +02:00
|
|
|
|
|
|
|
if (hash_search(invalid_page_tab,
|
|
|
|
(void *) &hentry->key,
|
|
|
|
HASH_REMOVE, NULL) == NULL)
|
|
|
|
elog(ERROR, "hash table corrupted");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-02 09:49:54 +01:00
|
|
|
/* Are there any unresolved references to invalid pages? */
|
|
|
|
bool
|
|
|
|
XLogHaveInvalidPages(void)
|
|
|
|
{
|
|
|
|
if (invalid_page_tab != NULL &&
|
|
|
|
hash_get_num_entries(invalid_page_tab) > 0)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-04-14 22:27:24 +02:00
|
|
|
/* Complain about any remaining invalid-page entries */
|
|
|
|
void
|
|
|
|
XLogCheckInvalidPages(void)
|
|
|
|
{
|
|
|
|
HASH_SEQ_STATUS status;
|
|
|
|
xl_invalid_page *hentry;
|
|
|
|
bool foundone = false;
|
|
|
|
|
|
|
|
if (invalid_page_tab == NULL)
|
|
|
|
return; /* nothing to do */
|
|
|
|
|
|
|
|
hash_seq_init(&status, invalid_page_tab);
|
|
|
|
|
|
|
|
/*
|
2006-10-04 02:30:14 +02:00
|
|
|
* Our strategy is to emit WARNING messages for all remaining entries and
|
|
|
|
* only PANIC after we've dumped all the available info.
|
2006-04-14 22:27:24 +02:00
|
|
|
*/
|
|
|
|
while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
|
|
|
|
{
|
2011-12-02 09:49:54 +01:00
|
|
|
report_invalid_page(WARNING, hentry->key.node, hentry->key.forkno,
|
|
|
|
hentry->key.blkno, hentry->present);
|
2006-04-14 22:27:24 +02:00
|
|
|
foundone = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (foundone)
|
|
|
|
elog(PANIC, "WAL contains references to invalid pages");
|
2008-06-12 11:12:31 +02:00
|
|
|
|
|
|
|
hash_destroy(invalid_page_tab);
|
|
|
|
invalid_page_tab = NULL;
|
2006-04-14 22:27:24 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/*
|
2008-11-03 16:10:17 +01:00
|
|
|
* XLogReadBuffer
|
2009-01-20 19:59:37 +01:00
|
|
|
* Read a page during XLOG replay.
|
|
|
|
*
|
|
|
|
* This is a shorthand of XLogReadBufferExtended() followed by
|
|
|
|
* LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE), for reading from the main
|
|
|
|
* fork.
|
|
|
|
*
|
2010-07-08 18:08:30 +02:00
|
|
|
* (Getting the buffer lock is not really necessary during single-process
|
|
|
|
* crash recovery, but some subroutines such as MarkBufferDirty will complain
|
|
|
|
* if we don't have the lock. In hot standby mode it's definitely necessary.)
|
2009-01-20 19:59:37 +01:00
|
|
|
*
|
|
|
|
* The returned buffer is exclusively-locked.
|
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
|
|
|
*
|
|
|
|
* For historical reasons, instead of a ReadBufferMode argument, this only
|
|
|
|
* supports RBM_ZERO (init == true) and RBM_NORMAL (init == false) modes.
|
|
|
|
*/
|
|
|
|
Buffer
|
|
|
|
XLogReadBuffer(RelFileNode rnode, BlockNumber blkno, bool init)
|
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
Buffer buf;
|
|
|
|
|
2009-01-20 19:59:37 +01:00
|
|
|
buf = XLogReadBufferExtended(rnode, MAIN_FORKNUM, blkno,
|
|
|
|
init ? RBM_ZERO : RBM_NORMAL);
|
|
|
|
if (BufferIsValid(buf))
|
|
|
|
LockBuffer(buf, BUFFER_LOCK_EXCLUSIVE);
|
|
|
|
|
|
|
|
return buf;
|
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
|
|
|
}
|
2006-04-14 22:27:24 +02:00
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
/*
|
2008-11-03 16:10:17 +01:00
|
|
|
* XLogReadBufferExtended
|
2006-03-29 23:17:39 +02:00
|
|
|
* Read a page during XLOG replay
|
2000-10-20 13:01:21 +02:00
|
|
|
*
|
2009-01-20 19:59:37 +01:00
|
|
|
* This is functionally comparable to ReadBufferExtended. There's some
|
|
|
|
* differences in the behavior wrt. the "mode" argument:
|
2006-03-29 23:17:39 +02:00
|
|
|
*
|
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
|
|
|
* In RBM_NORMAL mode, if the page doesn't exist, or contains all-zeroes, we
|
|
|
|
* return InvalidBuffer. In this case the caller should silently skip the
|
|
|
|
* update on this page. (In this situation, we expect that the page was later
|
|
|
|
* dropped or truncated. If we don't see evidence of that later in the WAL
|
|
|
|
* sequence, we'll complain at the end of WAL replay.)
|
|
|
|
*
|
|
|
|
* In RBM_ZERO and RBM_ZERO_ON_ERROR modes, if the page doesn't exist, the
|
|
|
|
* relation is extended with all-zeroes pages up to the given block number.
|
2008-08-11 13:05:11 +02:00
|
|
|
*/
|
|
|
|
Buffer
|
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
|
|
|
XLogReadBufferExtended(RelFileNode rnode, ForkNumber forknum,
|
|
|
|
BlockNumber blkno, ReadBufferMode mode)
|
2000-10-20 13:01:21 +02:00
|
|
|
{
|
2008-06-12 11:12:31 +02:00
|
|
|
BlockNumber lastblock;
|
2000-10-20 13:01:21 +02:00
|
|
|
Buffer buffer;
|
2008-06-12 11:12:31 +02:00
|
|
|
SMgrRelation smgr;
|
2000-10-20 13:01:21 +02:00
|
|
|
|
2006-03-29 23:17:39 +02:00
|
|
|
Assert(blkno != P_NEW);
|
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
/* Open the relation at smgr level */
|
2010-08-13 22:10:54 +02:00
|
|
|
smgr = smgropen(rnode, InvalidBackendId);
|
2008-06-12 11:12:31 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create the target file if it doesn't already exist. This lets us cope
|
|
|
|
* if the replay sequence contains writes to a relation that is later
|
|
|
|
* deleted. (The original coding of this routine would instead suppress
|
|
|
|
* the writes, but that seems like it risks losing valuable data if the
|
|
|
|
* filesystem loses an inode during a crash. Better to write the data
|
|
|
|
* until we are actually told to delete the file.)
|
|
|
|
*/
|
2008-11-19 11:34:52 +01:00
|
|
|
smgrcreate(smgr, forknum, true);
|
2008-06-12 11:12:31 +02:00
|
|
|
|
2008-08-11 13:05:11 +02:00
|
|
|
lastblock = smgrnblocks(smgr, forknum);
|
2008-06-12 11:12:31 +02:00
|
|
|
|
2006-03-29 23:17:39 +02:00
|
|
|
if (blkno < lastblock)
|
2000-10-20 13:01:21 +02:00
|
|
|
{
|
2006-03-29 23:17:39 +02:00
|
|
|
/* page exists in file */
|
2010-08-13 22:10:54 +02:00
|
|
|
buffer = ReadBufferWithoutRelcache(rnode, forknum, blkno,
|
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
|
|
|
mode, NULL);
|
2006-03-29 23:17:39 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* hm, page doesn't exist in file */
|
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
|
|
|
if (mode == RBM_NORMAL)
|
2006-04-14 22:27:24 +02:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
log_invalid_page(rnode, forknum, blkno, false);
|
2006-04-14 22:27:24 +02:00
|
|
|
return InvalidBuffer;
|
|
|
|
}
|
2006-03-29 23:17:39 +02:00
|
|
|
/* OK to extend the file */
|
|
|
|
/* we do this in recovery only - no rel-extension lock needed */
|
|
|
|
Assert(InRecovery);
|
2000-10-20 13:01:21 +02:00
|
|
|
buffer = InvalidBuffer;
|
2006-03-29 23:17:39 +02:00
|
|
|
while (blkno >= lastblock)
|
2000-10-20 13:01:21 +02:00
|
|
|
{
|
2006-03-29 23:17:39 +02:00
|
|
|
if (buffer != InvalidBuffer)
|
2006-04-01 01:32:07 +02:00
|
|
|
ReleaseBuffer(buffer);
|
2010-08-13 22:10:54 +02:00
|
|
|
buffer = ReadBufferWithoutRelcache(rnode, forknum,
|
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
|
|
|
P_NEW, mode, NULL);
|
2006-03-29 23:17:39 +02:00
|
|
|
lastblock++;
|
2000-10-20 13:01:21 +02:00
|
|
|
}
|
2006-03-29 23:17:39 +02:00
|
|
|
Assert(BufferGetBlockNumber(buffer) == blkno);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
if (mode == RBM_NORMAL)
|
2006-03-29 23:17:39 +02:00
|
|
|
{
|
|
|
|
/* check that page has been initialized */
|
2006-10-04 02:30:14 +02:00
|
|
|
Page page = (Page) BufferGetPage(buffer);
|
2006-03-29 23:17:39 +02:00
|
|
|
|
2009-01-20 19:59:37 +01:00
|
|
|
/*
|
|
|
|
* We assume that PageIsNew is safe without a lock. During recovery,
|
|
|
|
* there should be no other backends that could modify the buffer at
|
|
|
|
* the same time.
|
|
|
|
*/
|
2008-07-13 22:45:47 +02:00
|
|
|
if (PageIsNew(page))
|
2006-04-14 22:27:24 +02:00
|
|
|
{
|
2009-01-20 19:59:37 +01:00
|
|
|
ReleaseBuffer(buffer);
|
2008-08-11 13:05:11 +02:00
|
|
|
log_invalid_page(rnode, forknum, blkno, true);
|
2006-04-14 22:27:24 +02:00
|
|
|
return InvalidBuffer;
|
|
|
|
}
|
2000-10-20 13:01:21 +02:00
|
|
|
}
|
|
|
|
|
2006-01-11 09:43:13 +01:00
|
|
|
return buffer;
|
2000-10-20 13:01:21 +02:00
|
|
|
}
|
|
|
|
|
2004-07-22 00:31:26 +02:00
|
|
|
|
2000-10-20 13:01:21 +02:00
|
|
|
/*
|
2008-06-12 11:12:31 +02:00
|
|
|
* Struct actually returned by XLogFakeRelcacheEntry, though the declared
|
|
|
|
* return type is Relation.
|
2000-10-20 13:01:21 +02:00
|
|
|
*/
|
2008-06-12 11:12:31 +02:00
|
|
|
typedef struct
|
2000-10-20 13:01:21 +02:00
|
|
|
{
|
2009-06-11 16:49:15 +02:00
|
|
|
RelationData reldata; /* Note: this must be first */
|
|
|
|
FormData_pg_class pgc;
|
2008-06-12 11:12:31 +02:00
|
|
|
} FakeRelCacheEntryData;
|
2000-10-20 13:01:21 +02:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
typedef FakeRelCacheEntryData *FakeRelCacheEntry;
|
2001-03-22 05:01:46 +01:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
/*
|
|
|
|
* Create a fake relation cache entry for a physical relation
|
|
|
|
*
|
|
|
|
* It's often convenient to use the same functions in XLOG replay as in the
|
2009-06-11 16:49:15 +02:00
|
|
|
* main codepath, but those functions typically work with a relcache entry.
|
|
|
|
* We don't have a working relation cache during XLOG replay, but this
|
|
|
|
* function can be used to create a fake relcache entry instead. Only the
|
|
|
|
* fields related to physical storage, like rd_rel, are initialized, so the
|
2008-06-12 11:12:31 +02:00
|
|
|
* fake entry is only usable in low-level operations like ReadBuffer().
|
|
|
|
*
|
|
|
|
* Caller must free the returned entry with FreeFakeRelcacheEntry().
|
|
|
|
*/
|
|
|
|
Relation
|
|
|
|
CreateFakeRelcacheEntry(RelFileNode rnode)
|
2000-10-28 18:21:00 +02:00
|
|
|
{
|
2008-06-12 11:12:31 +02:00
|
|
|
FakeRelCacheEntry fakeentry;
|
2009-06-11 16:49:15 +02:00
|
|
|
Relation rel;
|
2000-10-20 13:01:21 +02:00
|
|
|
|
2012-09-14 15:35:07 +02:00
|
|
|
Assert(InRecovery);
|
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
/* Allocate the Relation struct and all related space in one block. */
|
|
|
|
fakeentry = palloc0(sizeof(FakeRelCacheEntryData));
|
|
|
|
rel = (Relation) fakeentry;
|
2000-10-28 18:21:00 +02:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
rel->rd_rel = &fakeentry->pgc;
|
|
|
|
rel->rd_node = rnode;
|
2010-08-30 18:46:23 +02:00
|
|
|
/* We will never be working with temp rels during recovery */
|
|
|
|
rel->rd_backend = InvalidBackendId;
|
2001-10-05 19:28:13 +02:00
|
|
|
|
2012-09-14 15:35:07 +02:00
|
|
|
/* It must be a permanent table if we're in recovery. */
|
|
|
|
rel->rd_rel->relpersistence = RELPERSISTENCE_PERMANENT;
|
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
/* We don't know the name of the relation; use relfilenode instead */
|
|
|
|
sprintf(RelationGetRelationName(rel), "%u", rnode.relNode);
|
2001-10-05 19:28:13 +02:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
/*
|
|
|
|
* We set up the lockRelId in case anything tries to lock the dummy
|
|
|
|
* relation. Note that this is fairly bogus since relNode may be
|
2009-06-11 16:49:15 +02:00
|
|
|
* different from the relation's OID. It shouldn't really matter though,
|
|
|
|
* since we are presumably running by ourselves and can't have any lock
|
|
|
|
* conflicts ...
|
2008-06-12 11:12:31 +02:00
|
|
|
*/
|
|
|
|
rel->rd_lockInfo.lockRelId.dbId = rnode.dbNode;
|
|
|
|
rel->rd_lockInfo.lockRelId.relId = rnode.relNode;
|
2000-10-28 18:21:00 +02:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
rel->rd_smgr = NULL;
|
2000-10-28 18:21:00 +02:00
|
|
|
|
2008-06-12 11:12:31 +02:00
|
|
|
return rel;
|
2000-10-20 13:01:21 +02:00
|
|
|
}
|
|
|
|
|
2004-02-11 23:55:26 +01:00
|
|
|
/*
|
2008-06-12 11:12:31 +02:00
|
|
|
* Free a fake relation cache entry.
|
2004-02-11 23:55:26 +01:00
|
|
|
*/
|
2008-06-12 11:12:31 +02:00
|
|
|
void
|
|
|
|
FreeFakeRelcacheEntry(Relation fakerel)
|
2000-10-20 13:01:21 +02:00
|
|
|
{
|
2008-06-12 11:12:31 +02:00
|
|
|
pfree(fakerel);
|
2000-10-20 13:01:21 +02:00
|
|
|
}
|
2004-02-11 23:55:26 +01:00
|
|
|
|
|
|
|
/*
|
2006-03-29 23:17:39 +02:00
|
|
|
* Drop a relation during XLOG replay
|
2004-02-11 23:55:26 +01:00
|
|
|
*
|
2008-06-12 11:12:31 +02:00
|
|
|
* This is called when the relation is about to be deleted; we need to remove
|
|
|
|
* any open "invalid-page" records for the relation.
|
2004-02-11 23:55:26 +01:00
|
|
|
*/
|
|
|
|
void
|
2008-08-11 13:05:11 +02:00
|
|
|
XLogDropRelation(RelFileNode rnode, ForkNumber forknum)
|
2004-02-11 23:55:26 +01:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
forget_invalid_pages(rnode, forknum, 0);
|
2004-02-11 23:55:26 +01:00
|
|
|
}
|
2006-03-29 23:17:39 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Drop a whole database during XLOG replay
|
|
|
|
*
|
|
|
|
* As above, but for DROP DATABASE instead of dropping a single rel
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
XLogDropDatabase(Oid dbid)
|
|
|
|
{
|
2008-06-12 11:12:31 +02:00
|
|
|
/*
|
|
|
|
* This is unnecessarily heavy-handed, as it will close SMgrRelation
|
2009-06-11 16:49:15 +02:00
|
|
|
* objects for other databases as well. DROP DATABASE occurs seldom enough
|
|
|
|
* that it's not worth introducing a variant of smgrclose for just this
|
|
|
|
* purpose. XXX: Or should we rather leave the smgr entries dangling?
|
2008-06-12 11:12:31 +02:00
|
|
|
*/
|
|
|
|
smgrcloseall();
|
2006-04-14 22:27:24 +02:00
|
|
|
|
|
|
|
forget_invalid_pages_db(dbid);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Truncate a relation during XLOG replay
|
|
|
|
*
|
2008-06-12 11:12:31 +02:00
|
|
|
* We need to clean up any open "invalid-page" records for the dropped pages.
|
2006-04-14 22:27:24 +02:00
|
|
|
*/
|
|
|
|
void
|
2008-08-11 13:05:11 +02:00
|
|
|
XLogTruncateRelation(RelFileNode rnode, ForkNumber forkNum,
|
|
|
|
BlockNumber nblocks)
|
2006-04-14 22:27:24 +02:00
|
|
|
{
|
2008-08-11 13:05:11 +02:00
|
|
|
forget_invalid_pages(rnode, forkNum, nblocks);
|
2006-03-29 23:17:39 +02:00
|
|
|
}
|