Skip to content

Commit 344afc7

Browse files
committed
modify segno. for pg_walfile_name() and pg_walfile_name_offset()
Previously these functions returned the previous segment number if the LSN was on a segment boundary. We now always return the current segment number for an LSN. Docs updated to reflect this change. Regression tests added, author Andres Freund. Also mentioned in thread https://postgr.es/m/flat/20220204225057.GA1535307%40nathanxps13#d964275c9540d8395e138efc0a75f7e8 BACKWARD INCOMPATIBILITY Reported-by: Kyotaro Horiguchi Discussion: https://postgr.es/m/20190726.172120.101752680.horikyota.ntt@gmail.com Co-authored-by: Kyotaro Horiguchi Backpatch-through: master
1 parent 5c4c7ef commit 344afc7

File tree

4 files changed

+45
-13
lines changed

4 files changed

+45
-13
lines changed

doc/src/sgml/func.sgml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27075,11 +27075,6 @@ postgres=# SELECT * FROM pg_walfile_name_offset((pg_backup_stop()).lsn);
2707527075
(1 row)
2707627076
</programlisting>
2707727077
Similarly, <function>pg_walfile_name</function> extracts just the write-ahead log file name.
27078-
When the given write-ahead log location is exactly at a write-ahead log file boundary, both
27079-
these functions return the name of the preceding write-ahead log file.
27080-
This is usually the desired behavior for managing write-ahead log archiving
27081-
behavior, since the preceding file is the last one that currently
27082-
needs to be archived.
2708327078
</para>
2708427079

2708527080
<para>

src/backend/access/transam/xlogfuncs.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,10 +374,6 @@ pg_last_wal_replay_lsn(PG_FUNCTION_ARGS)
374374
/*
375375
* Compute an xlog file name and decimal byte offset given a WAL location,
376376
* such as is returned by pg_backup_stop() or pg_switch_wal().
377-
*
378-
* Note that a location exactly at a segment boundary is taken to be in
379-
* the previous segment. This is usually the right thing, since the
380-
* expected usage is to determine which xlog file(s) are ready to archive.
381377
*/
382378
Datum
383379
pg_walfile_name_offset(PG_FUNCTION_ARGS)
@@ -414,7 +410,7 @@ pg_walfile_name_offset(PG_FUNCTION_ARGS)
414410
/*
415411
* xlogfilename
416412
*/
417-
XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
413+
XLByteToSeg(locationpoint, xlogsegno, wal_segment_size);
418414
XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
419415
wal_segment_size);
420416

@@ -457,7 +453,7 @@ pg_walfile_name(PG_FUNCTION_ARGS)
457453
errhint("%s cannot be executed during recovery.",
458454
"pg_walfile_name()")));
459455

460-
XLByteToPrevSeg(locationpoint, xlogsegno, wal_segment_size);
456+
XLByteToSeg(locationpoint, xlogsegno, wal_segment_size);
461457
XLogFileName(xlogfilename, GetWALInsertionTimeLine(), xlogsegno,
462458
wal_segment_size);
463459

src/test/regress/expected/misc_functions.out

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ SELECT count(*) > 0 AS ok FROM pg_control_system();
619619
t
620620
(1 row)
621621

622-
-- pg_split_walfile_name
622+
-- pg_split_walfile_name, pg_walfile_name & pg_walfile_name_offset
623623
SELECT * FROM pg_split_walfile_name(NULL);
624624
segment_number | timeline_id
625625
----------------+-------------
@@ -642,3 +642,31 @@ SELECT segment_number > 0 AS ok_segment_number, timeline_id
642642
t | 4294967295
643643
(1 row)
644644

645+
SELECT setting::int8 AS segment_size
646+
FROM pg_settings
647+
WHERE name = 'wal_segment_size'
648+
\gset
649+
SELECT segment_number, file_offset
650+
FROM pg_walfile_name_offset('0/0'::pg_lsn + :segment_size),
651+
pg_split_walfile_name(file_name);
652+
segment_number | file_offset
653+
----------------+-------------
654+
1 | 0
655+
(1 row)
656+
657+
SELECT segment_number, file_offset
658+
FROM pg_walfile_name_offset('0/0'::pg_lsn + :segment_size + 1),
659+
pg_split_walfile_name(file_name);
660+
segment_number | file_offset
661+
----------------+-------------
662+
1 | 1
663+
(1 row)
664+
665+
SELECT segment_number, file_offset = :segment_size - 1
666+
FROM pg_walfile_name_offset('0/0'::pg_lsn + :segment_size - 1),
667+
pg_split_walfile_name(file_name);
668+
segment_number | ?column?
669+
----------------+----------
670+
0 | t
671+
(1 row)
672+

src/test/regress/sql/misc_functions.sql

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,10 +230,23 @@ SELECT count(*) > 0 AS ok FROM pg_control_init();
230230
SELECT count(*) > 0 AS ok FROM pg_control_recovery();
231231
SELECT count(*) > 0 AS ok FROM pg_control_system();
232232

233-
-- pg_split_walfile_name
233+
-- pg_split_walfile_name, pg_walfile_name & pg_walfile_name_offset
234234
SELECT * FROM pg_split_walfile_name(NULL);
235235
SELECT * FROM pg_split_walfile_name('invalid');
236236
SELECT segment_number > 0 AS ok_segment_number, timeline_id
237237
FROM pg_split_walfile_name('000000010000000100000000');
238238
SELECT segment_number > 0 AS ok_segment_number, timeline_id
239239
FROM pg_split_walfile_name('ffffffFF00000001000000af');
240+
SELECT setting::int8 AS segment_size
241+
FROM pg_settings
242+
WHERE name = 'wal_segment_size'
243+
\gset
244+
SELECT segment_number, file_offset
245+
FROM pg_walfile_name_offset('0/0'::pg_lsn + :segment_size),
246+
pg_split_walfile_name(file_name);
247+
SELECT segment_number, file_offset
248+
FROM pg_walfile_name_offset('0/0'::pg_lsn + :segment_size + 1),
249+
pg_split_walfile_name(file_name);
250+
SELECT segment_number, file_offset = :segment_size - 1
251+
FROM pg_walfile_name_offset('0/0'::pg_lsn + :segment_size - 1),
252+
pg_split_walfile_name(file_name);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy