PGWire: an error raised while rows are sent does not skip the rest of the batch
In the extended query protocol, PostgreSQL skips every message after an error until the next Sync. The implicit transaction of the batch then rolls back.
QuestDB runs a SELECT's Execute lazily and sends its rows at Sync. When the send fails at that
point, the Executes queued after it still reply, and a DML statement among them stays committed.
Causes include a row larger than pg.send.buffer.size, a statement timeout, a cancel request,
and the query memory limit.
Reproduction (raw protocol, pg.send.buffer.size=512, table t (v INT)):
P '' "SELECT rpad('x', 2000, 'y')"; B '' ''; E '' 0;
P '' "INSERT INTO t VALUES (1)"; B '' ''; E '' 0;
SReply:
ParseComplete BindComplete
ErrorResponse "not enough space in send buffer [sendBufferSize=512, requiredSize=2011]"
ParseComplete BindComplete CommandComplete "INSERT 0 1"
ReadyForQuerySELECT count() FROM t returns 1.
Expected (PostgreSQL): ParseComplete BindComplete ErrorResponse ReadyForQuery. The INSERT does
not run and t stays empty.
Cause: PGConnectionContext.msgExecute() executes non-SELECT statements when their Execute
message arrives, and msgSync() commits the implicit transaction before syncPipeline() sends
the replies. isSkippingUntilSync() only knows about errors raised while messages are
processed, not about errors raised later, when syncPipeline() sends the rows. syncPipeline()
also keeps sending the replies of later entries after an entry reports an error
(isBatchFailed only fails an explicit transaction).
Impact: a client that pipelines a SELECT and a write in one batch can get an error for the batch while the write is still committed. A later SELECT in the batch also returns rows after the ErrorResponse. Drivers that assume the PostgreSQL skip-until-Sync rule can then report results against the wrong statement.
Verified on e756069d9527517d3ed1b76f2fdd9ee499681ed1 and on the head of PR #7680.
Location (at 7551ad4805, PR #7680 head; same logic on master e756069d95):
core/src/main/java/io/questdb/cutlass/pgwire/PGConnectionContext.java:1278msgExecute()PGConnectionContext.java:1584msgSync()(commits the implicit transaction)PGConnectionContext.java:1967syncPipeline()(sends replies; keeps going after an entry's error)PGConnectionContext.java:863isSkippingUntilSync()
Suggested fix: treat an error raised in syncPipeline() like one raised while messages are
processed. Stop sending replies for the later entries of the batch, and roll back the batch's
implicit transaction. That means committing the implicit transaction only after
syncPipeline() has sent every reply without an error.
Found while reviewing PR #7680 (branch vi_pgwire_deallocate). The PR changes nearby code but
does not change this behaviour.
Source: questdb/questdb