On 04.09.2024 11:09, Kyotaro Horiguchi wrote:
Instead, I'd like to propose separating the file and path-related definitions from xlog_internal.h, as shown in the attached first patch. This change would allow some modules to include files without unnecessary details.The second file is your patch, adjusted based on the first patch. I’d appreciate hearing from others on whether they find the first patch worthwhile. If it’s not considered worthwhile, then I believe having postmaster include xlog_internal.h would be the best approach.
I really liked the idea of extracting only the necessary and logically complete part from xlog_internal.h. But now the presence of macros related to the segment sizes in the xlogfilepaths.h seems does not correspond to its name. So i suggest further extract size-related definition and macros from xlogfilepaths.h to xlogfilesize.h. Here is a patch that tries to do this based on the your first patch. Would be glad to hear your opinion. With the best wishes, -- Anton A. Melnikov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company
From 4b6a12f6eaac98ee22dd63d64431968357905d77 Mon Sep 17 00:00:00 2001 From: "Anton A. Melnikov" <a.melni...@postgrespro.ru> Date: Sat, 7 Sep 2024 10:18:51 +0300 Subject: [PATCH] Extract size-related macros from xlogfilepaths.h to xlogfilesize.h --- src/include/access/xlogfilepaths.h | 65 +---------------------- src/include/access/xlogfilesize.h | 83 ++++++++++++++++++++++++++++++ 2 files changed, 85 insertions(+), 63 deletions(-) create mode 100644 src/include/access/xlogfilesize.h diff --git a/src/include/access/xlogfilepaths.h b/src/include/access/xlogfilepaths.h index cdde2ccae4..ed28cf975e 100644 --- a/src/include/access/xlogfilepaths.h +++ b/src/include/access/xlogfilepaths.h @@ -12,6 +12,7 @@ #define XLOG_FILEPATHS_H #include "access/xlogdefs.h" +#include "access/xlogfilesize.h" /* * The XLog directory and files (relative to $PGDATA) @@ -34,75 +35,13 @@ /* files to signal promotion to primary */ #define PROMOTE_SIGNAL_FILE "promote" -/* wal_segment_size can range from 1MB to 1GB */ -#define WalSegMinSize 1024 * 1024 -#define WalSegMaxSize 1024 * 1024 * 1024 -/* default number of min and max wal segments */ -#define DEFAULT_MIN_WAL_SEGS 5 -#define DEFAULT_MAX_WAL_SEGS 64 - - -/* - * These macros encapsulate knowledge about the exact layout of XLog file - * names, timeline history file names, and archive-status file names. - */ +/* Maximum length of XLog file name including possible suffix */ #define MAXFNAMELEN 64 /* Length of XLog file name */ #define XLOG_FNAME_LEN 24 -/* check that the given size is a valid wal_segment_size */ -#define IsPowerOf2(x) (x > 0 && ((x) & ((x)-1)) == 0) -#define IsValidWalSegSize(size) \ - (IsPowerOf2(size) && \ - ((size) >= WalSegMinSize && (size) <= WalSegMaxSize)) - -/* Number of segments in a logical XLOG file */ -#define XLogSegmentsPerXLogId(wal_segsz_bytes) \ - (UINT64CONST(0x100000000) / (wal_segsz_bytes)) - -/* - * Compute an XLogRecPtr from a segment number and offset. - */ -#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest) \ - (dest) = (segno) * (wal_segsz_bytes) + (offset) -/* - * Compute a segment number from an XLogRecPtr. - * - * For XLByteToSeg, do the computation at face value. For XLByteToPrevSeg, - * a boundary byte is taken to be in the previous segment. This is suitable - * for deciding which segment to write given a pointer to a record end, - * for example. - */ -#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \ - logSegNo = (xlrp) / (wal_segsz_bytes) - -#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \ - logSegNo = ((xlrp) - 1) / (wal_segsz_bytes) - -/* Compute the in-segment offset from an XLogRecPtr. */ -#define XLogSegmentOffset(xlogptr, wal_segsz_bytes) \ - ((xlogptr) & ((wal_segsz_bytes) - 1)) - -/* - * Convert values of GUCs measured in megabytes to equiv. segment count. - * Rounds down. - */ -#define XLogMBVarToSegs(mbvar, wal_segsz_bytes) \ - ((mbvar) / ((wal_segsz_bytes) / (1024 * 1024))) - -/* - * Is an XLogRecPtr within a particular XLOG segment? - * - * For XLByteInSeg, do the computation at face value. For XLByteInPrevSeg, - * a boundary byte is taken to be in the previous segment. - */ -#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes) \ - (((xlrp) / (wal_segsz_bytes)) == (logSegNo)) - -#define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \ - ((((xlrp) - 1) / (wal_segsz_bytes)) == (logSegNo)) /* * XLOG file name handling functions diff --git a/src/include/access/xlogfilesize.h b/src/include/access/xlogfilesize.h new file mode 100644 index 0000000000..fef30e02a6 --- /dev/null +++ b/src/include/access/xlogfilesize.h @@ -0,0 +1,83 @@ +/* + * xlogfilesize.h + * + * Size definitions and handling macros for PostgreSQL write-ahead logs. + * + * Portions Copyright (c) 1996-2024, PostgreSQL Global Development Group + * Portions Copyright (c) 1994, Regents of the University of California + * + * src/include/access/xlogfilesize.h + */ + +#ifndef XLOG_FILESIZE_H +#define XLOG_FILESIZE_H + +#include "access/xlogdefs.h" + +/* wal_segment_size can range from 1MB to 1GB */ +#define WalSegMinSize 1024 * 1024 +#define WalSegMaxSize 1024 * 1024 * 1024 + +/* default number of min and max wal segments */ +#define DEFAULT_MIN_WAL_SEGS 5 +#define DEFAULT_MAX_WAL_SEGS 64 + +/* + * These macros encapsulate knowledge about the exact layout of XLog + * files, timeline history fils and archive-status fils. + */ + +/* check that the given size is a valid wal_segment_size */ +#define IsPowerOf2(x) (x > 0 && ((x) & ((x)-1)) == 0) +#define IsValidWalSegSize(size) \ + (IsPowerOf2(size) && \ + ((size) >= WalSegMinSize && (size) <= WalSegMaxSize)) + +/* Number of segments in a logical XLOG file */ +#define XLogSegmentsPerXLogId(wal_segsz_bytes) \ + (UINT64CONST(0x100000000) / (wal_segsz_bytes)) + +/* + * Compute an XLogRecPtr from a segment number and offset. + */ +#define XLogSegNoOffsetToRecPtr(segno, offset, wal_segsz_bytes, dest) \ + (dest) = (segno) * (wal_segsz_bytes) + (offset) +/* + * Compute a segment number from an XLogRecPtr. + * + * For XLByteToSeg, do the computation at face value. For XLByteToPrevSeg, + * a boundary byte is taken to be in the previous segment. This is suitable + * for deciding which segment to write given a pointer to a record end, + * for example. + */ +#define XLByteToSeg(xlrp, logSegNo, wal_segsz_bytes) \ + logSegNo = (xlrp) / (wal_segsz_bytes) + +#define XLByteToPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \ + logSegNo = ((xlrp) - 1) / (wal_segsz_bytes) + +/* Compute the in-segment offset from an XLogRecPtr. */ +#define XLogSegmentOffset(xlogptr, wal_segsz_bytes) \ + ((xlogptr) & ((wal_segsz_bytes) - 1)) + +/* + * Convert values of GUCs measured in megabytes to equiv. segment count. + * Rounds down. + */ +#define XLogMBVarToSegs(mbvar, wal_segsz_bytes) \ + ((mbvar) / ((wal_segsz_bytes) / (1024 * 1024))) + +/* + * Is an XLogRecPtr within a particular XLOG segment? + * + * For XLByteInSeg, do the computation at face value. For XLByteInPrevSeg, + * a boundary byte is taken to be in the previous segment. + */ +#define XLByteInSeg(xlrp, logSegNo, wal_segsz_bytes) \ + (((xlrp) / (wal_segsz_bytes)) == (logSegNo)) + +#define XLByteInPrevSeg(xlrp, logSegNo, wal_segsz_bytes) \ + ((((xlrp) - 1) / (wal_segsz_bytes)) == (logSegNo)) + + +#endif /* XLOG_FILESIZE_H */ -- 2.46.0