PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
attmap.h File Reference
#include "access/attnum.h"
#include "access/tupdesc.h"
Include dependency graph for attmap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  AttrMap
 

Typedefs

typedef struct AttrMap AttrMap
 

Functions

AttrMapmake_attrmap (int maplen)
 
void free_attrmap (AttrMap *map)
 
AttrMapbuild_attrmap_by_name (TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
 
AttrMapbuild_attrmap_by_name_if_req (TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
 
AttrMapbuild_attrmap_by_position (TupleDesc indesc, TupleDesc outdesc, const char *msg)
 

Typedef Documentation

◆ AttrMap

typedef struct AttrMap AttrMap

Function Documentation

◆ build_attrmap_by_name()

AttrMap * build_attrmap_by_name ( TupleDesc  indesc,
TupleDesc  outdesc,
bool  missing_ok 
)

Definition at line 175 of file attmap.c.

178{
179 AttrMap *attrMap;
180 int outnatts;
181 int innatts;
182 int i;
183 int nextindesc = -1;
184
185 outnatts = outdesc->natts;
186 innatts = indesc->natts;
187
188 attrMap = make_attrmap(outnatts);
189 for (i = 0; i < outnatts; i++)
190 {
191 Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
192 char *attname;
193 Oid atttypid;
194 int32 atttypmod;
195 int j;
196
197 if (outatt->attisdropped)
198 continue; /* attrMap->attnums[i] is already 0 */
199 attname = NameStr(outatt->attname);
200 atttypid = outatt->atttypid;
201 atttypmod = outatt->atttypmod;
202
203 /*
204 * Now search for an attribute with the same name in the indesc. It
205 * seems likely that a partitioned table will have the attributes in
206 * the same order as the partition, so the search below is optimized
207 * for that case. It is possible that columns are dropped in one of
208 * the relations, but not the other, so we use the 'nextindesc'
209 * counter to track the starting point of the search. If the inner
210 * loop encounters dropped columns then it will have to skip over
211 * them, but it should leave 'nextindesc' at the correct position for
212 * the next outer loop.
213 */
214 for (j = 0; j < innatts; j++)
215 {
216 Form_pg_attribute inatt;
217
218 nextindesc++;
219 if (nextindesc >= innatts)
220 nextindesc = 0;
221
222 inatt = TupleDescAttr(indesc, nextindesc);
223 if (inatt->attisdropped)
224 continue;
225 if (strcmp(attname, NameStr(inatt->attname)) == 0)
226 {
227 /* Found it, check type */
228 if (atttypid != inatt->atttypid || atttypmod != inatt->atttypmod)
230 (errcode(ERRCODE_DATATYPE_MISMATCH),
231 errmsg("could not convert row type"),
232 errdetail("Attribute \"%s\" of type %s does not match corresponding attribute of type %s.",
233 attname,
234 format_type_be(outdesc->tdtypeid),
235 format_type_be(indesc->tdtypeid))));
236 attrMap->attnums[i] = inatt->attnum;
237 break;
238 }
239 }
240 if (attrMap->attnums[i] == 0 && !missing_ok)
242 (errcode(ERRCODE_DATATYPE_MISMATCH),
243 errmsg("could not convert row type"),
244 errdetail("Attribute \"%s\" of type %s does not exist in type %s.",
245 attname,
246 format_type_be(outdesc->tdtypeid),
247 format_type_be(indesc->tdtypeid))));
248 }
249 return attrMap;
250}
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:40
#define NameStr(name)
Definition: c.h:717
int32_t int32
Definition: c.h:498
int errdetail(const char *fmt,...)
Definition: elog.c:1204
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#define ERROR
Definition: elog.h:39
#define ereport(elevel,...)
Definition: elog.h:149
char * format_type_be(Oid type_oid)
Definition: format_type.c:343
int j
Definition: isn.c:78
int i
Definition: isn.c:77
NameData attname
Definition: pg_attribute.h:41
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:202
unsigned int Oid
Definition: postgres_ext.h:30
Definition: attmap.h:35
AttrNumber * attnums
Definition: attmap.h:36
Oid tdtypeid
Definition: tupdesc.h:138
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:160

References attname, AttrMap::attnums, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), i, j, make_attrmap(), NameStr, TupleDescData::natts, TupleDescData::tdtypeid, and TupleDescAttr().

Referenced by addFkRecurseReferencing(), ATExecAttachPartitionIdx(), ATPrepAlterColumnType(), AttachPartitionEnsureIndexes(), build_attrmap_by_name_if_req(), CloneFkReferenced(), CloneFkReferencing(), DefineIndex(), DefineRelation(), ExecInitPartitionInfo(), expandTableLikeClause(), map_partition_varattnos(), MergeConstraintsIntoExisting(), and RemoveInheritance().

◆ build_attrmap_by_name_if_req()

AttrMap * build_attrmap_by_name_if_req ( TupleDesc  indesc,
TupleDesc  outdesc,
bool  missing_ok 
)

Definition at line 261 of file attmap.c.

