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