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.43 2006/03/31 23:32:06 tgl Exp $
16 *-------------------------------------------------------------------------
20 #include "access/xlogutils.h"
21 #include "storage/bufmgr.h"
22 #include "storage/bufpage.h"
23 #include "storage/smgr.h"
24 #include "utils/hsearch.h"
29 * Read a page during XLOG replay
31 * This is functionally comparable to ReadBuffer followed by
32 * LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE): you get back a pinned
33 * and locked buffer. (Getting the lock is not really necessary, since we
34 * expect that this is only used during single-process XLOG replay, but
35 * some subroutines such as MarkBufferDirty will complain if we don't.)
37 * If "init" is true then the caller intends to rewrite the page fully
38 * using the info in the XLOG record. In this case we will extend the
39 * relation if needed to make the page exist, and we will not complain about
40 * the page being "new" (all zeroes).
42 * If "init" is false then the caller needs the page to be valid already.
43 * If the page doesn't exist or contains zeroes, we report failure.
45 * If the return value is InvalidBuffer (only possible when init = false),
46 * the caller should silently skip the update on this page. This currently
47 * never happens, but we retain it as part of the API spec for possible future
51 XLogReadBuffer(Relation reln, BlockNumber blkno, bool init)
53 BlockNumber lastblock = RelationGetNumberOfBlocks(reln);
56 Assert(blkno != P_NEW);
58 if (blkno < lastblock)
60 /* page exists in file */
61 buffer = ReadBuffer(reln, blkno);
65 /* hm, page doesn't exist in file */
67 elog(PANIC, "block %u of relation %u/%u/%u does not exist",
68 blkno, reln->rd_node.spcNode,
69 reln->rd_node.dbNode, reln->rd_node.relNode);
70 /* OK to extend the file */
71 /* we do this in recovery only - no rel-extension lock needed */
73 buffer = InvalidBuffer;
74 while (blkno >= lastblock)
76 if (buffer != InvalidBuffer)
77 ReleaseBuffer(buffer);
78 buffer = ReadBuffer(reln, P_NEW);
81 Assert(BufferGetBlockNumber(buffer) == blkno);
84 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
88 /* check that page has been initialized */
89 Page page = (Page) BufferGetPage(buffer);
91 if (PageIsNew((PageHeader) page))
92 elog(PANIC, "block %u of relation %u/%u/%u is uninitialized",
93 blkno, reln->rd_node.spcNode,
94 reln->rd_node.dbNode, reln->rd_node.relNode);
102 * Lightweight "Relation" cache --- this substitutes for the normal relcache
103 * during XLOG replay.
106 typedef struct XLogRelDesc
108 RelationData reldata;
109 struct XLogRelDesc *lessRecently;
110 struct XLogRelDesc *moreRecently;
113 typedef struct XLogRelCacheEntry
119 static HTAB *_xlrelcache;
120 static XLogRelDesc *_xlrelarr = NULL;
121 static Form_pg_class _xlpgcarr = NULL;
122 static int _xlast = 0;
123 static int _xlcnt = 0;
125 #define _XLOG_RELCACHESIZE 512
128 _xl_init_rel_cache(void)
132 _xlcnt = _XLOG_RELCACHESIZE;
134 _xlrelarr = (XLogRelDesc *) malloc(sizeof(XLogRelDesc) * _xlcnt);
135 memset(_xlrelarr, 0, sizeof(XLogRelDesc) * _xlcnt);
136 _xlpgcarr = (Form_pg_class) malloc(sizeof(FormData_pg_class) * _xlcnt);
137 memset(_xlpgcarr, 0, sizeof(FormData_pg_class) * _xlcnt);
139 _xlrelarr[0].moreRecently = &(_xlrelarr[0]);
140 _xlrelarr[0].lessRecently = &(_xlrelarr[0]);
142 memset(&ctl, 0, sizeof(ctl));
143 ctl.keysize = sizeof(RelFileNode);
144 ctl.entrysize = sizeof(XLogRelCacheEntry);
147 _xlrelcache = hash_create("XLOG relcache", _XLOG_RELCACHESIZE,
148 &ctl, HASH_ELEM | HASH_FUNCTION);
152 _xl_remove_hash_entry(XLogRelDesc *rdesc)
154 Form_pg_class tpgc = rdesc->reldata.rd_rel;
155 XLogRelCacheEntry *hentry;
157 rdesc->lessRecently->moreRecently = rdesc->moreRecently;
158 rdesc->moreRecently->lessRecently = rdesc->lessRecently;
160 hentry = (XLogRelCacheEntry *) hash_search(_xlrelcache,
161 (void *) &(rdesc->reldata.rd_node), HASH_REMOVE, NULL);
163 elog(PANIC, "_xl_remove_hash_entry: file was not found in cache");
165 RelationCloseSmgr(&(rdesc->reldata));
167 memset(rdesc, 0, sizeof(XLogRelDesc));
168 memset(tpgc, 0, sizeof(FormData_pg_class));
169 rdesc->reldata.rd_rel = tpgc;
173 _xl_new_reldesc(void)
180 _xlrelarr[_xlast].reldata.rd_rel = &(_xlpgcarr[_xlast]);
181 return &(_xlrelarr[_xlast]);
185 res = _xlrelarr[0].moreRecently;
187 _xl_remove_hash_entry(res);
195 XLogInitRelationCache(void)
197 _xl_init_rel_cache();
201 XLogCloseRelationCache(void)
203 HASH_SEQ_STATUS status;
204 XLogRelCacheEntry *hentry;
209 hash_seq_init(&status, _xlrelcache);
211 while ((hentry = (XLogRelCacheEntry *) hash_seq_search(&status)) != NULL)
212 _xl_remove_hash_entry(hentry->rdesc);
214 hash_destroy(_xlrelcache);
223 * Open a relation during XLOG replay
225 * Note: this once had an API that allowed NULL return on failure, but it
226 * no longer does; any failure results in elog().
229 XLogOpenRelation(RelFileNode rnode)
232 XLogRelCacheEntry *hentry;
235 hentry = (XLogRelCacheEntry *)
236 hash_search(_xlrelcache, (void *) &rnode, HASH_FIND, NULL);
242 res->lessRecently->moreRecently = res->moreRecently;
243 res->moreRecently->lessRecently = res->lessRecently;
247 res = _xl_new_reldesc();
249 sprintf(RelationGetRelationName(&(res->reldata)), "%u", rnode.relNode);
251 res->reldata.rd_node = rnode;
254 * We set up the lockRelId in case anything tries to lock the dummy
255 * relation. Note that this is fairly bogus since relNode may be
256 * different from the relation's OID. It shouldn't really matter
257 * though, since we are presumably running by ourselves and can't have
258 * any lock conflicts ...
260 res->reldata.rd_lockInfo.lockRelId.dbId = rnode.dbNode;
261 res->reldata.rd_lockInfo.lockRelId.relId = rnode.relNode;
263 hentry = (XLogRelCacheEntry *)
264 hash_search(_xlrelcache, (void *) &rnode, HASH_ENTER, &found);
267 elog(PANIC, "xlog relation already present on insert into cache");
271 res->reldata.rd_targblock = InvalidBlockNumber;
272 res->reldata.rd_smgr = NULL;
273 RelationOpenSmgr(&(res->reldata));
276 * Create the target file if it doesn't already exist. This lets us
277 * cope if the replay sequence contains writes to a relation that is
278 * later deleted. (The original coding of this routine would instead
279 * return NULL, causing the writes to be suppressed. But that seems
280 * like it risks losing valuable data if the filesystem loses an inode
281 * during a crash. Better to write the data until we are actually
282 * told to delete the file.)
284 smgrcreate(res->reldata.rd_smgr, res->reldata.rd_istemp, true);
287 res->moreRecently = &(_xlrelarr[0]);
288 res->lessRecently = _xlrelarr[0].lessRecently;
289 _xlrelarr[0].lessRecently = res;
290 res->lessRecently->moreRecently = res;
292 return &(res->reldata);
296 * Drop a relation during XLOG replay
298 * This is called when the relation is about to be deleted; we need to ensure
299 * that there is no dangling smgr reference in the xlog relation cache.
301 * Currently, we don't bother to physically remove the relation from the
302 * cache, we just let it age out normally.
305 XLogDropRelation(RelFileNode rnode)
308 XLogRelCacheEntry *hentry;
310 hentry = (XLogRelCacheEntry *)
311 hash_search(_xlrelcache, (void *) &rnode, HASH_FIND, NULL);
314 return; /* not in cache so no work */
316 rdesc = hentry->rdesc;
318 RelationCloseSmgr(&(rdesc->reldata));
322 * Drop a whole database during XLOG replay
324 * As above, but for DROP DATABASE instead of dropping a single rel
327 XLogDropDatabase(Oid dbid)
329 HASH_SEQ_STATUS status;
330 XLogRelCacheEntry *hentry;
332 hash_seq_init(&status, _xlrelcache);
334 while ((hentry = (XLogRelCacheEntry *) hash_seq_search(&status)) != NULL)
336 XLogRelDesc *rdesc = hentry->rdesc;
338 if (hentry->rnode.dbNode == dbid)
339 RelationCloseSmgr(&(rdesc->reldata));