PostgreSQL Source Code  git master
tupdesc.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * tupdesc.c
4  * POSTGRES tuple descriptor support code
5  *
6  * Portions Copyright (c) 1996-2018, PostgreSQL Global Development Group
7  * Portions Copyright (c) 1994, Regents of the University of California
8  *
9  *
10  * IDENTIFICATION
11  * src/backend/access/common/tupdesc.c
12  *
13  * NOTES
14  * some of the executor utility code such as "ExecTypeFromTL" should be
15  * moved here.
16  *
17  *-------------------------------------------------------------------------
18  */
19 
20 #include "postgres.h"
21 
22 #include "access/hash.h"
23 #include "access/htup_details.h"
24 #include "catalog/pg_collation.h"
25 #include "catalog/pg_type.h"
26 #include "miscadmin.h"
27 #include "parser/parse_type.h"
28 #include "utils/acl.h"
29 #include "utils/builtins.h"
30 #include "utils/hashutils.h"
31 #include "utils/resowner_private.h"
32 #include "utils/syscache.h"
33 
34 
35 /*
36  * CreateTemplateTupleDesc
37  * This function allocates an empty tuple descriptor structure.
38  *
39  * Tuple type ID information is initially set for an anonymous record type;
40  * caller can overwrite this if needed.
41  */
43 CreateTemplateTupleDesc(int natts, bool hasoid)
44 {
45  TupleDesc desc;
46 
47  /*
48  * sanity checks
49  */
50  AssertArg(natts >= 0);
51 
52  /*
53  * Allocate enough memory for the tuple descriptor, including the
54  * attribute rows.
55  *
56  * Note: the attribute array stride is sizeof(FormData_pg_attribute),
57  * since we declare the array elements as FormData_pg_attribute for
58  * notational convenience. However, we only guarantee that the first
59  * ATTRIBUTE_FIXED_PART_SIZE bytes of each entry are valid; most code that
60  * copies tupdesc entries around copies just that much. In principle that
61  * could be less due to trailing padding, although with the current
62  * definition of pg_attribute there probably isn't any padding.
63  */
64  desc = (TupleDesc) palloc(offsetof(struct tupleDesc, attrs) +
65  natts * sizeof(FormData_pg_attribute));
66 
67  /*
68  * Initialize other fields of the tupdesc.
69  */
70  desc->natts = natts;
71  desc->constr = NULL;
72  desc->tdtypeid = RECORDOID;
73  desc->tdtypmod = -1;
74  desc->tdhasoid = hasoid;
75  desc->tdrefcount = -1; /* assume not reference-counted */
76 
77  return desc;
78 }
79 
80 /*
81  * CreateTupleDesc
82  * This function allocates a new TupleDesc by copying a given
83  * Form_pg_attribute array.
84  *
85  * Tuple type ID information is initially set for an anonymous record type;
86  * caller can overwrite this if needed.
87  */
89 CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
90 {
91  TupleDesc desc;
92  int i;
93 
94  desc = CreateTemplateTupleDesc(natts, hasoid);
95 
96  for (i = 0; i < natts; ++i)
97  memcpy(TupleDescAttr(desc, i), attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
98 
99  return desc;
100 }
101 
102 /*
103  * CreateTupleDescCopy
104  * This function creates a new TupleDesc by copying from an existing
105  * TupleDesc.
106  *
107  * !!! Constraints and defaults are not copied !!!
108  */
109 TupleDesc
111 {
112  TupleDesc desc;
113  int i;
114 
115  desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);
116 
117  /* Flat-copy the attribute array */
118  memcpy(TupleDescAttr(desc, 0),
119  TupleDescAttr(tupdesc, 0),
120  desc->natts * sizeof(FormData_pg_attribute));
121 
122  /*
123  * Since we're not copying constraints and defaults, clear fields
124  * associated with them.
125  */
126  for (i = 0; i < desc->natts; i++)
127  {
128  Form_pg_attribute att = TupleDescAttr(desc, i);
129 
130  att->attnotnull = false;
131  att->atthasdef = false;
132  att->attidentity = '\0';
133  }
134 
135  /* We can copy the tuple type identification, too */
136  desc->tdtypeid = tupdesc->tdtypeid;
137  desc->tdtypmod = tupdesc->tdtypmod;
138 
139  return desc;
140 }
141 
142 /*
143  * CreateTupleDescCopyConstr
144  * This function creates a new TupleDesc by copying from an existing
145  * TupleDesc (including its constraints and defaults).
146  */
147 TupleDesc
149 {
150  TupleDesc desc;
151  TupleConstr *constr = tupdesc->constr;
152  int i;
153 
154  desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);
155 
156  /* Flat-copy the attribute array */
157  memcpy(TupleDescAttr(desc, 0),
158  TupleDescAttr(tupdesc, 0),
159  desc->natts * sizeof(FormData_pg_attribute));
160 
161  /* Copy the TupleConstr data structure, if any */
162  if (constr)
163  {
164  TupleConstr *cpy = (TupleConstr *) palloc0(sizeof(TupleConstr));
165 
166  cpy->has_not_null = constr->has_not_null;
167 
168  if ((cpy->num_defval = constr->num_defval) > 0)
169  {
170  cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));
171  memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
172  for (i = cpy->num_defval - 1; i >= 0; i--)
173  {
174  if (constr->defval[i].adbin)
175  cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
176  }
177  }
178 
179  if ((cpy->num_check = constr->num_check) > 0)
180  {
181  cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));
182  memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
183  for (i = cpy->num_check - 1; i >= 0; i--)
184  {
185  if (constr->check[i].ccname)
186  cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
187  if (constr->check[i].ccbin)
188  cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
189  cpy->check[i].ccvalid = constr->check[i].ccvalid;
190  cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
191  }
192  }
193 
194  desc->constr = cpy;
195  }
196 
197  /* We can copy the tuple type identification, too */
198  desc->tdtypeid = tupdesc->tdtypeid;
199  desc->tdtypmod = tupdesc->tdtypmod;
200 
201  return desc;
202 }
203 
204 /*
205  * TupleDescCopy
206  * Copy a tuple descriptor into caller-supplied memory.
207  * The memory may be shared memory mapped at any address, and must
208  * be sufficient to hold TupleDescSize(src) bytes.
209  *
210  * !!! Constraints and defaults are not copied !!!
211  */
212 void
214 {
215  int i;
216 
217  /* Flat-copy the header and attribute array */
218  memcpy(dst, src, TupleDescSize(src));
219 
220  /*
221  * Since we're not copying constraints and defaults, clear fields
222  * associated with them.
223  */
224  for (i = 0; i < dst->natts; i++)
225  {
226  Form_pg_attribute att = TupleDescAttr(dst, i);
227 
228  att->attnotnull = false;
229  att->atthasdef = false;
230  att->attidentity = '\0';
231  }
232  dst->constr = NULL;
233 
234  /*
235  * Also, assume the destination is not to be ref-counted. (Copying the
236  * source's refcount would be wrong in any case.)
237  */
238  dst->tdrefcount = -1;
239 }
240 
241 /*
242  * TupleDescCopyEntry
243  * This function copies a single attribute structure from one tuple
244  * descriptor to another.
245  *
246  * !!! Constraints and defaults are not copied !!!
247  */
248 void
250  TupleDesc src, AttrNumber srcAttno)
251 {
252  Form_pg_attribute dstAtt = TupleDescAttr(dst, dstAttno - 1);
253  Form_pg_attribute srcAtt = TupleDescAttr(src, srcAttno - 1);
254 
255  /*
256  * sanity checks
257  */
260  AssertArg(srcAttno >= 1);
261  AssertArg(srcAttno <= src->natts);
262  AssertArg(dstAttno >= 1);
263  AssertArg(dstAttno <= dst->natts);
264 
265  memcpy(dstAtt, srcAtt, ATTRIBUTE_FIXED_PART_SIZE);
266 
267  /*
268  * Aside from updating the attno, we'd better reset attcacheoff.
269  *
270  * XXX Actually, to be entirely safe we'd need to reset the attcacheoff of
271  * all following columns in dst as well. Current usage scenarios don't
272  * require that though, because all following columns will get initialized
273  * by other uses of this function or TupleDescInitEntry. So we cheat a
274  * bit to avoid a useless O(N^2) penalty.
275  */
276  dstAtt->attnum = dstAttno;
277  dstAtt->attcacheoff = -1;
278 
279  /* since we're not copying constraints or defaults, clear these */
280  dstAtt->attnotnull = false;
281  dstAtt->atthasdef = false;
282  dstAtt->attidentity = '\0';
283 }
284 
285 /*
286  * Free a TupleDesc including all substructure
287  */
288 void
290 {
291  int i;
292 
293  /*
294  * Possibly this should assert tdrefcount == 0, to disallow explicit
295  * freeing of un-refcounted tupdescs?
296  */
297  Assert(tupdesc->tdrefcount <= 0);
298 
299  if (tupdesc->constr)
300  {
301  if (tupdesc->constr->num_defval > 0)
302  {
303  AttrDefault *attrdef = tupdesc->constr->defval;
304 
305  for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
306  {
307  if (attrdef[i].adbin)
308  pfree(attrdef[i].adbin);
309  }
310  pfree(attrdef);
311  }
312  if (tupdesc->constr->num_check > 0)
313  {
314  ConstrCheck *check = tupdesc->constr->check;
315 
316  for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
317  {
318  if (check[i].ccname)
319  pfree(check[i].ccname);
320  if (check[i].ccbin)
321  pfree(check[i].ccbin);
322  }
323  pfree(check);
324  }
325  pfree(tupdesc->constr);
326  }
327 
328  pfree(tupdesc);
329 }
330 
331 /*
332  * Increment the reference count of a tupdesc, and log the reference in
333  * CurrentResourceOwner.
334  *
335  * Do not apply this to tupdescs that are not being refcounted. (Use the
336  * macro PinTupleDesc for tupdescs of uncertain status.)
337  */
338 void
340 {
341  Assert(tupdesc->tdrefcount >= 0);
342 
344  tupdesc->tdrefcount++;
346 }
347 
348 /*
349  * Decrement the reference count of a tupdesc, remove the corresponding
350  * reference from CurrentResourceOwner, and free the tupdesc if no more
351  * references remain.
352  *
353  * Do not apply this to tupdescs that are not being refcounted. (Use the
354  * macro ReleaseTupleDesc for tupdescs of uncertain status.)
355  */
356 void
358 {
359  Assert(tupdesc->tdrefcount > 0);
360 
362  if (--tupdesc->tdrefcount == 0)
363  FreeTupleDesc(tupdesc);
364 }
365 
366 /*
367  * Compare two TupleDesc structures for logical equality
368  *
369  * Note: we deliberately do not check the attrelid and tdtypmod fields.
370  * This allows typcache.c to use this routine to see if a cached record type
371  * matches a requested type, and is harmless for relcache.c's uses.
372  * We don't compare tdrefcount, either.
373  */
374 bool
376 {
377  int i,
378  j,
379  n;
380 
381  if (tupdesc1->natts != tupdesc2->natts)
382  return false;
383  if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
384  return false;
385  if (tupdesc1->tdhasoid != tupdesc2->tdhasoid)
386  return false;
387 
388  for (i = 0; i < tupdesc1->natts; i++)
389  {
390  Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
391  Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
392 
393  /*
394  * We do not need to check every single field here: we can disregard
395  * attrelid and attnum (which were used to place the row in the attrs
396  * array in the first place). It might look like we could dispense
397  * with checking attlen/attbyval/attalign, since these are derived
398  * from atttypid; but in the case of dropped columns we must check
399  * them (since atttypid will be zero for all dropped columns) and in
400  * general it seems safer to check them always.
401  *
402  * attcacheoff must NOT be checked since it's possibly not set in both
403  * copies.
404  */
405  if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
406  return false;
407  if (attr1->atttypid != attr2->atttypid)
408  return false;
409  if (attr1->attstattarget != attr2->attstattarget)
410  return false;
411  if (attr1->attlen != attr2->attlen)
412  return false;
413  if (attr1->attndims != attr2->attndims)
414  return false;
415  if (attr1->atttypmod != attr2->atttypmod)
416  return false;
417  if (attr1->attbyval != attr2->attbyval)
418  return false;
419  if (attr1->attstorage != attr2->attstorage)
420  return false;
421  if (attr1->attalign != attr2->attalign)
422  return false;
423  if (attr1->attnotnull != attr2->attnotnull)
424  return false;
425  if (attr1->atthasdef != attr2->atthasdef)
426  return false;
427  if (attr1->attidentity != attr2->attidentity)
428  return false;
429  if (attr1->attisdropped != attr2->attisdropped)
430  return false;
431  if (attr1->attislocal != attr2->attislocal)
432  return false;
433  if (attr1->attinhcount != attr2->attinhcount)
434  return false;
435  if (attr1->attcollation != attr2->attcollation)
436  return false;
437  /* attacl, attoptions and attfdwoptions are not even present... */
438  }
439 
440  if (tupdesc1->constr != NULL)
441  {
442  TupleConstr *constr1 = tupdesc1->constr;
443  TupleConstr *constr2 = tupdesc2->constr;
444 
445  if (constr2 == NULL)
446  return false;
447  if (constr1->has_not_null != constr2->has_not_null)
448  return false;
449  n = constr1->num_defval;
450  if (n != (int) constr2->num_defval)
451  return false;
452  for (i = 0; i < n; i++)
453  {
454  AttrDefault *defval1 = constr1->defval + i;
455  AttrDefault *defval2 = constr2->defval;
456 
457  /*
458  * We can't assume that the items are always read from the system
459  * catalogs in the same order; so use the adnum field to identify
460  * the matching item to compare.
461  */
462  for (j = 0; j < n; defval2++, j++)
463  {
464  if (defval1->adnum == defval2->adnum)
465  break;
466  }
467  if (j >= n)
468  return false;
469  if (strcmp(defval1->adbin, defval2->adbin) != 0)
470  return false;
471  }
472  n = constr1->num_check;
473  if (n != (int) constr2->num_check)
474  return false;
475  for (i = 0; i < n; i++)
476  {
477  ConstrCheck *check1 = constr1->check + i;
478  ConstrCheck *check2 = constr2->check;
479 
480  /*
481  * Similarly, don't assume that the checks are always read in the
482  * same order; match them up by name and contents. (The name
483  * *should* be unique, but...)
484  */
485  for (j = 0; j < n; check2++, j++)
486  {
487  if (strcmp(check1->ccname, check2->ccname) == 0 &&
488  strcmp(check1->ccbin, check2->ccbin) == 0 &&
489  check1->ccvalid == check2->ccvalid &&
490  check1->ccnoinherit == check2->ccnoinherit)
491  break;
492  }
493  if (j >= n)
494  return false;
495  }
496  }
497  else if (tupdesc2->constr != NULL)
498  return false;
499  return true;
500 }
501 
502 /*
503  * hashTupleDesc
504  * Compute a hash value for a tuple descriptor.
505  *
506  * If two tuple descriptors would be considered equal by equalTupleDescs()
507  * then their hash value will be equal according to this function.
508  *
509  * Note that currently contents of constraint are not hashed - it'd be a bit
510  * painful to do so, and conflicts just due to constraints are unlikely.
511  */
512 uint32
514 {
515  uint32 s;
516  int i;
517 
518  s = hash_combine(0, hash_uint32(desc->natts));
519  s = hash_combine(s, hash_uint32(desc->tdtypeid));
520  s = hash_combine(s, hash_uint32(desc->tdhasoid));
521  for (i = 0; i < desc->natts; ++i)
522  s = hash_combine(s, hash_uint32(TupleDescAttr(desc, i)->atttypid));
523 
524  return s;
525 }
526 
527 /*
528  * TupleDescInitEntry
529  * This function initializes a single attribute structure in
530  * a previously allocated tuple descriptor.
531  *
532  * If attributeName is NULL, the attname field is set to an empty string
533  * (this is for cases where we don't know or need a name for the field).
534  * Also, some callers use this function to change the datatype-related fields
535  * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
536  * to indicate that the attname field shouldn't be modified.
537  *
538  * Note that attcollation is set to the default for the specified datatype.
539  * If a nondefault collation is needed, insert it afterwards using
540  * TupleDescInitEntryCollation.
541  */
542 void
544  AttrNumber attributeNumber,
545  const char *attributeName,
546  Oid oidtypeid,
547  int32 typmod,
548  int attdim)
549 {
550  HeapTuple tuple;
551  Form_pg_type typeForm;
552  Form_pg_attribute att;
553 
554  /*
555  * sanity checks
556  */
557  AssertArg(PointerIsValid(desc));
558  AssertArg(attributeNumber >= 1);
559  AssertArg(attributeNumber <= desc->natts);
560 
561  /*
562  * initialize the attribute fields
563  */
564  att = TupleDescAttr(desc, attributeNumber - 1);
565 
566  att->attrelid = 0; /* dummy value */
567 
568  /*
569  * Note: attributeName can be NULL, because the planner doesn't always
570  * fill in valid resname values in targetlists, particularly for resjunk
571  * attributes. Also, do nothing if caller wants to re-use the old attname.
572  */
573  if (attributeName == NULL)
574  MemSet(NameStr(att->attname), 0, NAMEDATALEN);
575  else if (attributeName != NameStr(att->attname))
576  namestrcpy(&(att->attname), attributeName);
577 
578  att->attstattarget = -1;
579  att->attcacheoff = -1;
580  att->atttypmod = typmod;
581 
582  att->attnum = attributeNumber;
583  att->attndims = attdim;
584 
585  att->attnotnull = false;
586  att->atthasdef = false;
587  att->attidentity = '\0';
588  att->attisdropped = false;
589  att->attislocal = true;
590  att->attinhcount = 0;
591  /* attacl, attoptions and attfdwoptions are not present in tupledescs */
592 
593  tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
594  if (!HeapTupleIsValid(tuple))
595  elog(ERROR, "cache lookup failed for type %u", oidtypeid);
596  typeForm = (Form_pg_type) GETSTRUCT(tuple);
597 
598  att->atttypid = oidtypeid;
599  att->attlen = typeForm->typlen;
600  att->attbyval = typeForm->typbyval;
601  att->attalign = typeForm->typalign;
602  att->attstorage = typeForm->typstorage;
603  att->attcollation = typeForm->typcollation;
604 
605  ReleaseSysCache(tuple);
606 }
607 
608 /*
609  * TupleDescInitBuiltinEntry
610  * Initialize a tuple descriptor without catalog access. Only
611  * a limited range of builtin types are supported.
612  */
613 void
615  AttrNumber attributeNumber,
616  const char *attributeName,
617  Oid oidtypeid,
618  int32 typmod,
619  int attdim)
620 {
621  Form_pg_attribute att;
622 
623  /* sanity checks */
624  AssertArg(PointerIsValid(desc));
625  AssertArg(attributeNumber >= 1);
626  AssertArg(attributeNumber <= desc->natts);
627 
628  /* initialize the attribute fields */
629  att = TupleDescAttr(desc, attributeNumber - 1);
630  att->attrelid = 0; /* dummy value */
631 
632  /* unlike TupleDescInitEntry, we require an attribute name */
633  Assert(attributeName != NULL);
634  namestrcpy(&(att->attname), attributeName);
635 
636  att->attstattarget = -1;
637  att->attcacheoff = -1;
638  att->atttypmod = typmod;
639 
640  att->attnum = attributeNumber;
641  att->attndims = attdim;
642 
643  att->attnotnull = false;
644  att->atthasdef = false;
645  att->attidentity = '\0';
646  att->attisdropped = false;
647  att->attislocal = true;
648  att->attinhcount = 0;
649  /* attacl, attoptions and attfdwoptions are not present in tupledescs */
650 
651  att->atttypid = oidtypeid;
652 
653  /*
654  * Our goal here is to support just enough types to let basic builtin
655  * commands work without catalog access - e.g. so that we can do certain
656  * things even in processes that are not connected to a database.
657  */
658  switch (oidtypeid)
659  {
660  case TEXTOID:
661  case TEXTARRAYOID:
662  att->attlen = -1;
663  att->attbyval = false;
664  att->attalign = 'i';
665  att->attstorage = 'x';
666  att->attcollation = DEFAULT_COLLATION_OID;
667  break;
668 
669  case BOOLOID:
670  att->attlen = 1;
671  att->attbyval = true;
672  att->attalign = 'c';
673  att->attstorage = 'p';
674  att->attcollation = InvalidOid;
675  break;
676 
677  case INT4OID:
678  att->attlen = 4;
679  att->attbyval = true;
680  att->attalign = 'i';
681  att->attstorage = 'p';
682  att->attcollation = InvalidOid;
683  break;
684 
685  case INT8OID:
686  att->attlen = 8;
687  att->attbyval = FLOAT8PASSBYVAL;
688  att->attalign = 'd';
689  att->attstorage = 'p';
690  att->attcollation = InvalidOid;
691  break;
692  }
693 }
694 
695 /*
696  * TupleDescInitEntryCollation
697  *
698  * Assign a nondefault collation to a previously initialized tuple descriptor
699  * entry.
700  */
701 void
703  AttrNumber attributeNumber,
704  Oid collationid)
705 {
706  /*
707  * sanity checks
708  */
709  AssertArg(PointerIsValid(desc));
710  AssertArg(attributeNumber >= 1);
711  AssertArg(attributeNumber <= desc->natts);
712 
713  TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
714 }
715 
716 
717 /*
718  * BuildDescForRelation
719  *
720  * Given a relation schema (list of ColumnDef nodes), build a TupleDesc.
721  *
722  * Note: the default assumption is no OIDs; caller may modify the returned
723  * TupleDesc if it wants OIDs. Also, tdtypeid will need to be filled in
724  * later on.
725  */
726 TupleDesc
728 {
729  int natts;
730  AttrNumber attnum;
731  ListCell *l;
732  TupleDesc desc;
733  bool has_not_null;
734  char *attname;
735  Oid atttypid;
736  int32 atttypmod;
737  Oid attcollation;
738  int attdim;
739 
740  /*
741  * allocate a new tuple descriptor
742  */
743  natts = list_length(schema);
744  desc = CreateTemplateTupleDesc(natts, false);
745  has_not_null = false;
746 
747  attnum = 0;
748 
749  foreach(l, schema)
750  {
751  ColumnDef *entry = lfirst(l);
752  AclResult aclresult;
753  Form_pg_attribute att;
754 
755  /*
756  * for each entry in the list, get the name and type information from
757  * the list and have TupleDescInitEntry fill in the attribute
758  * information we need.
759  */
760  attnum++;
761 
762  attname = entry->colname;
763  typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod);
764 
765  aclresult = pg_type_aclcheck(atttypid, GetUserId(), ACL_USAGE);
766  if (aclresult != ACLCHECK_OK)
767  aclcheck_error_type(aclresult, atttypid);
768 
769  attcollation = GetColumnDefCollation(NULL, entry, atttypid);
770  attdim = list_length(entry->typeName->arrayBounds);
771 
772  if (entry->typeName->setof)
773  ereport(ERROR,
774  (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
775  errmsg("column \"%s\" cannot be declared SETOF",
776  attname)));
777 
778  TupleDescInitEntry(desc, attnum, attname,
779  atttypid, atttypmod, attdim);
780  att = TupleDescAttr(desc, attnum - 1);
781 
782  /* Override TupleDescInitEntry's settings as requested */
783  TupleDescInitEntryCollation(desc, attnum, attcollation);
784  if (entry->storage)
785  att->attstorage = entry->storage;
786 
787  /* Fill in additional stuff not handled by TupleDescInitEntry */
788  att->attnotnull = entry->is_not_null;
789  has_not_null |= entry->is_not_null;
790  att->attislocal = entry->is_local;
791  att->attinhcount = entry->inhcount;
792  }
793 
794  if (has_not_null)
795  {
796  TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
797 
798  constr->has_not_null = true;
799  constr->defval = NULL;
800  constr->num_defval = 0;
801  constr->check = NULL;
802  constr->num_check = 0;
803  desc->constr = constr;
804  }
805  else
806  {
807  desc->constr = NULL;
808  }
809 
810  return desc;
811 }
812 
813 /*
814  * BuildDescFromLists
815  *
816  * Build a TupleDesc given lists of column names (as String nodes),
817  * column type OIDs, typmods, and collation OIDs.
818  *
819  * No constraints are generated.
820  *
821  * This is essentially a cut-down version of BuildDescForRelation for use
822  * with functions returning RECORD.
823  */
824 TupleDesc
825 BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
826 {
827  int natts;
828  AttrNumber attnum;
829  ListCell *l1;
830  ListCell *l2;
831  ListCell *l3;
832  ListCell *l4;
833  TupleDesc desc;
834 
835  natts = list_length(names);
836  Assert(natts == list_length(types));
837  Assert(natts == list_length(typmods));
838  Assert(natts == list_length(collations));
839 
840  /*
841  * allocate a new tuple descriptor
842  */
843  desc = CreateTemplateTupleDesc(natts, false);
844 
845  attnum = 0;
846 
847  l2 = list_head(types);
848  l3 = list_head(typmods);
849  l4 = list_head(collations);
850  foreach(l1, names)
851  {
852  char *attname = strVal(lfirst(l1));
853  Oid atttypid;
854  int32 atttypmod;
855  Oid attcollation;
856 
857  atttypid = lfirst_oid(l2);
858  l2 = lnext(l2);
859  atttypmod = lfirst_int(l3);
860  l3 = lnext(l3);
861  attcollation = lfirst_oid(l4);
862  l4 = lnext(l4);
863 
864  attnum++;
865 
866  TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
867  TupleDescInitEntryCollation(desc, attnum, attcollation);
868  }
869 
870  return desc;
871 }
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:339
static uint32 hash_combine(uint32 a, uint32 b)
Definition: hashutils.h:17
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition: tupdesc.c:110
Oid tdtypeid
Definition: tupdesc.h:80
#define GETSTRUCT(TUP)
Definition: htup_details.h:661
char * ccname
Definition: tupdesc.h:30
bool tdhasoid
Definition: tupdesc.h:82
TupleDesc BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
Definition: tupdesc.c:825
char storage
Definition: parsenodes.h:652
struct typedefs * types
Definition: ecpg.c:29
bool is_local
Definition: parsenodes.h:648
bool ccnoinherit
Definition: tupdesc.h:33
Oid GetUserId(void)
Definition: miscinit.c:284
#define TEXTOID
Definition: pg_type.h:324
ConstrCheck * check
Definition: tupdesc.h:40
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:90
ResourceOwner CurrentResourceOwner
Definition: resowner.c:138
char * pstrdup(const char *in)
Definition: mcxt.c:1063
char * ccbin
Definition: tupdesc.h:31
#define INT4OID
Definition: pg_type.h:316
#define strVal(v)
Definition: value.h:54
int errcode(int sqlerrcode)
Definition: elog.c:575
#define MemSet(start, val, len)
Definition: c.h:897
uint32 hashTupleDesc(TupleDesc desc)
Definition: tupdesc.c:513
bool is_not_null
Definition: parsenodes.h:649
FormData_pg_type * Form_pg_type
Definition: pg_type.h:233
TupleDesc CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
Definition: tupdesc.c:89
unsigned int Oid
Definition: postgres_ext.h:31
int namestrcpy(Name name, const char *str)
Definition: name.c:216
#define TupleDescSize(src)
Definition: tupdesc.h:101
int natts
Definition: tupdesc.h:79
void ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
Definition: resowner.c:1094
int32 tdtypmod
Definition: tupdesc.h:81
void aclcheck_error_type(AclResult aclerr, Oid typeOid)
Definition: aclchk.c:3663
signed int int32
Definition: c.h:302
#define NAMEDATALEN
void pfree(void *pointer)
Definition: mcxt.c:936
AttrDefault * defval
Definition: tupdesc.h:39
#define TEXTARRAYOID
Definition: pg_type.h:470
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:614
#define ObjectIdGetDatum(X)
Definition: postgres.h:490
#define ERROR
Definition: elog.h:43
#define lfirst_int(lc)
Definition: pg_list.h:107
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition: tupdesc.c:249
bool setof
Definition: parsenodes.h:210
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition: tupdesc.c:702
#define DEFAULT_COLLATION_OID
Definition: pg_collation.h:75
AttrNumber adnum
Definition: tupdesc.h:24
static ListCell * list_head(const List *l)
Definition: pg_list.h:77
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
unsigned int uint32
Definition: c.h:314
#define ACL_USAGE
Definition: parsenodes.h:80
char * adbin
Definition: tupdesc.h:25
#define RECORDOID
Definition: pg_type.h:680
void ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
Definition: resowner.c:1103
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:148
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition: tupdesc.c:543
#define ATTRIBUTE_FIXED_PART_SIZE
Definition: pg_attribute.h:179
#define lnext(lc)
Definition: pg_list.h:105
void ResourceOwnerEnlargeTupleDescs(ResourceOwner owner)
Definition: resowner.c:1083
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:690
bool has_not_null
Definition: tupdesc.h:43
Oid GetColumnDefCollation(ParseState *pstate, ColumnDef *coldef, Oid typeOid)
Definition: parse_type.c:521
HeapTuple SearchSysCache1(int cacheId, Datum key1)
Definition: syscache.c:1112
Datum hash_uint32(uint32 k)
Definition: hashfunc.c:853
void * palloc0(Size size)
Definition: mcxt.c:864
AclResult
Definition: acl.h:178
void ReleaseSysCache(HeapTuple tuple)
Definition: syscache.c:1160
void typenameTypeIdAndMod(ParseState *pstate, const TypeName *typeName, Oid *typeid_p, int32 *typmod_p)
Definition: parse_type.c:293
#define InvalidOid
Definition: postgres_ext.h:36
FormData_pg_attribute
Definition: pg_attribute.h:171
uint16 num_defval
Definition: tupdesc.h:41
struct tupleDesc * TupleDesc
#define INT8OID
Definition: pg_type.h:304
#define HeapTupleIsValid(tuple)
Definition: htup.h:77
#define Assert(condition)
Definition: c.h:688
#define lfirst(lc)
Definition: pg_list.h:106
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition: tupdesc.c:357
TupleConstr * constr
Definition: tupdesc.h:84
static int list_length(const List *l)
Definition: pg_list.h:89
TypeName * typeName
Definition: parsenodes.h:646
#define BOOLOID
Definition: pg_type.h:288
TupleDesc CreateTemplateTupleDesc(int natts, bool hasoid)
Definition: tupdesc.c:43
void FreeTupleDesc(TupleDesc tupdesc)
Definition: tupdesc.c:289
List * arrayBounds
Definition: parsenodes.h:214
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition: tupdesc.c:213
void * palloc(Size size)
Definition: mcxt.c:835
int errmsg(const char *fmt,...)
Definition: elog.c:797
bool ccvalid
Definition: tupdesc.h:32
int tdrefcount
Definition: tupdesc.h:83
int i
int inhcount
Definition: parsenodes.h:647
#define NameStr(name)
Definition: c.h:565
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition: tupdesc.c:375
char * colname
Definition: parsenodes.h:645
uint16 num_check
Definition: tupdesc.h:42
#define elog
Definition: elog.h:219
TupleDesc BuildDescForRelation(List *schema)
Definition: tupdesc.c:727
AclResult pg_type_aclcheck(Oid type_oid, Oid roleid, AclMode mode)
Definition: aclchk.c:4740
Definition: pg_list.h:45
#define PointerIsValid(pointer)
Definition: c.h:582
int16 AttrNumber
Definition: attnum.h:21
#define offsetof(type, field)
Definition: c.h:611
#define lfirst_oid(lc)
Definition: pg_list.h:108