|
| 1 | +/*------------------------------------------------------------------------- |
| 2 | + * |
| 3 | + * sequence_page.h |
| 4 | + * Helper macros for page manipulations with sequence access methods. |
| 5 | + * |
| 6 | + * These macros are useful for sequence access methods that hold their data |
| 7 | + * on a single page, like the in-core "local" method. |
| 8 | + * |
| 9 | + * Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group |
| 10 | + * Portions Copyright (c) 1994, Regents of the University of California |
| 11 | + * |
| 12 | + * src/include/access/sequence_page.h |
| 13 | + * |
| 14 | + *------------------------------------------------------------------------- |
| 15 | + */ |
| 16 | +#ifndef SEQUENCE_PAGE_H |
| 17 | +#define SEQUENCE_PAGE_H |
| 18 | + |
| 19 | +/* |
| 20 | + * Initialize the first page of a sequence relation. This embeds the |
| 21 | + * handling for the special magic number, and enforces a frozen XID, |
| 22 | + * for VACUUM. |
| 23 | + * |
| 24 | + * Since VACUUM does not process sequences, we have to force the tuple to |
| 25 | + * have xmin = FrozenTransactionId now. Otherwise it would become |
| 26 | + * invisible to SELECTs after 2G transactions. It is okay to do this |
| 27 | + * because if the current transaction aborts, no other xact will ever |
| 28 | + * examine the sequence tuple anyway. |
| 29 | + * |
| 30 | + * "seqam_special" is the structure used for the special area of a |
| 31 | + * sequence access method. |
| 32 | + * "seqam_magic_value" is a value stored in the special area, used for |
| 33 | + * the validation of the page. |
| 34 | + */ |
| 35 | +#define SEQUENCE_PAGE_INIT(seqam_special, seqam_magic_value) \ |
| 36 | +do { \ |
| 37 | + buf = ExtendBufferedRel(BMR_REL(rel), forkNum, NULL, \ |
| 38 | + EB_LOCK_FIRST | EB_SKIP_EXTENSION_LOCK); \ |
| 39 | + Assert(BufferGetBlockNumber(buf) == 0); \ |
| 40 | + \ |
| 41 | + page = BufferGetPage(buf); \ |
| 42 | + \ |
| 43 | + PageInit(page, BufferGetPageSize(buf), sizeof(seqam_special)); \ |
| 44 | + sm = (seqam_special *) PageGetSpecialPointer(page); \ |
| 45 | + sm->magic = seqam_magic_value; \ |
| 46 | + \ |
| 47 | + /* Now insert sequence tuple */ \ |
| 48 | + HeapTupleHeaderSetXmin(tuple->t_data, FrozenTransactionId); \ |
| 49 | + HeapTupleHeaderSetXminFrozen(tuple->t_data); \ |
| 50 | + HeapTupleHeaderSetCmin(tuple->t_data, FirstCommandId); \ |
| 51 | + HeapTupleHeaderSetXmax(tuple->t_data, InvalidTransactionId); \ |
| 52 | + tuple->t_data->t_infomask |= HEAP_XMAX_INVALID; \ |
| 53 | + ItemPointerSet(&tuple->t_data->t_ctid, 0, FirstOffsetNumber); \ |
| 54 | +} while(0) |
| 55 | + |
| 56 | + |
| 57 | +/* |
| 58 | + * Read the first page of a sequence relation, previously initialized with |
| 59 | + * SEQUENCE_PAGE_INIT. |
| 60 | + * |
| 61 | + * "Form_seqam_data" is the data retrieved from the page. |
| 62 | + * "seqam_special" is the structure used for the special area of a |
| 63 | + * sequence access method. |
| 64 | + * "seqam_magic_value" is a value stored in the special area, used for |
| 65 | + * the validation of the page. |
| 66 | + */ |
| 67 | +#define SEQUENCE_PAGE_READ(Form_seqam_data, seqam_special, seqam_magic_value) \ |
| 68 | +do { \ |
| 69 | + Page page; \ |
| 70 | + ItemId lp; \ |
| 71 | + \ |
| 72 | + *buf = ReadBuffer(rel, 0); \ |
| 73 | + LockBuffer(*buf, BUFFER_LOCK_EXCLUSIVE); \ |
| 74 | + \ |
| 75 | + page = BufferGetPage(*buf); \ |
| 76 | + sm = (seqam_special *) PageGetSpecialPointer(page); \ |
| 77 | + \ |
| 78 | + if (sm->magic != seqam_magic_value) \ |
| 79 | + elog(ERROR, "bad magic number in sequence \"%s\": %08X", \ |
| 80 | + RelationGetRelationName(rel), sm->magic); \ |
| 81 | + \ |
| 82 | + lp = PageGetItemId(page, FirstOffsetNumber); \ |
| 83 | + Assert(ItemIdIsNormal(lp)); \ |
| 84 | + \ |
| 85 | + /* \ |
| 86 | + * Note we currently only bother to set these two fields of \ |
| 87 | + * *seqdatatuple. \ |
| 88 | + */ \ |
| 89 | + seqdatatuple->t_data = (HeapTupleHeader) PageGetItem(page, lp); \ |
| 90 | + seqdatatuple->t_len = ItemIdGetLength(lp); \ |
| 91 | + \ |
| 92 | + seq = (Form_seqam_data) GETSTRUCT(seqdatatuple); \ |
| 93 | +} while(0) |
| 94 | + |
| 95 | +#endif /* SEQUENCE_PAGE_H */ |
0 commit comments