PostgreSQL – What is the Meaning of WAL File Name?

postgresql

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

Best Answer

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?