Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 94ec005

Browse files
committed
In INSERT/UPDATE, use the table's real tuple descriptor as target.
This back-patches commit 20d3fe9 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
1 parent 16eadc4 commit 94ec005

File tree

6 files changed

+102
-66
lines changed

6 files changed

+102
-66
lines changed

src/backend/commands/trigger.c

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ static bool GetTupleForTrigger(EState *estate,
8989
LockTupleMode lockmode,
9090
TupleTableSlot *oldslot,
9191
TupleTableSlot **newSlot);
92-
static HeapTuple MaterializeTupleForTrigger(TupleTableSlot *slot,
93-
bool *shouldFree);
9492
static bool TriggerEnabled(EState *estate, ResultRelInfo *relinfo,
9593
Trigger *trigger, TriggerEvent event,
9694
Bitmapset *modifiedCols,
@@ -3036,7 +3034,7 @@ ExecBRUpdateTriggers(EState *estate, EPQState *epqstate,
30363034
ExecCopySlot(newslot, epqslot_clean);
30373035
}
30383036

3039-
trigtuple = MaterializeTupleForTrigger(oldslot, &should_free_trig);
3037+
trigtuple = ExecFetchSlotHeapTuple(oldslot, true, &should_free_trig);
30403038
}
30413039
else
30423040
{
@@ -3404,40 +3402,6 @@ GetTupleForTrigger(EState *estate,
34043402
return true;
34053403
}
34063404

3407-
/*
3408-
* Extract a HeapTuple that we can pass off to trigger functions.
3409-
*
3410-
* We must materialize the tuple and make sure it is not dependent on any
3411-
* attrmissing data. This is needed for the old row in BEFORE UPDATE
3412-
* triggers, since they can choose to pass back this exact tuple as the update
3413-
* result, causing the tuple to be inserted into an executor slot that lacks
3414-
* the attrmissing data.
3415-
*
3416-
* Currently we don't seem to need to remove the attrmissing dependency in any
3417-
* other cases, but keep this as a separate function to simplify fixing things
3418-
* if that changes.
3419-
*/
3420-
static HeapTuple
3421-
MaterializeTupleForTrigger(TupleTableSlot *slot, bool *shouldFree)
3422-
{
3423-
HeapTuple tup;
3424-
TupleDesc tupdesc = slot->tts_tupleDescriptor;
3425-
3426-
tup = ExecFetchSlotHeapTuple(slot, true, shouldFree);
3427-
if (HeapTupleHeaderGetNatts(tup->t_data) < tupdesc->natts &&
3428-
tupdesc->constr && tupdesc->constr->missing)
3429-
{
3430-
HeapTuple newtup;
3431-
3432-
newtup = heap_expand_tuple(tup, tupdesc);
3433-
if (*shouldFree)
3434-
heap_freetuple(tup);
3435-
*shouldFree = true;
3436-
tup = newtup;
3437-
}
3438-
return tup;
3439-
}
3440-
34413405
/*
34423406
* Is trigger enabled to fire?
34433407
*/

src/backend/executor/execJunk.c

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,23 +54,48 @@
5454
*
5555
* The source targetlist is passed in. The output tuple descriptor is
5656
* built from the non-junk tlist entries.
57-
* An optional resultSlot can be passed as well.
57+
* An optional resultSlot can be passed as well; otherwise, we create one.
5858
*/
5959
JunkFilter *
6060
ExecInitJunkFilter(List *targetList, TupleTableSlot *slot)
6161
{
62-
JunkFilter *junkfilter;
6362
TupleDesc cleanTupType;
64-
int cleanLength;
65-
AttrNumber *cleanMap;
66-
ListCell *t;
67-
AttrNumber cleanResno;
6863

6964
/*
7065
* Compute the tuple descriptor for the cleaned tuple.
7166
*/
7267
cleanTupType = ExecCleanTypeFromTL(targetList);
7368

69+
/*
70+
* The rest is the same as ExecInitJunkFilterInsertion, ie, we want to map
71+
* every non-junk targetlist column into the output tuple.
72+
*/
73+
return ExecInitJunkFilterInsertion(targetList, cleanTupType, slot);
74+
}
75+
76+
/*
77+
* ExecInitJunkFilterInsertion
78+
*
79+
* Initialize a JunkFilter for insertions into a table.
80+
*
81+
* Here, we are given the target "clean" tuple descriptor rather than
82+
* inferring it from the targetlist. Although the target descriptor can
83+
* contain deleted columns, that is not of concern here, since the targetlist
84+
* should contain corresponding NULL constants (cf. ExecCheckPlanOutput).
85+
* It is assumed that the caller has checked that the table's columns match up
86+
* with the non-junk columns of the targetlist.
87+
*/
88+
JunkFilter *
89+
ExecInitJunkFilterInsertion(List *targetList,
90+
TupleDesc cleanTupType,
91+
TupleTableSlot *slot)
92+
{
93+
JunkFilter *junkfilter;
94+
int cleanLength;
95+
AttrNumber *cleanMap;
96+
ListCell *t;
97+
AttrNumber cleanResno;
98+
7499
/*
75100
* Use the given slot, or make a new slot if we weren't given one.
76101
*/
@@ -93,17 +118,18 @@ ExecInitJunkFilter(List *targetList, TupleTableSlot *slot)
93118
if (cleanLength > 0)
94119
{
95120
cleanMap = (AttrNumber *) palloc(cleanLength * sizeof(AttrNumber));
96-
cleanResno = 1;
121+
cleanResno = 0;
97122
foreach(t, targetList)
98123
{
99124
TargetEntry *tle = lfirst(t);
100125

101126
if (!tle->resjunk)
102127
{
103-
cleanMap[cleanResno - 1] = tle->resno;
128+
cleanMap[cleanResno] = tle->resno;
104129
cleanResno++;
105130
}
106131
}
132+
Assert(cleanResno == cleanLength);
107133
}
108134
else
109135
cleanMap = NULL;

src/backend/executor/nodeModifyTable.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,15 +2660,27 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags)
26602660
TupleTableSlot *junkresslot;
26612661

26622662
subplan = mtstate->mt_plans[i]->plan;
2663-
if (operation == CMD_INSERT || operation == CMD_UPDATE)
2664-
ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
2665-
subplan->targetlist);
26662663

