Skip to content

Commit a87987c

Browse files
committed
Move WAL sequence code into its own file
This split exists for most of the other RMGRs, and makes cleaner the separation between the WAL code, the redo code and the record description code (already in its own file) when it comes to the sequence RMGR. The redo and masking routines are moved to a new file, sequence_xlog.c. All the RMGR routines are now located in a new header, sequence_xlog.h. This separation is useful for a different patch related to sequences that I have been working on, where it makes a refactoring of sequence.c easier if its RMGR routines and its core routines are split. Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> Reviewed-by: Kirill Reshke <reshkekirill@gmail.com> Discussion: https://postgr.es/m/aSfTxIWjiXkTKh1E@paquier.xyz
1 parent d03668e commit a87987c

File tree

9 files changed

+132
-95
lines changed

9 files changed

+132
-95
lines changed

src/backend/access/rmgrdesc/seqdesc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*/
1515
#include "postgres.h"
1616

17-
#include "commands/sequence.h"
17+
#include "commands/sequence_xlog.h"
1818

1919

2020
void

src/backend/access/transam/rmgr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "access/xact.h"
3434
#include "catalog/storage_xlog.h"
3535
#include "commands/dbcommands_xlog.h"
36-
#include "commands/sequence.h"
36+
#include "commands/sequence_xlog.h"
3737
#include "commands/tablespace.h"
3838
#include "replication/decode.h"
3939
#include "replication/message.h"

src/backend/commands/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ OBJS = \
5353
schemacmds.o \
5454
seclabel.o \
5555
sequence.o \
56+
sequence_xlog.o \
5657
statscmds.o \
5758
subscriptioncmds.o \
5859
tablecmds.o \

src/backend/commands/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ backend_sources += files(
4141
'schemacmds.c',
4242
'seclabel.c',
4343
'sequence.c',
44+
'sequence_xlog.c',
4445
'statscmds.c',
4546
'subscriptioncmds.c',
4647
'tablecmds.c',

src/backend/commands/sequence.c

Lines changed: 2 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,14 @@
1414
*/
1515
#include "postgres.h"
1616

17-
#include "access/bufmask.h"
1817
#include "access/htup_details.h"
1918
#include "access/multixact.h"
2019
#include "access/relation.h"
2120
#include "access/sequence.h"
2221
#include "access/table.h"
2322
#include "access/transam.h"
2423
#include "access/xact.h"
25-
#include "access/xlog.h"
2624
#include "access/xloginsert.h"
27-
#include "access/xlogutils.h"
2825
#include "catalog/dependency.h"
2926
#include "catalog/indexing.h"
3027
#include "catalog/namespace.h"
@@ -34,11 +31,13 @@
3431
#include "catalog/storage_xlog.h"
3532
#include "commands/defrem.h"
3633
#include "commands/sequence.h"
34+
#include "commands/sequence_xlog.h"
3735
#include "commands/tablecmds.h"
3836
#include "funcapi.h"
3937
#include "miscadmin.h"
4038
#include "nodes/makefuncs.h"
4139
#include "parser/parse_type.h"
40+
#include "storage/bufmgr.h"
4241
#include "storage/lmgr.h"
4342
#include "storage/proc.h"
4443
#include "storage/smgr.h"
@@ -58,16 +57,6 @@
5857
*/
5958
#define SEQ_LOG_VALS 32
6059

61-
/*
62-
* The "special area" of a sequence's buffer page looks like this.
63-
*/
64-
#define SEQ_MAGIC 0x1717
65-
66-
typedef struct sequence_magic
67-
{
68-
uint32 magic;
69-
} sequence_magic;
70-
7160
/*
7261
* We store a SeqTable item for every sequence we have touched in the current
7362
* session. This is needed to hold onto nextval/currval state. (We can't
@@ -1907,56 +1896,6 @@ pg_sequence_last_value(PG_FUNCTION_ARGS)
19071896
PG_RETURN_NULL();
19081897
}
19091898

1910-
1911-
void
1912-
seq_redo(XLogReaderState *record)
1913-
{
1914-
XLogRecPtr lsn = record->EndRecPtr;
1915-
uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
1916-
Buffer buffer;
1917-
Page page;
1918-
Page localpage;
1919-
char *item;
1920-
Size itemsz;
1921-
xl_seq_rec *xlrec = (xl_seq_rec *) XLogRecGetData(record);
1922-
sequence_magic *sm;
1923-
1924-
if (info != XLOG_SEQ_LOG)
1925-
elog(PANIC, "seq_redo: unknown op code %u", info);
1926-
1927-
buffer = XLogInitBufferForRedo(record, 0);
1928-
page = BufferGetPage(buffer);
1929-
1930-
/*
1931-
* We always reinit the page. However, since this WAL record type is also
1932-
* used for updating sequences, it's possible that a hot-standby backend
1933-
* is examining the page concurrently; so we mustn't transiently trash the
1934-
* buffer. The solution is to build the correct new page contents in
1935-
* local workspace and then memcpy into the buffer. Then only bytes that
1936-
* are supposed to change will change, even transiently. We must palloc
1937-
* the local page for alignment reasons.
1938-
*/
1939-
localpage = (Page) palloc(BufferGetPageSize(buffer));
1940-
1941-
PageInit(localpage, BufferGetPageSize(buffer), sizeof(sequence_magic));
1942-
sm = (sequence_magic *) PageGetSpecialPointer(localpage);
1943-
sm->magic = SEQ_MAGIC;
1944-
1945-
item = (char *) xlrec + sizeof(xl_seq_rec);
1946-
itemsz = XLogRecGetDataLen(record) - sizeof(xl_seq_rec);
1947-
1948-
if (PageAddItem(localpage, item, itemsz, FirstOffsetNumber, false, false) == InvalidOffsetNumber)
1949-
elog(PANIC, "seq_redo: failed to add item to page");
1950-
1951-
PageSetLSN(localpage, lsn);
1952-
1953-
memcpy(page, localpage, BufferGetPageSize(buffer));
1954-
MarkBufferDirty(buffer);
1955-
UnlockReleaseBuffer(buffer);
1956-
1957-
pfree(localpage);
1958-
}
1959-
19601899
/*
19611900
* Flush cached sequence information.
19621901
*/
@@ -1971,14 +1910,3 @@ ResetSequenceCaches(void)
19711910

