PostgreSQL Source Code  git master
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 178 of file attmap.c.

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

◆ build_attrmap_by_name_if_req()

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

Definition at line 264 of file attmap.c.

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

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

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

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

References palloc0(), and res.

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