264{
265 AttrMap *attrMap;
266
267 /* Verify compatibility and prepare attribute-number map */
268 attrMap = build_attrmap_by_name(indesc, outdesc, missing_ok);
269
270 /* Check if the map has a one-to-one match */
271 if (check_attrmap_match(indesc, outdesc, attrMap))
272 {
273 /* Runtime conversion is not needed */
274 free_attrmap(attrMap);
275 return NULL;
276 }
277
278 return attrMap;
279}
void free_attrmap(AttrMap *map)
Definition: attmap.c:56
AttrMap * build_attrmap_by_name(TupleDesc indesc, TupleDesc outdesc, bool missing_ok)
Definition: attmap.c:175
static bool check_attrmap_match(TupleDesc indesc, TupleDesc outdesc, AttrMap *attrMap)
Definition: attmap.c:288

References build_attrmap_by_name(), check_attrmap_match(), and free_attrmap().

Referenced by addFkRecurseReferenced(), convert_tuples_by_name(), ExecConstraints(), ExecGetRootToChildMap(), ExecInitPartitionDispatchInfo(), ExecPartitionCheckEmitError(), ExecWithCheckOptions(), init_tuple_slot(), and ReportNotNullViolationError().

◆ build_attrmap_by_position()

AttrMap * build_attrmap_by_position ( TupleDesc  indesc,
TupleDesc  outdesc,
const char *  msg 
)

Definition at line 75 of file attmap.c.

78{
79 AttrMap *attrMap;
80 int nincols;
81 int noutcols;
82 int n;
83 int i;
84 int j;
85 bool same;
86
87 /*
88 * The length is computed as the number of attributes of the expected
89 * rowtype as it includes dropped attributes in its count.
90 */
91 n = outdesc->natts;
92 attrMap = make_attrmap(n);
93
94 j = 0; /* j is next physical input attribute */
95 nincols = noutcols = 0; /* these count non-dropped attributes */
96 same = true;
97 for (i = 0; i < n; i++)
98 {
99 Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
100
101 if (outatt->attisdropped)
102 continue; /* attrMap->attnums[i] is already 0 */
103 noutcols++;
104 for (; j < indesc->natts; j++)
105 {
106 Form_pg_attribute inatt = TupleDescAttr(indesc, j);
107
108 if (inatt->attisdropped)
109 continue;
110 nincols++;
111
112 /* Found matching column, now check type */
113 if (outatt->atttypid != inatt->atttypid ||
114 (outatt->atttypmod != inatt->atttypmod && outatt->atttypmod >= 0))
116 (errcode(ERRCODE_DATATYPE_MISMATCH),
117 errmsg_internal("%s", _(msg)),
118 errdetail("Returned type %s does not match expected type %s in column \"%s\" (position %d).",
119 format_type_with_typemod(inatt->atttypid,
120 inatt->atttypmod),
121 format_type_with_typemod(outatt->atttypid,
122 outatt->atttypmod),
123 NameStr(outatt->attname),
124 noutcols)));
125 attrMap->attnums[i] = (AttrNumber) (j + 1);
126 j++;
127 break;
128 }
129 if (attrMap->attnums[i] == 0)
130 same = false; /* we'll complain below */
131 }
132
133 /* Check for unused input columns */
134 for (; j < indesc->natts; j++)
135 {
136 if (TupleDescCompactAttr(indesc, j)->attisdropped)
137 continue;
138 nincols++;
139 same = false; /* we'll complain below */
140 }
141
142 /* Report column count mismatch using the non-dropped-column counts */
143 if (!same)
145 (errcode(ERRCODE_DATATYPE_MISMATCH),
146 errmsg_internal("%s", _(msg)),
147 errdetail("Number of returned columns (%d) does not match "
148 "expected column count (%d).",
149 nincols, noutcols)));
150
151 /* Check if the map has a one-to-one match */
152 if (check_attrmap_match(indesc, outdesc, attrMap))
153 {
154 /* Runtime conversion is not needed */
155 free_attrmap(attrMap);
156 return NULL;
157 }
158
159 return attrMap;
160}
int16 AttrNumber
Definition: attnum.h:21
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1158
#define _(x)
Definition: elog.c:91
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:362
bool attisdropped
Definition: tupdesc.h:77
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:175

References _, CompactAttribute::attisdropped, AttrMap::attnums, check_attrmap_match(), ereport, errcode(), errdetail(), errmsg_internal(), ERROR, format_type_with_typemod(), free_attrmap(), i, j, make_attrmap(), NameStr, TupleDescData::natts, TupleDescAttr(), and TupleDescCompactAttr().

Referenced by convert_tuples_by_position().

◆ free_attrmap()

◆ make_attrmap()

AttrMap * make_attrmap ( int  maplen)

Definition at line 40 of file attmap.c.

41{
42 AttrMap *res;
43
44 res = (AttrMap *) palloc0(sizeof(AttrMap));
45 res->maplen = maplen;
46 res->attnums = (AttrNumber *) palloc0(sizeof(AttrNumber) * maplen);
47 return res;
48}
void * palloc0(Size size)
Definition: mcxt.c:1973
int maplen
Definition: attmap.h:37

References AttrMap::attnums, AttrMap::maplen, and palloc0().

Referenced by build_attrmap_by_name(), build_attrmap_by_position(), logicalrep_partition_open(), logicalrep_rel_open(), and MergeAttributes().