Skip to content

Commit 55daf3f

Browse files
committed
PG-1411 Make pg_resetwal work with TDE
As pg_resetwal removes old WAL segments and creates new one with empty record we can do that write in unencrypted mode. However that requires new WAL key creation in case if encryption was enabled before.
1 parent f939a24 commit 55daf3f

File tree

4 files changed

+67
-1
lines changed

4 files changed

+67
-1
lines changed

src/bin/pg_resetwal/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
/pg_resetwal
22
/tmp_check/
3+
4+
# Source files copied from src/backend/access/transam/
5+
/xlogreader.c

src/bin/pg_resetwal/Makefile

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,29 @@ subdir = src/bin/pg_resetwal
1515
top_builddir = ../../..
1616
include $(top_builddir)/src/Makefile.global
1717

18+
override CPPFLAGS := -DFRONTEND $(CPPFLAGS)
1819
LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils
1920

2021
OBJS = \
2122
$(WIN32RES) \
2223
pg_resetwal.o
2324

25+
ifeq ($(enable_percona_ext),yes)
26+
27+
OBJS += \
28+
$(top_srcdir)/src/fe_utils/simple_list.o \
29+
$(top_builddir)/src/libtde/libtdexlog.a \
30+
$(top_builddir)/src/libtde/libtde.a \
31+
xlogreader.o
32+
33+
override CPPFLAGS := -I$(top_srcdir)/contrib/pg_tde/src/include -I$(top_srcdir)/contrib/pg_tde/src/libkmip/libkmip/include $(CPPFLAGS)
34+
endif
35+
2436
all: pg_resetwal
2537

38+
xlogreader.c: % : $(top_srcdir)/src/backend/access/transam/%
39+
rm -f $@ && $(LN_S) $< .
40+
2641
pg_resetwal: $(OBJS) | submake-libpgport
2742
$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
2843

@@ -36,7 +51,7 @@ uninstall:
3651
rm -f '$(DESTDIR)$(bindir)/pg_resetwal$(X)'
3752

3853
clean distclean:
39-
rm -f pg_resetwal$(X) $(OBJS)
54+
rm -f pg_resetwal$(X) $(OBJS) xlogreader.c
4055
rm -rf tmp_check
4156

4257
check:

src/bin/pg_resetwal/meson.build

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,22 @@ if host_system == 'windows'
1010
'--FILEDESC', 'pg_resetwal - reset PostgreSQL WAL log'])
1111
endif
1212

13+
link_w = []
14+
include_dirs = [postgres_inc]
15+
16+
if percona_ext == true
17+
link_w = [pg_tde_frontend]
18+
include_dirs = [postgres_inc, pg_tde_inc]
19+
pg_resetwal_sources += xlogreader_sources
20+
endif
21+
1322
pg_resetwal = executable('pg_resetwal',
1423
pg_resetwal_sources,
1524
dependencies: [frontend_code],
25+
c_args: ['-DFRONTEND'], # needed for xlogreader et al
1626
kwargs: default_bin_args,
27+
include_directories: include_dirs,
28+
link_with: link_w
1729
)
1830
bin_targets += pg_resetwal
1931

src/bin/pg_resetwal/pg_resetwal.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@
5959
#include "pg_getopt.h"
6060
#include "storage/large_object.h"
6161

62+
#ifdef PERCONA_EXT
63+
#include "pg_tde.h"
64+
#include "access/pg_tde_fe_init.h"
65+
#include "access/pg_tde_xlog_smgr.h"
66+
#include "access/xlog_smgr.h"
67+
#endif
68+
6269
static ControlFileData ControlFile; /* pg_control values */
6370
static XLogSegNo newXlogSegNo; /* new XLOG segment # */
6471
static bool guessed = false; /* T if we had to guess at any values */
@@ -344,6 +351,15 @@ main(int argc, char *argv[])
344351
}
345352
#endif
346353

354+
#ifdef PERCONA_EXT
355+
{
356+
char tde_path[MAXPGPATH];
357+
snprintf(tde_path, sizeof(tde_path), "%s/%s", DataDir, PG_TDE_DATA_DIR);
358+
pg_tde_fe_init(tde_path);
359+
TDEXLogSmgrInit();
360+
}
361+
#endif
362+
347363
get_restricted_token();
348364

349365
/* Set mask based on PGDATA permissions */
@@ -488,6 +504,15 @@ main(int argc, char *argv[])
488504
exit(1);
489505
}
490506

507+
#ifdef PERCONA_EXT
508+
/*
509+
* As we always write empty record here we can do it in unencrypted mode.
510+
* We initialize writes here because new WAL key may be created in some cases,
511+
* and we want it only when we are ready to perform acutal writes.
512+
*/
513+
TDEXLogSmgrInitWrite(false);
514+
#endif
515+
491516
/*
492517
* Else, do the dirty deed.
493518
*/
@@ -1134,14 +1159,25 @@ WriteEmptyXLOG(void)
11341159
pg_fatal("could not open file \"%s\": %m", path);
11351160

11361161
errno = 0;
1162+
#ifdef PERCONA_EXT
1163+
if (xlog_smgr->seg_write(fd, buffer.data, XLOG_BLCKSZ, 0,
1164+
ControlFile.checkPointCopy.ThisTimeLineID,
1165+
newXlogSegNo, WalSegSz) != XLOG_BLCKSZ)
1166+
#else
11371167
if (write(fd, buffer.data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1168+
#endif
11381169
{
11391170
/* if write didn't set errno, assume problem is no disk space */
11401171
if (errno == 0)
11411172
errno = ENOSPC;
11421173
pg_fatal("could not write file \"%s\": %m", path);
11431174
}
11441175

1176+
#ifdef PERCONA_EXT
1177+
/* If we used xlog smgr, we need to update the file offset */
1178+
lseek(fd, XLOG_BLCKSZ, SEEK_CUR);
1179+
#endif
1180+
11451181
/* Fill the rest of the file with zeroes */
11461182
memset(buffer.data, 0, XLOG_BLCKSZ);
11471183
for (nbytes = XLOG_BLCKSZ; nbytes < WalSegSz; nbytes += XLOG_BLCKSZ)

0 commit comments

Comments
 (0)