1 /*-------------------------------------------------------------------------
4 * code to create and destroy physical storage for relations
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * src/backend/catalog/storage.c
14 * Some of this code used to be in storage/smgr/smgr.c, and the
15 * function names still reflect that.
17 *-------------------------------------------------------------------------
22 #include "access/visibilitymap.h"
23 #include "access/xact.h"
24 #include "access/xlogutils.h"
25 #include "catalog/catalog.h"
26 #include "catalog/storage.h"
27 #include "storage/freespace.h"
28 #include "storage/smgr.h"
29 #include "utils/memutils.h"
30 #include "utils/rel.h"
33 * We keep a list of all relations (represented as RelFileNode values)
34 * that have been created or deleted in the current transaction. When
35 * a relation is created, we create the physical file immediately, but
36 * remember it so that we can delete the file again if the current
37 * transaction is aborted. Conversely, a deletion request is NOT
38 * executed immediately, but is just entered in the list. When and if
39 * the transaction commits, we can delete the physical file.
41 * To handle subtransactions, every entry is marked with its transaction
42 * nesting level. At subtransaction commit, we reassign the subtransaction's
43 * entries to the parent nesting level. At subtransaction abort, we can
44 * immediately execute the abort-time actions for all entries of the current
47 * NOTE: the list is kept in TopMemoryContext to be sure it won't disappear
48 * unbetimes. It'd probably be OK to keep it in TopTransactionContext,
49 * but I'm being paranoid.
52 typedef struct PendingRelDelete
54 RelFileNode relnode; /* relation that may need to be deleted */
55 BackendId backend; /* InvalidBackendId if not a temp rel */
56 bool atCommit; /* T=delete at commit; F=delete at abort */
57 int nestLevel; /* xact nesting level of request */
58 struct PendingRelDelete *next; /* linked-list link */
61 static PendingRelDelete *pendingDeletes = NULL; /* head of linked list */
64 * Declarations for smgr-related XLOG records
66 * Note: we log file creation and truncation here, but logging of deletion
67 * actions is handled by xact.c, because it is part of transaction commit.
70 /* XLOG gives us high 4 bits */
71 #define XLOG_SMGR_CREATE 0x10
72 #define XLOG_SMGR_TRUNCATE 0x20
74 typedef struct xl_smgr_create
80 typedef struct xl_smgr_truncate
88 * RelationCreateStorage
89 * Create physical storage for a relation.
91 * Create the underlying disk file storage for the relation. This only
92 * creates the main fork; additional forks are created lazily by the
93 * modules that need them.
95 * This function is transactional. The creation is WAL-logged, and if the
96 * transaction aborts later on, the storage will be destroyed.
99 RelationCreateStorage(RelFileNode rnode, char relpersistence)
101 PendingRelDelete *pending;
106 switch (relpersistence)
108 case RELPERSISTENCE_TEMP:
109 backend = MyBackendId;
112 case RELPERSISTENCE_UNLOGGED:
113 backend = InvalidBackendId;
116 case RELPERSISTENCE_PERMANENT:
117 backend = InvalidBackendId;
121 elog(ERROR, "invalid relpersistence: %c", relpersistence);
122 return; /* placate compiler */
125 srel = smgropen(rnode, backend);
126 smgrcreate(srel, MAIN_FORKNUM, false);
129 log_smgrcreate(&srel->smgr_rnode.node, MAIN_FORKNUM);
131 /* Add the relation to the list of stuff to delete at abort */
132 pending = (PendingRelDelete *)
133 MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
134 pending->relnode = rnode;
135 pending->backend = backend;
136 pending->atCommit = false; /* delete if abort */
137 pending->nestLevel = GetCurrentTransactionNestLevel();
138 pending->next = pendingDeletes;
139 pendingDeletes = pending;
143 * Perform XLogInsert of a XLOG_SMGR_CREATE record to WAL.
146 log_smgrcreate(RelFileNode *rnode, ForkNumber forkNum)
148 xl_smgr_create xlrec;
152 * Make an XLOG entry reporting the file creation.
154 xlrec.rnode = *rnode;
155 xlrec.forkNum = forkNum;
157 rdata.data = (char *) &xlrec;
158 rdata.len = sizeof(xlrec);
159 rdata.buffer = InvalidBuffer;
162 XLogInsert(RM_SMGR_ID, XLOG_SMGR_CREATE, &rdata);
166 * RelationDropStorage
167 * Schedule unlinking of physical storage at transaction commit.
170 RelationDropStorage(Relation rel)
172 PendingRelDelete *pending;
174 /* Add the relation to the list of stuff to delete at commit */
175 pending = (PendingRelDelete *)
176 MemoryContextAlloc(TopMemoryContext, sizeof(PendingRelDelete));
177 pending->relnode = rel->rd_node;
178 pending->backend = rel->rd_backend;
179 pending->atCommit = true; /* delete if commit */
180 pending->nestLevel = GetCurrentTransactionNestLevel();
181 pending->next = pendingDeletes;
182 pendingDeletes = pending;
185 * NOTE: if the relation was created in this transaction, it will now be
186 * present in the pending-delete list twice, once with atCommit true and
187 * once with atCommit false. Hence, it will be physically deleted at end
188 * of xact in either case (and the other entry will be ignored by
189 * smgrDoPendingDeletes, so no error will occur). We could instead remove
190 * the existing list entry and delete the physical file immediately, but
191 * for now I'll keep the logic simple.
194 RelationCloseSmgr(rel);
198 * RelationPreserveStorage
199 * Mark a relation as not to be deleted after all.
201 * We need this function because relation mapping changes are committed
202 * separately from commit of the whole transaction, so it's still possible
203 * for the transaction to abort after the mapping update is done.
204 * When a new physical relation is installed in the map, it would be
205 * scheduled for delete-on-abort, so we'd delete it, and be in trouble.
206 * The relation mapper fixes this by telling us to not delete such relations
207 * after all as part of its commit.
209 * No-op if the relation is not among those scheduled for deletion.
212 RelationPreserveStorage(RelFileNode rnode)
214 PendingRelDelete *pending;
215 PendingRelDelete *prev;
216 PendingRelDelete *next;
219 for (pending = pendingDeletes; pending != NULL; pending = next)
221 next = pending->next;
222 if (RelFileNodeEquals(rnode, pending->relnode))
224 /* we should only find delete-on-abort entries, else trouble */
225 if (pending->atCommit)
226 elog(ERROR, "cannot preserve a delete-on-commit relation");
227 /* unlink and delete list entry */
231 pendingDeletes = next;
233 /* prev does not change */
237 /* unrelated entry, don't touch it */
245 * Physically truncate a relation to the specified number of blocks.
247 * This includes getting rid of any buffers for the blocks that are to be
251 RelationTruncate(Relation rel, BlockNumber nblocks)
256 /* Open it at the smgr level if not already done */
257 RelationOpenSmgr(rel);
260 * Make sure smgr_targblock etc aren't pointing somewhere past new end
262 rel->rd_smgr->smgr_targblock = InvalidBlockNumber;
263 rel->rd_smgr->smgr_fsm_nblocks = InvalidBlockNumber;
264 rel->rd_smgr->smgr_vm_nblocks = InvalidBlockNumber;
266 /* Truncate the FSM first if it exists */
267 fsm = smgrexists(rel->rd_smgr, FSM_FORKNUM);
269 FreeSpaceMapTruncateRel(rel, nblocks);
271 /* Truncate the visibility map too if it exists. */
272 vm = smgrexists(rel->rd_smgr, VISIBILITYMAP_FORKNUM);
274 visibilitymap_truncate(rel, nblocks);
277 * We WAL-log the truncation before actually truncating, which means
278 * trouble if the truncation fails. If we then crash, the WAL replay
279 * likely isn't going to succeed in the truncation either, and cause a
280 * PANIC. It's tempting to put a critical section here, but that cure
281 * would be worse than the disease. It would turn a usually harmless
282 * failure to truncate, that might spell trouble at WAL replay, into a
285 if (RelationNeedsWAL(rel))
288 * Make an XLOG entry reporting the file truncation.
292 xl_smgr_truncate xlrec;
294 xlrec.blkno = nblocks;
295 xlrec.rnode = rel->rd_node;
297 rdata.data = (char *) &xlrec;
298 rdata.len = sizeof(xlrec);
299 rdata.buffer = InvalidBuffer;
302 lsn = XLogInsert(RM_SMGR_ID, XLOG_SMGR_TRUNCATE, &rdata);
305 * Flush, because otherwise the truncation of the main relation might
306 * hit the disk before the WAL record, and the truncation of the FSM
307 * or visibility map. If we crashed during that window, we'd be left
308 * with a truncated heap, but the FSM or visibility map would still
309 * contain entries for the non-existent heap pages.
315 /* Do the real work */
316 smgrtruncate(rel->rd_smgr, MAIN_FORKNUM, nblocks);
320 * smgrDoPendingDeletes() -- Take care of relation deletes at end of xact.
322 * This also runs when aborting a subxact; we want to clean up a failed
323 * subxact immediately.
325 * Note: It's possible that we're being asked to remove a relation that has
326 * no physical storage in any fork. In particular, it's possible that we're
327 * cleaning up an old temporary relation for which RemovePgTempFiles has
328 * already recovered the physical storage.
331 smgrDoPendingDeletes(bool isCommit)
333 int nestLevel = GetCurrentTransactionNestLevel();
334 PendingRelDelete *pending;
335 PendingRelDelete *prev;
336 PendingRelDelete *next;
339 for (pending = pendingDeletes; pending != NULL; pending = next)
341 next = pending->next;
342 if (pending->nestLevel < nestLevel)
344 /* outer-level entries should not be processed yet */
349 /* unlink list entry first, so we don't retry on failure */
353 pendingDeletes = next;
354 /* do deletion if called for */
355 if (pending->atCommit == isCommit)
360 srel = smgropen(pending->relnode, pending->backend);
361 for (i = 0; i <= MAX_FORKNUM; i++)
363 if (smgrexists(srel, i))
364 smgrdounlink(srel, i, false);
368 /* must explicitly free the list entry */
370 /* prev does not change */
376 * smgrGetPendingDeletes() -- Get a list of non-temp relations to be deleted.
378 * The return value is the number of relations scheduled for termination.
379 * *ptr is set to point to a freshly-palloc'd array of RelFileNodes.
380 * If there are no relations to be deleted, *ptr is set to NULL.
382 * Only non-temporary relations are included in the returned list. This is OK
383 * because the list is used only in contexts where temporary relations don't
384 * matter: we're either writing to the two-phase state file (and transactions
385 * that have touched temp tables can't be prepared) or we're writing to xlog
386 * (and all temporary files will be zapped if we restart anyway, so no need
387 * for redo to do it also).
389 * Note that the list does not include anything scheduled for termination
390 * by upper-level transactions.
393 smgrGetPendingDeletes(bool forCommit, RelFileNode **ptr)
395 int nestLevel = GetCurrentTransactionNestLevel();
398 PendingRelDelete *pending;
401 for (pending = pendingDeletes; pending != NULL; pending = pending->next)
403 if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
404 && pending->backend == InvalidBackendId)
412 rptr = (RelFileNode *) palloc(nrels * sizeof(RelFileNode));
414 for (pending = pendingDeletes; pending != NULL; pending = pending->next)
416 if (pending->nestLevel >= nestLevel && pending->atCommit == forCommit
417 && pending->backend == InvalidBackendId)
419 *rptr = pending->relnode;
427 * PostPrepare_smgr -- Clean up after a successful PREPARE
429 * What we have to do here is throw away the in-memory state about pending
430 * relation deletes. It's all been recorded in the 2PC state file and
431 * it's no longer smgr's job to worry about it.
434 PostPrepare_smgr(void)
436 PendingRelDelete *pending;
437 PendingRelDelete *next;
439 for (pending = pendingDeletes; pending != NULL; pending = next)
441 next = pending->next;
442 pendingDeletes = next;
443 /* must explicitly free the list entry */
450 * AtSubCommit_smgr() --- Take care of subtransaction commit.
452 * Reassign all items in the pending-deletes list to the parent transaction.
455 AtSubCommit_smgr(void)
457 int nestLevel = GetCurrentTransactionNestLevel();
458 PendingRelDelete *pending;
460 for (pending = pendingDeletes; pending != NULL; pending = pending->next)
462 if (pending->nestLevel >= nestLevel)
463 pending->nestLevel = nestLevel - 1;
468 * AtSubAbort_smgr() --- Take care of subtransaction abort.
470 * Delete created relations and forget about deleted relations.
471 * We can execute these operations immediately because we know this
472 * subtransaction will not commit.
475 AtSubAbort_smgr(void)
477 smgrDoPendingDeletes(false);
481 smgr_redo(XLogRecPtr lsn, XLogRecord *record)
483 uint8 info = record->xl_info & ~XLR_INFO_MASK;
485 /* Backup blocks are not used in smgr records */
486 Assert(!(record->xl_info & XLR_BKP_BLOCK_MASK));
488 if (info == XLOG_SMGR_CREATE)
490 xl_smgr_create *xlrec = (xl_smgr_create *) XLogRecGetData(record);
493 reln = smgropen(xlrec->rnode, InvalidBackendId);
494 smgrcreate(reln, xlrec->forkNum, true);
496 else if (info == XLOG_SMGR_TRUNCATE)
498 xl_smgr_truncate *xlrec = (xl_smgr_truncate *) XLogRecGetData(record);
502 reln = smgropen(xlrec->rnode, InvalidBackendId);
505 * Forcibly create relation if it doesn't exist (which suggests that
506 * it was dropped somewhere later in the WAL sequence). As in
507 * XLogOpenRelation, we prefer to recreate the rel and replay the log
508 * as best we can until the drop is seen.
510 smgrcreate(reln, MAIN_FORKNUM, true);
512 smgrtruncate(reln, MAIN_FORKNUM, xlrec->blkno);
514 /* Also tell xlogutils.c about it */
515 XLogTruncateRelation(xlrec->rnode, MAIN_FORKNUM, xlrec->blkno);
517 /* Truncate FSM and VM too */
518 rel = CreateFakeRelcacheEntry(xlrec->rnode);
520 if (smgrexists(reln, FSM_FORKNUM))
521 FreeSpaceMapTruncateRel(rel, xlrec->blkno);
522 if (smgrexists(reln, VISIBILITYMAP_FORKNUM))
523 visibilitymap_truncate(rel, xlrec->blkno);
525 FreeFakeRelcacheEntry(rel);
528 elog(PANIC, "smgr_redo: unknown op code %u", info);
532 smgr_desc(StringInfo buf, uint8 xl_info, char *rec)
534 uint8 info = xl_info & ~XLR_INFO_MASK;
536 if (info == XLOG_SMGR_CREATE)
538 xl_smgr_create *xlrec = (xl_smgr_create *) rec;
539 char *path = relpathperm(xlrec->rnode, xlrec->forkNum);
541 appendStringInfo(buf, "file create: %s", path);
544 else if (info == XLOG_SMGR_TRUNCATE)
546 xl_smgr_truncate *xlrec = (xl_smgr_truncate *) rec;
547 char *path = relpathperm(xlrec->rnode, MAIN_FORKNUM);
549 appendStringInfo(buf, "file truncate: %s to %u blocks", path,
554 appendStringInfo(buf, "UNKNOWN");