BUG #19941: Acknowledged INSERT lost after a simple Query inside an extended-protocol batch (XX000)
Thread notes
1 note#bug
hackorum-bot#bug
@global #bug
The following bug has been logged on the website:
Bug reference: 19941
Logged by: MD. Ibrahim
Email address: ibrahimthecosmic@gmail.com
PostgreSQL version: 19.0
Operating system: Linux x86_64 (Debian, docker postgres:19)
Description:
PostgreSQL version: 18.7 and 19.0 (docker images postgres:18 and
postgres:19,
Debian 18.7-1.pgdg13+5 and 19.0-2.pgdg13+1); also seen on 18.6.
Platform: Linux x86_64
A client sends, in one write:
Parse ("", "INSERT INTO b17 VALUES (1)"), Bind, Execute,
Query ("SELECT 2"), Sync
Every message is answered normally: ParseComplete, BindComplete,
CommandComplete "INSERT 0 1"; RowDescription, DataRow, CommandComplete,
ReadyForQuery 'T' for the Query; ReadyForQuery 'I' for the Sync.
Despite the 'I', the transaction is still open: pg_stat_activity shows the
backend as "idle in transaction" with a backend_xid, and the row is not
visible to other sessions. The session's next command, whatever it is,
fails with
ERROR: StartTransactionCommand: unexpected state STARTED (XX000)
(xact.c line 3132 on 18.7, 3192 on 19.0), followed by ReadyForQuery 'I',
and the transaction is rolled back: the INSERT, acknowledged with
"INSERT 0 1" and a ReadyForQuery 'I', is lost (SELECT count(*) returns 0).
The command after that succeeds. A SET in place of "SELECT 2" is undone the
same way (ParameterStatus reports the new value with the batch, the old one
with the error).
Neither a second Sync nor an extended-protocol COMMIT (Parse, Bind, Execute,
Sync) saves it: the Sync answers 'I' and changes nothing, the COMMIT's Parse
gets the same XX000. A Parse alone before the Query (no Bind/Execute) does
not trigger it, nor does a batch that ends in an error. Sending the Sync in
a separate write gives the same result.
I know mixing the simple and extended protocols inside one cycle is
unsupported territory (BUG #17565, and the "Inconsistent behavior in mixing
simple/extended query protocol" thread of July 2025). What sets this one
apart: every message of the batch is answered normally and the batch ends
with ReadyForQuery 'I', yet a write it reported done is rolled back later,
by an unrelated command, with an internal error. Nothing in the batch's
answers tells the client. Behind a transaction-mode connection pooler, the
failing command is usually another client's.
If the server cannot keep the transaction going, failing the Query (or the
batch) with a proper error would at least tell the client.
Reproduction: the script below (Python 3, standard library only; the
messages are built by hand; it creates and drops a table b17). Against a
server with trust authentication:
docker run --rm -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432
postgres:19
python3 repro.py 127.0.0.1 5432 postgres
The batch it sends:
msg(b'P', cstr('') + cstr('INSERT INTO b17 VALUES (1)') +
struct.pack('!h', 0))
+ msg(b'B', cstr('') + cstr('') + struct.pack('!hhh', 0, 0, 0))
+ msg(b'E', cstr('') + struct.pack('!i', 0))
+ msg(b'Q', cstr('SELECT 2'))
+ msg(b'S')
then, after ReadyForQuery 'I', msg(b'Q', cstr('SELECT 3')), which gets the
ErrorResponse, and SELECT count(*) FROM b17, which returns 0.
Found while writing protocol conformance tests for a connection pooler.
--- repro.py ---
#!/usr/bin/env python3
"""An acknowledged INSERT lost, and XX000 "StartTransactionCommand:
unexpected state STARTED", after a simple Query inside an extended batch.
Needs a server that accepts the user without a password (trust), e.g.
docker run --rm -e POSTGRES_HOST_AUTH_METHOD=trust -p 5432:5432
postgres:19
Usage: python3 repro.py [host] [port] [user]
"""
import socket
import struct
import sys
host = sys.argv[1] if len(sys.argv) > 1 else '127.0.0.1'
port = int(sys.argv[2]) if len(sys.argv) > 2 else 5432
user = sys.argv[3] if len(sys.argv) > 3 else 'postgres'
def msg(tag, body=b''):
return tag + struct.pack('!I', len(body) + 4) + body
def cstr(s):
return s.encode() + b'\0'
sock = socket.create_connection((host, port))
buf = b''
def read():
global buf
while True:
if len(buf) >= 5:
n = struct.unpack('!I', buf[1:5])[0]
if len(buf) >= n + 1:
tag, body, buf = buf[:1], buf[5:n + 1], buf[n + 1:]
return tag, body
data = sock.recv(65536)
if not data:
sys.exit('connection closed')
buf += data
def until_ready(label):
print(label)
while True:
tag, body = read()
print(' ', tag.decode(), body)
if tag == b'Z' and body == b'I':
return
startup = struct.pack('!I', 3 << 16) + cstr('user') + cstr(user) + b'\0'
sock.sendall(struct.pack('!I', len(startup) + 4) + startup)
while True:
tag, body = read()
if tag == b'R' and struct.unpack('!I', body[:4])[0] != 0:
sys.exit('the server asks for a password; use trust authentication')
if tag == b'E':
sys.exit(body)
if tag == b'Z':
break
sock.sendall(msg(b'Q', cstr('CREATE TABLE b17 (a int)')))
until_ready('setup:')
# One write: an unnamed statement executed, then a simple Query, then Sync.
sock.sendall(
msg(b'P', cstr('') + cstr('INSERT INTO b17 VALUES (1)') +
struct.pack('!h', 0))
+ msg(b'B', cstr('') + cstr('') + struct.pack('!hhh', 0, 0, 0))
+ msg(b'E', cstr('') + struct.pack('!i', 0))
+ msg(b'Q', cstr('SELECT 2'))
+ msg(b'S')
)
# ParseComplete, BindComplete, CommandComplete 'INSERT 0 1',
# RowDescription, DataRow, CommandComplete, ReadyForQuery 'T',
# ReadyForQuery 'I'.
until_ready('batch:')
# The backend now shows 'idle in transaction' in pg_stat_activity.
sock.sendall(msg(b'Q', cstr('SELECT 3')))
until_ready('next Query (fails with XX000):')
sock.sendall(msg(b'Q', cstr('SELECT count(*) FROM b17')))
until_ready('the one after (succeeds; count is 0: the INSERT is gone):')
sock.sendall(msg(b'Q', cstr('DROP TABLE b17')))
until_ready('cleanup:')
sock.sendall(msg(b'X'))