1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-12 21:01:52 +03:00

Fix WHERE CURRENT OF when the referenced cursor uses an index-only scan.

"UPDATE/DELETE WHERE CURRENT OF cursor_name" failed, with an error message
like "cannot extract system attribute from virtual tuple", if the cursor
was using a index-only scan for the target table.  Fix it by digging the
current TID out of the indexscan state.

It seems likely that the same failure could occur for CustomScan plans
and perhaps some FDW plan types, so that leaving this to be treated as an
internal error with an obscure message isn't as good an idea as it first
seemed.  Hence, add a bit of heaptuple.c infrastructure to let us deliver
a more on-topic message.  I chose to make the message match what you get
for the case where execCurrentOf can't identify the target scan node at
all, "cursor "foo" is not a simply updatable scan of table "bar"".
Perhaps it should be different, but we can always adjust that later.

In the future, it might be nice to provide hooks that would let custom
scan providers and/or FDWs deal with this in other ways; but that's
not a suitable topic for a back-patchable bug fix.

It's been like this all along, so back-patch to all supported branches.

Yugo Nagata and Tom Lane

Discussion: https://postgr.es/m/20180201013349.937dfc5f.nagata@sraoss.co.jp
This commit is contained in:
Tom Lane
2018-03-17 14:59:31 -04:00
parent e400840b1d
commit 8f5ac44043
5 changed files with 119 additions and 18 deletions

View File

@ -1366,6 +1366,32 @@ slot_attisnull(TupleTableSlot *slot, int attnum)
return heap_attisnull(tuple, attnum);
}
/*
* slot_getsysattr
* This function fetches a system attribute of the slot's current tuple.
* Unlike slot_getattr, if the slot does not contain system attributes,
* this will return false (with a NULL attribute value) instead of
* throwing an error.
*/
bool
slot_getsysattr(TupleTableSlot *slot, int attnum,
Datum *value, bool *isnull)
{
HeapTuple tuple = slot->tts_tuple;
Assert(attnum < 0); /* else caller error */
if (tuple == NULL ||
tuple == &(slot->tts_minhdr))
{
/* No physical tuple, or minimal tuple, so fail */
*value = (Datum) 0;
*isnull = true;
return false;
}
*value = heap_getsysattr(tuple, attnum, slot->tts_tupleDescriptor, isnull);
return true;
}
/*
* heap_freetuple
*/