]> granicus.if.org Git - postgresql/blob - src/backend/access/transam/xlogutils.c
Make DROP TABLE rollback-able: postpone physical file delete until commit.
[postgresql] / src / backend / access / transam / xlogutils.c
1 /*-------------------------------------------------------------------------
2  *
3  * xlogutils.c
4  *
5  *
6  * Portions Copyright (c) 1996-2000, PostgreSQL, Inc
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *-------------------------------------------------------------------------
10  */
11
12 #ifdef XLOG
13
14 #include "postgres.h"
15
16 #include "access/xlog.h"
17 #include "access/transam.h"
18 #include "access/xact.h"
19 #include "storage/bufpage.h"
20 #include "storage/bufmgr.h"
21 #include "storage/smgr.h"
22 #include "access/htup.h"
23 #include "access/xlogutils.h"
24 #include "catalog/pg_database.h"
25 #include "lib/hasht.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                                 buffer = ReadBuffer(reln, P_NEW);
204                                 lastblock++;
205                         }
206                 }
207                 if (buffer != InvalidBuffer)
208                         LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
209                 return(buffer);
210         }
211
212         buffer = ReadBuffer(reln, blkno);
213         if (buffer != InvalidBuffer)
214                 LockBuffer(buffer, BUFFER_LOCK_EXCLUSIVE);
215         return(buffer);
216 }
217
218 /*
219  * "Relation" cache
220  */
221
222 typedef struct XLogRelDesc
223 {
224         RelationData                    reldata;
225         struct XLogRelDesc         *lessRecently;
226         struct XLogRelDesc         *moreRecently;
227 } XLogRelDesc;
228
229 typedef struct XLogRelCacheEntry
230 {
231         RelFileNode             rnode;
232         XLogRelDesc        *rdesc;
233 } XLogRelCacheEntry;
234
235 static HTAB                                *_xlrelcache;
236 static XLogRelDesc                 *_xlrelarr = NULL;
237 static Form_pg_class            _xlpgcarr = NULL;
238 static int                                      _xlast = 0;
239 static int                                      _xlcnt = 0;
240 #define _XLOG_RELCACHESIZE      512
241
242 static void
243 _xl_init_rel_cache(void)
244 {
245         HASHCTL                 ctl;
246
247         _xlcnt = _XLOG_RELCACHESIZE;
248         _xlast = 0;
249         _xlrelarr = (XLogRelDesc*) malloc(sizeof(XLogRelDesc) * _xlcnt);
250         memset(_xlrelarr, 0, sizeof(XLogRelDesc) * _xlcnt);
251         _xlpgcarr = (Form_pg_class) malloc(sizeof(FormData_pg_class) * _xlcnt);
252         memset(_xlpgcarr, 0, sizeof(FormData_pg_class) * _xlcnt);
253
254         _xlrelarr[0].moreRecently = &(_xlrelarr[0]);
255         _xlrelarr[0].lessRecently = &(_xlrelarr[0]);
256
257         memset(&ctl, 0, (int) sizeof(ctl));
258         ctl.keysize = sizeof(RelFileNode);
259         ctl.datasize = sizeof(XLogRelDesc*);
260         ctl.hash = tag_hash;
261
262         _xlrelcache = hash_create(_XLOG_RELCACHESIZE, &ctl,
263                                                                 HASH_ELEM | HASH_FUNCTION);
264 }
265
266 static void
267 _xl_remove_hash_entry(XLogRelDesc **edata, int dummy)
268 {
269         XLogRelCacheEntry          *hentry;
270         bool                                    found;
271         XLogRelDesc                        *rdesc = *edata;
272         Form_pg_class                   tpgc = rdesc->reldata.rd_rel;
273
274         rdesc->lessRecently->moreRecently = rdesc->moreRecently;
275         rdesc->moreRecently->lessRecently = rdesc->lessRecently;
276
277         hentry = (XLogRelCacheEntry*) hash_search(_xlrelcache, 
278                 (char*)&(rdesc->reldata.rd_node), HASH_REMOVE, &found);
279
280         if (hentry == NULL)
281                 elog(STOP, "_xl_remove_hash_entry: can't delete from cache");
282         if (!found)
283                 elog(STOP, "_xl_remove_hash_entry: file was not found in cache");
284
285         if (rdesc->reldata.rd_fd >= 0)
286                 smgrclose(DEFAULT_SMGR, &(rdesc->reldata));
287
288         memset(rdesc, 0, sizeof(XLogRelDesc));
289         memset(tpgc, 0, sizeof(FormData_pg_class));
290         rdesc->reldata.rd_rel = tpgc;
291
292         return;
293 }
294
295 static XLogRelDesc*
296 _xl_new_reldesc(void)
297 {
298         XLogRelDesc        *res;
299
300         _xlast++;
301         if (_xlast < _xlcnt)
302         {
303                 _xlrelarr[_xlast].reldata.rd_rel = &(_xlpgcarr[_xlast]);
304                 return(&(_xlrelarr[_xlast]));
305         }
306
307         /* reuse */
308         res = _xlrelarr[0].moreRecently;
309
310         _xl_remove_hash_entry(&res, 0);
311
312         _xlast--;
313         return(res);
314 }
315
316 extern void CreateDummyCaches(void);
317 extern void DestroyDummyCaches(void);
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 }
401
402 #endif