19721911
last_used_seq = NULL;
19731912
}
1974-
1975-
/*
1976-
* Mask a Sequence page before performing consistency checks on it.
1977-
*/
1978-
void
1979-
seq_mask(char *page, BlockNumber blkno)
1980-
{
1981-
mask_page_lsn_and_checksum(page);
1982-
1983-
mask_unused_space(page);
1984-
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* sequence.c
4+
* RMGR WAL routines for sequences.
5+
*
6+
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
*
10+
* IDENTIFICATION
11+
* src/backend/commands/sequence_xlog.c
12+
*
13+
*-------------------------------------------------------------------------
14+
*/
15+
#include "postgres.h"
16+
17+
#include "access/bufmask.h"
18+
#include "access/xlogutils.h"
19+
#include "commands/sequence_xlog.h"
20+
#include "storage/bufmgr.h"
21+
22+
void
23+
seq_redo(XLogReaderState *record)
24+
{
25+
XLogRecPtr lsn = record->EndRecPtr;
26+
uint8 info = XLogRecGetInfo(record) & ~XLR_INFO_MASK;
27+
Buffer buffer;
28+
Page page;
29+
Page localpage;
30+
char *item;
31+
Size itemsz;
32+
xl_seq_rec *xlrec = (xl_seq_rec *) XLogRecGetData(record);
33+
sequence_magic *sm;
34+
35+
if (info != XLOG_SEQ_LOG)
36+
elog(PANIC, "seq_redo: unknown op code %u", info);
37+
38+
buffer = XLogInitBufferForRedo(record, 0);
39+
page = BufferGetPage(buffer);
40+
41+
/*
42+
* We always reinit the page. However, since this WAL record type is also
43+
* used for updating sequences, it's possible that a hot-standby backend
44+
* is examining the page concurrently; so we mustn't transiently trash the
45+
* buffer. The solution is to build the correct new page contents in
46+
* local workspace and then memcpy into the buffer. Then only bytes that
47+
* are supposed to change will change, even transiently. We must palloc
48+
* the local page for alignment reasons.
49+
*/
50+
localpage = (Page) palloc(BufferGetPageSize(buffer));
51+
52+
PageInit(localpage, BufferGetPageSize(buffer), sizeof(sequence_magic));
53+
sm = (sequence_magic *) PageGetSpecialPointer(localpage);
54+
sm->magic = SEQ_MAGIC;
55+
56+
item = (char *) xlrec + sizeof(xl_seq_rec);
57+
itemsz = XLogRecGetDataLen(record) - sizeof(xl_seq_rec);
58+
59+
if (PageAddItem(localpage, item, itemsz, FirstOffsetNumber, false, false) == InvalidOffsetNumber)
60+
elog(PANIC, "seq_redo: failed to add item to page");
61+
62+
PageSetLSN(localpage, lsn);
63+
64+
memcpy(page, localpage, BufferGetPageSize(buffer));
65+
MarkBufferDirty(buffer);
66+
UnlockReleaseBuffer(buffer);
67+
68+
pfree(localpage);
69+
}
70+
71+
/*
72+
* Mask a Sequence page before performing consistency checks on it.
73+
*/
74+
void
75+
seq_mask(char *page, BlockNumber blkno)
76+
{
77+
mask_page_lsn_and_checksum(page);
78+
79+
mask_unused_space(page);
80+
}

src/bin/pg_waldump/rmgrdesc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#include "access/xlog_internal.h"
2525
#include "catalog/storage_xlog.h"
2626
#include "commands/dbcommands_xlog.h"
27-
#include "commands/sequence.h"
27+
#include "commands/sequence_xlog.h"
2828
#include "commands/tablespace.h"
2929
#include "replication/message.h"
3030
#include "replication/origin.h"

src/include/commands/sequence.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,10 @@
1313
#ifndef SEQUENCE_H
1414
#define SEQUENCE_H
1515

16-
#include "access/xlogreader.h"
1716
#include "catalog/objectaddress.h"
1817
#include "fmgr.h"
19-
#include "lib/stringinfo.h"
2018
#include "nodes/parsenodes.h"
2119
#include "parser/parse_node.h"
22-
#include "storage/relfilelocator.h"
23-
2420

2521
typedef struct FormData_pg_sequence_data
2622
{
@@ -42,15 +38,6 @@ typedef FormData_pg_sequence_data *Form_pg_sequence_data;
4238
#define SEQ_COL_FIRSTCOL SEQ_COL_LASTVAL
4339
#define SEQ_COL_LASTCOL SEQ_COL_CALLED
4440

45-
/* XLOG stuff */
46-
#define XLOG_SEQ_LOG 0x00
47-
48-
typedef struct xl_seq_rec
49-
{
50-
RelFileLocator locator;
51-
/* SEQUENCE TUPLE DATA FOLLOWS AT THE END */
52-
} xl_seq_rec;
53-
5441
extern int64 nextval_internal(Oid relid, bool check_permissions);
5542
extern Datum nextval(PG_FUNCTION_ARGS);
5643
extern List *sequence_options(Oid relid);
@@ -63,9 +50,4 @@ extern void ResetSequence(Oid seq_relid);
6350
extern void SetSequence(Oid relid, int64 next, bool is_called);
6451
extern void ResetSequenceCaches(void);
6552

66-
extern void seq_redo(XLogReaderState *record);
67-
extern void seq_desc(StringInfo buf, XLogReaderState *record);
68-
extern const char *seq_identify(uint8 info);
69-
extern void seq_mask(char *page, BlockNumber blkno);
70-
7153
#endif /* SEQUENCE_H */
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*-------------------------------------------------------------------------
2+
*
3+
* sequence_xlog.h
4+
* Sequence WAL definitions.
5+
*
6+
* Portions Copyright (c) 1996-2025, PostgreSQL Global Development Group
7+
* Portions Copyright (c) 1994, Regents of the University of California
8+
*
9+
* src/include/commands/sequence_xlog.h
10+
*
11+
*-------------------------------------------------------------------------
12+
*/
13+
14+
#ifndef SEQUENCE_XLOG_H
15+
#define SEQUENCE_XLOG_H
16+
17+
#include "access/xlogreader.h"
18+
#include "lib/stringinfo.h"
19+
20+
/* Record identifier */
21+
#define XLOG_SEQ_LOG 0x00
22+
23+
/*
24+
* The "special area" of a sequence's buffer page looks like this.
25+
*/
26+
#define SEQ_MAGIC 0x1717
27+
28+
typedef struct sequence_magic
29+
{
30+
uint32 magic;
31+
} sequence_magic;
32+
33+
/* Sequence WAL record */
34+
typedef struct xl_seq_rec
35+
{
36+
RelFileLocator locator;
37+
/* SEQUENCE TUPLE DATA FOLLOWS AT THE END */
38+
} xl_seq_rec;
39+
40+
extern void seq_redo(XLogReaderState *record);
41+
extern void seq_desc(StringInfo buf, XLogReaderState *record);
42+
extern const char *seq_identify(uint8 info);
43+
extern void seq_mask(char *page, BlockNumber blkno);
44+
45+
#endif /* SEQUENCE_XLOG_H */

0 commit comments

Comments
 (0)