1 /*-------------------------------------------------------------------------
4 * POSTGRES tuple descriptor support code
6 * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
7 * Portions Copyright (c) 1994, Regents of the University of California
11 * $PostgreSQL: pgsql/src/backend/access/common/tupdesc.c,v 1.131 2010/01/02 16:57:33 momjian Exp $
14 * some of the executor utility code such as "ExecTypeFromTL" should be
17 *-------------------------------------------------------------------------
22 #include "catalog/pg_type.h"
23 #include "parser/parse_type.h"
24 #include "utils/builtins.h"
25 #include "utils/resowner.h"
26 #include "utils/syscache.h"
30 * CreateTemplateTupleDesc
31 * This function allocates an empty tuple descriptor structure.
33 * Tuple type ID information is initially set for an anonymous record type;
34 * caller can overwrite this if needed.
37 CreateTemplateTupleDesc(int natts, bool hasoid)
46 AssertArg(natts >= 0);
49 * Allocate enough memory for the tuple descriptor, including the
50 * attribute rows, and set up the attribute row pointers.
52 * Note: we assume that sizeof(struct tupleDesc) is a multiple of the
53 * struct pointer alignment requirement, and hence we don't need to insert
54 * alignment padding between the struct and the array of attribute row
57 * Note: Only the fixed part of pg_attribute rows is included in tuple
58 * descriptors, so we only need ATTRIBUTE_FIXED_PART_SIZE space per attr.
59 * That might need alignment padding, however.
61 attroffset = sizeof(struct tupleDesc) + natts * sizeof(Form_pg_attribute);
62 attroffset = MAXALIGN(attroffset);
63 stg = palloc(attroffset + natts * MAXALIGN(ATTRIBUTE_FIXED_PART_SIZE));
64 desc = (TupleDesc) stg;
68 Form_pg_attribute *attrs;
71 attrs = (Form_pg_attribute *) (stg + sizeof(struct tupleDesc));
74 for (i = 0; i < natts; i++)
76 attrs[i] = (Form_pg_attribute) stg;
77 stg += MAXALIGN(ATTRIBUTE_FIXED_PART_SIZE);
84 * Initialize other fields of the tupdesc.
88 desc->tdtypeid = RECORDOID;
90 desc->tdhasoid = hasoid;
91 desc->tdrefcount = -1; /* assume not reference-counted */
98 * This function allocates a new TupleDesc pointing to a given
99 * Form_pg_attribute array.
101 * Note: if the TupleDesc is ever freed, the Form_pg_attribute array
102 * will not be freed thereby.
104 * Tuple type ID information is initially set for an anonymous record type;
105 * caller can overwrite this if needed.
108 CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
115 AssertArg(natts >= 0);
117 desc = (TupleDesc) palloc(sizeof(struct tupleDesc));
121 desc->tdtypeid = RECORDOID;
123 desc->tdhasoid = hasoid;
124 desc->tdrefcount = -1; /* assume not reference-counted */
130 * CreateTupleDescCopy
131 * This function creates a new TupleDesc by copying from an existing
134 * !!! Constraints and defaults are not copied !!!
137 CreateTupleDescCopy(TupleDesc tupdesc)
142 desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);
144 for (i = 0; i < desc->natts; i++)
146 memcpy(desc->attrs[i], tupdesc->attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
147 desc->attrs[i]->attnotnull = false;
148 desc->attrs[i]->atthasdef = false;
151 desc->tdtypeid = tupdesc->tdtypeid;
152 desc->tdtypmod = tupdesc->tdtypmod;
158 * CreateTupleDescCopyConstr
159 * This function creates a new TupleDesc by copying from an existing
160 * TupleDesc (including its constraints and defaults).
163 CreateTupleDescCopyConstr(TupleDesc tupdesc)
166 TupleConstr *constr = tupdesc->constr;
169 desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);
171 for (i = 0; i < desc->natts; i++)
173 memcpy(desc->attrs[i], tupdesc->attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
178 TupleConstr *cpy = (TupleConstr *) palloc0(sizeof(TupleConstr));
180 cpy->has_not_null = constr->has_not_null;
182 if ((cpy->num_defval = constr->num_defval) > 0)
184 cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));
185 memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
186 for (i = cpy->num_defval - 1; i >= 0; i--)
188 if (constr->defval[i].adbin)
189 cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
193 if ((cpy->num_check = constr->num_check) > 0)
195 cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));
196 memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
197 for (i = cpy->num_check - 1; i >= 0; i--)
199 if (constr->check[i].ccname)
200 cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
201 if (constr->check[i].ccbin)
202 cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
209 desc->tdtypeid = tupdesc->tdtypeid;
210 desc->tdtypmod = tupdesc->tdtypmod;
216 * Free a TupleDesc including all substructure
219 FreeTupleDesc(TupleDesc tupdesc)
224 * Possibly this should assert tdrefcount == 0, to disallow explicit
225 * freeing of un-refcounted tupdescs?
227 Assert(tupdesc->tdrefcount <= 0);
231 if (tupdesc->constr->num_defval > 0)
233 AttrDefault *attrdef = tupdesc->constr->defval;
235 for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
237 if (attrdef[i].adbin)
238 pfree(attrdef[i].adbin);
242 if (tupdesc->constr->num_check > 0)
244 ConstrCheck *check = tupdesc->constr->check;
246 for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
249 pfree(check[i].ccname);
251 pfree(check[i].ccbin);
255 pfree(tupdesc->constr);
262 * Increment the reference count of a tupdesc, and log the reference in
263 * CurrentResourceOwner.
265 * Do not apply this to tupdescs that are not being refcounted. (Use the
266 * macro PinTupleDesc for tupdescs of uncertain status.)
269 IncrTupleDescRefCount(TupleDesc tupdesc)
271 Assert(tupdesc->tdrefcount >= 0);
273 ResourceOwnerEnlargeTupleDescs(CurrentResourceOwner);
274 tupdesc->tdrefcount++;
275 ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
279 * Decrement the reference count of a tupdesc, remove the corresponding
280 * reference from CurrentResourceOwner, and free the tupdesc if no more
283 * Do not apply this to tupdescs that are not being refcounted. (Use the
284 * macro ReleaseTupleDesc for tupdescs of uncertain status.)
287 DecrTupleDescRefCount(TupleDesc tupdesc)
289 Assert(tupdesc->tdrefcount > 0);
291 ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
292 if (--tupdesc->tdrefcount == 0)
293 FreeTupleDesc(tupdesc);
297 * Compare two TupleDesc structures for logical equality
299 * Note: we deliberately do not check the attrelid and tdtypmod fields.
300 * This allows typcache.c to use this routine to see if a cached record type
301 * matches a requested type, and is harmless for relcache.c's uses.
302 * We don't compare tdrefcount, either.
305 equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
311 if (tupdesc1->natts != tupdesc2->natts)
313 if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
315 if (tupdesc1->tdhasoid != tupdesc2->tdhasoid)
318 for (i = 0; i < tupdesc1->natts; i++)
320 Form_pg_attribute attr1 = tupdesc1->attrs[i];
321 Form_pg_attribute attr2 = tupdesc2->attrs[i];
324 * We do not need to check every single field here: we can disregard
325 * attrelid and attnum (which were used to place the row in the attrs
326 * array in the first place). It might look like we could dispense
327 * with checking attlen/attbyval/attalign, since these are derived
328 * from atttypid; but in the case of dropped columns we must check
329 * them (since atttypid will be zero for all dropped columns) and in
330 * general it seems safer to check them always.
332 * attcacheoff must NOT be checked since it's possibly not set in both
335 if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
337 if (attr1->atttypid != attr2->atttypid)
339 if (attr1->attstattarget != attr2->attstattarget)
341 if (attr1->attdistinct != attr2->attdistinct)
343 if (attr1->attlen != attr2->attlen)
345 if (attr1->attndims != attr2->attndims)
347 if (attr1->atttypmod != attr2->atttypmod)
349 if (attr1->attbyval != attr2->attbyval)
351 if (attr1->attstorage != attr2->attstorage)
353 if (attr1->attalign != attr2->attalign)
355 if (attr1->attnotnull != attr2->attnotnull)
357 if (attr1->atthasdef != attr2->atthasdef)
359 if (attr1->attisdropped != attr2->attisdropped)
361 if (attr1->attislocal != attr2->attislocal)
363 if (attr1->attinhcount != attr2->attinhcount)
365 /* attacl is ignored, since it's not even present... */
368 if (tupdesc1->constr != NULL)
370 TupleConstr *constr1 = tupdesc1->constr;
371 TupleConstr *constr2 = tupdesc2->constr;
375 if (constr1->has_not_null != constr2->has_not_null)
377 n = constr1->num_defval;
378 if (n != (int) constr2->num_defval)
380 for (i = 0; i < n; i++)
382 AttrDefault *defval1 = constr1->defval + i;
383 AttrDefault *defval2 = constr2->defval;
386 * We can't assume that the items are always read from the system
387 * catalogs in the same order; so use the adnum field to identify
388 * the matching item to compare.
390 for (j = 0; j < n; defval2++, j++)
392 if (defval1->adnum == defval2->adnum)
397 if (strcmp(defval1->adbin, defval2->adbin) != 0)
400 n = constr1->num_check;
401 if (n != (int) constr2->num_check)
403 for (i = 0; i < n; i++)
405 ConstrCheck *check1 = constr1->check + i;
406 ConstrCheck *check2 = constr2->check;
409 * Similarly, don't assume that the checks are always read in the
410 * same order; match them up by name and contents. (The name
411 * *should* be unique, but...)
413 for (j = 0; j < n; check2++, j++)
415 if (strcmp(check1->ccname, check2->ccname) == 0 &&
416 strcmp(check1->ccbin, check2->ccbin) == 0)
423 else if (tupdesc2->constr != NULL)
430 * This function initializes a single attribute structure in
431 * a previously allocated tuple descriptor.
434 TupleDescInitEntry(TupleDesc desc,
435 AttrNumber attributeNumber,
436 const char *attributeName,
442 Form_pg_type typeForm;
443 Form_pg_attribute att;
448 AssertArg(PointerIsValid(desc));
449 AssertArg(attributeNumber >= 1);
450 AssertArg(attributeNumber <= desc->natts);
453 * initialize the attribute fields
455 att = desc->attrs[attributeNumber - 1];
457 att->attrelid = 0; /* dummy value */
460 * Note: attributeName can be NULL, because the planner doesn't always
461 * fill in valid resname values in targetlists, particularly for resjunk
464 if (attributeName != NULL)
465 namestrcpy(&(att->attname), attributeName);
467 MemSet(NameStr(att->attname), 0, NAMEDATALEN);
469 att->attstattarget = -1;
470 att->attdistinct = 0;
471 att->attcacheoff = -1;
472 att->atttypmod = typmod;
474 att->attnum = attributeNumber;
475 att->attndims = attdim;
477 att->attnotnull = false;
478 att->atthasdef = false;
479 att->attisdropped = false;
480 att->attislocal = true;
481 att->attinhcount = 0;
482 /* attacl is not set because it's not present in tupledescs */
484 tuple = SearchSysCache(TYPEOID,
485 ObjectIdGetDatum(oidtypeid),
487 if (!HeapTupleIsValid(tuple))
488 elog(ERROR, "cache lookup failed for type %u", oidtypeid);
489 typeForm = (Form_pg_type) GETSTRUCT(tuple);
491 att->atttypid = oidtypeid;
492 att->attlen = typeForm->typlen;
493 att->attbyval = typeForm->typbyval;
494 att->attalign = typeForm->typalign;
495 att->attstorage = typeForm->typstorage;
497 ReleaseSysCache(tuple);
502 * BuildDescForRelation
504 * Given a relation schema (list of ColumnDef nodes), build a TupleDesc.
506 * Note: the default assumption is no OIDs; caller may modify the returned
507 * TupleDesc if it wants OIDs. Also, tdtypeid will need to be filled in
511 BuildDescForRelation(List *schema)
524 * allocate a new tuple descriptor
526 natts = list_length(schema);
527 desc = CreateTemplateTupleDesc(natts, false);
528 has_not_null = false;
534 ColumnDef *entry = lfirst(l);
537 * for each entry in the list, get the name and type information from
538 * the list and have TupleDescInitEntry fill in the attribute
539 * information we need.
543 attname = entry->colname;
544 atttypid = typenameTypeId(NULL, entry->typeName, &atttypmod);
545 attdim = list_length(entry->typeName->arrayBounds);
547 if (entry->typeName->setof)
549 (errcode(ERRCODE_INVALID_TABLE_DEFINITION),
550 errmsg("column \"%s\" cannot be declared SETOF",
553 TupleDescInitEntry(desc, attnum, attname,
554 atttypid, atttypmod, attdim);
556 /* Override TupleDescInitEntry's settings as requested */
558 desc->attrs[attnum - 1]->attstorage = entry->storage;
560 /* Fill in additional stuff not handled by TupleDescInitEntry */
561 desc->attrs[attnum - 1]->attnotnull = entry->is_not_null;
562 has_not_null |= entry->is_not_null;
563 desc->attrs[attnum - 1]->attislocal = entry->is_local;
564 desc->attrs[attnum - 1]->attinhcount = entry->inhcount;
569 TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
571 constr->has_not_null = true;
572 constr->defval = NULL;
573 constr->num_defval = 0;
574 constr->check = NULL;
575 constr->num_check = 0;
576 desc->constr = constr;
589 * Build a TupleDesc given lists of column names (as String nodes),
590 * column type OIDs, and column typmods. No constraints are generated.
592 * This is essentially a cut-down version of BuildDescForRelation for use
593 * with functions returning RECORD.
596 BuildDescFromLists(List *names, List *types, List *typmods)
605 natts = list_length(names);
606 Assert(natts == list_length(types));
607 Assert(natts == list_length(typmods));
610 * allocate a new tuple descriptor
612 desc = CreateTemplateTupleDesc(natts, false);
616 l2 = list_head(types);
617 l3 = list_head(typmods);
620 char *attname = strVal(lfirst(l1));
624 atttypid = lfirst_oid(l2);
626 atttypmod = lfirst_int(l3);
631 TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);