26672664
junkresslot =
26682665
ExecInitExtraTupleSlot(estate, NULL,
26692666
table_slot_callbacks(resultRelInfo->ri_RelationDesc));
2670-
j = ExecInitJunkFilter(subplan->targetlist,
2671-
junkresslot);
2667+
2668+
/*
2669+
* For an INSERT or UPDATE, the result tuple must always match
2670+
* the target table's descriptor. For a DELETE, it won't
2671+
* (indeed, there's probably no non-junk output columns).
2672+
*/
2673+
if (operation == CMD_INSERT || operation == CMD_UPDATE)
2674+
{
2675+
ExecCheckPlanOutput(resultRelInfo->ri_RelationDesc,
2676+
subplan->targetlist);
2677+
j = ExecInitJunkFilterInsertion(subplan->targetlist,
2678+
RelationGetDescr(resultRelInfo->ri_RelationDesc),
2679+
junkresslot);
2680+
}
2681+
else
2682+
j = ExecInitJunkFilter(subplan->targetlist,
2683+
junkresslot);
26722684

26732685
if (operation == CMD_UPDATE || operation == CMD_DELETE)
26742686
{

src/include/executor/executor.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ extern void ResetTupleHashTable(TupleHashTable hashtable);
150150
*/
151151
extern JunkFilter *ExecInitJunkFilter(List *targetList,
152152
TupleTableSlot *slot);
153+
extern JunkFilter *ExecInitJunkFilterInsertion(List *targetList,
154+
TupleDesc cleanTupType,
155+
TupleTableSlot *slot);
153156
extern JunkFilter *ExecInitJunkFilterConversion(List *targetList,
154157
TupleDesc cleanTupType,
155158
TupleTableSlot *slot);

src/test/regress/expected/triggers.out

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -216,34 +216,56 @@ select * from trigtest;
216216

217217
drop table trigtest;
218218
-- Check behavior with an implicit column default, too (bug #16644)
219-
create table trigtest (a integer);
219+
create table trigtest (
220+
a integer,
221+
b bool default true not null,
222+
c text default 'xyzzy' not null);
220223
create trigger trigger_return_old
221224
before insert or delete or update on trigtest
222225
for each row execute procedure trigger_return_old();
223226
insert into trigtest values(1);
224227
select * from trigtest;
225-
a
226-
---
227-
1
228+
a | b | c
229+
---+---+-------
230+
1 | t | xyzzy
231+
(1 row)
232+
233+
alter table trigtest add column d integer default 42 not null;
234+
select * from trigtest;
235+
a | b | c | d
236+
---+---+-------+----
237+
1 | t | xyzzy | 42
238+
(1 row)
239+
240+
update trigtest set a = 2 where a = 1 returning *;
241+
a | b | c | d
242+
---+---+-------+----
243+
1 | t | xyzzy | 42
244+
(1 row)
245+
246+
select * from trigtest;
247+
a | b | c | d
248+
---+---+-------+----
249+
1 | t | xyzzy | 42
228250
(1 row)
229251

230-
alter table trigtest add column b integer default 42 not null;
252+
alter table trigtest drop column b;
231253
select * from trigtest;
232-
a | b
233-
---+----
234-
1 | 42
254+
a | c | d
255+
---+-------+----
256+
1 | xyzzy | 42
235257
(1 row)
236258

237259
update trigtest set a = 2 where a = 1 returning *;
238-
a | b
239-
---+----
240-
1 | 42
260+
a | c | d
261+
---+-------+----
262+
1 | xyzzy | 42
241263
(1 row)
242264

243265
select * from trigtest;
244-
a | b
245-
---+----
246-
1 | 42
266+
a | c | d
267+
---+-------+----
268+
1 | xyzzy | 42
247269
(1 row)
248270

249271
drop table trigtest;

src/test/regress/sql/triggers.sql

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,10 @@ select * from trigtest;
155155
drop table trigtest;
156156

157157
-- Check behavior with an implicit column default, too (bug #16644)
158-
create table trigtest (a integer);
158+
create table trigtest (
159+
a integer,
160+
b bool default true not null,
161+
c text default 'xyzzy' not null);
159162

160163
create trigger trigger_return_old
161164
before insert or delete or update on trigtest
@@ -164,7 +167,13 @@ create trigger trigger_return_old
164167
insert into trigtest values(1);
165168
select * from trigtest;
166169

167-
alter table trigtest add column b integer default 42 not null;
170+
alter table trigtest add column d integer default 42 not null;
171+
172+
select * from trigtest;
173+
update trigtest set a = 2 where a = 1 returning *;
174+
select * from trigtest;
175+
176+
alter table trigtest drop column b;
168177

169178
select * from trigtest;
170179
update trigtest set a = 2 where a = 1 returning *;

0 commit comments

Comments
 (0)