pg_walinspect: add functions to locate and list WAL by time and LSN - #419
Open
pg-hub-mirror[bot] wants to merge 1 commit into
Open
pg-hub-mirror[bot] wants to merge 1 commit into
pg-hub-mirror[bot] wants to merge 1 commit into
Conversation
Add two functions to pg_walinspect for locating WAL around a wall-clock time and for listing the WAL segment files covering an LSN range. pg_get_wal_location_at_time() takes a target timestamp and optional before/after intervals and returns an LSN range bracketed by WAL records that contain timestamps. WAL segment modification times are used only as a starting-position hint; the returned boundaries are determined from timestamps stored in WAL records. Since most WAL records do not carry timestamps, the returned LSN range can be wider than the requested time range. pg_get_wal_files() returns the retained WAL segment files intersecting a specified LSN range, together with the complete start and end LSN of each segment. It also handles ranges crossing timeline switches in the current timeline's history. To avoid duplicating timestamp extraction logic, expose the existing getRecordTimestamp() helper from xlogrecovery.c as GetXLogRecordTimestamp() and reuse it from pg_walinspect. These functions are intended to make it easier to investigate database activity when the approximate time of an event is known but the corresponding WAL location is not. Author: Chao Li <lic@highgo.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
pgsql-hackers80E9F0AD-CFC5-4BE5-81DE-D8FE35E10A1C@gmail.comPatch files:
Hi,
I'd like to propose adding two new SQL functions to pg_walinspect.
Motivation
During HOW2026 in Jinan this April, a DBA talked to me that PG lacks an easy way to locate WAL files by time.
He described a scenario where somebody had done something wrong in a production database, for example accidentally deleting important data. The administrator knew that the incident had happened at around 1am the last night and wanted to confirm what had happened by analyzing WAL.
The problem was that, with thousands of WAL files in pg_wal, where should he start?
Analysis
After looking into this use case, I think the underlying requirement is slightly different from simply finding a WAL file by timestamp.
Given an approximate point in time, the user really wants an LSN range that is likely to contain the WAL records associated with the operation of interest, for example a DELETE.
PostgreSQL already provides several related facilities:
However, WAL file modification times don't necessarily reflect the chronological position of the WAL segment. For example, on my local cluster:
Here, segment 1 has a later modification time than segment 2. Since modification times do not necessarily follow WAL segment order, using them to locate the WAL corresponding to a particular point in time can be difficult and sometimes confusing.
So fundamentally, what is missing is an easy way to map a wall-clock time to a useful WAL/LSN range.
Design
I propose adding two SQL functions.
pg_get_wal_location_at_time(target_time timestamptz, before interval DEFAULT '1 minute', after interval DEFAULT '1 minute’)
Given a target time and optional before and after intervals, it returns a WAL range around that time.
Not all WAL records contain timestamps, so the implementation reuses the existing timestamp-extraction logic from xlogrecovery.c, exposed by this patch as GetXLogRecordTimestamp(), to identify timestamp-bearing WAL records.
pg_get_wal_files(start_lsn pg_lsn, end_lsn pg_lsn DEFAULT NULL)
Given a start LSN and an optional end LSN, it returns the WAL files covering that LSN range.
Unlike pg_walfile_name(), which returns only a WAL file name for a single LSN, this function returns each relevant WAL file together with that segment's start and end LSNs.
Implementation
Both functions are added to the pg_walinspect extension.
There is already quite a bit of reusable WAL-reading infrastructure there, and semantically it also seems like the natural place for this functionality.
The implementation builds on the existing pg_walinspect and XLog reader infrastructure. It does not introduce a new WAL format or a separate WAL parser.
Demo
For example, suppose I deleted a tuple at around 16:11 yesterday. I can first locate the WAL range around that time:
Here, you may notice that end_lsn is much later than the specified timestamp plus 5 minutes. I chose this example intentionally.
As mentioned above, not all WAL records carry a timestamp. In this demo database, no timestamp-bearing WAL records were generated for quite some time after the target period, so the next usable timestamp did not appear until today. As a result, the returned WAL range is much wider than the requested five-minute window.
This also illustrates why the function returns an approximate WAL range based on timestamp-bearing records rather than an exact time-to-LSN mapping.
Then I can inspect that LSN range:
Now we know the LSN range for the DELETE. If we want to determine which WAL file contains it:
From there, the WAL file can be examined further with pg_waldump or other WAL-mining tools.
PFA v1. Reviews and comments and suggestions are greatly appreciated.
Best regards,
Chao Li (Evan)
HighGo Software Co., Ltd.
https://www.highgo.com/