PostgreSQL Source Code  git master
attmap.c File Reference
#include "postgres.h"
#include "access/attmap.h"
#include "access/htup_details.h"
#include "utils/builtins.h"
Include dependency graph for attmap.c:

Go to the source code of this file.

Functions

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

Function Documentation

◆ build_attrmap_by_name()

AttrMap* build_attrmap_by_name ( TupleDesc  indesc,
TupleDesc  outdesc 
)

Definition at line 174 of file attmap.c.

References attname, AttrMap::attnums, atttypid, ereport, errcode(), errdetail(), errmsg(), ERROR, format_type_be(), i, 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(), and map_partition_varattnos().

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

◆ build_attrmap_by_name_if_req()

AttrMap* build_attrmap_by_name_if_req ( TupleDesc  indesc,
TupleDesc  outdesc 
)

Definition at line 259 of file attmap.c.

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

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

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

◆ build_attrmap_by_position()

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

Definition at line 76 of file attmap.c.

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

Referenced by convert_tuples_by_position().

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 }
void free_attrmap(AttrMap *map)
Definition: attmap.c:57
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int errcode(int sqlerrcode)
Definition: elog.c:608
unsigned int Oid
Definition: postgres_ext.h:31
signed int int32
Definition: c.h:355
Definition: attmap.h:34
#define ERROR
Definition: elog.h:43
static bool check_attrmap_match(TupleDesc indesc, TupleDesc outdesc, AttrMap *attrMap)
Definition: attmap.c:285
int errdetail(const char *fmt,...)
Definition: elog.c:955
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
Oid atttypid
Definition: pg_attribute.h:49
#define ereport(elevel, rest)
Definition: elog.h:141
char * format_type_with_typemod(Oid type_oid, int32 typemod)
Definition: format_type.c:345
int errmsg_internal(const char *fmt,...)
Definition: elog.c:909
AttrNumber * attnums
Definition: attmap.h:36
AttrMap * make_attrmap(int maplen)
Definition: attmap.c:41
int i
int16 AttrNumber
Definition: attnum.h:21
#define _(x)
Definition: elog.c:87

◆ check_attrmap_match()

static bool check_attrmap_match ( TupleDesc  indesc,
TupleDesc  outdesc,
AttrMap attrMap 
)
static

Definition at line 285 of file attmap.c.

References AttrMap::attnums, i, AttrMap::maplen, TupleDescData::natts, and TupleDescAttr.

Referenced by build_attrmap_by_name_if_req(), and build_attrmap_by_position().

288 {
289  int i;
290 
291  /* no match if attribute numbers are not the same */
292  if (indesc->natts != outdesc->natts)
293  return false;
294 
295  for (i = 0; i < attrMap->maplen; i++)
296  {
297  Form_pg_attribute inatt = TupleDescAttr(indesc, i);
298  Form_pg_attribute outatt = TupleDescAttr(outdesc, i);
299 
300  /*
301  * If the input column has a missing attribute, we need a conversion.
302  */
303  if (inatt->atthasmissing)
304  return false;
305 
306  if (attrMap->attnums[i] == (i + 1))
307  continue;
308 
309  /*
310  * If it's a dropped column and the corresponding input column is also
311  * dropped, we don't need a conversion. However, attlen and attalign
312  * must agree.
313  */
314  if (attrMap->attnums[i] == 0 &&
315  inatt->attisdropped &&
316  inatt->attlen == outatt->attlen &&
317  inatt->attalign == outatt->attalign)
318  continue;
319 
320  return false;
321  }
322 
323  return true;
324 }
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
int maplen
Definition: attmap.h:37
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
AttrNumber * attnums
Definition: attmap.h:36
int i

◆ free_attrmap()

void free_attrmap ( AttrMap map)

Definition at line 57 of file attmap.c.

References AttrMap::attnums, and pfree().

Referenced by addFkRecurseReferenced(), ATExecAttachPartitionIdx(), build_attrmap_by_name_if_req(), build_attrmap_by_position(), DefineIndex(), free_conversion_map(), and MergeAttributes().

58 {
59  pfree(map->attnums);
60  pfree(map);
61 }
void pfree(void *pointer)
Definition: mcxt.c:1056
AttrNumber * attnums
Definition: attmap.h:36

◆ make_attrmap()

AttrMap* make_attrmap ( int  maplen)

Definition at line 41 of file attmap.c.

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

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

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 }
int maplen
Definition: attmap.h:37
Definition: attmap.h:34
void * palloc0(Size size)
Definition: mcxt.c:980
AttrNumber * attnums
Definition: attmap.h:36
int16 AttrNumber
Definition: attnum.h:21