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-2006, 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.47 2006/07/14 14:52:17 momjian 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
176 * and 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).
211 * If "init" is false then the caller needs the page to be valid already.
212 * If the page doesn't exist or contains zeroes, we return InvalidBuffer.
213 * In this case the caller should silently skip the update on this page.
214 * (In this situation, we expect that the page was later dropped or truncated.
215 * If we don't see evidence of that later in the WAL sequence, we'll complain
216 * at the end of WAL replay.)
219 XLogReadBuffer(Relation reln, BlockNumber blkno, bool init)
221 BlockNumber lastblock = RelationGetNumberOfBlocks(reln);
224 Assert(blkno != P_NEW);
226 if (blkno < lastblock)
228 /* page exists in file */
229 buffer = ReadBuffer(reln, blkno);
233 /* hm, page doesn't exist in file */
236 log_invalid_page(reln->rd_node, blkno, false);
237 return InvalidBuffer;
239 /* OK to extend the file */
240 /* we do this in recovery only - no rel-extension lock needed */
242 buffer = InvalidBuffer;
243 while (blkno >= lastblock)
245 if (buffer != InvalidBuffer)
246 ReleaseBuffer(buffer);
247 buffer = ReadBuffer(reln, P_NEW);
250 Assert(BufferGetBlockNumber(buffer) == blkno);
253 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
257 /* check that page has been initialized */
258 Page page = (Page) BufferGetPage(buffer);
260 if (PageIsNew((PageHeader) page))
262 UnlockReleaseBuffer(buffer);
263 log_invalid_page(reln->rd_node, blkno, true);
264 return InvalidBuffer;
273 * Lightweight "Relation" cache --- this substitutes for the normal relcache
274 * during XLOG replay.
277 typedef struct XLogRelDesc
279 RelationData reldata;
280 struct XLogRelDesc *lessRecently;
281 struct XLogRelDesc *moreRecently;
284 typedef struct XLogRelCacheEntry
290 static HTAB *_xlrelcache;
291 static XLogRelDesc *_xlrelarr = NULL;
292 static Form_pg_class _xlpgcarr = NULL;
293 static int _xlast = 0;
294 static int _xlcnt = 0;
296 #define _XLOG_RELCACHESIZE 512
299 _xl_init_rel_cache(void)
303 _xlcnt = _XLOG_RELCACHESIZE;
305 _xlrelarr = (XLogRelDesc *) malloc(sizeof(XLogRelDesc) * _xlcnt);
306 memset(_xlrelarr, 0, sizeof(XLogRelDesc) * _xlcnt);
307 _xlpgcarr = (Form_pg_class) malloc(sizeof(FormData_pg_class) * _xlcnt);
308 memset(_xlpgcarr, 0, sizeof(FormData_pg_class) * _xlcnt);
310 _xlrelarr[0].moreRecently = &(_xlrelarr[0]);
311 _xlrelarr[0].lessRecently = &(_xlrelarr[0]);
313 memset(&ctl, 0, sizeof(ctl));
314 ctl.keysize = sizeof(RelFileNode);
315 ctl.entrysize = sizeof(XLogRelCacheEntry);
318 _xlrelcache = hash_create("XLOG relcache", _XLOG_RELCACHESIZE,
319 &ctl, HASH_ELEM | HASH_FUNCTION);
323 _xl_remove_hash_entry(XLogRelDesc *rdesc)
325 Form_pg_class tpgc = rdesc->reldata.rd_rel;
326 XLogRelCacheEntry *hentry;
328 rdesc->lessRecently->moreRecently = rdesc->moreRecently;
329 rdesc->moreRecently->lessRecently = rdesc->lessRecently;
331 hentry = (XLogRelCacheEntry *) hash_search(_xlrelcache,
332 (void *) &(rdesc->reldata.rd_node), HASH_REMOVE, NULL);
334 elog(PANIC, "_xl_remove_hash_entry: file was not found in cache");
336 RelationCloseSmgr(&(rdesc->reldata));
338 memset(rdesc, 0, sizeof(XLogRelDesc));
339 memset(tpgc, 0, sizeof(FormData_pg_class));
340 rdesc->reldata.rd_rel = tpgc;
344 _xl_new_reldesc(void)
351 _xlrelarr[_xlast].reldata.rd_rel = &(_xlpgcarr[_xlast]);
352 return &(_xlrelarr[_xlast]);
356 res = _xlrelarr[0].moreRecently;
358 _xl_remove_hash_entry(res);
366 XLogInitRelationCache(void)
368 _xl_init_rel_cache();
369 invalid_page_tab = NULL;
373 XLogCloseRelationCache(void)
375 HASH_SEQ_STATUS status;
376 XLogRelCacheEntry *hentry;
381 hash_seq_init(&status, _xlrelcache);
383 while ((hentry = (XLogRelCacheEntry *) hash_seq_search(&status)) != NULL)
384 _xl_remove_hash_entry(hentry->rdesc);
386 hash_destroy(_xlrelcache);
395 * Open a relation during XLOG replay
397 * Note: this once had an API that allowed NULL return on failure, but it
398 * no longer does; any failure results in elog().
401 XLogOpenRelation(RelFileNode rnode)
404 XLogRelCacheEntry *hentry;
407 hentry = (XLogRelCacheEntry *)
408 hash_search(_xlrelcache, (void *) &rnode, HASH_FIND, NULL);
414 res->lessRecently->moreRecently = res->moreRecently;
415 res->moreRecently->lessRecently = res->lessRecently;
419 res = _xl_new_reldesc();
421 sprintf(RelationGetRelationName(&(res->reldata)), "%u", rnode.relNode);
423 res->reldata.rd_node = rnode;
426 * We set up the lockRelId in case anything tries to lock the dummy
427 * relation. Note that this is fairly bogus since relNode may be
428 * different from the relation's OID. It shouldn't really matter
429 * though, since we are presumably running by ourselves and can't have
430 * any lock conflicts ...
432 res->reldata.rd_lockInfo.lockRelId.dbId = rnode.dbNode;
433 res->reldata.rd_lockInfo.lockRelId.relId = rnode.relNode;
435 hentry = (XLogRelCacheEntry *)
436 hash_search(_xlrelcache, (void *) &rnode, HASH_ENTER, &found);
439 elog(PANIC, "xlog relation already present on insert into cache");
443 res->reldata.rd_targblock = InvalidBlockNumber;
444 res->reldata.rd_smgr = NULL;
445 RelationOpenSmgr(&(res->reldata));
448 * Create the target file if it doesn't already exist. This lets us
449 * cope if the replay sequence contains writes to a relation that is
450 * later deleted. (The original coding of this routine would instead
451 * return NULL, causing the writes to be suppressed. But that seems
452 * like it risks losing valuable data if the filesystem loses an inode
453 * during a crash. Better to write the data until we are actually
454 * told to delete the file.)
456 smgrcreate(res->reldata.rd_smgr, res->reldata.rd_istemp, true);
459 res->moreRecently = &(_xlrelarr[0]);
460 res->lessRecently = _xlrelarr[0].lessRecently;
461 _xlrelarr[0].lessRecently = res;
462 res->lessRecently->moreRecently = res;
464 return &(res->reldata);
468 * Drop a relation during XLOG replay
470 * This is called when the relation is about to be deleted; we need to ensure
471 * that there is no dangling smgr reference in the xlog relation cache.
473 * Currently, we don't bother to physically remove the relation from the
474 * cache, we just let it age out normally.
476 * This also takes care of removing any open "invalid-page" records for
480 XLogDropRelation(RelFileNode rnode)
482 XLogRelCacheEntry *hentry;
484 hentry = (XLogRelCacheEntry *)
485 hash_search(_xlrelcache, (void *) &rnode, HASH_FIND, NULL);
489 XLogRelDesc *rdesc = hentry->rdesc;
491 RelationCloseSmgr(&(rdesc->reldata));
494 forget_invalid_pages(rnode, 0);
498 * Drop a whole database during XLOG replay
500 * As above, but for DROP DATABASE instead of dropping a single rel
503 XLogDropDatabase(Oid dbid)
505 HASH_SEQ_STATUS status;
506 XLogRelCacheEntry *hentry;
508 hash_seq_init(&status, _xlrelcache);
510 while ((hentry = (XLogRelCacheEntry *) hash_seq_search(&status)) != NULL)
512 XLogRelDesc *rdesc = hentry->rdesc;
514 if (hentry->rnode.dbNode == dbid)
515 RelationCloseSmgr(&(rdesc->reldata));
518 forget_invalid_pages_db(dbid);
522 * Truncate a relation during XLOG replay
524 * We don't need to do anything to the fake relcache, but we do need to
525 * clean up any open "invalid-page" records for the dropped pages.
528 XLogTruncateRelation(RelFileNode rnode, BlockNumber nblocks)
530 forget_invalid_pages(rnode, nblocks);