Skip to content

Server streams WAL segments lazily #255

Open

Description

@Jubilee101

We've noticed for quite a while that server sometimes does not sends us WAL segment eagerly - it just idles and sends status report request to keep the connection open, even when it has more segments not yet streamed to us.

We'll try to force server to do CHECKPOINT, so that all the records on WAL get applied to disk, and see if that triggers streaming.

Activity

  1. added
    bug
    on Apr 9, 2024
  2. self-assigned this
    on Apr 9, 2024
  3. added
    enhancement
    and removed
    bug
    on Apr 9, 2024
  4. jesperpedersen commented on Apr 9, 2024

    @jesperpedersen
    Member

    We only require a REPLICATION user so we don't have access to executing the CHECKPOINT command

  5. jesperpedersen commented on Apr 9, 2024

    @jesperpedersen
    Member

    For PostgreSQL 15+ we could add the pg_checkpoint role, and update our requirements for pg_hba.conf to allow non-replication based login.

    So, it has to be an optional operation

  6. jesperpedersen commented on Apr 9, 2024

    @jesperpedersen
    Member

    Unless we can come up with something else

  7. Jubilee101 commented on Apr 9, 2024

    @Jubilee101
    MemberAuthor

    For PostgreSQL 15+ we could add the pg_checkpoint role, and update our requirements for pg_hba.conf to allow non-replication based login.

    So, it has to be an optional operation

    ..That doesn't seem ideal. I'll think about it.

  8. bassamadnan commented on Apr 26, 2025

    @bassamadnan
    Collaborator

    Would influencing wal_writer_delay have an impact on this? i've been reading on some material but most of these come from configuring the database itself and would have tradeoffs.

  9. Jubilee101 commented on Apr 27, 2025

    @Jubilee101
    MemberAuthor

    Would influencing wal_writer_delay have an impact on this? i've been reading on some material but most of these come from configuring the database itself and would have tradeoffs.

    That sounds like a direction to investigate. If nothing else we can probably identify the cause of the issue. But judging from the description I kinda doubt if this is the reason. The default value is merely 200ms, which is quite negligible. The problem is that the issue is a little bit hard to steadily recreate. We probably need more logging on this issue as well, like the LSN difference between server and us whenever the server sends a keep alive message.

  10. bassamadnan commented on Apr 28, 2025

    @bassamadnan
    Collaborator

    That is very true -- i've been wanting to ask how was this detected in the first place? Seems like a very interesting problem

  11. Jubilee101 commented on Jul 19, 2025

    @Jubilee101
    MemberAuthor

    Updates

    We currently have no steady way to recreate the issue. But it's very likely to show up during pgbench. To somehow recreate the issue, you can do

    createdb -E UTF8 -O myuser test
    pgbench -i -s 5 -U myuser test
    pgbench -M prepared -T 15 -U myuser test
    

    Rerun pgbench multiple times and you'll see a case like the following:
    WAL streamer stops receiving stream data at LSN 2/2D1D4F0 (the segment is 000000010000000200000002.partial), which is also what SELECT pg_current_wal_flush_lsn () or SELECT pg_current_wal_lsn () returns. However, there are more log segment files on the serverside to come: from 000000010000000200000003 to 000000010000000200000006. This situation doesn't improve with CHECKPOINT command, it merely advances the current WAL lsn to 2/2D1D738.

    At first we suspect that those segments are pre-generated or reused old segments by the server for furture records, given that the amount of records generated were significant. But upon parsing the contents, those segments actually seems to have meanings. For example, parsing 000000010000000200000003 with our pgmoneta-walinfo gives:

    Incomplete | | | | | Skipped
    Heap2 | 1/FDFFFD58 | 2/30016F8 | 59 | 59 | 171110 | cutoff xid 0 flags 0x03 blkref #0: rel 1663/16561/16601 forknum 2 blk 0 blkref #1: rel 1663/16561/16601 forknum 0 blk 7581
    Heap2 | 1/FE0016F8 | 2/3001738 | 6515 | 6515 | 171110 | 61 tuples flags 0x20 blkref #0: rel 1663/16561/16601 forknum 0 blk 7582
    Heap2 | 1/FE001738 | 2/30030C8 | 59 | 59 | 171110 | cutoff xid 0 flags 0x03 blkref #0: rel 1663/16561/16601 forknum 2 blk 0 blkref #1: rel 1663/16561/16601 forknum 0 blk 7582
    

    But comparing with result generated by pg_waldump, we found that this content is actually invalid. Doing pg_waldump data/pg_wal/000000010000000200000003 data/pg_wal/000000010000000200000003 gives

    pg_waldump: error: could not find a valid record after 2/3000000
    

    In fact, even 00000010000000200000002 is incomplete at server side:

    $ pg_waldump data/pg_wal/000000010000000200000002 data/pg_wal/000000010000000200000002
    ...
    pg_waldump: error: error in WAL record at 2/2D1D9F8: invalid record length at 2/2D1DA70: expected at least 24, got 0
    

    With the above analysis I think it's safe to conclude that the server isn't actually streaming WAL to us lazily, the extra WAL segments don't actually contain any real contents, and may just be reused or pregenerated segments. The content generated by our walinfo tool, however, remains a mystery, why does server generate seemingly meaningful records beforehand instead of simply padding 0? Or are they old records that didn't get overwritten?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancement

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions