1 /*-------------------------------------------------------------------------
5 * PostgreSQL transaction log manager utility routines
7 * This file contains support routines that are used by XLOG replay functions.
8 * None of this code is used during normal system operation.
11 * Portions Copyright (c) 1996-2007, PostgreSQL Global Development Group
12 * Portions Copyright (c) 1994, Regents of the University of California
14 * $PostgreSQL: pgsql/src/backend/access/transam/xlogutils.c,v 1.50 2007/05/02 23:18:03 tgl Exp $
16 *-------------------------------------------------------------------------
20 #include "access/xlogutils.h"
21 #include "storage/bufpage.h"
22 #include "storage/smgr.h"
23 #include "utils/hsearch.h"
27 * During XLOG replay, we may see XLOG records for incremental updates of
28 * pages that no longer exist, because their relation was later dropped or
29 * truncated. (Note: this is only possible when full_page_writes = OFF,
30 * since when it's ON, the first reference we see to a page should always
31 * be a full-page rewrite not an incremental update.) Rather than simply
32 * ignoring such records, we make a note of the referenced page, and then
33 * complain if we don't actually see a drop or truncate covering the page
36 typedef struct xl_invalid_page_key
38 RelFileNode node; /* the relation */
39 BlockNumber blkno; /* the page */
40 } xl_invalid_page_key;
42 typedef struct xl_invalid_page
44 xl_invalid_page_key key; /* hash key ... must be first */
45 bool present; /* page existed but contained zeroes */
48 static HTAB *invalid_page_tab = NULL;
51 /* Log a reference to an invalid page */
53 log_invalid_page(RelFileNode node, BlockNumber blkno, bool present)
55 xl_invalid_page_key key;
56 xl_invalid_page *hentry;
60 * Log references to invalid pages at DEBUG1 level. This allows some
61 * tracing of the cause (note the elog context mechanism will tell us
62 * something about the XLOG record that generated the reference).
65 elog(DEBUG1, "page %u of relation %u/%u/%u is uninitialized",
66 blkno, node.spcNode, node.dbNode, node.relNode);
68 elog(DEBUG1, "page %u of relation %u/%u/%u does not exist",
69 blkno, node.spcNode, node.dbNode, node.relNode);
71 if (invalid_page_tab == NULL)
73 /* create hash table when first needed */
76 memset(&ctl, 0, sizeof(ctl));
77 ctl.keysize = sizeof(xl_invalid_page_key);
78 ctl.entrysize = sizeof(xl_invalid_page);
81 invalid_page_tab = hash_create("XLOG invalid-page table",
84 HASH_ELEM | HASH_FUNCTION);
87 /* we currently assume xl_invalid_page_key contains no padding */
90 hentry = (xl_invalid_page *)
91 hash_search(invalid_page_tab, (void *) &key, HASH_ENTER, &found);
95 /* hash_search already filled in the key */
96 hentry->present = present;
100 /* repeat reference ... leave "present" as it was */
104 /* Forget any invalid pages >= minblkno, because they've been dropped */
106 forget_invalid_pages(RelFileNode node, BlockNumber minblkno)
108 HASH_SEQ_STATUS status;
109 xl_invalid_page *hentry;
111 if (invalid_page_tab == NULL)
112 return; /* nothing to do */
114 hash_seq_init(&status, invalid_page_tab);
116 while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
118 if (RelFileNodeEquals(hentry->key.node, node) &&
119 hentry->key.blkno >= minblkno)
121 elog(DEBUG2, "page %u of relation %u/%u/%u has been dropped",
122 hentry->key.blkno, hentry->key.node.spcNode,
123 hentry->key.node.dbNode, hentry->key.node.relNode);
125 if (hash_search(invalid_page_tab,
126 (void *) &hentry->key,
127 HASH_REMOVE, NULL) == NULL)
128 elog(ERROR, "hash table corrupted");
133 /* Forget any invalid pages in a whole database */
135 forget_invalid_pages_db(Oid dbid)
137 HASH_SEQ_STATUS status;
138 xl_invalid_page *hentry;
140 if (invalid_page_tab == NULL)
141 return; /* nothing to do */
143 hash_seq_init(&status, invalid_page_tab);
145 while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
147 if (hentry->key.node.dbNode == dbid)
149 elog(DEBUG2, "page %u of relation %u/%u/%u has been dropped",
150 hentry->key.blkno, hentry->key.node.spcNode,
151 hentry->key.node.dbNode, hentry->key.node.relNode);
153 if (hash_search(invalid_page_tab,
154 (void *) &hentry->key,
155 HASH_REMOVE, NULL) == NULL)
156 elog(ERROR, "hash table corrupted");
161 /* Complain about any remaining invalid-page entries */
163 XLogCheckInvalidPages(void)
165 HASH_SEQ_STATUS status;
166 xl_invalid_page *hentry;
167 bool foundone = false;
169 if (invalid_page_tab == NULL)
170 return; /* nothing to do */
172 hash_seq_init(&status, invalid_page_tab);
175 * Our strategy is to emit WARNING messages for all remaining entries and
176 * only PANIC after we've dumped all the available info.
178 while ((hentry = (xl_invalid_page *) hash_seq_search(&status)) != NULL)
181 elog(WARNING, "page %u of relation %u/%u/%u was uninitialized",
182 hentry->key.blkno, hentry->key.node.spcNode,
183 hentry->key.node.dbNode, hentry->key.node.relNode);
185 elog(WARNING, "page %u of relation %u/%u/%u did not exist",
186 hentry->key.blkno, hentry->key.node.spcNode,
187 hentry->key.node.dbNode, hentry->key.node.relNode);
192 elog(PANIC, "WAL contains references to invalid pages");
198 * Read a page during XLOG replay
200 * This is functionally comparable to ReadBuffer followed by
201 * LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE): you get back a pinned
202 * and locked buffer. (Getting the lock is not really necessary, since we
203 * expect that this is only used during single-process XLOG replay, but
204 * some subroutines such as MarkBufferDirty will complain if we don't.)
206 * If "init" is true then the caller intends to rewrite the page fully
207 * using the info in the XLOG record. In this case we will extend the
208 * relation if needed to make the page exist, and we will not complain about
209 * the page being "new" (all zeroes); in fact, we usually will supply a
210 * zeroed buffer without reading the page at all, so as to avoid unnecessary
211 * failure if the page is present on disk but has corrupt headers.
213 * If "init" is false then the caller needs the page to be valid already.
214 * If the page doesn't exist or contains zeroes, we return InvalidBuffer.
215 * In this case the caller should silently skip the update on this page.
216 * (In this situation, we expect that the page was later dropped or truncated.
217 * If we don't see evidence of that later in the WAL sequence, we'll complain
218 * at the end of WAL replay.)
221 XLogReadBuffer(Relation reln, BlockNumber blkno, bool init)
223 BlockNumber lastblock = RelationGetNumberOfBlocks(reln);
226 Assert(blkno != P_NEW);
228 if (blkno < lastblock)
230 /* page exists in file */
232 buffer = ReadOrZeroBuffer(reln, blkno);
234 buffer = ReadBuffer(reln, blkno);
238 /* hm, page doesn't exist in file */
241 log_invalid_page(reln->rd_node, blkno, false);
242 return InvalidBuffer;
244 /* OK to extend the file */
245 /* we do this in recovery only - no rel-extension lock needed */
247 buffer = InvalidBuffer;
248 while (blkno >= lastblock)
250 if (buffer != InvalidBuffer)
251 ReleaseBuffer(buffer);
252 buffer = ReadBuffer(reln, P_NEW);
255 Assert(BufferGetBlockNumber(buffer) == blkno);
258 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
262 /* check that page has been initialized */
263 Page page = (Page) BufferGetPage(buffer);
265 if (PageIsNew((PageHeader) page))
267 UnlockReleaseBuffer(buffer);
268 log_invalid_page(reln->rd_node, blkno, true);
269 return InvalidBuffer;
278 * Lightweight "Relation" cache --- this substitutes for the normal relcache
279 * during XLOG replay.
282 typedef struct XLogRelDesc
284 RelationData reldata;
285 struct XLogRelDesc *lessRecently;
286 struct XLogRelDesc *moreRecently;
289 typedef struct XLogRelCacheEntry
295 static HTAB *_xlrelcache;
296 static XLogRelDesc *_xlrelarr = NULL;
297 static Form_pg_class _xlpgcarr = NULL;
298 static int _xlast = 0;
299 static int _xlcnt = 0;
301 #define _XLOG_RELCACHESIZE 512
304 _xl_init_rel_cache(void)
308 _xlcnt = _XLOG_RELCACHESIZE;
310 _xlrelarr = (XLogRelDesc *) malloc(sizeof(XLogRelDesc) * _xlcnt);
311 memset(_xlrelarr, 0, sizeof(XLogRelDesc) * _xlcnt);
312 _xlpgcarr = (Form_pg_class) malloc(sizeof(FormData_pg_class) * _xlcnt);
313 memset(_xlpgcarr, 0, sizeof(FormData_pg_class) * _xlcnt);
315 _xlrelarr[0].moreRecently = &(_xlrelarr[0]);
316 _xlrelarr[0].lessRecently = &(_xlrelarr[0]);
318 memset(&ctl, 0, sizeof(ctl));
319 ctl.keysize = sizeof(RelFileNode);
320 ctl.entrysize = sizeof(XLogRelCacheEntry);
323 _xlrelcache = hash_create("XLOG relcache", _XLOG_RELCACHESIZE,
324 &ctl, HASH_ELEM | HASH_FUNCTION);
328 _xl_remove_hash_entry(XLogRelDesc *rdesc)
330 Form_pg_class tpgc = rdesc->reldata.rd_rel;
331 XLogRelCacheEntry *hentry;
333 rdesc->lessRecently->moreRecently = rdesc->moreRecently;
334 rdesc->moreRecently->lessRecently = rdesc->lessRecently;
336 hentry = (XLogRelCacheEntry *) hash_search(_xlrelcache,
337 (void *) &(rdesc->reldata.rd_node), HASH_REMOVE, NULL);
339 elog(PANIC, "_xl_remove_hash_entry: file was not found in cache");
341 RelationCloseSmgr(&(rdesc->reldata));
343 memset(rdesc, 0, sizeof(XLogRelDesc));
344 memset(tpgc, 0, sizeof(FormData_pg_class));
345 rdesc->reldata.rd_rel = tpgc;
349 _xl_new_reldesc(void)
356 _xlrelarr[_xlast].reldata.rd_rel = &(_xlpgcarr[_xlast]);
357 return &(_xlrelarr[_xlast]);
361 res = _xlrelarr[0].moreRecently;
363 _xl_remove_hash_entry(res);
371 XLogInitRelationCache(void)
373 _xl_init_rel_cache();
374 invalid_page_tab = NULL;
378 XLogCloseRelationCache(void)
380 HASH_SEQ_STATUS status;
381 XLogRelCacheEntry *hentry;
386 hash_seq_init(&status, _xlrelcache);
388 while ((hentry = (XLogRelCacheEntry *) hash_seq_search(&status)) != NULL)
389 _xl_remove_hash_entry(hentry->rdesc);
391 hash_destroy(_xlrelcache);
400 * Open a relation during XLOG replay
402 * Note: this once had an API that allowed NULL return on failure, but it
403 * no longer does; any failure results in elog().
406 XLogOpenRelation(RelFileNode rnode)
409 XLogRelCacheEntry *hentry;
412 hentry = (XLogRelCacheEntry *)
413 hash_search(_xlrelcache, (void *) &rnode, HASH_FIND, NULL);
419 res->lessRecently->moreRecently = res->moreRecently;
420 res->moreRecently->lessRecently = res->lessRecently;
424 res = _xl_new_reldesc();
426 sprintf(RelationGetRelationName(&(res->reldata)), "%u", rnode.relNode);
428 res->reldata.rd_node = rnode;
431 * We set up the lockRelId in case anything tries to lock the dummy
432 * relation. Note that this is fairly bogus since relNode may be
433 * different from the relation's OID. It shouldn't really matter
434 * though, since we are presumably running by ourselves and can't have
435 * any lock conflicts ...
437 res->reldata.rd_lockInfo.lockRelId.dbId = rnode.dbNode;
438 res->reldata.rd_lockInfo.lockRelId.relId = rnode.relNode;
440 hentry = (XLogRelCacheEntry *)
441 hash_search(_xlrelcache, (void *) &rnode, HASH_ENTER, &found);
444 elog(PANIC, "xlog relation already present on insert into cache");
448 res->reldata.rd_targblock = InvalidBlockNumber;
449 res->reldata.rd_smgr = NULL;
450 RelationOpenSmgr(&(res->reldata));
453 * Create the target file if it doesn't already exist. This lets us
454 * cope if the replay sequence contains writes to a relation that is
455 * later deleted. (The original coding of this routine would instead
456 * return NULL, causing the writes to be suppressed. But that seems
457 * like it risks losing valuable data if the filesystem loses an inode
458 * during a crash. Better to write the data until we are actually
459 * told to delete the file.)
461 smgrcreate(res->reldata.rd_smgr, res->reldata.rd_istemp, true);
464 res->moreRecently = &(_xlrelarr[0]);
465 res->lessRecently = _xlrelarr[0].lessRecently;
466 _xlrelarr[0].lessRecently = res;
467 res->lessRecently->moreRecently = res;
469 return &(res->reldata);
473 * Drop a relation during XLOG replay
475 * This is called when the relation is about to be deleted; we need to ensure
476 * that there is no dangling smgr reference in the xlog relation cache.
478 * Currently, we don't bother to physically remove the relation from the
479 * cache, we just let it age out normally.
481 * This also takes care of removing any open "invalid-page" records for
485 XLogDropRelation(RelFileNode rnode)
487 XLogRelCacheEntry *hentry;
489 hentry = (XLogRelCacheEntry *)
490 hash_search(_xlrelcache, (void *) &rnode, HASH_FIND, NULL);
494 XLogRelDesc *rdesc = hentry->rdesc;
496 RelationCloseSmgr(&(rdesc->reldata));
499 forget_invalid_pages(rnode, 0);
503 * Drop a whole database during XLOG replay
505 * As above, but for DROP DATABASE instead of dropping a single rel
508 XLogDropDatabase(Oid dbid)
510 HASH_SEQ_STATUS status;
511 XLogRelCacheEntry *hentry;
513 hash_seq_init(&status, _xlrelcache);
515 while ((hentry = (XLogRelCacheEntry *) hash_seq_search(&status)) != NULL)
517 XLogRelDesc *rdesc = hentry->rdesc;
519 if (hentry->rnode.dbNode == dbid)
520 RelationCloseSmgr(&(rdesc->reldata));
523 forget_invalid_pages_db(dbid);
527 * Truncate a relation during XLOG replay
529 * We don't need to do anything to the fake relcache, but we do need to
530 * clean up any open "invalid-page" records for the dropped pages.
533 XLogTruncateRelation(RelFileNode rnode, BlockNumber nblocks)
535 forget_invalid_pages(rnode, nblocks);