1 /*-------------------------------------------------------------------------
2  *
3  * tupdesc.c
4  *	  POSTGRES tuple descriptor support code
5  *
6  * Portions Copyright (c) 1996-2016, 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/htup_details.h"
23 #include "catalog/pg_type.h"
24 #include "miscadmin.h"
25 #include "parser/parse_type.h"
26 #include "utils/acl.h"
27 #include "utils/builtins.h"
28 #include "utils/resowner_private.h"
29 #include "utils/syscache.h"
30 
31 
32 /*
33  * CreateTemplateTupleDesc
34  *		This function allocates an empty tuple descriptor structure.
35  *
36  * Tuple type ID information is initially set for an anonymous record type;
37  * caller can overwrite this if needed.
38  */
39 TupleDesc
CreateTemplateTupleDesc(int natts,bool hasoid)40 CreateTemplateTupleDesc(int natts, bool hasoid)
41 {
42 	TupleDesc	desc;
43 	char	   *stg;
44 	int			attroffset;
45 
46 	/*
47 	 * sanity checks
48 	 */
49 	AssertArg(natts >= 0);
50 
51 	/*
52 	 * Allocate enough memory for the tuple descriptor, including the
53 	 * attribute rows, and set up the attribute row pointers.
54 	 *
55 	 * Note: we assume that sizeof(struct tupleDesc) is a multiple of the
56 	 * struct pointer alignment requirement, and hence we don't need to insert
57 	 * alignment padding between the struct and the array of attribute row
58 	 * pointers.
59 	 *
60 	 * Note: Only the fixed part of pg_attribute rows is included in tuple
61 	 * descriptors, so we only need ATTRIBUTE_FIXED_PART_SIZE space per attr.
62 	 * That might need alignment padding, however.
63 	 */
64 	attroffset = sizeof(struct tupleDesc) + natts * sizeof(Form_pg_attribute);
65 	attroffset = MAXALIGN(attroffset);
66 	stg = palloc(attroffset + natts * MAXALIGN(ATTRIBUTE_FIXED_PART_SIZE));
67 	desc = (TupleDesc) stg;
68 
69 	if (natts > 0)
70 	{
71 		Form_pg_attribute *attrs;
72 		int			i;
73 
74 		attrs = (Form_pg_attribute *) (stg + sizeof(struct tupleDesc));
75 		desc->attrs = attrs;
76 		stg += attroffset;
77 		for (i = 0; i < natts; i++)
78 		{
79 			attrs[i] = (Form_pg_attribute) stg;
80 			stg += MAXALIGN(ATTRIBUTE_FIXED_PART_SIZE);
81 		}
82 	}
83 	else
84 		desc->attrs = NULL;
85 
86 	/*
87 	 * Initialize other fields of the tupdesc.
88 	 */
89 	desc->natts = natts;
90 	desc->constr = NULL;
91 	desc->tdtypeid = RECORDOID;
92 	desc->tdtypmod = -1;
93 	desc->tdhasoid = hasoid;
94 	desc->tdrefcount = -1;		/* assume not reference-counted */
95 
96 	return desc;
97 }
98 
99 /*
100  * CreateTupleDesc
101  *		This function allocates a new TupleDesc pointing to a given
102  *		Form_pg_attribute array.
103  *
104  * Note: if the TupleDesc is ever freed, the Form_pg_attribute array
105  * will not be freed thereby.
106  *
107  * Tuple type ID information is initially set for an anonymous record type;
108  * caller can overwrite this if needed.
109  */
110 TupleDesc
CreateTupleDesc(int natts,bool hasoid,Form_pg_attribute * attrs)111 CreateTupleDesc(int natts, bool hasoid, Form_pg_attribute *attrs)
112 {
113 	TupleDesc	desc;
114 
115 	/*
116 	 * sanity checks
117 	 */
118 	AssertArg(natts >= 0);
119 
120 	desc = (TupleDesc) palloc(sizeof(struct tupleDesc));
121 	desc->attrs = attrs;
122 	desc->natts = natts;
123 	desc->constr = NULL;
124 	desc->tdtypeid = RECORDOID;
125 	desc->tdtypmod = -1;
126 	desc->tdhasoid = hasoid;
127 	desc->tdrefcount = -1;		/* assume not reference-counted */
128 
129 	return desc;
130 }
131 
132 /*
133  * CreateTupleDescCopy
134  *		This function creates a new TupleDesc by copying from an existing
135  *		TupleDesc.
136  *
137  * !!! Constraints and defaults are not copied !!!
138  */
139 TupleDesc
CreateTupleDescCopy(TupleDesc tupdesc)140 CreateTupleDescCopy(TupleDesc tupdesc)
141 {
142 	TupleDesc	desc;
143 	int			i;
144 
145 	desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);
146 
147 	for (i = 0; i < desc->natts; i++)
148 	{
149 		memcpy(desc->attrs[i], tupdesc->attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
150 		desc->attrs[i]->attnotnull = false;
151 		desc->attrs[i]->atthasdef = false;
152 	}
153 
154 	desc->tdtypeid = tupdesc->tdtypeid;
155 	desc->tdtypmod = tupdesc->tdtypmod;
156 
157 	return desc;
158 }
159 
160 /*
161  * CreateTupleDescCopyConstr
162  *		This function creates a new TupleDesc by copying from an existing
163  *		TupleDesc (including its constraints and defaults).
164  */
165 TupleDesc
CreateTupleDescCopyConstr(TupleDesc tupdesc)166 CreateTupleDescCopyConstr(TupleDesc tupdesc)
167 {
168 	TupleDesc	desc;
169 	TupleConstr *constr = tupdesc->constr;
170 	int			i;
171 
172 	desc = CreateTemplateTupleDesc(tupdesc->natts, tupdesc->tdhasoid);
173 
174 	for (i = 0; i < desc->natts; i++)
175 	{
176 		memcpy(desc->attrs[i], tupdesc->attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
177 	}
178 
179 	if (constr)
180 	{
181 		TupleConstr *cpy = (TupleConstr *) palloc0(sizeof(TupleConstr));
182 
183 		cpy->has_not_null = constr->has_not_null;
184 
185 		if ((cpy->num_defval = constr->num_defval) > 0)
186 		{
187 			cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));
188 			memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
189 			for (i = cpy->num_defval - 1; i >= 0; i--)
190 			{
191 				if (constr->defval[i].adbin)
192 					cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
193 			}
194 		}
195 
196 		if ((cpy->num_check = constr->num_check) > 0)
197 		{
198 			cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));
199 			memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
200 			for (i = cpy->num_check - 1; i >= 0; i--)
201 			{
202 				if (constr->check[i].ccname)
203 					cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
204 				if (constr->check[i].ccbin)
205 					cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
206 				cpy->check[i].ccvalid = constr->check[i].ccvalid;
207 				cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
208 			}
209 		}
210 
211 		desc->constr = cpy;
212 	}
213 
214 	desc->tdtypeid = tupdesc->tdtypeid;
215 	desc->tdtypmod = tupdesc->tdtypmod;
216 
217 	return desc;
218 }
219 
220 /*
221  * TupleDescCopyEntry
222  *		This function copies a single attribute structure from one tuple
223  *		descriptor to another.
224  *
225  * !!! Constraints and defaults are not copied !!!
226  */
227 void
TupleDescCopyEntry(TupleDesc dst,AttrNumber dstAttno,TupleDesc src,AttrNumber srcAttno)228 TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
229 				   TupleDesc src, AttrNumber srcAttno)
230 {
231 	/*
232 	 * sanity checks
233 	 */
234 	AssertArg(PointerIsValid(src));
235 	AssertArg(PointerIsValid(dst));
236 	AssertArg(srcAttno >= 1);
237 	AssertArg(srcAttno <= src->natts);
238 	AssertArg(dstAttno >= 1);
239 	AssertArg(dstAttno <= dst->natts);
240 
241 	memcpy(dst->attrs[dstAttno - 1], src->attrs[srcAttno - 1],
242 		   ATTRIBUTE_FIXED_PART_SIZE);
243 
244 	/*
245 	 * Aside from updating the attno, we'd better reset attcacheoff.
246 	 *
247 	 * XXX Actually, to be entirely safe we'd need to reset the attcacheoff of
248 	 * all following columns in dst as well.  Current usage scenarios don't
249 	 * require that though, because all following columns will get initialized
250 	 * by other uses of this function or TupleDescInitEntry.  So we cheat a
251 	 * bit to avoid a useless O(N^2) penalty.
252 	 */
253 	dst->attrs[dstAttno - 1]->attnum = dstAttno;
254 	dst->attrs[dstAttno - 1]->attcacheoff = -1;
255 
256 	/* since we're not copying constraints or defaults, clear these */
257 	dst->attrs[dstAttno - 1]->attnotnull = false;
258 	dst->attrs[dstAttno - 1]->atthasdef = false;
259 }
260 
261 /*
262  * Free a TupleDesc including all substructure
263  */
264 void
FreeTupleDesc(TupleDesc tupdesc)265 FreeTupleDesc(TupleDesc tupdesc)
266 {
267 	int			i;
268 
269 	/*
270 	 * Possibly this should assert tdrefcount == 0, to disallow explicit
271 	 * freeing of un-refcounted tupdescs?
272 	 */
273 	Assert(tupdesc->tdrefcount <= 0);
274 
275 	if (tupdesc->constr)
276 	{
277 		if (tupdesc->constr->num_defval > 0)
278 		{
279 			AttrDefault *attrdef = tupdesc->constr->defval;
280 
281 			for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
282 			{
283 				if (attrdef[i].adbin)
284 					pfree(attrdef[i].adbin);
285 			}
286 			pfree(attrdef);
287 		}
288 		if (tupdesc->constr->num_check > 0)
289 		{
290 			ConstrCheck *check = tupdesc->constr->check;
291 
292 			for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
293 			{
294 				if (check[i].ccname)
295 					pfree(check[i].ccname);
296 				if (check[i].ccbin)
297 					pfree(check[i].ccbin);
298 			}
299 			pfree(check);
300 		}
301 		pfree(tupdesc->constr);
302 	}
303 
304 	pfree(tupdesc);
305 }
306 
307 /*
308  * Increment the reference count of a tupdesc, and log the reference in
309  * CurrentResourceOwner.
310  *
311  * Do not apply this to tupdescs that are not being refcounted.  (Use the
312  * macro PinTupleDesc for tupdescs of uncertain status.)
313  */
314 void
IncrTupleDescRefCount(TupleDesc tupdesc)315 IncrTupleDescRefCount(TupleDesc tupdesc)
316 {
317 	Assert(tupdesc->tdrefcount >= 0);
318 
319 	ResourceOwnerEnlargeTupleDescs(CurrentResourceOwner);
320 	tupdesc->tdrefcount++;
321 	ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
322 }
323 
324 /*
325  * Decrement the reference count of a tupdesc, remove the corresponding
326  * reference from CurrentResourceOwner, and free the tupdesc if no more
327  * references remain.
328  *
329  * Do not apply this to tupdescs that are not being refcounted.  (Use the
330  * macro ReleaseTupleDesc for tupdescs of uncertain status.)
331  */
332 void
DecrTupleDescRefCount(TupleDesc tupdesc)333 DecrTupleDescRefCount(TupleDesc tupdesc)
334 {
335 	Assert(tupdesc->tdrefcount > 0);
336 
337 	ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
338 	if (--tupdesc->tdrefcount == 0)
339 		FreeTupleDesc(tupdesc);
340 }
341 
342 /*
343  * Compare two TupleDesc structures for logical equality
344  *
345  * Note: we deliberately do not check the attrelid and tdtypmod fields.
346  * This allows typcache.c to use this routine to see if a cached record type
347  * matches a requested type, and is harmless for relcache.c's uses.
348  * We don't compare tdrefcount, either.
349  */
350 bool
equalTupleDescs(TupleDesc tupdesc1,TupleDesc tupdesc2)351 equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
352 {
353 	int			i,
354 				j,
355 				n;
356 
357 	if (tupdesc1->natts != tupdesc2->natts)
358 		return false;
359 	if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
360 		return false;
361 	if (tupdesc1->tdhasoid != tupdesc2->tdhasoid)
362 		return false;
363 
364 	for (i = 0; i < tupdesc1->natts; i++)
365 	{
366 		Form_pg_attribute attr1 = tupdesc1->attrs[i];
367 		Form_pg_attribute attr2 = tupdesc2->attrs[i];
368 
369 		/*
370 		 * We do not need to check every single field here: we can disregard
371 		 * attrelid and attnum (which were used to place the row in the attrs
372 		 * array in the first place).  It might look like we could dispense
373 		 * with checking attlen/attbyval/attalign, since these are derived
374 		 * from atttypid; but in the case of dropped columns we must check
375 		 * them (since atttypid will be zero for all dropped columns) and in
376 		 * general it seems safer to check them always.
377 		 *
378 		 * attcacheoff must NOT be checked since it's possibly not set in both
379 		 * copies.
380 		 */
381 		if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
382 			return false;
383 		if (attr1->atttypid != attr2->atttypid)
384 			return false;
385 		if (attr1->attstattarget != attr2->attstattarget)
386 			return false;
387 		if (attr1->attlen != attr2->attlen)
388 			return false;
389 		if (attr1->attndims != attr2->attndims)
390 			return false;
391 		if (attr1->atttypmod != attr2->atttypmod)
392 			return false;
393 		if (attr1->attbyval != attr2->attbyval)
394 			return false;
395 		if (attr1->attstorage != attr2->attstorage)
396 			return false;
397 		if (attr1->attalign != attr2->attalign)
398 			return false;
399 		if (attr1->attnotnull != attr2->attnotnull)
400 			return false;
401 		if (attr1->atthasdef != attr2->atthasdef)
402 			return false;
403 		if (attr1->attisdropped != attr2->attisdropped)
404 			return false;
405 		if (attr1->attislocal != attr2->attislocal)
406 			return false;
407 		if (attr1->attinhcount != attr2->attinhcount)
408 			return false;
409 		if (attr1->attcollation != attr2->attcollation)
410 			return false;
411 		/* attacl, attoptions and attfdwoptions are not even present... */
412 	}
413 
414 	if (tupdesc1->constr != NULL)
415 	{
416 		TupleConstr *constr1 = tupdesc1->constr;
417 		TupleConstr *constr2 = tupdesc2->constr;
418 
419 		if (constr2 == NULL)
420 			return false;
421 		if (constr1->has_not_null != constr2->has_not_null)
422 			return false;
423 		n = constr1->num_defval;
424 		if (n != (int) constr2->num_defval)
425 			return false;
426 		for (i = 0; i < n; i++)
427 		{
428 			AttrDefault *defval1 = constr1->defval + i;
429 			AttrDefault *defval2 = constr2->defval;
430 
431 			/*
432 			 * We can't assume that the items are always read from the system
433 			 * catalogs in the same order; so use the adnum field to identify
434 			 * the matching item to compare.
435 			 */
436 			for (j = 0; j < n; defval2++, j++)
437 			{
438 				if (defval1->adnum == defval2->adnum)
439 					break;
440 			}
441 			if (j >= n)
442 				return false;
443 			if (strcmp(defval1->adbin, defval2->adbin) != 0)
444 				return false;
445 		}
446 		n = constr1->num_check;
447 		if (n != (int) constr2->num_check)
448 			return false;
449 		for (i = 0; i < n; i++)
450 		{
451 			ConstrCheck *check1 = constr1->check + i;
452 			ConstrCheck *check2 = constr2->check;
453 
454 			/*
455 			 * Similarly, don't assume that the checks are always read in the
456 			 * same order; match them up by name and contents. (The name
457 			 * *should* be unique, but...)
458 			 */
459 			for (j = 0; j < n; check2++, j++)
460 			{
461 				if (strcmp(check1->ccname, check2->ccname) == 0 &&
462 					strcmp(check1->ccbin, check2->ccbin) == 0 &&
463 					check1->ccvalid == check2->ccvalid &&
464 					check1->ccnoinherit == check2->ccnoinherit)
465 					break;
466 			}
467 			if (j >= n)
468 				return false;
469 		}
470 	}
471 	else if (tupdesc2->constr != NULL)
472 		return false;
473 	return true;
474 }
475 
476 /*
477  * TupleDescInitEntry
478  *		This function initializes a single attribute structure in
479  *		a previously allocated tuple descriptor.
480  *
481  * If attributeName is NULL, the attname field is set to an empty string
482  * (this is for cases where we don't know or need a name for the field).
483  * Also, some callers use this function to change the datatype-related fields
484  * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
485  * to indicate that the attname field shouldn't be modified.
486  *
487  * Note that attcollation is set to the default for the specified datatype.
488  * If a nondefault collation is needed, insert it afterwards using
489  * TupleDescInitEntryCollation.
490  */
491 void
TupleDescInitEntry(TupleDesc desc,AttrNumber attributeNumber,const char * attributeName,Oid oidtypeid,int32 typmod,int attdim)492 TupleDescInitEntry(TupleDesc desc,
493 				   AttrNumber attributeNumber,
494 				   const char *attributeName,
495 				   Oid oidtypeid,
496 				   int32 typmod,
497 				   int attdim)
498 {
499 	HeapTuple	tuple;
500 	Form_pg_type typeForm;
501 	Form_pg_attribute att;
502 
503 	/*
504 	 * sanity checks
505 	 */
506 	AssertArg(PointerIsValid(desc));
507 	AssertArg(attributeNumber >= 1);
508 	AssertArg(attributeNumber <= desc->natts);
509 
510 	/*
511 	 * initialize the attribute fields
512 	 */
513 	att = desc->attrs[attributeNumber - 1];
514 
515 	att->attrelid = 0;			/* dummy value */
516 
517 	/*
518 	 * Note: attributeName can be NULL, because the planner doesn't always
519 	 * fill in valid resname values in targetlists, particularly for resjunk
520 	 * attributes. Also, do nothing if caller wants to re-use the old attname.
521 	 */
522 	if (attributeName == NULL)
523 		MemSet(NameStr(att->attname), 0, NAMEDATALEN);
524 	else if (attributeName != NameStr(att->attname))
525 		namestrcpy(&(att->attname), attributeName);
526 
527 	att->attstattarget = -1;
528 	att->attcacheoff = -1;
529 	att->atttypmod = typmod;
530 
531 	att->attnum = attributeNumber;
532 	att->attndims = attdim;
533 
534 	att->attnotnull = false;
535 	att->atthasdef = false;
536 	att->attisdropped = false;
537 	att->attislocal = true;
538 	att->attinhcount = 0;
539 	/* attacl, attoptions and attfdwoptions are not present in tupledescs */
540 
541 	tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
542 	if (!HeapTupleIsValid(tuple))
543 		elog(ERROR, "cache lookup failed for type %u", oidtypeid);
544 	typeForm = (Form_pg_type) GETSTRUCT(tuple);
545 
546 	att->atttypid = oidtypeid;
547 	att->attlen = typeForm->typlen;
548 	att->attbyval = typeForm->typbyval;
549 	att->attalign = typeForm->typalign;
550 	att->attstorage = typeForm->typstorage;
551 	att->attcollation = typeForm->typcollation;
552 
553 	ReleaseSysCache(tuple);
554 }
555 
556 /*
557  * TupleDescInitEntryCollation
558  *
559  * Assign a nondefault collation to a previously initialized tuple descriptor
560  * entry.
561  */
562 void
TupleDescInitEntryCollation(TupleDesc desc,AttrNumber attributeNumber,Oid collationid)563 TupleDescInitEntryCollation(TupleDesc desc,
564 							AttrNumber attributeNumber,
565 							Oid collationid)
566 {
567 	/*
568 	 * sanity checks
569 	 */
570 	AssertArg(PointerIsValid(desc));
571 	AssertArg(attributeNumber >= 1);
572 	AssertArg(attributeNumber <= desc->natts);
573 
574 	desc->attrs[attributeNumber - 1]->attcollation = collationid;
575 }
576 
577 
578 /*
579  * BuildDescForRelation
580  *
581  * Given a relation schema (list of ColumnDef nodes), build a TupleDesc.
582  *
583  * Note: the default assumption is no OIDs; caller may modify the returned
584  * TupleDesc if it wants OIDs.  Also, tdtypeid will need to be filled in
585  * later on.
586  */
587 TupleDesc
BuildDescForRelation(List * schema)588 BuildDescForRelation(List *schema)
589 {
590 	int			natts;
591 	AttrNumber	attnum;
592 	ListCell   *l;
593 	TupleDesc	desc;
594 	bool		has_not_null;
595 	char	   *attname;
596 	Oid			atttypid;
597 	int32		atttypmod;
598 	Oid			attcollation;
599 	int			attdim;
600 
601 	/*
602 	 * allocate a new tuple descriptor
603 	 */
604 	natts = list_length(schema);
605 	desc = CreateTemplateTupleDesc(natts, false);
606 	has_not_null = false;
607 
608 	attnum = 0;
609 
610 	foreach(l, schema)
611 	{
612 		ColumnDef  *entry = lfirst(l);
613 		AclResult	aclresult;
614 
615 		/*
616 		 * for each entry in the list, get the name and type information from
617 		 * the list and have TupleDescInitEntry fill in the attribute
618 		 * information we need.
619 		 */
620 		attnum++;
621 
622 		attname = entry->colname;
623 		typenameTypeIdAndMod(NULL, entry->typeName, &atttypid, &atttypmod);
624 
625 		aclresult = pg_type_aclcheck(atttypid, GetUserId(), ACL_USAGE);
626 		if (aclresult != ACLCHECK_OK)
627 			aclcheck_error_type(aclresult, atttypid);
628 
629 		attcollation = GetColumnDefCollation(NULL, entry, atttypid);
630 		attdim = list_length(entry->typeName->arrayBounds);
631 
632 		if (entry->typeName->setof)
633 			ereport(ERROR,
634 					(errcode(ERRCODE_INVALID_TABLE_DEFINITION),
635 					 errmsg("column \"%s\" cannot be declared SETOF",
636 							attname)));
637 
638 		TupleDescInitEntry(desc, attnum, attname,
639 						   atttypid, atttypmod, attdim);
640 
641 		/* Override TupleDescInitEntry's settings as requested */
642 		TupleDescInitEntryCollation(desc, attnum, attcollation);
643 		if (entry->storage)
644 			desc->attrs[attnum - 1]->attstorage = entry->storage;
645 
646 		/* Fill in additional stuff not handled by TupleDescInitEntry */
647 		desc->attrs[attnum - 1]->attnotnull = entry->is_not_null;
648 		has_not_null |= entry->is_not_null;
649 		desc->attrs[attnum - 1]->attislocal = entry->is_local;
650 		desc->attrs[attnum - 1]->attinhcount = entry->inhcount;
651 	}
652 
653 	if (has_not_null)
654 	{
655 		TupleConstr *constr = (TupleConstr *) palloc0(sizeof(TupleConstr));
656 
657 		constr->has_not_null = true;
658 		constr->defval = NULL;
659 		constr->num_defval = 0;
660 		constr->check = NULL;
661 		constr->num_check = 0;
662 		desc->constr = constr;
663 	}
664 	else
665 	{
666 		desc->constr = NULL;
667 	}
668 
669 	return desc;
670 }
671 
672 /*
673  * BuildDescFromLists
674  *
675  * Build a TupleDesc given lists of column names (as String nodes),
676  * column type OIDs, typmods, and collation OIDs.
677  *
678  * No constraints are generated.
679  *
680  * This is essentially a cut-down version of BuildDescForRelation for use
681  * with functions returning RECORD.
682  */
683 TupleDesc
BuildDescFromLists(List * names,List * types,List * typmods,List * collations)684 BuildDescFromLists(List *names, List *types, List *typmods, List *collations)
685 {
686 	int			natts;
687 	AttrNumber	attnum;
688 	ListCell   *l1;
689 	ListCell   *l2;
690 	ListCell   *l3;
691 	ListCell   *l4;
692 	TupleDesc	desc;
693 
694 	natts = list_length(names);
695 	Assert(natts == list_length(types));
696 	Assert(natts == list_length(typmods));
697 	Assert(natts == list_length(collations));
698 
699 	/*
700 	 * allocate a new tuple descriptor
701 	 */
702 	desc = CreateTemplateTupleDesc(natts, false);
703 
704 	attnum = 0;
705 
706 	l2 = list_head(types);
707 	l3 = list_head(typmods);
708 	l4 = list_head(collations);
709 	foreach(l1, names)
710 	{
711 		char	   *attname = strVal(lfirst(l1));
712 		Oid			atttypid;
713 		int32		atttypmod;
714 		Oid			attcollation;
715 
716 		atttypid = lfirst_oid(l2);
717 		l2 = lnext(l2);
718 		atttypmod = lfirst_int(l3);
719 		l3 = lnext(l3);
720 		attcollation = lfirst_oid(l4);
721 		l4 = lnext(l4);
722 
723 		attnum++;
724 
725 		TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
726 		TupleDescInitEntryCollation(desc, attnum, attcollation);
727 	}
728 
729 	return desc;
730 }
731