]> granicus.if.org Git - postgresql/blob - src/backend/access/transam/xlogutils.c
XLOG (and related) changes:
[postgresql] / src / backend / access / transam / xlogutils.c
1 /*-------------------------------------------------------------------------
2  *
3  * xlogutils.c
4  *
5  *
6  * Portions Copyright (c) 1996-2001, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  * $Header: /cvsroot/pgsql/src/backend/access/transam/xlogutils.c,v 1.14 2001/03/13 01:17:05 tgl Exp $
10  *
11  *-------------------------------------------------------------------------
12  */
13 #include "postgres.h"
14
15 #include "access/xlog.h"
16 #include "access/transam.h"
17 #include "access/xact.h"
18 #include "storage/bufpage.h"
19 #include "storage/bufmgr.h"
20 #include "storage/smgr.h"
21 #include "access/htup.h"
22 #include "access/xlogutils.h"
23 #include "catalog/pg_database.h"
24 #include "lib/hasht.h"
25 #include "utils/relcache.h"
26
27 /*
28  * ---------------------------------------------------------------
29  *
30  * Index support functions
31  *
32  *----------------------------------------------------------------
33  */
34
35 /*
36  * Check if specified heap tuple was inserted by given
37  * xaction/command and return
38  *
39  * - -1 if not
40  * - 0  if there is no tuple at all
41  * - 1  if yes
42  */
43 int
44 XLogIsOwnerOfTuple(RelFileNode hnode, ItemPointer iptr, 
45                                         TransactionId xid, CommandId cid)
46 {
47         Relation                reln;
48         Buffer                  buffer;
49         Page                    page;
50         ItemId                  lp;
51         HeapTupleHeader htup;
52
53         reln = XLogOpenRelation(false, RM_HEAP_ID, hnode);
54         if (!RelationIsValid(reln))
55                 return(0);
56
57         buffer = ReadBuffer(reln, ItemPointerGetBlockNumber(iptr));
58         if (!BufferIsValid(buffer))
59                 return(0);
60
61         LockBuffer(buffer, BUFFER_LOCK_SHARE);
62         page = (Page) BufferGetPage(buffer);
63         if (PageIsNew((PageHeader) page) ||
64                 ItemPointerGetOffsetNumber(iptr) > PageGetMaxOffsetNumber(page))
65         {
66                 UnlockAndReleaseBuffer(buffer);
67                 return(0);
68         }
69         lp = PageGetItemId(page, ItemPointerGetOffsetNumber(iptr));
70         if (!ItemIdIsUsed(lp) || ItemIdDeleted(lp))
71         {
72                 UnlockAndReleaseBuffer(buffer);
73                 return(0);
74         }
75
76         htup = (HeapTupleHeader) PageGetItem(page, lp);
77
78         Assert(PageGetSUI(page) == ThisStartUpID);
79         if (htup->t_xmin != xid || htup->t_cmin != cid)
80         {
81                 UnlockAndReleaseBuffer(buffer);
82                 return(-1);
83         }
84
85         UnlockAndReleaseBuffer(buffer);
86         return(1);
87 }
88
89 /*
90  * MUST BE CALLED ONLY ON RECOVERY.
91  *
92  * Check if exists valid (inserted by not aborted xaction) heap tuple
93  * for given item pointer
94  */
95 bool
96 XLogIsValidTuple(RelFileNode hnode, ItemPointer iptr)
97 {
98         Relation                reln;
99         Buffer                  buffer;
100         Page                    page;
101         ItemId                  lp;
102         HeapTupleHeader htup;
103
104         reln = XLogOpenRelation(false, RM_HEAP_ID, hnode);
105         if (!RelationIsValid(reln))
106                 return(false);
107
108         buffer = ReadBuffer(reln, ItemPointerGetBlockNumber(iptr));
109         if (!BufferIsValid(buffer))
110                 return(false);
111
112         LockBuffer(buffer, BUFFER_LOCK_SHARE);
113         page = (Page) BufferGetPage(buffer);
114         if (PageIsNew((PageHeader) page) ||
115                 ItemPointerGetOffsetNumber(iptr) > PageGetMaxOffsetNumber(page))
116         {
117                 UnlockAndReleaseBuffer(buffer);
118                 return(false);
119         }
120
121         if (PageGetSUI(page) != ThisStartUpID)
122         {
123                 Assert(PageGetSUI(page) < ThisStartUpID);
124                 UnlockAndReleaseBuffer(buffer);
125                 return(true);
126         }
127
128         lp = PageGetItemId(page, ItemPointerGetOffsetNumber(iptr));
129         if (!ItemIdIsUsed(lp) || ItemIdDeleted(lp))
130         {
131                 UnlockAndReleaseBuffer(buffer);
132                 return(false);
133         }
134
135         htup = (HeapTupleHeader) PageGetItem(page, lp);
136
137         /* MUST CHECK WASN'T TUPLE INSERTED IN PREV STARTUP */
138
139         if (!(htup->t_infomask & HEAP_XMIN_COMMITTED))
140         {
141                 if (htup->t_infomask & HEAP_XMIN_INVALID ||
142                         (htup->t_infomask & HEAP_MOVED_IN &&
143                         TransactionIdDidAbort((TransactionId)htup->t_cmin)) ||
144                         TransactionIdDidAbort(htup->t_xmin))
145                 {
146                         UnlockAndReleaseBuffer(buffer);
147                         return(false);
148                 }
149         }
150
151         UnlockAndReleaseBuffer(buffer);
152         return(true);
153 }
154
155 /*
156  * Open pg_log in recovery
157  */
158 extern Relation LogRelation;    /* pg_log relation */
159
160 void
161 XLogOpenLogRelation(void)
162 {
163         Relation        logRelation;
164
165         Assert(!LogRelation);
166         logRelation = (Relation) malloc(sizeof(RelationData));
167         memset(logRelation, 0, sizeof(RelationData));
168         logRelation->rd_rel = (Form_pg_class) malloc(sizeof(FormData_pg_class));
169         memset(logRelation->rd_rel, 0, sizeof(FormData_pg_class));
170
171         sprintf(RelationGetPhysicalRelationName(logRelation), "pg_log");
172         logRelation->rd_node.tblNode = InvalidOid;
173         logRelation->rd_node.relNode = RelOid_pg_log;
174         logRelation->rd_fd = -1;
175         logRelation->rd_fd = smgropen(DEFAULT_SMGR, logRelation, false);
176         if (logRelation->rd_fd < 0)
177                 elog(STOP, "XLogOpenLogRelation: failed to open pg_log");
178         LogRelation = logRelation;
179 }
180
181 /*
182  * ---------------------------------------------------------------
183  *
184  * Storage related support functions
185  *
186  *----------------------------------------------------------------
187  */
188
189 Buffer
190 XLogReadBuffer(bool extend, Relation reln, BlockNumber blkno)
191 {
192         BlockNumber     lastblock = RelationGetNumberOfBlocks(reln);
193         Buffer          buffer;
194
195         if (blkno >= lastblock)
196         {
197                 buffer = InvalidBuffer;
198                 if (extend)             /* we do this in recovery only - no locks */
199                 {
200                         Assert(InRecovery);
201                         while (lastblock <= blkno)
202                         {
203                                 if (buffer != InvalidBuffer)
204                                         ReleaseBuffer(buffer); /* must be WriteBuffer()? */
205                                 buffer = ReadBuffer(reln, P_NEW);
206                                 lastblock++;
207                         }
208                 }
209                 if (buffer != InvalidBuffer)
210                         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
211                 return(buffer);
212         }
213
214         buffer = ReadBuffer(reln, blkno);
215         if (buffer != InvalidBuffer)
216                 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
217         return(buffer);
218 }
219
220 /*
221  * "Relation" cache
222  */
223
224 typedef struct XLogRelDesc
225 {
226         RelationData                    reldata;
227         struct XLogRelDesc         *lessRecently;
228         struct XLogRelDesc         *moreRecently;
229 } XLogRelDesc;
230
231 typedef struct XLogRelCacheEntry
232 {
233         RelFileNode             rnode;
234         XLogRelDesc        *rdesc;
235 } XLogRelCacheEntry;
236
237 static HTAB                                *_xlrelcache;
238 static XLogRelDesc                 *_xlrelarr = NULL;
239 static Form_pg_class            _xlpgcarr = NULL;
240 static int                                      _xlast = 0;
241 static int                                      _xlcnt = 0;
242 #define _XLOG_RELCACHESIZE      512
243
244 static void
245 _xl_init_rel_cache(void)
246 {
247         HASHCTL                 ctl;
248
249         _xlcnt = _XLOG_RELCACHESIZE;
250         _xlast = 0;
251         _xlrelarr = (XLogRelDesc*) malloc(sizeof(XLogRelDesc) * _xlcnt);
252         memset(_xlrelarr, 0, sizeof(XLogRelDesc) * _xlcnt);
253         _xlpgcarr = (Form_pg_class) malloc(sizeof(FormData_pg_class) * _xlcnt);
254         memset(_xlpgcarr, 0, sizeof(FormData_pg_class) * _xlcnt);
255
256         _xlrelarr[0].moreRecently = &(_xlrelarr[0]);
257         _xlrelarr[0].lessRecently = &(_xlrelarr[0]);
258
259         memset(&ctl, 0, (int) sizeof(ctl));
260         ctl.keysize = sizeof(RelFileNode);
261         ctl.datasize = sizeof(XLogRelDesc*);
262         ctl.hash = tag_hash;
263
264         _xlrelcache = hash_create(_XLOG_RELCACHESIZE, &ctl,
265                                                                 HASH_ELEM | HASH_FUNCTION);
266 }
267
268 static void
269 _xl_remove_hash_entry(XLogRelDesc **edata, Datum dummy)
270 {
271         XLogRelCacheEntry          *hentry;
272         bool                                    found;
273         XLogRelDesc                        *rdesc = *edata;
274         Form_pg_class                   tpgc = rdesc->reldata.rd_rel;
275
276         rdesc->lessRecently->moreRecently = rdesc->moreRecently;
277         rdesc->moreRecently->lessRecently = rdesc->lessRecently;
278
279         hentry = (XLogRelCacheEntry*) hash_search(_xlrelcache, 
280                 (char*)&(rdesc->reldata.rd_node), HASH_REMOVE, &found);
281
282         if (hentry == NULL)
283                 elog(STOP, "_xl_remove_hash_entry: can't delete from cache");
284         if (!found)
285                 elog(STOP, "_xl_remove_hash_entry: file was not found in cache");
286
287         if (rdesc->reldata.rd_fd >= 0)
288                 smgrclose(DEFAULT_SMGR, &(rdesc->reldata));
289
290         memset(rdesc, 0, sizeof(XLogRelDesc));
291         memset(tpgc, 0, sizeof(FormData_pg_class));
292         rdesc->reldata.rd_rel = tpgc;
293
294         return;
295 }
296
297 static XLogRelDesc*
298 _xl_new_reldesc(void)
299 {
300         XLogRelDesc        *res;
301
302         _xlast++;
303         if (_xlast < _xlcnt)
304         {
305                 _xlrelarr[_xlast].reldata.rd_rel = &(_xlpgcarr[_xlast]);
306                 return(&(_xlrelarr[_xlast]));
307         }
308
309         /* reuse */
310         res = _xlrelarr[0].moreRecently;
311
312         _xl_remove_hash_entry(&res, 0);
313
314         _xlast--;
315         return(res);
316 }
317
318
319 void
320 XLogInitRelationCache(void)
321 {
322         CreateDummyCaches();
323         _xl_init_rel_cache();
324 }
325
326 void
327 XLogCloseRelationCache(void)
328 {
329
330         DestroyDummyCaches();
331
332         if (!_xlrelarr)
333                 return;
334
335         HashTableWalk(_xlrelcache, (HashtFunc) _xl_remove_hash_entry, 0);
336         hash_destroy(_xlrelcache);
337
338         free(_xlrelarr);
339         free(_xlpgcarr);
340
341         _xlrelarr = NULL;
342 }
343
344 Relation
345 XLogOpenRelation(bool redo, RmgrId rmid, RelFileNode rnode)
346 {
347         XLogRelDesc                        *res;
348         XLogRelCacheEntry          *hentry;
349         bool                                    found;
350
351         hentry = (XLogRelCacheEntry*) 
352                         hash_search(_xlrelcache, (char*)&rnode, HASH_FIND, &found);
353
354         if (hentry == NULL)
355                 elog(STOP, "XLogOpenRelation: error in cache");
356
357         if (found)
358         {
359                 res = hentry->rdesc;
360
361                 res->lessRecently->moreRecently = res->moreRecently;
362                 res->moreRecently->lessRecently = res->lessRecently;
363         }
364         else
365         {
366                 res = _xl_new_reldesc();
367
368                 sprintf(RelationGetPhysicalRelationName(&(res->reldata)), "%u", rnode.relNode);
369
370                 /* unexisting DB id */
371                 res->reldata.rd_lockInfo.lockRelId.dbId = RecoveryDb;
372                 res->reldata.rd_lockInfo.lockRelId.relId = rnode.relNode;
373                 res->reldata.rd_node = rnode;
374
375                 hentry = (XLogRelCacheEntry*) 
376                         hash_search(_xlrelcache, (char*)&rnode, HASH_ENTER, &found);
377
378                 if (hentry == NULL)
379                         elog(STOP, "XLogOpenRelation: can't insert into cache");
380
381                 if (found)
382                         elog(STOP, "XLogOpenRelation: file found on insert into cache");
383
384                 hentry->rdesc = res;
385
386                 res->reldata.rd_fd = -1;
387                 res->reldata.rd_fd = smgropen(DEFAULT_SMGR, &(res->reldata),
388                                                                           true /* allow failure */);
389         }
390
391         res->moreRecently = &(_xlrelarr[0]);
392         res->lessRecently = _xlrelarr[0].lessRecently;
393         _xlrelarr[0].lessRecently = res;
394         res->lessRecently->moreRecently = res;
395
396         if (res->reldata.rd_fd < 0)             /* file doesn't exist */
397                 return(NULL);
398
399         return(&(res->reldata));
400 }