Skip to content

Commit 6e437ce

Browse files
committed
Make ExecGetInsertedCols() and friends more robust and improve comments.
If ExecGetInsertedCols(), ExecGetUpdatedCols() or ExecGetExtraUpdatedCols() were called with a ResultRelInfo that's not in the range table and isn't a partition routing target, the functions would dereference a NULL pointer, relinfo->ri_RootResultRelInfo. Such ResultRelInfos are created when firing RI triggers in tables that are not modified directly. None of the current callers of these functions pass such relations, so this isn't a live bug, but let's make them more robust. Also update comment in ResultRelInfo; after commit 6214e2b, ri_RangeTableIndex is zero for ResultRelInfos created for partition tuple routing. Noted by Coverity. Backpatch down to v11, like commit 6214e2b. Reviewed-by: Tom Lane, Amit Langote
1 parent 1fefe88 commit 6e437ce

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

src/backend/executor/execUtils.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1075,10 +1075,10 @@ Bitmapset *
10751075
ExecGetInsertedCols(ResultRelInfo *relinfo, EState *estate)
10761076
{
10771077
/*
1078-
* The columns are stored in the range table entry. If this ResultRelInfo
1079-
* doesn't have an entry in the range table (i.e. if it represents a
1080-
* partition routing target), fetch the parent's RTE and map the columns
1081-
* to the order they are in the partition.
1078+
* The columns are stored in the range table entry. If this ResultRelInfo
1079+
* represents a partition routing target, and doesn't have an entry of its
1080+
* own in the range table, fetch the parent's RTE and map the columns to
1081+
* the order they are in the partition.
10821082
*/
10831083
if (relinfo->ri_RangeTableIndex != 0)
10841084
{
@@ -1087,7 +1087,7 @@ ExecGetInsertedCols(ResultRelInfo *relinfo, EState *estate)
10871087

10881088
return rte->insertedCols;
10891089
}
1090-
else
1090+
else if (relinfo->ri_RootResultRelInfo)
10911091
{
10921092
ResultRelInfo *rootRelInfo = relinfo->ri_RootResultRelInfo;
10931093
RangeTblEntry *rte = rt_fetch(rootRelInfo->ri_RangeTableIndex,
@@ -1102,6 +1102,16 @@ ExecGetInsertedCols(ResultRelInfo *relinfo, EState *estate)
11021102
else
11031103
return rte->insertedCols;
11041104
}
1105+
else
1106+
{
1107+
/*
1108+
* The relation isn't in the range table and it isn't a partition
1109+
* routing target. This ResultRelInfo must've been created only for
1110+
* firing triggers and the relation is not being inserted into. (See
1111+
* ExecGetTriggerResultRel.)
1112+
*/
1113+
return NULL;
1114+
}
11051115
}
11061116

11071117
/* Return a bitmap representing columns being updated */
@@ -1116,7 +1126,7 @@ ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate)
11161126

11171127
return rte->updatedCols;
11181128
}
1119-
else
1129+
else if (relinfo->ri_RootResultRelInfo)
11201130
{
11211131
ResultRelInfo *rootRelInfo = relinfo->ri_RootResultRelInfo;
11221132
RangeTblEntry *rte = rt_fetch(rootRelInfo->ri_RangeTableIndex,
@@ -1131,4 +1141,6 @@ ExecGetUpdatedCols(ResultRelInfo *relinfo, EState *estate)
11311141
else
11321142
return rte->updatedCols;
11331143
}
1144+
else
1145+
return NULL;
11341146
}

0 commit comments

Comments
 (0)