pg_snapshot_recv accepts an XIP entry equal to xmax

From: wangyang25(at)otcaix(dot)iscas(dot)ac(dot)cn
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: pg_snapshot_recv accepts an XIP entry equal to xmax
Date: 2026-11-23 11:20:16
Message-ID: 179543281613.3780281.17642351937147665681@otcaix.iscas.ac.cn
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

pg_snapshot_recv accepts an XIP entry equal to xmax

Summary

PostgreSQL 19.0 accepts a binary pg_snapshot whose XIP list contains an ID equal to xmax. The documented snapshot invariant is xmin <= X < xmax; the accepted value is returned by pg_snapshot_xip() and appears in the text representation. The text input path rejects the equivalent value.

Reproduction

This libpq client sends a binary pg_snapshot with one XIP member. The optional argument selects the XIP value; it defaults to 20.

#include <arpa/inet.h>
#include <libpq-fe.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static void
put_u64_be(unsigned char *dst, uint64_t value)
{
uint32_t hi = htonl((uint32_t) (value >> 32));
uint32_t lo = htonl((uint32_t) value);

memcpy(dst, &hi, sizeof(hi));
memcpy(dst + sizeof(hi), &lo, sizeof(lo));
}

int
main(int argc, char **argv)
{
PGconn *conn = PQconnectdb("");
PGresult *res;
Oid type_oid;
unsigned char payload[28];
uint32_t nxip = htonl(1);
uint64_t xip = 20;
const char *values[1];
int lengths[1] = {sizeof(payload)};
int formats[1] = {1};

if (argc > 2)
{
fprintf(stderr, "usage: %s [xip]\n", argv[0]);
return 2;
}
if (argc == 2)
{
char *end;

xip = strtoull(argv[1], &end, 10);
if (*end != '\0')
{
fprintf(stderr, "xip must be an unsigned integer\n");
return 2;
}
}

if (PQstatus(conn) != CONNECTION_OK)
{
fprintf(stderr, "%s", PQerrorMessage(conn));
PQfinish(conn);
return 1;
}

res = PQexec(conn, "SELECT 'pg_snapshot'::regtype::oid");
if (PQresultStatus(res) != PGRES_TUPLES_OK)
{
fprintf(stderr, "%s", PQresultErrorMessage(res));
PQclear(res);
PQfinish(conn);
return 1;
}
type_oid = (Oid) strtoul(PQgetvalue(res, 0, 0), NULL, 10);
PQclear(res);

memcpy(payload, &nxip, sizeof(nxip));
put_u64_be(payload + 4, 10); /* xmin */
put_u64_be(payload + 12, 20); /* xmax */
put_u64_be(payload + 20, xip);
values[0] = (const char *) payload;

res = PQexecParams(conn,
"SELECT $1::text, "
"(SELECT array_agg(xip::text) "
" FROM pg_snapshot_xip($1) AS t(xip))",
1, &type_oid, values, lengths, formats, 0);
if (PQresultStatus(res) == PGRES_TUPLES_OK)
printf("snapshot=%s xip_list=%s\n",
PQgetvalue(res, 0, 0), PQgetvalue(res, 0, 1));
else
{
fprintf(stderr, "SQLSTATE %s: %s",
PQresultErrorField(res, PG_DIAG_SQLSTATE),
PQresultErrorMessage(res));
PQclear(res);
PQfinish(conn);
return 1;
}

PQclear(res);
PQfinish(conn);
return 0;
}

Compile against the PostgreSQL client library:

cc -Wall -Wextra -I"$(pg_config --includedir)" \
-L"$(pg_config --libdir)" -Wl,-rpath,"$(pg_config --libdir)" \
pg_snapshot_xip.c -lpq -o pg_snapshot_xip

Set the standard libpq connection variables for an account that can connect, then run:

./pg_snapshot_xip 19
./pg_snapshot_xip 20
./pg_snapshot_xip 21

PostgreSQL 19.0 returns:

$ ./pg_snapshot_xip 19
snapshot=10:20:19 xip_list={19}
$ ./pg_snapshot_xip 20
snapshot=10:20:20 xip_list={20}
$ ./pg_snapshot_xip 21
SQLSTATE 22P03: ERROR: invalid external pg_snapshot data

The equivalent text-input check is:

SELECT pg_input_is_valid('10:20:19', 'pg_snapshot') AS lower_xip_valid,
pg_input_is_valid('10:20:20', 'pg_snapshot') AS xip_equal_xmax_valid;

Run that check in a clean psql -X session; it returns t, f.

Expected behavior

The binary receiver should reject xip=20 when xmax=20. PostgreSQL's snapshot documentation (https://www.postgresql.org/docs/19/functions-info.html) defines XIP members as transaction IDs in the half-open range xmin <= X < xmax; xmax is one past the highest completed transaction ID.

Actual behavior

With xmin=10 and xmax=20, binary XIP values 19 and 20 are accepted, while 21 is rejected. pg_snapshot_xip() exposes the accepted value 20. The text input validity check rejects 10:20:20.

Source observation

The PostgreSQL 19.0 pg_snapshot structure itself documents its XIP entries as in-progress transaction IDs satisfying xmin <= xip[i] < xmax (type definition (https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/utils/adt/xid8funcs.c#L64-L69)) pg_snapshot_recv() rejects an XIP only when it is strictly greater than xmax, while the text parser rejects values greater than or equal to xmax (binary receiver (https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/utils/adt/xid8funcs.c#L495-L503) text parser (https://github.com/postgres/postgres/blob/5c839a2a08b9b87a349be97371fac6c5a2873b10/src/backend/utils/adt/xid8funcs.c#L295-L306))

Tested environment

- PostgreSQL 19.0 server and psql client (REL_19_0, commit 5c839a2a08b9b87a349be97371fac6c5a2873b10); PostgreSQL 18.7 also reproduced the equality case.
- Ubuntu 24.04, Linux kernel 7.0.0-38-generic, x86-64, GCC 13.3.0. Build options: --without-readline --without-zlib --without-icu --without-llvm.
- A fresh cluster was initialized with initdb --no-locale -E UTF8 --auth-local=trust --auth-host=reject and listened on a local socket only. Startup settings were listen_addresses='', port=55491, max_connections=20, and shared_buffers=32MB; the socket directory was local to the test environment. No other configuration was changed.
- The libpq client used the matching PostgreSQL 19.0 headers and library.

This report is limited to accepting an invalid binary snapshot value. It does not claim that pg_visible_in_snapshot() returns an incorrect visibility result or that stored data is corrupted.

Browse pgsql-bugs by date

  From Date Subject
Next Message Nitin Motiani 2026-11-23 11:40:04 Re: BUG #19931: postgres_fdw: FOR UPDATE over a pushed-down join ignores a pseudoconstant qual
Previous Message Shlok Kyal 2026-11-23 10:00:47 Re: Logical replication commits duplicates in DEFERRABLE unique indexes