Summary:
On YB PG19, reading a sequence as a table -- e.g. SELECT * FROM s, SELECT
last_value FROM s -- fails with:
ERROR: could not open file "base/<db>/<relfilenode>": No such file or directory
(nextval/setval are unaffected; they go through commands/sequence.c.) Repro:
TestPgSequencesWithCacheFlag.testSetvalAndSelect /
testSelectDirectlyFromSequenceTable.
Cause: a YB sequence's state is stored in DocDB (the sequences_data system table),
not in an on-disk relation file the way vanilla PG stores it. PG's executor still reads
a sequence relation through the buffer manager as though it had on-disk blocks, so
YB intercepts buffer reads of a RELKIND_SEQUENCE relation and fills the buffer
from the DocDB value instead of reading from disk. That interception lived only in
ReadBufferExtended. PG commit b7b0f3f27241e424b7103397489464d907cef2c4
("Use streaming I/O in sequential scans") routed heap sequential scans through the
read stream API (StartReadBuffers / WaitReadBuffers), which does not call
ReadBufferExtended and instead requests the block straight from the
storage manager (smgr) -- so a sequence scan tried to open the sequence's relfilenode
file, which YB never creates, and failed.
Fix: move the interception down to StartReadBuffersImpl, the point below both StartReadBuffer
(the ReadBufferExtended -> ReadBuffer_common path that previously held the special case) and
StartReadBuffers (the read stream path it the old interception missed).
read_stream sets operation->rel, so the relation is available there.
For a sequence relation it reserves the in-memory buffer for block 0, fills it from DocDB
via YbReadSequenceBuffer() (reusing PinBufferForBlock + ZeroAndLockBuffer,
so buffer state and locking are unchanged), and reports no I/O needed. It refills on every read,
keeping the value current. The redundant ReadBufferExtended special case is removed;
non-sequence reads are guarded by relkind and a NULL operation->rel check.
Test Plan:
Local release build on mac (release-clang-dynamic-arm64-ninja):
- org.yb.pgsql.TestPgSequencesWithCacheFlag#testSetvalAndSelect -- PASS
- org.yb.pgsql.TestPgSequencesWithCacheFlag#testSelectDirectlyFromSequenceTable -- PASS
Reviewers: jason
Reviewed By: jason
Subscribers: yql
Differential Revision: https://phorge.dev.yugabyte.com/D54892