Subject: Last removed wal segment file

From: mahendrakar s <[email-masked]>
Date: Mon, 23 Dec 2024 09:22:06 +0530

Hi Hackers,

Is there any psql utility function that returns the last removed wal
segment file?
I see there is a method: XLogGetLastRemovedSegno in xlog.c.
Would be happy to add a new psql function around it (similar to:
pg_walfile_name, pg_last_wal_replay_lsn, etc other utility functions
we have).

Thanks,
Mahendrakar.

---

From: Michael Paquier <[email-masked]>
Date: Mon, 23 Dec 2024 14:16:01 +0900

> pg_walfile_name, pg_last_wal_replay_lsn, etc other utility functions

What's your use-case for it?  I have to admit that finding out this
information can be a bit annoying as it requires checking the state of
archiving and the replication slots from which you would get a LSN
(not a segment name).  Of course, one can use pg_ls_waldir() these
days and it would also work based on timeline number and even partial
segments which are not removed by the checkpointer.  That's more
expensive than looking at a single counter in shmem.

If you wrap a SQL function around that, the segment number would not
be enough to guess which segment is removed: the timeline of the file
removed should also be added.  Hence XLogCtlData should be extended
with a kind of lastRemovedSegTLI or equivalent to be able to rebuild
the file name from the data in shared memory.  Note that
UpdateLastRemovedPtr() extracts the timeline number, does not store
it.

Also, note that UpdateLastRemovedPtr() has a small race condition when
it comes to monitoring: it is called before the file is physically
removed in RemoveOldXlogFiles().  It would not really matter much in
practice, I guess..
--
Michael