1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-17 06:41:09 +03:00

In INSERT/UPDATE, use the table's real tuple descriptor as target.

This back-patches commit 20d3fe900 into the v12 and v13 branches.
At the time I thought that commit was not fixing any observable
bug, but Bertrand Drouvot showed otherwise: adding a dropped column
to the previously-considered scenario crashes v12 and v13, unless the
dropped column happens to be an integer.  That is, of course, because
the tupdesc we derive from the plan output tlist fails to describe
the dropped column accurately, so that we'll do the wrong thing with
a tuple in which that column isn't NULL.

There is no bug in pre-v12 branches because they already did use
the table's real tuple descriptor for any trigger-returned tuple.
It seems that this set of bugs can be blamed on the changes that
removed es_trig_tuple_slot, though I've not attempted to pin that
down precisely.

Although there's no code change needed in HEAD, update the test case
to include a dropped column there too.

Discussion: https://postgr.es/m/db5d97c8-f48a-51e2-7b08-b73d5434d425@amazon.com
Discussion: https://postgr.es/m/16644-5da7ef98a7ac4545@postgresql.org
This commit is contained in:
Tom Lane
2020-11-08 13:08:36 -05:00
parent 16eadc4695
commit 94ec005f33
6 changed files with 102 additions and 66 deletions

View File

@ -89,8 +89,6 @@ static bool GetTupleForTrigger(EState *estate,
LockTupleMode lockmode,
TupleTableSlot *oldslot,
TupleTableSlot **newSlot);
static HeapTuple MaterializeTupleForTrigger(TupleTableSlot *slot,
bool *shouldFree);
static bool TriggerEnabled(EState *estate, ResultRelInfo *relinfo,
Trigger *trigger, TriggerEvent event,
Bitmapset *modifiedCols,
@ -3036,7 +3034,7 @@ ExecBRUpdateTriggers(EState *estate, EPQState *epqstate,
ExecCopySlot(newslot, epqslot_clean);
}
trigtuple = MaterializeTupleForTrigger(oldslot, &should_free_trig);
trigtuple = ExecFetchSlotHeapTuple(oldslot, true, &should_free_trig);
}
else
{
@ -3404,40 +3402,6 @@ GetTupleForTrigger(EState *estate,
return true;
}
/*
* Extract a HeapTuple that we can pass off to trigger functions.
*
* We must materialize the tuple and make sure it is not dependent on any
* attrmissing data. This is needed for the old row in BEFORE UPDATE
* triggers, since they can choose to pass back this exact tuple as the update
* result, causing the tuple to be inserted into an executor slot that lacks
* the attrmissing data.
*
* Currently we don't seem to need to remove the attrmissing dependency in any
* other cases, but keep this as a separate function to simplify fixing things
* if that changes.
*/
static HeapTuple
MaterializeTupleForTrigger(TupleTableSlot *slot, bool *shouldFree)
{
HeapTuple tup;
TupleDesc tupdesc = slot->tts_tupleDescriptor;
tup = ExecFetchSlotHeapTuple(slot, true, shouldFree);
if (HeapTupleHeaderGetNatts(tup->t_data) < tupdesc->natts &&
tupdesc->constr && tupdesc->constr->missing)
{
HeapTuple newtup;
newtup = heap_expand_tuple(tup, tupdesc);
if (*shouldFree)
heap_freetuple(tup);
*shouldFree = true;
tup = newtup;
}
return tup;
}
/*
* Is trigger enabled to fire?
*/