Skip to content

Commit 16d69ec

Browse files
committed
Remove buggy and dead code from CreateTriggerFiringOn
Here we remove some dead code from CreateTriggerFiringOn() which was attempting to find the relevant child partition index corresponding to the given indexOid. As it turned out, thanks to -Wshadow=compatible-local, this code was buggy as the code which was finding the child indexes assigned those to a shadowed variable that directly went out of scope. The code which thought it was looking at the List of child indexes was always referencing an empty List. On further investigation, this code is dead. We never call CreateTriggerFiringOn() passing a valid indexOid in a way that the function would actually ever execute the code in question. So, for lack of a way to test if a fix actually works, let's just remove the dead code instead. As a reminder, if there is ever a need to resurrect this code, an Assert() has been added to remind future feature developers that they might need to write some code to find the corresponding child index. Reported-by: Justin Pryzby Reviewed-by: Justin Pryzby Discussion: https://postgr.es/m/20220819211824.GX26426@telsasoft.com
1 parent 8c06a28 commit 16d69ec

File tree

1 file changed

+5
-39
lines changed

1 file changed

+5
-39
lines changed

src/backend/commands/trigger.c

+5-39
Original file line numberDiff line numberDiff line change
@@ -1147,8 +1147,6 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
11471147
if (partition_recurse)
11481148
{
11491149
PartitionDesc partdesc = RelationGetPartitionDesc(rel, true);
1150-
List *idxs = NIL;
1151-
List *childTbls = NIL;
11521150
int i;
11531151
MemoryContext oldcxt,
11541152
perChildCxt;
@@ -1158,53 +1156,23 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
11581156
ALLOCSET_SMALL_SIZES);
11591157

11601158
/*
1161-
* When a trigger is being created associated with an index, we'll
1162-
* need to associate the trigger in each child partition with the
1163-
* corresponding index on it.
1159+
* We don't currently expect to be called with a valid indexOid. If
1160+
* that ever changes then we'll need to quite code here to find the
1161+
* corresponding child index.
11641162
*/
1165-
if (OidIsValid(indexOid))
1166-
{
1167-
ListCell *l;
1168-
List *idxs = NIL;
1169-
1170-
idxs = find_inheritance_children(indexOid, ShareRowExclusiveLock);
1171-
foreach(l, idxs)
1172-
childTbls = lappend_oid(childTbls,
1173-
IndexGetRelation(lfirst_oid(l),
1174-
false));
1175-
}
1163+
Assert(!OidIsValid(indexOid));
11761164

11771165
oldcxt = MemoryContextSwitchTo(perChildCxt);
11781166

11791167
/* Iterate to create the trigger on each existing partition */
11801168
for (i = 0; i < partdesc->nparts; i++)
11811169
{
1182-
Oid indexOnChild = InvalidOid;
1183-
ListCell *l;
1184-
ListCell *l2;
11851170
CreateTrigStmt *childStmt;
11861171
Relation childTbl;
11871172
Node *qual;
11881173

11891174
childTbl = table_open(partdesc->oids[i], ShareRowExclusiveLock);
11901175

1191-
/* Find which of the child indexes is the one on this partition */
1192-
if (OidIsValid(indexOid))
1193-
{
1194-
forboth(l, idxs, l2, childTbls)
1195-
{
1196-
if (lfirst_oid(l2) == partdesc->oids[i])
1197-
{
1198-
indexOnChild = lfirst_oid(l);
1199-
break;
1200-
}
1201-
}
1202-
if (!OidIsValid(indexOnChild))
1203-
elog(ERROR, "failed to find index matching index \"%s\" in partition \"%s\"",
1204-
get_rel_name(indexOid),
1205-
get_rel_name(partdesc->oids[i]));
1206-
}
1207-
12081176
/*
12091177
* Initialize our fabricated parse node by copying the original
12101178
* one, then resetting fields that we pass separately.
@@ -1224,7 +1192,7 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
12241192

12251193
CreateTriggerFiringOn(childStmt, queryString,
12261194
partdesc->oids[i], refRelOid,
1227-
InvalidOid, indexOnChild,
1195+
InvalidOid, InvalidOid,
12281196
funcoid, trigoid, qual,
12291197
isInternal, true, trigger_fires_when);
12301198

@@ -1235,8 +1203,6 @@ CreateTriggerFiringOn(CreateTrigStmt *stmt, const char *queryString,
12351203

12361204
MemoryContextSwitchTo(oldcxt);
12371205
MemoryContextDelete(perChildCxt);
1238-
list_free(idxs);
1239-
list_free(childTbls);
12401206
}
12411207

12421208
/* Keep lock on target rel until end of xact */

0 commit comments

Comments
 (0)