pgsql: Be sure to rewind the tuplestore read pointer in non-leader CTES

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Be sure to rewind the tuplestore read pointer in non-leader CTES
Date: 2016-09-22 15:35:15
Message-ID: E1bn61r-0006Oh-Vw@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Be sure to rewind the tuplestore read pointer in non-leader CTEScan nodes.

ExecInitCteScan supposed that it didn't have to do anything to the extra
tuplestore read pointer it gets from tuplestore_alloc_read_pointer.
However, it needs this read pointer to be positioned at the start of the
tuplestore, while tuplestore_alloc_read_pointer is actually defined as
cloning the current position of read pointer 0. In normal situations
that accidentally works because we initialize the whole plan tree at once,
before anything gets read. But it fails in an EvalPlanQual recheck, as
illustrated in bug #14328 from Dima Pavlov. To fix, just forcibly rewind
the pointer after tuplestore_alloc_read_pointer. The cost of doing so is
negligible unless the tuplestore is already in TSS_READFILE state, which
wouldn't happen in normal cases. We could consider altering tuplestore's
API to make that case cheaper, but that would make for a more invasive
back-patch and it doesn't seem worth it.

This has been broken probably for as long as we've had CTEs, so back-patch
to all supported branches.

Discussion: <32468(dot)1474548308(at)sss(dot)pgh(dot)pa(dot)us>

Branch
------
master

Details
-------
http://git.postgresql.org/pg/commitdiff/96dd77d349424f270d129f8f40e75f762ddcca7d

Modified Files
--------------
src/backend/executor/nodeCtescan.c | 4 ++++
src/test/isolation/expected/eval-plan-qual.out | 21 +++++++++++++++++++++
src/test/isolation/specs/eval-plan-qual.spec | 17 +++++++++++++++++
3 files changed, 42 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Tom Lane 2016-09-22 15:35:16 pgsql: Be sure to rewind the tuplestore read pointer in non-leader CTES
Previous Message Peter Eisentraut 2016-09-22 12:58:44 pgsql: Add tests for various connection string issues