1 /*-------------------------------------------------------------------------
4 * code to create and destroy physical storage for relations
6 * Portions Copyright (c) 1996-2024, 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/xlog.h"
25 #include "access/xloginsert.h"
26 #include "access/xlogutils.h"
27 #include "catalog/storage.h"
28 #include "catalog/storage_xlog.h"
29 #include "miscadmin.h"
30 #include "storage/bulk_write.h"
31 #include "storage/freespace.h"
32 #include "storage/proc.h"
33 #include "storage/smgr.h"
34 #include "utils/hsearch.h"
35 #include "utils/memutils.h"
36 #include "utils/rel.h"
39 int wal_skip_threshold
= 2048; /* in kilobytes */
42 * We keep a list of all relations (represented as RelFileLocator values)
43 * that have been created or deleted in the current transaction. When
44 * a relation is created, we create the physical file immediately, but
45 * remember it so that we can delete the file again if the current
46 * transaction is aborted. Conversely, a deletion request is NOT
47 * executed immediately, but is just entered in the list. When and if
48 * the transaction commits, we can delete the physical file.
50 * To handle subtransactions, every entry is marked with its transaction
51 * nesting level. At subtransaction commit, we reassign the subtransaction's
52 * entries to the parent nesting level. At subtransaction abort, we can
53 * immediately execute the abort-time actions for all entries of the current
56 * NOTE: the list is kept in TopMemoryContext to be sure it won't disappear
57 * unbetimes. It'd probably be OK to keep it in TopTransactionContext,
58 * but I'm being paranoid.
61 typedef struct PendingRelDelete
63 RelFileLocator rlocator
; /* relation that may need to be deleted */
64 ProcNumber procNumber
; /* INVALID_PROC_NUMBER if not a temp rel */
65 bool atCommit
; /* T=delete at commit; F=delete at abort */
66 int nestLevel
; /* xact nesting level of request */
67 struct PendingRelDelete
*next
; /* linked-list link */
70 typedef struct PendingRelSync
72 RelFileLocator rlocator
;
73 bool is_truncated
; /* Has the file experienced truncation? */
76 static PendingRelDelete
*pendingDeletes
= NULL
; /* head of linked list */
77 static HTAB
*pendingSyncHash
= NULL
;
82 * Queue an at-commit fsync.
85 AddPendingSync(const RelFileLocator
*rlocator
)
87 PendingRelSync
*pending
;
90 /* create the hash if not yet */
95 ctl
.keysize
= sizeof(RelFileLocator
);
96 ctl
.entrysize
= sizeof(PendingRelSync
);
97 ctl
.hcxt
= TopTransactionContext
;
98 pendingSyncHash
= hash_create("pending sync hash", 16, &ctl
,
99 HASH_ELEM
| HASH_BLOBS
| HASH_CONTEXT
);
102 pending
= hash_search(pendingSyncHash
, rlocator
, HASH_ENTER
, &found
);
104 pending
->is_truncated
= false;
108 * RelationCreateStorage
109 * Create physical storage for a relation.
111 * Create the underlying disk file storage for the relation. This only
112 * creates the main fork; additional forks are created lazily by the
113 * modules that need them.
115 * This function is transactional. The creation is WAL-logged, and if the
116 * transaction aborts later on, the storage will be destroyed. A caller
117 * that does not want the storage to be destroyed in case of an abort may
118 * pass register_delete = false.
121 RelationCreateStorage(RelFileLocator rlocator
, char relpersistence
,
122 bool register_delete
)
125 ProcNumber procNumber
;
128 Assert(!IsInParallelMode()); /* couldn't update pendingSyncHash */
130 switch (relpersistence
)
132 case RELPERSISTENCE_TEMP
:
133 procNumber
= ProcNumberForTempRelations();
136 case RELPERSISTENCE_UNLOGGED
:
137 procNumber
= INVALID_PROC_NUMBER
;
140 case RELPERSISTENCE_PERMANENT
:
141 procNumber
= INVALID_PROC_NUMBER
;
145 elog(ERROR
, "invalid relpersistence: %c", relpersistence
);
146 return NULL
; /* placate compiler */
149 srel
= smgropen(rlocator
, procNumber
);
150 smgrcreate(srel
, MAIN_FORKNUM
, false);
153 log_smgrcreate(&srel
->smgr_rlocator
.locator
, MAIN_FORKNUM
);
156 * Add the relation to the list of stuff to delete at abort, if we are
161 PendingRelDelete
*pending
;
163 pending
= (PendingRelDelete
*)
164 MemoryContextAlloc(TopMemoryContext
, sizeof(PendingRelDelete
));
165 pending
->rlocator
= rlocator
;
166 pending
->procNumber
= procNumber
;
167 pending
->atCommit
= false; /* delete if abort */
168 pending
->nestLevel
= GetCurrentTransactionNestLevel();
169 pending
->next
= pendingDeletes
;
170 pendingDeletes
= pending
;
173 if (relpersistence
== RELPERSISTENCE_PERMANENT
&& !XLogIsNeeded())
175 Assert(procNumber
== INVALID_PROC_NUMBER
);
176 AddPendingSync(&rlocator
);
183 * Perform XLogInsert of an XLOG_SMGR_CREATE record to WAL.
186 log_smgrcreate(const RelFileLocator
*rlocator
, ForkNumber forkNum
)
188 xl_smgr_create xlrec
;
191 * Make an XLOG entry reporting the file creation.
193 xlrec
.rlocator
= *rlocator
;
194 xlrec
.forkNum
= forkNum
;
197 XLogRegisterData((char *) &xlrec
, sizeof(xlrec
));
198 XLogInsert(RM_SMGR_ID
, XLOG_SMGR_CREATE
| XLR_SPECIAL_REL_UPDATE
);
202 * RelationDropStorage
203 * Schedule unlinking of physical storage at transaction commit.
206 RelationDropStorage(Relation rel
)
208 PendingRelDelete
*pending
;
210 /* Add the relation to the list of stuff to delete at commit */
211 pending
= (PendingRelDelete
*)
212 MemoryContextAlloc(TopMemoryContext
, sizeof(PendingRelDelete
));
213 pending
->rlocator
= rel
->rd_locator
;
214 pending
->procNumber
= rel
->rd_backend
;
215 pending
->atCommit
= true; /* delete if commit */
216 pending
->nestLevel
= GetCurrentTransactionNestLevel();
217 pending
->next
= pendingDeletes
;
218 pendingDeletes
= pending
;
221 * NOTE: if the relation was created in this transaction, it will now be
222 * present in the pending-delete list twice, once with atCommit true and
223 * once with atCommit false. Hence, it will be physically deleted at end
224 * of xact in either case (and the other entry will be ignored by
225 * smgrDoPendingDeletes, so no error will occur). We could instead remove
226 * the existing list entry and delete the physical file immediately, but
227 * for now I'll keep the logic simple.
230 RelationCloseSmgr(rel
);
234 * RelationPreserveStorage
235 * Mark a relation as not to be deleted after all.
237 * We need this function because relation mapping changes are committed
238 * separately from commit of the whole transaction, so it's still possible
239 * for the transaction to abort after the mapping update is done.
240 * When a new physical relation is installed in the map, it would be
241 * scheduled for delete-on-abort, so we'd delete it, and be in trouble.
242 * The relation mapper fixes this by telling us to not delete such relations
243 * after all as part of its commit.
245 * We also use this to reuse an old build of an index during ALTER TABLE, this
246 * time removing the delete-at-commit entry.
248 * No-op if the relation is not among those scheduled for deletion.
251 RelationPreserveStorage(RelFileLocator rlocator
, bool atCommit
)
253 PendingRelDelete
*pending
;
254 PendingRelDelete
*prev
;
255 PendingRelDelete
*next
;
258 for (pending
= pendingDeletes
; pending
!= NULL
; pending
= next
)
260 next
= pending
->next
;
261 if (RelFileLocatorEquals(rlocator
, pending
->rlocator
)
262 && pending
->atCommit
== atCommit
)
264 /* unlink and delete list entry */
268 pendingDeletes
= next
;
270 /* prev does not change */
274 /* unrelated entry, don't touch it */
282 * Physically truncate a relation to the specified number of blocks.
284 * This includes getting rid of any buffers for the blocks that are to be
288 RelationTruncate(Relation rel
, BlockNumber nblocks
)
292 bool need_fsm_vacuum
= false;
293 ForkNumber forks
[MAX_FORKNUM
];
294 BlockNumber blocks
[MAX_FORKNUM
];
299 * Make sure smgr_targblock etc aren't pointing somewhere past new end.
300 * (Note: don't rely on this reln pointer below this loop.)
302 reln
= RelationGetSmgr(rel
);
303 reln
->smgr_targblock
= InvalidBlockNumber
;
304 for (int i
= 0; i
<= MAX_FORKNUM
; ++i
)
305 reln
->smgr_cached_nblocks
[i
] = InvalidBlockNumber
;
307 /* Prepare for truncation of MAIN fork of the relation */
308 forks
[nforks
] = MAIN_FORKNUM
;
309 blocks
[nforks
] = nblocks
;
312 /* Prepare for truncation of the FSM if it exists */
313 fsm
= smgrexists(RelationGetSmgr(rel
), FSM_FORKNUM
);
316 blocks
[nforks
] = FreeSpaceMapPrepareTruncateRel(rel
, nblocks
);
317 if (BlockNumberIsValid(blocks
[nforks
]))
319 forks
[nforks
] = FSM_FORKNUM
;
321 need_fsm_vacuum
= true;
325 /* Prepare for truncation of the visibility map too if it exists */
326 vm
= smgrexists(RelationGetSmgr(rel
), VISIBILITYMAP_FORKNUM
);
329 blocks
[nforks
] = visibilitymap_prepare_truncate(rel
, nblocks
);
330 if (BlockNumberIsValid(blocks
[nforks
]))
332 forks
[nforks
] = VISIBILITYMAP_FORKNUM
;
337 RelationPreTruncate(rel
);
340 * The code which follows can interact with concurrent checkpoints in two
343 * First, the truncation operation might drop buffers that the checkpoint
344 * otherwise would have flushed. If it does, then it's essential that the
345 * files actually get truncated on disk before the checkpoint record is
346 * written. Otherwise, if reply begins from that checkpoint, the
347 * to-be-truncated blocks might still exist on disk but have older
348 * contents than expected, which can cause replay to fail. It's OK for the
349 * blocks to not exist on disk at all, but not for them to have the wrong
350 * contents. For this reason, we need to set DELAY_CHKPT_COMPLETE while
351 * this code executes.
353 * Second, the call to smgrtruncate() below will in turn call
354 * RegisterSyncRequest(). We need the sync request created by that call to
355 * be processed before the checkpoint completes. CheckPointGuts() will
356 * call ProcessSyncRequests(), but if we register our sync request after
357 * that happens, then the WAL record for the truncation could end up
358 * preceding the checkpoint record, while the actual sync doesn't happen
359 * until the next checkpoint. To prevent that, we need to set
360 * DELAY_CHKPT_START here. That way, if the XLOG_SMGR_TRUNCATE precedes
361 * the redo pointer of a concurrent checkpoint, we're guaranteed that the
362 * corresponding sync request will be processed before the checkpoint
365 Assert((MyProc
->delayChkptFlags
& (DELAY_CHKPT_START
| DELAY_CHKPT_COMPLETE
)) == 0);
366 MyProc
->delayChkptFlags
|= DELAY_CHKPT_START
| DELAY_CHKPT_COMPLETE
;
369 * We WAL-log the truncation before actually truncating, which means
370 * trouble if the truncation fails. If we then crash, the WAL replay
371 * likely isn't going to succeed in the truncation either, and cause a
372 * PANIC. It's tempting to put a critical section here, but that cure
373 * would be worse than the disease. It would turn a usually harmless
374 * failure to truncate, that might spell trouble at WAL replay, into a
377 if (RelationNeedsWAL(rel
))
380 * Make an XLOG entry reporting the file truncation.
383 xl_smgr_truncate xlrec
;
385 xlrec
.blkno
= nblocks
;
386 xlrec
.rlocator
= rel
->rd_locator
;
387 xlrec
.flags
= SMGR_TRUNCATE_ALL
;
390 XLogRegisterData((char *) &xlrec
, sizeof(xlrec
));
392 lsn
= XLogInsert(RM_SMGR_ID
,
393 XLOG_SMGR_TRUNCATE
| XLR_SPECIAL_REL_UPDATE
);
396 * Flush, because otherwise the truncation of the main relation might
397 * hit the disk before the WAL record, and the truncation of the FSM
398 * or visibility map. If we crashed during that window, we'd be left
399 * with a truncated heap, but the FSM or visibility map would still
400 * contain entries for the non-existent heap pages.
407 * This will first remove any buffers from the buffer pool that should no
408 * longer exist after truncation is complete, and then truncate the
409 * corresponding files on disk.
411 smgrtruncate(RelationGetSmgr(rel
), forks
, nforks
, blocks
);
413 /* We've done all the critical work, so checkpoints are OK now. */
414 MyProc
->delayChkptFlags
&= ~(DELAY_CHKPT_START
| DELAY_CHKPT_COMPLETE
);
417 * Update upper-level FSM pages to account for the truncation. This is
418 * important because the just-truncated pages were likely marked as
419 * all-free, and would be preferentially selected.
421 * NB: There's no point in delaying checkpoints until this is done.
422 * Because the FSM is not WAL-logged, we have to be prepared for the
423 * possibility of corruption after a crash anyway.
426 FreeSpaceMapVacuumRange(rel
, nblocks
, InvalidBlockNumber
);
430 * RelationPreTruncate
431 * Perform AM-independent work before a physical truncation.
433 * If an access method's relation_nontransactional_truncate does not call
434 * RelationTruncate(), it must call this before decreasing the table size.
437 RelationPreTruncate(Relation rel
)
439 PendingRelSync
*pending
;
441 if (!pendingSyncHash
)
444 pending
= hash_search(pendingSyncHash
,
445 &(RelationGetSmgr(rel
)->smgr_rlocator
.locator
),
448 pending
->is_truncated
= true;
452 * Copy a fork's data, block by block.
454 * Note that this requires that there is no dirty data in shared buffers. If
455 * it's possible that there are, callers need to flush those using
456 * e.g. FlushRelationBuffers(rel).
458 * Also note that this is frequently called via locutions such as
459 * RelationCopyStorage(RelationGetSmgr(rel), ...);
460 * That's safe only because we perform only smgr and WAL operations here.
461 * If we invoked anything else, a relcache flush could cause our SMgrRelation
462 * argument to become a dangling pointer.
465 RelationCopyStorage(SMgrRelation src
, SMgrRelation dst
,
466 ForkNumber forkNum
, char relpersistence
)
469 bool copying_initfork
;
472 BulkWriteState
*bulkstate
;
475 * The init fork for an unlogged relation in many respects has to be
476 * treated the same as normal relation, changes need to be WAL logged and
477 * it needs to be synced to disk.
479 copying_initfork
= relpersistence
== RELPERSISTENCE_UNLOGGED
&&
480 forkNum
== INIT_FORKNUM
;
483 * We need to log the copied data in WAL iff WAL archiving/streaming is
484 * enabled AND it's a permanent relation. This gives the same answer as
485 * "RelationNeedsWAL(rel) || copying_initfork", because we know the
486 * current operation created new relation storage.
488 use_wal
= XLogIsNeeded() &&
489 (relpersistence
== RELPERSISTENCE_PERMANENT
|| copying_initfork
);
491 bulkstate
= smgr_bulk_start_smgr(dst
, forkNum
, use_wal
);
493 nblocks
= smgrnblocks(src
, forkNum
);
495 for (blkno
= 0; blkno
< nblocks
; blkno
++)
499 /* If we got a cancel signal during the copy of the data, quit */
500 CHECK_FOR_INTERRUPTS();
502 buf
= smgr_bulk_get_buf(bulkstate
);
503 smgrread(src
, forkNum
, blkno
, (Page
) buf
);
505 if (!PageIsVerifiedExtended((Page
) buf
, blkno
,
506 PIV_LOG_WARNING
| PIV_REPORT_STAT
))
509 * For paranoia's sake, capture the file path before invoking the
510 * ereport machinery. This guards against the possibility of a
511 * relcache flush caused by, e.g., an errcontext callback.
512 * (errcontext callbacks shouldn't be risking any such thing, but
513 * people have been known to forget that rule.)
515 char *relpath
= relpathbackend(src
->smgr_rlocator
.locator
,
516 src
->smgr_rlocator
.backend
,
520 (errcode(ERRCODE_DATA_CORRUPTED
),
521 errmsg("invalid page in block %u of relation %s",
526 * Queue the page for WAL-logging and writing out. Unfortunately we
527 * don't know what kind of a page this is, so we have to log the full
528 * page including any unused space.
530 smgr_bulk_write(bulkstate
, blkno
, buf
, false);
532 smgr_bulk_finish(bulkstate
);
536 * RelFileLocatorSkippingWAL
537 * Check if a BM_PERMANENT relfilelocator is using WAL.
539 * Changes to certain relations must not write WAL; see "Skipping WAL for
540 * New RelFileLocator" in src/backend/access/transam/README. Though it is
541 * known from Relation efficiently, this function is intended for the code
542 * paths not having access to Relation.
545 RelFileLocatorSkippingWAL(RelFileLocator rlocator
)
547 if (!pendingSyncHash
||
548 hash_search(pendingSyncHash
, &rlocator
, HASH_FIND
, NULL
) == NULL
)
555 * EstimatePendingSyncsSpace
556 * Estimate space needed to pass syncs to parallel workers.
559 EstimatePendingSyncsSpace(void)
563 entries
= pendingSyncHash
? hash_get_num_entries(pendingSyncHash
) : 0;
564 return mul_size(1 + entries
, sizeof(RelFileLocator
));
568 * SerializePendingSyncs
569 * Serialize syncs for parallel workers.
572 SerializePendingSyncs(Size maxSize
, char *startAddress
)
576 HASH_SEQ_STATUS scan
;
577 PendingRelSync
*sync
;
578 PendingRelDelete
*delete;
580 RelFileLocator
*dest
= (RelFileLocator
*) startAddress
;
582 if (!pendingSyncHash
)
585 /* Create temporary hash to collect active relfilelocators */
586 ctl
.keysize
= sizeof(RelFileLocator
);
587 ctl
.entrysize
= sizeof(RelFileLocator
);
588 ctl
.hcxt
= CurrentMemoryContext
;
589 tmphash
= hash_create("tmp relfilelocators",
590 hash_get_num_entries(pendingSyncHash
), &ctl
,
591 HASH_ELEM
| HASH_BLOBS
| HASH_CONTEXT
);
593 /* collect all rlocator from pending syncs */
594 hash_seq_init(&scan
, pendingSyncHash
);
595 while ((sync
= (PendingRelSync
*) hash_seq_search(&scan
)))
596 (void) hash_search(tmphash
, &sync
->rlocator
, HASH_ENTER
, NULL
);
598 /* remove deleted rnodes */
599 for (delete = pendingDeletes
; delete != NULL
; delete = delete->next
)
600 if (delete->atCommit
)
601 (void) hash_search(tmphash
, &delete->rlocator
,
604 hash_seq_init(&scan
, tmphash
);
605 while ((src
= (RelFileLocator
*) hash_seq_search(&scan
)))
608 hash_destroy(tmphash
);
611 MemSet(dest
, 0, sizeof(RelFileLocator
));
615 * RestorePendingSyncs
616 * Restore syncs within a parallel worker.
618 * RelationNeedsWAL() and RelFileLocatorSkippingWAL() must offer the correct
619 * answer to parallel workers. Only smgrDoPendingSyncs() reads the
620 * is_truncated field, at end of transaction. Hence, don't restore it.
623 RestorePendingSyncs(char *startAddress
)
625 RelFileLocator
*rlocator
;
627 Assert(pendingSyncHash
== NULL
);
628 for (rlocator
= (RelFileLocator
*) startAddress
; rlocator
->relNumber
!= 0;
630 AddPendingSync(rlocator
);
634 * smgrDoPendingDeletes() -- Take care of relation deletes at end of xact.
636 * This also runs when aborting a subxact; we want to clean up a failed
637 * subxact immediately.
639 * Note: It's possible that we're being asked to remove a relation that has
640 * no physical storage in any fork. In particular, it's possible that we're
641 * cleaning up an old temporary relation for which RemovePgTempFiles has
642 * already recovered the physical storage.
645 smgrDoPendingDeletes(bool isCommit
)
647 int nestLevel
= GetCurrentTransactionNestLevel();
648 PendingRelDelete
*pending
;
649 PendingRelDelete
*prev
;
650 PendingRelDelete
*next
;
653 SMgrRelation
*srels
= NULL
;
656 for (pending
= pendingDeletes
; pending
!= NULL
; pending
= next
)
658 next
= pending
->next
;
659 if (pending
->nestLevel
< nestLevel
)
661 /* outer-level entries should not be processed yet */
666 /* unlink list entry first, so we don't retry on failure */
670 pendingDeletes
= next
;
671 /* do deletion if called for */
672 if (pending
->atCommit
== isCommit
)
676 srel
= smgropen(pending
->rlocator
, pending
->procNumber
);
678 /* allocate the initial array, or extend it, if needed */
682 srels
= palloc(sizeof(SMgrRelation
) * maxrels
);
684 else if (maxrels
<= nrels
)
687 srels
= repalloc(srels
, sizeof(SMgrRelation
) * maxrels
);
690 srels
[nrels
++] = srel
;
692 /* must explicitly free the list entry */
694 /* prev does not change */
700 smgrdounlinkall(srels
, nrels
, false);
702 for (int i
= 0; i
< nrels
; i
++)
710 * smgrDoPendingSyncs() -- Take care of relation syncs at end of xact.
713 smgrDoPendingSyncs(bool isCommit
, bool isParallelWorker
)
715 PendingRelDelete
*pending
;
718 SMgrRelation
*srels
= NULL
;
719 HASH_SEQ_STATUS scan
;
720 PendingRelSync
*pendingsync
;
722 Assert(GetCurrentTransactionNestLevel() == 1);
724 if (!pendingSyncHash
)
725 return; /* no relation needs sync */
727 /* Abort -- just throw away all pending syncs */
730 pendingSyncHash
= NULL
;
734 AssertPendingSyncs_RelationCache();
736 /* Parallel worker -- just throw away all pending syncs */
737 if (isParallelWorker
)
739 pendingSyncHash
= NULL
;
743 /* Skip syncing nodes that smgrDoPendingDeletes() will delete. */
744 for (pending
= pendingDeletes
; pending
!= NULL
; pending
= pending
->next
)
745 if (pending
->atCommit
)
746 (void) hash_search(pendingSyncHash
, &pending
->rlocator
,
749 hash_seq_init(&scan
, pendingSyncHash
);
750 while ((pendingsync
= (PendingRelSync
*) hash_seq_search(&scan
)))
753 BlockNumber nblocks
[MAX_FORKNUM
+ 1];
754 BlockNumber total_blocks
= 0;
757 srel
= smgropen(pendingsync
->rlocator
, INVALID_PROC_NUMBER
);
760 * We emit newpage WAL records for smaller relations.
762 * Small WAL records have a chance to be flushed along with other
763 * backends' WAL records. We emit WAL records instead of syncing for
764 * files that are smaller than a certain threshold, expecting faster
765 * commit. The threshold is defined by the GUC wal_skip_threshold.
767 if (!pendingsync
->is_truncated
)
769 for (fork
= 0; fork
<= MAX_FORKNUM
; fork
++)
771 if (smgrexists(srel
, fork
))
773 BlockNumber n
= smgrnblocks(srel
, fork
);
775 /* we shouldn't come here for unlogged relations */
776 Assert(fork
!= INIT_FORKNUM
);
781 nblocks
[fork
] = InvalidBlockNumber
;
786 * Sync file or emit WAL records for its contents.
788 * Although we emit WAL record if the file is small enough, do file
789 * sync regardless of the size if the file has experienced a
790 * truncation. It is because the file would be followed by trailing
791 * garbage blocks after a crash recovery if, while a past longer file
792 * had been flushed out, we omitted syncing-out of the file and
793 * emitted WAL instead. You might think that we could choose WAL if
794 * the current main fork is longer than ever, but there's a case where
795 * main fork is longer than ever but FSM fork gets shorter.
797 if (pendingsync
->is_truncated
||
798 total_blocks
* BLCKSZ
/ 1024 >= wal_skip_threshold
)
800 /* allocate the initial array, or extend it, if needed */
804 srels
= palloc(sizeof(SMgrRelation
) * maxrels
);
806 else if (maxrels
<= nrels
)
809 srels
= repalloc(srels
, sizeof(SMgrRelation
) * maxrels
);
812 srels
[nrels
++] = srel
;
816 /* Emit WAL records for all blocks. The file is small enough. */
817 for (fork
= 0; fork
<= MAX_FORKNUM
; fork
++)
819 int n
= nblocks
[fork
];
822 if (!BlockNumberIsValid(n
))
826 * Emit WAL for the whole file. Unfortunately we don't know
827 * what kind of a page this is, so we have to log the full
828 * page including any unused space. ReadBufferExtended()
829 * counts some pgstat events; unfortunately, we discard them.
831 rel
= CreateFakeRelcacheEntry(srel
->smgr_rlocator
.locator
);
832 log_newpage_range(rel
, fork
, 0, n
, false);
833 FreeFakeRelcacheEntry(rel
);
838 pendingSyncHash
= NULL
;
842 smgrdosyncall(srels
, nrels
);
848 * smgrGetPendingDeletes() -- Get a list of non-temp relations to be deleted.
850 * The return value is the number of relations scheduled for termination.
851 * *ptr is set to point to a freshly-palloc'd array of RelFileLocators.
852 * If there are no relations to be deleted, *ptr is set to NULL.
854 * Only non-temporary relations are included in the returned list. This is OK
855 * because the list is used only in contexts where temporary relations don't
856 * matter: we're either writing to the two-phase state file (and transactions
857 * that have touched temp tables can't be prepared) or we're writing to xlog
858 * (and all temporary files will be zapped if we restart anyway, so no need
859 * for redo to do it also).
861 * Note that the list does not include anything scheduled for termination
862 * by upper-level transactions.
865 smgrGetPendingDeletes(bool forCommit
, RelFileLocator
**ptr
)
867 int nestLevel
= GetCurrentTransactionNestLevel();
869 RelFileLocator
*rptr
;
870 PendingRelDelete
*pending
;
873 for (pending
= pendingDeletes
; pending
!= NULL
; pending
= pending
->next
)
875 if (pending
->nestLevel
>= nestLevel
&& pending
->atCommit
== forCommit
876 && pending
->procNumber
== INVALID_PROC_NUMBER
)
884 rptr
= (RelFileLocator
*) palloc(nrels
* sizeof(RelFileLocator
));
886 for (pending
= pendingDeletes
; pending
!= NULL
; pending
= pending
->next
)
888 if (pending
->nestLevel
>= nestLevel
&& pending
->atCommit
== forCommit
889 && pending
->procNumber
== INVALID_PROC_NUMBER
)
891 *rptr
= pending
->rlocator
;
899 * PostPrepare_smgr -- Clean up after a successful PREPARE
901 * What we have to do here is throw away the in-memory state about pending
902 * relation deletes. It's all been recorded in the 2PC state file and
903 * it's no longer smgr's job to worry about it.
906 PostPrepare_smgr(void)
908 PendingRelDelete
*pending
;
909 PendingRelDelete
*next
;
911 for (pending
= pendingDeletes
; pending
!= NULL
; pending
= next
)
913 next
= pending
->next
;
914 pendingDeletes
= next
;
915 /* must explicitly free the list entry */
922 * AtSubCommit_smgr() --- Take care of subtransaction commit.
924 * Reassign all items in the pending-deletes list to the parent transaction.
927 AtSubCommit_smgr(void)
929 int nestLevel
= GetCurrentTransactionNestLevel();
930 PendingRelDelete
*pending
;
932 for (pending
= pendingDeletes
; pending
!= NULL
; pending
= pending
->next
)
934 if (pending
->nestLevel
>= nestLevel
)
935 pending
->nestLevel
= nestLevel
- 1;
940 * AtSubAbort_smgr() --- Take care of subtransaction abort.
942 * Delete created relations and forget about deleted relations.
943 * We can execute these operations immediately because we know this
944 * subtransaction will not commit.
947 AtSubAbort_smgr(void)
949 smgrDoPendingDeletes(false);
953 smgr_redo(XLogReaderState
*record
)
955 XLogRecPtr lsn
= record
->EndRecPtr
;
956 uint8 info
= XLogRecGetInfo(record
) & ~XLR_INFO_MASK
;
958 /* Backup blocks are not used in smgr records */
959 Assert(!XLogRecHasAnyBlockRefs(record
));
961 if (info
== XLOG_SMGR_CREATE
)
963 xl_smgr_create
*xlrec
= (xl_smgr_create
*) XLogRecGetData(record
);
966 reln
= smgropen(xlrec
->rlocator
, INVALID_PROC_NUMBER
);
967 smgrcreate(reln
, xlrec
->forkNum
, true);
969 else if (info
== XLOG_SMGR_TRUNCATE
)
971 xl_smgr_truncate
*xlrec
= (xl_smgr_truncate
*) XLogRecGetData(record
);
974 ForkNumber forks
[MAX_FORKNUM
];
975 BlockNumber blocks
[MAX_FORKNUM
];
977 bool need_fsm_vacuum
= false;
979 reln
= smgropen(xlrec
->rlocator
, INVALID_PROC_NUMBER
);
982 * Forcibly create relation if it doesn't exist (which suggests that
983 * it was dropped somewhere later in the WAL sequence). As in
984 * XLogReadBufferForRedo, we prefer to recreate the rel and replay the
985 * log as best we can until the drop is seen.
987 smgrcreate(reln
, MAIN_FORKNUM
, true);
990 * Before we perform the truncation, update minimum recovery point to
991 * cover this WAL record. Once the relation is truncated, there's no
992 * going back. The buffer manager enforces the WAL-first rule for
993 * normal updates to relation files, so that the minimum recovery
994 * point is always updated before the corresponding change in the data
995 * file is flushed to disk. We have to do the same manually here.
997 * Doing this before the truncation means that if the truncation fails
998 * for some reason, you cannot start up the system even after restart,
999 * until you fix the underlying situation so that the truncation will
1000 * succeed. Alternatively, we could update the minimum recovery point
1001 * after truncation, but that would leave a small window where the
1002 * WAL-first rule could be violated.
1006 /* Prepare for truncation of MAIN fork */
1007 if ((xlrec
->flags
& SMGR_TRUNCATE_HEAP
) != 0)
1009 forks
[nforks
] = MAIN_FORKNUM
;
1010 blocks
[nforks
] = xlrec
->blkno
;
1013 /* Also tell xlogutils.c about it */
1014 XLogTruncateRelation(xlrec
->rlocator
, MAIN_FORKNUM
, xlrec
->blkno
);
1017 /* Prepare for truncation of FSM and VM too */
1018 rel
= CreateFakeRelcacheEntry(xlrec
->rlocator
);
1020 if ((xlrec
->flags
& SMGR_TRUNCATE_FSM
) != 0 &&
1021 smgrexists(reln
, FSM_FORKNUM
))
1023 blocks
[nforks
] = FreeSpaceMapPrepareTruncateRel(rel
, xlrec
->blkno
);
1024 if (BlockNumberIsValid(blocks
[nforks
]))
1026 forks
[nforks
] = FSM_FORKNUM
;
1028 need_fsm_vacuum
= true;
1031 if ((xlrec
->flags
& SMGR_TRUNCATE_VM
) != 0 &&
1032 smgrexists(reln
, VISIBILITYMAP_FORKNUM
))
1034 blocks
[nforks
] = visibilitymap_prepare_truncate(rel
, xlrec
->blkno
);
1035 if (BlockNumberIsValid(blocks
[nforks
]))
1037 forks
[nforks
] = VISIBILITYMAP_FORKNUM
;
1042 /* Do the real work to truncate relation forks */
1044 smgrtruncate(reln
, forks
, nforks
, blocks
);
1047 * Update upper-level FSM pages to account for the truncation. This is
1048 * important because the just-truncated pages were likely marked as
1049 * all-free, and would be preferentially selected.
1051 if (need_fsm_vacuum
)
1052 FreeSpaceMapVacuumRange(rel
, xlrec
->blkno
,
1053 InvalidBlockNumber
);
1055 FreeFakeRelcacheEntry(rel
);
1058 elog(PANIC
, "smgr_redo: unknown op code %u", info
);