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

Commit ad86d15

Browse files
committed
Add 'missing_ok' argument to build_attrmap_by_name
When it's given as true, return a 0 in the position of the missing column rather than raising an error. This is currently unused, but it allows us to reimplement column permission checking in a subsequent commit. It seems worth breaking into a separate commit because it affects unrelated code. Author: Amit Langote <amitlangote09@gmail.com> Discussion: https://postgr.es/m/CA+HiwqFfiai=qBxPDTjaio_ZcaqUKh+FC=prESrB8ogZgFNNNQ@mail.gmail.com
1 parent 00ae5d6 commit ad86d15

File tree

10 files changed

+55
-28
lines changed

10 files changed

+55
-28
lines changed

src/backend/access/common/attmap.c

+10-4
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@ build_attrmap_by_position(TupleDesc indesc,
169169
* and output columns by name. (Dropped columns are ignored in both input and
170170
* output.) This is normally a subroutine for convert_tuples_by_name in
171171
* tupconvert.c, but can be used standalone.
172+
*
173+
* If 'missing_ok' is true, a column from 'outdesc' not being present in
174+
* 'indesc' is not flagged as an error; AttrMap.attnums[] entry for such an
175+
* outdesc column will be 0 in that case.
172176
*/
173177
AttrMap *
174178
build_attrmap_by_name(TupleDesc indesc,
175-
TupleDesc outdesc)
179+
TupleDesc outdesc,
180+
bool missing_ok)
176181
{
177182
AttrMap *attrMap;
178183
int outnatts;
@@ -235,7 +240,7 @@ build_attrmap_by_name(TupleDesc indesc,
235240
break;
236241
}
237242
}
238-
if (attrMap->attnums[i] == 0)
243+
if (attrMap->attnums[i] == 0 && !missing_ok)
239244
ereport(ERROR,
240245
(errcode(ERRCODE_DATATYPE_MISMATCH),
241246
errmsg("could not convert row type"),
@@ -257,12 +262,13 @@ build_attrmap_by_name(TupleDesc indesc,
257262
*/
258263
AttrMap *
259264
build_attrmap_by_name_if_req(TupleDesc indesc,
260-
TupleDesc outdesc)
265+
TupleDesc outdesc,
266+
bool missing_ok)
261267
{
262268
AttrMap *attrMap;
263269

264270
/* Verify compatibility and prepare attribute-number map */
265-
attrMap = build_attrmap_by_name(indesc, outdesc);
271+
attrMap = build_attrmap_by_name(indesc, outdesc, missing_ok);
266272

267273
/* Check if the map has a one-to-one match */
268274
if (check_attrmap_match(indesc, outdesc, attrMap))

src/backend/access/common/tupconvert.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ convert_tuples_by_name(TupleDesc indesc,
107107
int n = outdesc->natts;
108108

109109
/* Verify compatibility and prepare attribute-number map */
110-
attrMap = build_attrmap_by_name_if_req(indesc, outdesc);
110+
attrMap = build_attrmap_by_name_if_req(indesc, outdesc, false);
111111

112112
if (attrMap == NULL)
113113
{

src/backend/catalog/partition.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,8 @@ map_partition_varattnos(List *expr, int fromrel_varno,
227227
bool found_whole_row;
228228

229229
part_attmap = build_attrmap_by_name(RelationGetDescr(to_rel),
230-
RelationGetDescr(from_rel));
230+
RelationGetDescr(from_rel),
231+
false);
231232
expr = (List *) map_variable_attnos((Node *) expr,
232233
fromrel_varno, 0,
233234
part_attmap,

src/backend/commands/indexcmds.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1290,7 +1290,8 @@ DefineIndex(Oid relationId,
12901290
childidxs = RelationGetIndexList(childrel);
12911291
attmap =
12921292
build_attrmap_by_name(RelationGetDescr(childrel),
1293-
parentDesc);
1293+
parentDesc,
1294+
false);
12941295

12951296
foreach(cell, childidxs)
12961297
{

src/backend/commands/tablecmds.c

+16-8
Original file line numberDiff line numberDiff line change
@@ -1206,7 +1206,8 @@ DefineRelation(CreateStmt *stmt, char relkind, Oid ownerId,
12061206
}
12071207

12081208
attmap = build_attrmap_by_name(RelationGetDescr(rel),
1209-
RelationGetDescr(parent));
1209+
RelationGetDescr(parent),
1210+
false);
12101211
idxstmt =
12111212
generateClonedIndexStmt(NULL, idxRel,
12121213
attmap, &constraintOid);
@@ -9647,7 +9648,8 @@ addFkRecurseReferenced(List **wqueue, Constraint *fkconstraint, Relation rel,
96479648
* definition to match the partition's column layout.
96489649
*/
96499650
map = build_attrmap_by_name_if_req(RelationGetDescr(partRel),
9650-
RelationGetDescr(pkrel));
9651+
RelationGetDescr(pkrel),
9652+
false);
96519653
if (map)
96529654
{
96539655
mapped_pkattnum = palloc(sizeof(AttrNumber) * numfks);
@@ -9814,7 +9816,8 @@ addFkRecurseReferencing(List **wqueue, Constraint *fkconstraint, Relation rel,
98149816
CheckTableNotInUse(partition, "ALTER TABLE");
98159817

98169818
attmap = build_attrmap_by_name(RelationGetDescr(partition),
9817-
RelationGetDescr(rel));
9819+
RelationGetDescr(rel),
9820+
false);
98189821
for (int j = 0; j < numfks; j++)
98199822
mapped_fkattnum[j] = attmap->attnums[fkattnum[j] - 1];
98209823

@@ -10022,7 +10025,8 @@ CloneFkReferenced(Relation parentRel, Relation partitionRel)
1002210025
trigrel = table_open(TriggerRelationId, RowExclusiveLock);
1002310026

1002410027
attmap = build_attrmap_by_name(RelationGetDescr(partitionRel),
10025-
RelationGetDescr(parentRel));
10028+
RelationGetDescr(parentRel),
10029+
false);
1002610030
foreach(cell, clone)
1002710031
{
1002810032
Oid constrOid = lfirst_oid(cell);
@@ -10219,7 +10223,8 @@ CloneFkReferencing(List **wqueue, Relation parentRel, Relation partRel)
1021910223
* different. This map is used to convert them.
1022010224
*/
1022110225
attmap = build_attrmap_by_name(RelationGetDescr(partRel),
10222-
RelationGetDescr(parentRel));
10226+
RelationGetDescr(parentRel),
10227+
false);
1022310228

1022410229
partFKs = copyObject(RelationGetFKeyList(partRel));
1022510230

@@ -12335,7 +12340,8 @@ ATPrepAlterColumnType(List **wqueue,
1233512340
cmd = copyObject(cmd);
1233612341

1233712342
attmap = build_attrmap_by_name(RelationGetDescr(childrel),
12338-
RelationGetDescr(rel));
12343+
RelationGetDescr(rel),
12344+
false);
1233912345
((ColumnDef *) cmd->def)->cooked_default =
1234012346
map_variable_attnos(def->cooked_default,
1234112347
1, 0,
@@ -18043,7 +18049,8 @@ AttachPartitionEnsureIndexes(Relation rel, Relation attachrel)
1804318049
/* construct an indexinfo to compare existing indexes against */
1804418050
info = BuildIndexInfo(idxRel);
1804518051
attmap = build_attrmap_by_name(RelationGetDescr(attachrel),
18046-
RelationGetDescr(rel));
18052+
RelationGetDescr(rel),
18053+
false);
1804718054
constraintOid = get_relation_idx_constraint_oid(RelationGetRelid(rel), idx);
1804818055

1804918056
/*
@@ -18981,7 +18988,8 @@ ATExecAttachPartitionIdx(List **wqueue, Relation parentIdx, RangeVar *name)
1898118988
childInfo = BuildIndexInfo(partIdx);
1898218989
parentInfo = BuildIndexInfo(parentIdx);
1898318990
attmap = build_attrmap_by_name(RelationGetDescr(partTbl),
18984-
RelationGetDescr(parentTbl));
18991+
RelationGetDescr(parentTbl),
18992+
false);
1898518993
if (!CompareIndexInfo(childInfo, parentInfo,
1898618994
partIdx->rd_indcollation,
1898718995
parentIdx->rd_indcollation,

src/backend/executor/execMain.c

+7-4
Original file line numberDiff line numberDiff line change
@@ -1859,7 +1859,7 @@ ExecPartitionCheckEmitError(ResultRelInfo *resultRelInfo,
18591859

18601860
old_tupdesc = RelationGetDescr(resultRelInfo->ri_RelationDesc);
18611861
/* a reverse map */
1862-
map = build_attrmap_by_name_if_req(old_tupdesc, tupdesc);
1862+
map = build_attrmap_by_name_if_req(old_tupdesc, tupdesc, false);
18631863

18641864
/*
18651865
* Partition-specific slot's tupdesc can't be changed, so allocate a
@@ -1944,7 +1944,8 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
19441944
tupdesc = RelationGetDescr(rootrel->ri_RelationDesc);
19451945
/* a reverse map */
19461946
map = build_attrmap_by_name_if_req(orig_tupdesc,
1947-
tupdesc);
1947+
tupdesc,
1948+
false);
19481949

19491950
/*
19501951
* Partition-specific slot's tupdesc can't be changed, so
@@ -1996,7 +1997,8 @@ ExecConstraints(ResultRelInfo *resultRelInfo,
19961997
tupdesc = RelationGetDescr(rootrel->ri_RelationDesc);
19971998
/* a reverse map */
19981999
map = build_attrmap_by_name_if_req(old_tupdesc,
1999-
tupdesc);
2000+
tupdesc,
2001+
false);
20002002

20012003
/*
20022004
* Partition-specific slot's tupdesc can't be changed, so
@@ -2103,7 +2105,8 @@ ExecWithCheckOptions(WCOKind kind, ResultRelInfo *resultRelInfo,
21032105
tupdesc = RelationGetDescr(rootrel->ri_RelationDesc);
21042106
/* a reverse map */
21052107
map = build_attrmap_by_name_if_req(old_tupdesc,
2106-
tupdesc);
2108+
tupdesc,
2109+
false);
21072110

21082111
/*
21092112
* Partition-specific slot's tupdesc can't be changed,

src/backend/executor/execPartition.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -582,7 +582,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
582582
*/
583583
part_attmap =
584584
build_attrmap_by_name(RelationGetDescr(partrel),
585-
RelationGetDescr(firstResultRel));
585+
RelationGetDescr(firstResultRel),
586+
false);
586587
wcoList = (List *)
587588
map_variable_attnos((Node *) wcoList,
588589
firstVarno, 0,
@@ -639,7 +640,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
639640
if (part_attmap == NULL)
640641
part_attmap =
641642
build_attrmap_by_name(RelationGetDescr(partrel),
642-
RelationGetDescr(firstResultRel));
643+
RelationGetDescr(firstResultRel),
644+
false);
643645
returningList = (List *)
644646
map_variable_attnos((Node *) returningList,
645647
firstVarno, 0,
@@ -780,7 +782,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
780782
if (part_attmap == NULL)
781783
part_attmap =
782784
build_attrmap_by_name(RelationGetDescr(partrel),
783-
RelationGetDescr(firstResultRel));
785+
RelationGetDescr(firstResultRel),
786+
false);
784787
onconflset = (List *)
785788
map_variable_attnos((Node *) onconflset,
786789
INNER_VAR, 0,
@@ -878,7 +881,8 @@ ExecInitPartitionInfo(ModifyTableState *mtstate, EState *estate,
878881
if (part_attmap == NULL)
879882
part_attmap =
880883
build_attrmap_by_name(RelationGetDescr(partrel),
881-
RelationGetDescr(firstResultRel));
884+
RelationGetDescr(firstResultRel),
885+
false);
882886

883887
if (unlikely(!leaf_part_rri->ri_projectNewInfoValid))
884888
ExecInitMergeTupleSlots(mtstate, leaf_part_rri);
@@ -1147,7 +1151,8 @@ ExecInitPartitionDispatchInfo(EState *estate,
11471151
* routing.
11481152
*/
11491153
pd->tupmap = build_attrmap_by_name_if_req(RelationGetDescr(parent_pd->reldesc),
1150-
tupdesc);
1154+
tupdesc,
1155+
false);
11511156
pd->tupslot = pd->tupmap ?
11521157
MakeSingleTupleTableSlot(tupdesc, &TTSOpsVirtual) : NULL;
11531158
}

src/backend/parser/parse_utilcmd.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,8 @@ expandTableLikeClause(RangeVar *heapRel, TableLikeClause *table_like_clause)
12321232
* have a failure since both tables are locked.
12331233
*/
12341234
attmap = build_attrmap_by_name(RelationGetDescr(childrel),
1235-
tupleDesc);
1235+
tupleDesc,
1236+
false);
12361237

12371238
/*
12381239
* Process defaults, if required.

src/backend/replication/pgoutput/pgoutput.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1125,7 +1125,7 @@ init_tuple_slot(PGOutputData *data, Relation relation,
11251125
/* Map must live as long as the session does. */
11261126
oldctx = MemoryContextSwitchTo(CacheMemoryContext);
11271127

1128-
entry->attrmap = build_attrmap_by_name_if_req(indesc, outdesc);
1128+
entry->attrmap = build_attrmap_by_name_if_req(indesc, outdesc, false);
11291129

11301130
MemoryContextSwitchTo(oldctx);
11311131
RelationClose(ancestor);

src/include/access/attmap.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ extern void free_attrmap(AttrMap *map);
4242

4343
/* Conversion routines to build mappings */
4444
extern AttrMap *build_attrmap_by_name(TupleDesc indesc,
45-
TupleDesc outdesc);
45+
TupleDesc outdesc,
46+
bool missing_ok);
4647
extern AttrMap *build_attrmap_by_name_if_req(TupleDesc indesc,
47-
TupleDesc outdesc);
48+
TupleDesc outdesc,
49+
bool missing_ok);
4850
extern AttrMap *build_attrmap_by_position(TupleDesc indesc,
4951
TupleDesc outdesc,
5052
const char *msg);

0 commit comments

Comments
 (0)