Skip to content

Commit 5b0d92a

Browse files
dAdAbirddutow
authored andcommitted
Make XLog storage extensible
and allow extensions to override it For now, it extends on `pread` and `pwrite` from/into segment files. This is the minimum we need for full XLog encryption with pg_de.
1 parent 8b86257 commit 5b0d92a

File tree

6 files changed

+50
-5
lines changed

6 files changed

+50
-5
lines changed

src/backend/access/transam/xlog.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
#include "access/xloginsert.h"
6262
#include "access/xlogreader.h"
6363
#include "access/xlogrecovery.h"
64+
#include "access/xlog_smgr.h"
6465
#include "access/xlogutils.h"
6566
#include "backup/basebackup.h"
6667
#include "catalog/catversion.h"
@@ -2442,7 +2443,7 @@ XLogWrite(XLogwrtRqst WriteRqst, TimeLineID tli, bool flexible)
24422443
INSTR_TIME_SET_ZERO(start);
24432444

24442445
pgstat_report_wait_start(WAIT_EVENT_WAL_WRITE);
2445-
written = pg_pwrite(openLogFile, from, nleft, startoffset);
2446+
written = xlog_smgr->seg_write(openLogFile, from, nleft, startoffset);
24462447
pgstat_report_wait_end();
24472448

24482449
/*

src/backend/access/transam/xlogreader.c

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "access/xlog_internal.h"
3030
#include "access/xlogreader.h"
3131
#include "access/xlogrecord.h"
32+
#include "access/xlog_smgr.h"
3233
#include "catalog/pg_control.h"
3334
#include "common/pg_lzcompress.h"
3435
#include "replication/origin.h"
@@ -63,6 +64,21 @@ static void WALOpenSegmentInit(WALOpenSegment *seg, WALSegmentContext *segcxt,
6364
*/
6465
#define DEFAULT_DECODE_BUFFER_SIZE (64 * 1024)
6566

67+
/*
68+
* XLog storage manager
69+
*
70+
* TODO: should be in xlog.c or new xlog_smgr.c ?
71+
* Now it's here because pg_rewind and other tools compile only
72+
* w/ xlogreader.c
73+
*/
74+
XLogSmgr *xlog_smgr = &xlog_smgr_standard;
75+
76+
void
77+
SetXLogSmgr(XLogSmgr *xlsmgr)
78+
{
79+
xlog_smgr = xlsmgr;
80+
}
81+
6682
/*
6783
* Construct a string in state->errormsg_buf explaining what's wrong with
6884
* the current record being read.
@@ -1557,7 +1573,7 @@ WALRead(XLogReaderState *state,
15571573

15581574
/* Reset errno first; eases reporting non-errno-affecting errors */
15591575
errno = 0;
1560-
readbytes = pg_pread(state->seg.ws_file, p, segbytes, (off_t) startoff);
1576+
readbytes = xlog_smgr->seg_read(state->seg.ws_file, p, segbytes, (off_t) startoff);
15611577

15621578
#ifndef FRONTEND
15631579
pgstat_report_wait_end();

src/backend/access/transam/xlogrecovery.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "access/xlogprefetcher.h"
4040
#include "access/xlogreader.h"
4141
#include "access/xlogrecovery.h"
42+
#include "access/xlog_smgr.h"
4243
#include "access/xlogutils.h"
4344
#include "backup/basebackup.h"
4445
#include "catalog/pg_control.h"
@@ -3397,7 +3398,7 @@ XLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr, int reqLen,
33973398
readOff = targetPageOff;
33983399

33993400
pgstat_report_wait_start(WAIT_EVENT_WAL_READ);
3400-
r = pg_pread(readFile, readBuf, XLOG_BLCKSZ, (off_t) readOff);
3401+
r = xlog_smgr->seg_read(readFile, readBuf, XLOG_BLCKSZ, (off_t) readOff);
34013402
if (r != XLOG_BLCKSZ)
34023403
{
34033404
char fname[MAXFNAMELEN];

src/backend/replication/walreceiver.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#include "access/xlog_internal.h"
5858
#include "access/xlogarchive.h"
5959
#include "access/xlogrecovery.h"
60+
#include "access/xlog_smgr.h"
6061
#include "catalog/pg_authid.h"
6162
#include "funcapi.h"
6263
#include "libpq/pqformat.h"
@@ -941,7 +942,7 @@ XLogWalRcvWrite(char *buf, Size nbytes, XLogRecPtr recptr, TimeLineID tli)
941942
/* OK to write the logs */
942943
errno = 0;
943944

944-
byteswritten = pg_pwrite(recvFile, buf, segbytes, (off_t) startoff);
945+
byteswritten = xlog_smgr->seg_write(recvFile, buf, segbytes, (off_t) startoff);
945946
if (byteswritten <= 0)
946947
{
947948
char xlogfname[MAXFNAMELEN];

src/include/access/xlog_internal.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,10 @@ typedef XLogLongPageHeaderData *XLogLongPageHeader;
7878
#define XLP_BKP_REMOVABLE 0x0004
7979
/* Replaces a missing contrecord; see CreateOverwriteContrecordRecord */
8080
#define XLP_FIRST_IS_OVERWRITE_CONTRECORD 0x0008
81+
/* The page is encrypted */
82+
#define XLP_ENCRYPTED 0x0010
8183
/* All defined flag bits in xlp_info (used for validity checking of header) */
82-
#define XLP_ALL_FLAGS 0x000F
84+
#define XLP_ALL_FLAGS 0x001F
8385

8486
#define XLogPageHeaderSize(hdr) \
8587
(((hdr)->xlp_info & XLP_LONG_HEADER) ? SizeOfXLogLongPHD : SizeOfXLogShortPHD)

src/include/access/xlog_smgr.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef XLOG_SMGR_H
2+
#define XLOG_SMGR_H
3+
4+
#include "postgres.h"
5+
6+
#include <unistd.h>
7+
8+
/* XLog storage manager interface */
9+
typedef struct XLogSmgr {
10+
ssize_t (*seg_read) (int fd, void *buf, size_t count, off_t offset);
11+
12+
ssize_t (*seg_write) (int fd, const void *buf, size_t count, off_t offset);
13+
} XLogSmgr;
14+
15+
/* Default (standard) XLog storage manager */
16+
static const XLogSmgr xlog_smgr_standard = {
17+
.seg_read = pg_pread,
18+
.seg_write = pg_pwrite,
19+
};
20+
21+
extern XLogSmgr *xlog_smgr;
22+
extern void SetXLogSmgr(XLogSmgr *xlsmgr);
23+
24+
#endif /* XLOG_SMGR_H */

0 commit comments

Comments
 (0)