5

We know WAL file locate in the $PGDATA/pg_xlog directory, but I don't know the meaning of WAL file ,such as a WAL file which named "0000000A00005283000000E0",

I see some's blog say WAL file name respect a format name subdivided into 3 sequences of 8 hexa digits defining:

Timeline ID

Block ID

Segment ID

But I still don't understand this. anyone can explain this?

--query

francs=> select pg_current_xlog_location();
 pg_current_xlog_location 
--------------------------
 5283/D9C2A320
(1 row)

francs=> select pg_xlogfile_name(pg_current_xlog_insert_location());
     pg_xlogfile_name     
--------------------------
 0000000A00005283000000E0
1
  • Wha you don't understand in detail?`
    – frlan
    May 27, 2014 at 9:20

1 Answer 1

13

WAL file naming is really an implementation detail. See the source code - starting with the implementation of pg_xlogfile_name in src/backend/access/transam/xlogfuncs.c, which uses XLogFileName in src/include/access/xlog_internal.h:

#define XLogFileName(fname, tli, logSegNo)  \
    snprintf(fname, MAXFNAMELEN, "%08X%08X%08X", tli,       \
             (uint32) ((logSegNo) / XLogSegmentsPerXLogId), \
             (uint32) ((logSegNo) % XLogSegmentsPerXLogId))

From there you can see that in current PostgreSQL versions the archive name is eight zero-padded hex digits of timeline ID, then a somewhat oddly formatted value for the segment that works out to the high 32 bits of a 64-bit segment number zero-padded out to 8 hex digits, then the low 32 bits zero padded out to 8 hex digits. That format is really a historical quirk.

You can work that out because of the definition of XLogSegmentsPerXLogId:

#define XLogSegmentsPerXLogId   (UINT64CONST(0x100000000) / XLOG_SEG_SIZE)

which is 1 << 32 i.e. 2^32, so really the XLogFileName is just taking the high and low 32 bits.

That said, you shouldn't need to do anything with WAL based on the file names, except use them to uniquely identify a WAL file, so I'm wondering why you're asking this. What're you trying to achieve?

5
  • Thanks for replying, I just want to know the exact meaning of wal filename.
    – francs
    May 28, 2014 at 1:59
  • Does the wal composite of Timeline ID, Block ID, segment ID? I see this from the blog michael.otacoo.com/postgresql-2/…
    – francs
    May 28, 2014 at 2:11
  • 1
    But can we assume, at least, that the filenames are lexicographically ordered in time?
    – bereal
    May 28, 2014 at 4:36
  • The block ID isn't in the WAL file name. Timeline ID and segment number are. I'm not really sure where Michael gets "block ID" - the only usage of "block ID" in the sources refers to heap tables. I think he just means the major half of the segment number. May 28, 2014 at 5:59
  • 2
    Yes, they'll be lexographically ordered, in that the filename is essentially a padded tuple of (timelineid, segid_top_32bits, segid_bottom_32bits). But please use the existing tools for managing WAL, like pg_archivecleanup, rather than rolling your own. If the WAL naming scheme changes those tools will get updated; your home rolled one will not. May 28, 2014 at 6:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.