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 177 of file attmap.c.

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

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 263 of file attmap.c.

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

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

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

◆ 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 att = TupleDescAttr(outdesc, i);
100  Oid atttypid;
101  int32 atttypmod;
102 
103  if (att->attisdropped)
104  continue; /* attrMap->attnums[i] is already 0 */
105  noutcols++;
106  atttypid = att->atttypid;
107  atttypmod = att->atttypmod;
108  for (; j < indesc->natts; j++)
109  {
110  att = TupleDescAttr(indesc, j);
111  if (att->attisdropped)
112  continue;
113  nincols++;
114 
115  /* Found matching column, now check type */
116  if (atttypid != att->atttypid ||
117  (atttypmod != att->atttypmod && atttypmod >= 0))
118  ereport(ERROR,
119  (errcode(ERRCODE_DATATYPE_MISMATCH),
120  errmsg_internal("%s", _(msg)),
121  errdetail("Returned type %s does not match expected type %s in column %d.",
122  format_type_with_typemod(att->atttypid,
123  att->atttypmod),
124  format_type_with_typemod(atttypid,
125  atttypmod),
126  noutcols)));
127  attrMap->attnums[i] = (AttrNumber) (j + 1);
128  j++;
129  break;
130  }
131  if (attrMap->attnums[i] == 0)
132  same = false; /* we'll complain below */
133  }
134 
135  /* Check for unused input columns */
136  for (; j < indesc->natts; j++)
137  {
138  if (TupleDescAttr(indesc, j)->attisdropped)
139  continue;
140  nincols++;
141  same = false; /* we'll complain below */
142  }
143 
144  /* Report column count mismatch using the non-dropped-column counts */
145  if (!same)
146  ereport(ERROR,
147  (errcode(ERRCODE_DATATYPE_MISMATCH),
148  errmsg_internal("%s", _(msg)),
149  errdetail("Number of returned columns (%d) does not match "
150  "expected column count (%d).",
151  nincols, noutcols)));
152 
153  /* Check if the map has a one-to-one match */
154  if (check_attrmap_match(indesc, outdesc, attrMap))
155  {
156  /* Runtime conversion is not needed */
157  free_attrmap(attrMap);
158  return NULL;
159  }
160 
161  return attrMap;
162 }
int16 AttrNumber
Definition: attnum.h:21
int errmsg_internal(const char *fmt,...)
Definition: elog.c:1157
#define _(x)
Definition: elog.c:90
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:362

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

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:1347

References palloc0(), and res.

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