Skip to content

Commit 04a7d45

Browse files
committed
PGPRO-2432: Improve reading of compressed WAL's if Postgrestries to read same record many times
1 parent bed0a45 commit 04a7d45

File tree

1 file changed

+26
-14
lines changed

1 file changed

+26
-14
lines changed

src/parsexlog.c

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ typedef struct XLogPageReadPrivate
104104
#ifdef HAVE_LIBZ
105105
gzFile gz_xlogfile;
106106
char gz_xlogpath[MAXPGPATH];
107+
108+
char gz_buf[XLOG_BLCKSZ];
109+
uint32 gz_prev_off;
107110
#endif
108111
} XLogPageReadPrivate;
109112

@@ -1057,22 +1060,30 @@ SimpleXLogPageRead(XLogReaderState *xlogreader, XLogRecPtr targetPagePtr,
10571060
#ifdef HAVE_LIBZ
10581061
else
10591062
{
1060-
if (gzseek(private_data->gz_xlogfile, (z_off_t) targetPageOff, SEEK_SET) == -1)
1063+
if (private_data->gz_prev_off != 0 &&
1064+
private_data->gz_prev_off == targetPageOff)
1065+
memcpy(readBuf, private_data->gz_buf, XLOG_BLCKSZ);
1066+
else
10611067
{
1062-
elog(WARNING, "Thread [%d]: Could not seek in compressed WAL segment \"%s\": %s",
1063-
private_data->thread_num,
1064-
private_data->gz_xlogpath,
1065-
get_gz_error(private_data->gz_xlogfile));
1066-
return -1;
1067-
}
1068+
if (gzseek(private_data->gz_xlogfile, (z_off_t) targetPageOff, SEEK_SET) == -1)
1069+
{
1070+
elog(WARNING, "Thread [%d]: Could not seek in compressed WAL segment \"%s\": %s",
1071+
private_data->thread_num,
1072+
private_data->gz_xlogpath,
1073+
get_gz_error(private_data->gz_xlogfile));
1074+
return -1;
1075+
}
10681076

1069-
if (gzread(private_data->gz_xlogfile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1070-
{
1071-
elog(WARNING, "Thread [%d]: Could not read from compressed WAL segment \"%s\": %s",
1072-
private_data->thread_num,
1073-
private_data->gz_xlogpath,
1074-
get_gz_error(private_data->gz_xlogfile));
1075-
return -1;
1077+
if (gzread(private_data->gz_xlogfile, readBuf, XLOG_BLCKSZ) != XLOG_BLCKSZ)
1078+
{
1079+
elog(WARNING, "Thread [%d]: Could not read from compressed WAL segment \"%s\": %s",
1080+
private_data->thread_num,
1081+
private_data->gz_xlogpath,
1082+
get_gz_error(private_data->gz_xlogfile));
1083+
return -1;
1084+
}
1085+
private_data->gz_prev_off = targetPageOff;
1086+
memcpy(private_data->gz_buf, readBuf, XLOG_BLCKSZ);
10761087
}
10771088
}
10781089
#endif
@@ -1131,6 +1142,7 @@ CleanupXLogPageRead(XLogReaderState *xlogreader)
11311142
{
11321143
gzclose(private_data->gz_xlogfile);
11331144
private_data->gz_xlogfile = NULL;
1145+
private_data->gz_prev_off = 0;
11341146
}
11351147
#endif
11361148
private_data->xlogexists = false;

0 commit comments

Comments
 (0)