1
0
mirror of https://github.com/postgres/postgres.git synced 2025-06-11 20:28:21 +03:00

postgres_fdw: Avoid "cursor can only scan forward" error.

Commit d844cd75a disallowed rewind in a non-scrollable cursor to resolve
anomalies arising from such a cursor operation.  However, this failed to
take into account the assumption in postgres_fdw that when rescanning a
foreign relation, it can rewind the cursor created for scanning the
foreign relation without specifying the SCROLL option, regardless of its
scrollability, causing this error when it tried to do such a rewind in a
non-scrollable cursor.  Fix by modifying postgres_fdw to instead
recreate the cursor, regardless of its scrollability, when rescanning
the foreign relation.  (If we had a way to check its scrollability, we
could improve this by rewinding it if it is scrollable and recreating it
if not, but we do not have it, so this commit modifies it to recreate it
in any case.)

Per bug #17889 from Eric Cyr.  Devrim Gunduz also reported this problem.
Back-patch to v15 where that commit enforced the prohibition.

Reviewed by Tom Lane.

Discussion: https://postgr.es/m/17889-e8c39a251d258dda%40postgresql.org
Discussion: https://postgr.es/m/b415ac3255f8352d1ea921cf3b7ba39e0587768a.camel%40gunduz.org
This commit is contained in:
Etsuro Fujita
2024-07-19 13:15:00 +09:00
parent c145f321b6
commit 5c571a34d0
3 changed files with 85 additions and 5 deletions

View File

@ -1662,9 +1662,12 @@ postgresReScanForeignScan(ForeignScanState *node)
/*
* If any internal parameters affecting this node have changed, we'd
* better destroy and recreate the cursor. Otherwise, rewinding it should
* be good enough. If we've only fetched zero or one batch, we needn't
* even rewind the cursor, just rescan what we have.
* better destroy and recreate the cursor. Otherwise, if the remote
* server is v14 or older, rewinding it should be good enough; if not,
* rewind is only allowed for scrollable cursors, but we don't have a way
* to check the scrollability of it, so destroy and recreate it in any
* case. If we've only fetched zero or one batch, we needn't even rewind
* the cursor, just rescan what we have.
*/
if (node->ss.ps.chgParam != NULL)
{
@ -1674,8 +1677,15 @@ postgresReScanForeignScan(ForeignScanState *node)
}
else if (fsstate->fetch_ct_2 > 1)
{
snprintf(sql, sizeof(sql), "MOVE BACKWARD ALL IN c%u",
fsstate->cursor_number);
if (PQserverVersion(fsstate->conn) < 150000)
snprintf(sql, sizeof(sql), "MOVE BACKWARD ALL IN c%u",
fsstate->cursor_number);
else
{
fsstate->cursor_exists = false;
snprintf(sql, sizeof(sql), "CLOSE c%u",
fsstate->cursor_number);
}
}
else
{