Skip to content

Commit f29751d

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 a329aeb commit f29751d

File tree

4 files changed

+67
-0
lines changed

4 files changed

+67
-0
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: 15 additions & 0 deletions
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) \
23+
xlogreader.o \
2224
pg_resetwal.o
2325

26+
ifeq ($(enable_percona_ext),yes)
27+
28+
OBJS += \
29+
$(top_srcdir)/src/fe_utils/simple_list.o \
30+
$(top_builddir)/src/libtde/libtdexlog.a \
31+
$(top_builddir)/src/libtde/libtde.a
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

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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@
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+
#include "catalog/tde_global_space.h"
68+
#endif
69+
6270
static ControlFileData ControlFile; /* pg_control values */
6371
static XLogSegNo newXlogSegNo; /* new XLOG segment # */
6472
static bool guessed = false; /* T if we had to guess at any values */
@@ -344,6 +352,15 @@ main(int argc, char *argv[])
344352
}
345353
#endif
346354

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

349366
/* Set mask based on PGDATA permissions */
@@ -488,6 +505,15 @@ main(int argc, char *argv[])
488505
exit(1);
489506
}
490507

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

11361162
errno = 0;
1163+
#ifdef PERCONA_EXT
1164+
if (xlog_smgr->seg_write(fd, buffer.data, XLOG_BLCKSZ, 0,
1165+
ControlFile.checkPointCopy.ThisTimeLineID,
1166+
newXlogSegNo, WalSegSz) != XLOG_BLCKSZ)
1167+
#else
11371168
if (write(fd, buffer.data, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1169+
#endif
11381170
{
11391171
/* if write didn't set errno, assume problem is no disk space */
11401172
if (errno == 0)
11411173
errno = ENOSPC;
11421174
pg_fatal("could not write file \"%s\": %m", path);
11431175
}
11441176

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

0 commit comments

Comments
 (0)