PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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-2026, 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"
25#include "catalog/catalog.h"
27#include "catalog/pg_type.h"
28#include "common/hashfn.h"
29#include "utils/builtins.h"
30#include "utils/datum.h"
31#include "utils/resowner.h"
32#include "utils/syscache.h"
33
34/* ResourceOwner callbacks to hold tupledesc references */
35static void ResOwnerReleaseTupleDesc(Datum res);
36static char *ResOwnerPrintTupleDesc(Datum res);
37
39{
40 .name = "tupdesc reference",
41 .release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
42 .release_priority = RELEASE_PRIO_TUPDESC_REFS,
43 .ReleaseResource = ResOwnerReleaseTupleDesc,
44 .DebugPrint = ResOwnerPrintTupleDesc
45};
46
47/* Convenience wrappers over ResourceOwnerRemember/Forget */
48static inline void
53
54static inline void
59
60/*
61 * populate_compact_attribute_internal
62 * Helper function for populate_compact_attribute()
63 */
64static inline void
67{
68 memset(dst, 0, sizeof(CompactAttribute));
69
70 dst->attcacheoff = -1;
71 dst->attlen = src->attlen;
72
73 dst->attbyval = src->attbyval;
74 dst->attispackable = (src->attstorage != TYPSTORAGE_PLAIN);
75 dst->atthasmissing = src->atthasmissing;
76 dst->attisdropped = src->attisdropped;
77 dst->attgenerated = (src->attgenerated != '\0');
78
79 /*
80 * Assign nullability status for this column. Assuming that a constraint
81 * exists, at this point we don't know if a not-null constraint is valid,
82 * so we assign UNKNOWN unless the table is a catalog, in which case we
83 * know it's valid.
84 */
85 dst->attnullability = !src->attnotnull ? ATTNULLABLE_UNRESTRICTED :
88
89 /* Compute numeric alignment requirement, too */
90 dst->attalignby = typalign_to_alignby(src->attalign);
91}
92
93/*
94 * populate_compact_attribute
95 * Fill in the corresponding CompactAttribute element from the
96 * Form_pg_attribute for the given attribute number. This must be called
97 * whenever a change is made to a Form_pg_attribute in the TupleDesc.
98 */
99void
101{
102 Form_pg_attribute src = TupleDescAttr(tupdesc, attnum);
104
105 /*
106 * Don't use TupleDescCompactAttr to prevent infinite recursion in assert
107 * builds.
108 */
109 dst = &tupdesc->compact_attrs[attnum];
110
112}
113
114/*
115 * verify_compact_attribute
116 * In Assert enabled builds, we verify that the CompactAttribute is
117 * populated correctly. This helps find bugs in places such as ALTER
118 * TABLE where code makes changes to the FormData_pg_attribute but
119 * forgets to call populate_compact_attribute().
120 *
121 * This is used in TupleDescCompactAttr(), but declared here to allow access
122 * to populate_compact_attribute_internal().
123 */
124void
126{
127#ifdef USE_ASSERT_CHECKING
129 Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum);
131
132 /*
133 * Make a temp copy of the TupleDesc's CompactAttribute. This may be a
134 * shared TupleDesc and the attcacheoff might get changed by another
135 * backend.
136 */
137 memcpy(&cattr, &tupdesc->compact_attrs[attnum], sizeof(CompactAttribute));
138
139 /*
140 * Populate the temporary CompactAttribute from the corresponding
141 * Form_pg_attribute
142 */
144
145 /*
146 * Make the attcacheoff match since it's been reset to -1 by
147 * populate_compact_attribute_internal. Same with attnullability.
148 */
149 tmp.attcacheoff = cattr.attcacheoff;
150 tmp.attnullability = cattr.attnullability;
151
152 /* Check the freshly populated CompactAttribute matches the TupleDesc's */
153 Assert(memcmp(&tmp, &cattr, sizeof(CompactAttribute)) == 0);
154#endif
155}
156
157/*
158 * CreateTemplateTupleDesc
159 * This function allocates an empty tuple descriptor structure.
160 *
161 * Tuple type ID information is initially set for an anonymous record type;
162 * caller can overwrite this if needed.
163 */
166{
167 TupleDesc desc;
168
169 /*
170 * sanity checks
171 */
172 Assert(natts >= 0);
173
174 /*
175 * Allocate enough memory for the tuple descriptor, the CompactAttribute
176 * array and also an array of FormData_pg_attribute.
177 *
178 * Note: the FormData_pg_attribute array stride is
179 * sizeof(FormData_pg_attribute), since we declare the array elements as
180 * FormData_pg_attribute for notational convenience. However, we only
181 * guarantee that the first ATTRIBUTE_FIXED_PART_SIZE bytes of each entry
182 * are valid; most code that copies tupdesc entries around copies just
183 * that much. In principle that could be less due to trailing padding,
184 * although with the current definition of pg_attribute there probably
185 * isn't any padding.
186 */
187 desc = (TupleDesc) palloc(offsetof(struct TupleDescData, compact_attrs) +
188 natts * sizeof(CompactAttribute) +
189 natts * sizeof(FormData_pg_attribute));
190
191 /*
192 * Initialize other fields of the tupdesc.
193 */
194 desc->natts = natts;
195 desc->constr = NULL;
196 desc->tdtypeid = RECORDOID;
197 desc->tdtypmod = -1;
198 desc->tdrefcount = -1; /* assume not reference-counted */
199
200 /* This will be set to the correct value by TupleDescFinalize() */
201 desc->firstNonCachedOffsetAttr = -1;
202 desc->firstNonGuaranteedAttr = -1;
203
204 return desc;
205}
206
207/*
208 * CreateTupleDesc
209 * This function allocates a new TupleDesc by copying a given
210 * Form_pg_attribute array.
211 *
212 * Tuple type ID information is initially set for an anonymous record type;
213 * caller can overwrite this if needed.
214 */
217{
218 TupleDesc desc;
219 int i;
220
221 desc = CreateTemplateTupleDesc(natts);
222
223 for (i = 0; i < natts; ++i)
224 {
227 }
228
229 TupleDescFinalize(desc);
230
231 return desc;
232}
233
234/*
235 * CreateTupleDescCopy
236 * This function creates a new TupleDesc by copying from an existing
237 * TupleDesc.
238 *
239 * !!! Constraints and defaults are not copied !!!
240 */
243{
244 TupleDesc desc;
245 int i;
246
247 desc = CreateTemplateTupleDesc(tupdesc->natts);
248
249 /* Flat-copy the attribute array */
250 memcpy(TupleDescAttr(desc, 0),
251 TupleDescAttr(tupdesc, 0),
252 desc->natts * sizeof(FormData_pg_attribute));
253
254 /*
255 * Since we're not copying constraints and defaults, clear fields
256 * associated with them.
257 */
258 for (i = 0; i < desc->natts; i++)
259 {
261
262 att->attnotnull = false;
263 att->atthasdef = false;
264 att->atthasmissing = false;
265 att->attidentity = '\0';
266 att->attgenerated = '\0';
267
269 }
270
271 /* We can copy the tuple type identification, too */
272 desc->tdtypeid = tupdesc->tdtypeid;
273 desc->tdtypmod = tupdesc->tdtypmod;
274
275 TupleDescFinalize(desc);
276
277 return desc;
278}
279
280/*
281 * CreateTupleDescTruncatedCopy
282 * This function creates a new TupleDesc with only the first 'natts'
283 * attributes from an existing TupleDesc
284 *
285 * !!! Constraints and defaults are not copied !!!
286 */
289{
290 TupleDesc desc;
291 int i;
292
294
295 desc = CreateTemplateTupleDesc(natts);
296
297 /* Flat-copy the attribute array */
298 memcpy(TupleDescAttr(desc, 0),
299 TupleDescAttr(tupdesc, 0),
300 desc->natts * sizeof(FormData_pg_attribute));
301
302 /*
303 * Since we're not copying constraints and defaults, clear fields
304 * associated with them.
305 */
306 for (i = 0; i < desc->natts; i++)
307 {
309
310 att->attnotnull = false;
311 att->atthasdef = false;
312 att->atthasmissing = false;
313 att->attidentity = '\0';
314 att->attgenerated = '\0';
315
317 }
318
319 /* We can copy the tuple type identification, too */
320 desc->tdtypeid = tupdesc->tdtypeid;
321 desc->tdtypmod = tupdesc->tdtypmod;
322
323 TupleDescFinalize(desc);
324
325 return desc;
326}
327
328/*
329 * CreateTupleDescCopyConstr
330 * This function creates a new TupleDesc by copying from an existing
331 * TupleDesc (including its constraints and defaults).
332 */
335{
336 TupleDesc desc;
337 TupleConstr *constr = tupdesc->constr;
338 int i;
339
340 desc = CreateTemplateTupleDesc(tupdesc->natts);
341
342 /* Flat-copy the attribute array */
343 memcpy(TupleDescAttr(desc, 0),
344 TupleDescAttr(tupdesc, 0),
345 desc->natts * sizeof(FormData_pg_attribute));
346
347 for (i = 0; i < desc->natts; i++)
348 {
350
353 }
354
355 /* Copy the TupleConstr data structure, if any */
356 if (constr)
357 {
359
360 cpy->has_not_null = constr->has_not_null;
361 cpy->has_generated_stored = constr->has_generated_stored;
362 cpy->has_generated_virtual = constr->has_generated_virtual;
363
364 if ((cpy->num_defval = constr->num_defval) > 0)
365 {
366 cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));
367 memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
368 for (i = cpy->num_defval - 1; i >= 0; i--)
369 cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
370 }
371
372 if (constr->missing)
373 {
374 cpy->missing = (AttrMissing *) palloc(tupdesc->natts * sizeof(AttrMissing));
375 memcpy(cpy->missing, constr->missing, tupdesc->natts * sizeof(AttrMissing));
376 for (i = tupdesc->natts - 1; i >= 0; i--)
377 {
378 if (constr->missing[i].am_present)
379 {
380 CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
381
382 cpy->missing[i].am_value = datumCopy(constr->missing[i].am_value,
383 attr->attbyval,
384 attr->attlen);
385 }
386 }
387 }
388
389 if ((cpy->num_check = constr->num_check) > 0)
390 {
391 cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));
392 memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
393 for (i = cpy->num_check - 1; i >= 0; i--)
394 {
395 cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
396 cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
397 cpy->check[i].ccenforced = constr->check[i].ccenforced;
398 cpy->check[i].ccvalid = constr->check[i].ccvalid;
399 cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
400 }
401 }
402
403 desc->constr = cpy;
404 }
405
406 /* We can copy the tuple type identification, too */
407 desc->tdtypeid = tupdesc->tdtypeid;
408 desc->tdtypmod = tupdesc->tdtypmod;
409
410 TupleDescFinalize(desc);
411
412 return desc;
413}
414
415/*
416 * TupleDescCopy
417 * Copy a tuple descriptor into caller-supplied memory.
418 * The memory may be shared memory mapped at any address, and must
419 * be sufficient to hold TupleDescSize(src) bytes.
420 *
421 * !!! Constraints and defaults are not copied !!!
422 */
423void
425{
426 int i;
427
428 /* Flat-copy the header and attribute arrays */
429 memcpy(dst, src, TupleDescSize(src));
430
431 /*
432 * Since we're not copying constraints and defaults, clear fields
433 * associated with them.
434 */
435 for (i = 0; i < dst->natts; i++)
436 {
438
439 att->attnotnull = false;
440 att->atthasdef = false;
441 att->atthasmissing = false;
442 att->attidentity = '\0';
443 att->attgenerated = '\0';
444
446 }
447 dst->constr = NULL;
448
449 /*
450 * Also, assume the destination is not to be ref-counted. (Copying the
451 * source's refcount would be wrong in any case.)
452 */
453 dst->tdrefcount = -1;
454
456}
457
458/*
459 * TupleDescCopyEntry
460 * This function copies a single attribute structure from one tuple
461 * descriptor to another.
462 *
463 * !!! Constraints and defaults are not copied !!!
464 *
465 * The caller must take care of calling TupleDescFinalize() on 'dst' once all
466 * TupleDesc changes have been made.
467 */
468void
471{
474
475 /*
476 * sanity checks
477 */
478 Assert(src);
479 Assert(dst);
480 Assert(srcAttno >= 1);
482 Assert(dstAttno >= 1);
484
486
487 dstAtt->attnum = dstAttno;
488
489 /* since we're not copying constraints or defaults, clear these */
490 dstAtt->attnotnull = false;
491 dstAtt->atthasdef = false;
492 dstAtt->atthasmissing = false;
493 dstAtt->attidentity = '\0';
494 dstAtt->attgenerated = '\0';
495
497}
498
499/*
500 * TupleDescFinalize
501 * Finalize the given TupleDesc. This must be called after the
502 * attributes arrays have been populated or adjusted by any code.
503 *
504 * Must be called after populate_compact_attribute() and before
505 * BlessTupleDesc().
506 */
507void
509{
510 int firstNonCachedOffsetAttr = 0;
511 int firstNonGuaranteedAttr = tupdesc->natts;
512 int off = 0;
513
514 for (int i = 0; i < tupdesc->natts; i++)
515 {
517
518 /*
519 * Find the highest attnum which is guaranteed to exist in all tuples
520 * in the table. We currently only pay attention to byval attributes
521 * to allow additional optimizations during tuple deformation.
522 */
523 if (firstNonGuaranteedAttr == tupdesc->natts &&
524 (cattr->attnullability != ATTNULLABLE_VALID || !cattr->attbyval ||
525 cattr->atthasmissing || cattr->attisdropped || cattr->attlen <= 0))
526 firstNonGuaranteedAttr = i;
527
528 if (cattr->attlen <= 0)
529 break;
530
531 off = att_nominal_alignby(off, cattr->attalignby);
532
533 /*
534 * attcacheoff is an int16, so don't try to cache any offsets larger
535 * than will fit in that type. Any attributes which are offset more
536 * than 2^15 are likely due to variable-length attributes. Since we
537 * don't cache offsets for or beyond variable-length attributes, using
538 * an int16 rather than an int32 here is unlikely to cost us anything.
539 */
540 if (off > PG_INT16_MAX)
541 break;
542
543 cattr->attcacheoff = (int16) off;
544
545 off += cattr->attlen;
546 firstNonCachedOffsetAttr = i + 1;
547 }
548
549 tupdesc->firstNonCachedOffsetAttr = firstNonCachedOffsetAttr;
550 tupdesc->firstNonGuaranteedAttr = firstNonGuaranteedAttr;
551}
552
553/*
554 * Free a TupleDesc including all substructure
555 */
556void
558{
559 int i;
560
561 /*
562 * Possibly this should assert tdrefcount == 0, to disallow explicit
563 * freeing of un-refcounted tupdescs?
564 */
565 Assert(tupdesc->tdrefcount <= 0);
566
567 if (tupdesc->constr)
568 {
569 if (tupdesc->constr->num_defval > 0)
570 {
571 AttrDefault *attrdef = tupdesc->constr->defval;
572
573 for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
574 pfree(attrdef[i].adbin);
575 pfree(attrdef);
576 }
577 if (tupdesc->constr->missing)
578 {
579 AttrMissing *attrmiss = tupdesc->constr->missing;
580
581 for (i = tupdesc->natts - 1; i >= 0; i--)
582 {
583 if (attrmiss[i].am_present
584 && !TupleDescAttr(tupdesc, i)->attbyval)
585 pfree(DatumGetPointer(attrmiss[i].am_value));
586 }
588 }
589 if (tupdesc->constr->num_check > 0)
590 {
591 ConstrCheck *check = tupdesc->constr->check;
592
593 for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
594 {
595 pfree(check[i].ccname);
596 pfree(check[i].ccbin);
597 }
598 pfree(check);
599 }
600 pfree(tupdesc->constr);
601 }
602
603 pfree(tupdesc);
604}
605
606/*
607 * Increment the reference count of a tupdesc, and log the reference in
608 * CurrentResourceOwner.
609 *
610 * Do not apply this to tupdescs that are not being refcounted. (Use the
611 * macro PinTupleDesc for tupdescs of uncertain status.)
612 */
613void
622
623/*
624 * Decrement the reference count of a tupdesc, remove the corresponding
625 * reference from CurrentResourceOwner, and free the tupdesc if no more
626 * references remain.
627 *
628 * Do not apply this to tupdescs that are not being refcounted. (Use the
629 * macro ReleaseTupleDesc for tupdescs of uncertain status.)
630 */
631void
633{
634 Assert(tupdesc->tdrefcount > 0);
635
637 if (--tupdesc->tdrefcount == 0)
638 FreeTupleDesc(tupdesc);
639}
640
641/*
642 * Compare two TupleDesc structures for logical equality
643 */
644bool
646{
647 int i,
648 n;
649
650 if (tupdesc1->natts != tupdesc2->natts)
651 return false;
652 if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
653 return false;
654
655 /* tdtypmod and tdrefcount are not checked */
656
657 for (i = 0; i < tupdesc1->natts; i++)
658 {
661
662 /*
663 * We do not need to check every single field here: we can disregard
664 * attrelid and attnum (which were used to place the row in the attrs
665 * array in the first place). It might look like we could dispense
666 * with checking attlen/attbyval/attalign, since these are derived
667 * from atttypid; but in the case of dropped columns we must check
668 * them (since atttypid will be zero for all dropped columns) and in
669 * general it seems safer to check them always.
670 *
671 * We intentionally ignore atthasmissing, since that's not very
672 * relevant in tupdescs, which lack the attmissingval field.
673 */
674 if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
675 return false;
676 if (attr1->atttypid != attr2->atttypid)
677 return false;
678 if (attr1->attlen != attr2->attlen)
679 return false;
680 if (attr1->attndims != attr2->attndims)
681 return false;
682 if (attr1->atttypmod != attr2->atttypmod)
683 return false;
684 if (attr1->attbyval != attr2->attbyval)
685 return false;
686 if (attr1->attalign != attr2->attalign)
687 return false;
688 if (attr1->attstorage != attr2->attstorage)
689 return false;
690 if (attr1->attcompression != attr2->attcompression)
691 return false;
692 if (attr1->attnotnull != attr2->attnotnull)
693 return false;
694
695 /*
696 * When the column has a not-null constraint, we also need to consider
697 * its validity aspect, which only manifests in CompactAttribute->
698 * attnullability, so verify that.
699 */
700 if (attr1->attnotnull)
701 {
704
705 Assert(cattr1->attnullability != ATTNULLABLE_UNKNOWN);
706 Assert((cattr1->attnullability == ATTNULLABLE_UNKNOWN) ==
707 (cattr2->attnullability == ATTNULLABLE_UNKNOWN));
708
709 if (cattr1->attnullability != cattr2->attnullability)
710 return false;
711 }
712 if (attr1->atthasdef != attr2->atthasdef)
713 return false;
714 if (attr1->attidentity != attr2->attidentity)
715 return false;
716 if (attr1->attgenerated != attr2->attgenerated)
717 return false;
718 if (attr1->attisdropped != attr2->attisdropped)
719 return false;
720 if (attr1->attislocal != attr2->attislocal)
721 return false;
722 if (attr1->attinhcount != attr2->attinhcount)
723 return false;
724 if (attr1->attcollation != attr2->attcollation)
725 return false;
726 /* variable-length fields are not even present... */
727 }
728
729 if (tupdesc1->constr != NULL)
730 {
731 TupleConstr *constr1 = tupdesc1->constr;
732 TupleConstr *constr2 = tupdesc2->constr;
733
734 if (constr2 == NULL)
735 return false;
736 if (constr1->has_not_null != constr2->has_not_null)
737 return false;
738 if (constr1->has_generated_stored != constr2->has_generated_stored)
739 return false;
740 if (constr1->has_generated_virtual != constr2->has_generated_virtual)
741 return false;
742 n = constr1->num_defval;
743 if (n != (int) constr2->num_defval)
744 return false;
745 /* We assume here that both AttrDefault arrays are in adnum order */
746 for (i = 0; i < n; i++)
747 {
748 AttrDefault *defval1 = constr1->defval + i;
749 AttrDefault *defval2 = constr2->defval + i;
750
751 if (defval1->adnum != defval2->adnum)
752 return false;
753 if (strcmp(defval1->adbin, defval2->adbin) != 0)
754 return false;
755 }
756 if (constr1->missing)
757 {
758 if (!constr2->missing)
759 return false;
760 for (i = 0; i < tupdesc1->natts; i++)
761 {
762 AttrMissing *missval1 = constr1->missing + i;
763 AttrMissing *missval2 = constr2->missing + i;
764
765 if (missval1->am_present != missval2->am_present)
766 return false;
767 if (missval1->am_present)
768 {
770
771 if (!datumIsEqual(missval1->am_value, missval2->am_value,
772 missatt1->attbyval, missatt1->attlen))
773 return false;
774 }
775 }
776 }
777 else if (constr2->missing)
778 return false;
779 n = constr1->num_check;
780 if (n != (int) constr2->num_check)
781 return false;
782
783 /*
784 * Similarly, we rely here on the ConstrCheck entries being sorted by
785 * name. If there are duplicate names, the outcome of the comparison
786 * is uncertain, but that should not happen.
787 */
788 for (i = 0; i < n; i++)
789 {
790 ConstrCheck *check1 = constr1->check + i;
791 ConstrCheck *check2 = constr2->check + i;
792
793 if (!(strcmp(check1->ccname, check2->ccname) == 0 &&
794 strcmp(check1->ccbin, check2->ccbin) == 0 &&
795 check1->ccenforced == check2->ccenforced &&
796 check1->ccvalid == check2->ccvalid &&
797 check1->ccnoinherit == check2->ccnoinherit))
798 return false;
799 }
800 }
801 else if (tupdesc2->constr != NULL)
802 return false;
803 return true;
804}
805
806/*
807 * equalRowTypes
808 *
809 * This determines whether two tuple descriptors have equal row types. This
810 * only checks those fields in pg_attribute that are applicable for row types,
811 * while ignoring those fields that define the physical row storage or those
812 * that define table column metadata.
813 *
814 * Specifically, this checks:
815 *
816 * - same number of attributes
817 * - same composite type ID (but could both be zero)
818 * - corresponding attributes (in order) have same the name, type, typmod,
819 * collation
820 *
821 * This is used to check whether two record types are compatible, whether
822 * function return row types are the same, and other similar situations.
823 *
824 * (XXX There was some discussion whether attndims should be checked here, but
825 * for now it has been decided not to.)
826 *
827 * Note: We deliberately do not check the tdtypmod field. This allows
828 * typcache.c to use this routine to see if a cached record type matches a
829 * requested type.
830 */
831bool
833{
834 if (tupdesc1->natts != tupdesc2->natts)
835 return false;
836 if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
837 return false;
838
839 for (int i = 0; i < tupdesc1->natts; i++)
840 {
843
844 if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
845 return false;
846 if (attr1->atttypid != attr2->atttypid)
847 return false;
848 if (attr1->atttypmod != attr2->atttypmod)
849 return false;
850 if (attr1->attcollation != attr2->attcollation)
851 return false;
852
853 /* Record types derived from tables could have dropped fields. */
854 if (attr1->attisdropped != attr2->attisdropped)
855 return false;
856 }
857
858 return true;
859}
860
861/*
862 * hashRowType
863 *
864 * If two tuple descriptors would be considered equal by equalRowTypes()
865 * then their hash value will be equal according to this function.
866 */
867uint32
869{
870 uint32 s;
871 int i;
872
873 s = hash_combine(0, hash_bytes_uint32(desc->natts));
875 for (i = 0; i < desc->natts; ++i)
877
878 return s;
879}
880
881/*
882 * TupleDescInitEntry
883 * This function initializes a single attribute structure in
884 * a previously allocated tuple descriptor.
885 *
886 * If attributeName is NULL, the attname field is set to an empty string
887 * (this is for cases where we don't know or need a name for the field).
888 * Also, some callers use this function to change the datatype-related fields
889 * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
890 * to indicate that the attname field shouldn't be modified.
891 *
892 * Note that attcollation is set to the default for the specified datatype.
893 * If a nondefault collation is needed, insert it afterwards using
894 * TupleDescInitEntryCollation.
895 */
896void
899 const char *attributeName,
901 int32 typmod,
902 int attdim)
903{
904 HeapTuple tuple;
907
908 /*
909 * sanity checks
910 */
911 Assert(desc);
914 Assert(attdim >= 0);
916
917 /*
918 * initialize the attribute fields
919 */
920 att = TupleDescAttr(desc, attributeNumber - 1);
921
922 att->attrelid = 0; /* dummy value */
923
924 /*
925 * Note: attributeName can be NULL, because the planner doesn't always
926 * fill in valid resname values in targetlists, particularly for resjunk
927 * attributes. Also, do nothing if caller wants to re-use the old attname.
928 */
929 if (attributeName == NULL)
930 MemSet(NameStr(att->attname), 0, NAMEDATALEN);
931 else if (attributeName != NameStr(att->attname))
932 namestrcpy(&(att->attname), attributeName);
933
934 att->atttypmod = typmod;
935
936 att->attnum = attributeNumber;
937 att->attndims = attdim;
938
939 att->attnotnull = false;
940 att->atthasdef = false;
941 att->atthasmissing = false;
942 att->attidentity = '\0';
943 att->attgenerated = '\0';
944 att->attisdropped = false;
945 att->attislocal = true;
946 att->attinhcount = 0;
947 /* variable-length fields are not present in tupledescs */
948
950 if (!HeapTupleIsValid(tuple))
951 elog(ERROR, "cache lookup failed for type %u", oidtypeid);
953
954 att->atttypid = oidtypeid;
955 att->attlen = typeForm->typlen;
956 att->attbyval = typeForm->typbyval;
957 att->attalign = typeForm->typalign;
958 att->attstorage = typeForm->typstorage;
959 att->attcompression = InvalidCompressionMethod;
960 att->attcollation = typeForm->typcollation;
961
963
964 ReleaseSysCache(tuple);
965}
966
967/*
968 * TupleDescInitBuiltinEntry
969 * Initialize a tuple descriptor without catalog access. Only
970 * a limited range of builtin types are supported.
971 */
972void
975 const char *attributeName,
977 int32 typmod,
978 int attdim)
979{
981
982 /* sanity checks */
983 Assert(desc);
986 Assert(attdim >= 0);
988
989 /* initialize the attribute fields */
990 att = TupleDescAttr(desc, attributeNumber - 1);
991 att->attrelid = 0; /* dummy value */
992
993 /* unlike TupleDescInitEntry, we require an attribute name */
995 namestrcpy(&(att->attname), attributeName);
996
997 att->atttypmod = typmod;
998
999 att->attnum = attributeNumber;
1000 att->attndims = attdim;
1001
1002 att->attnotnull = false;
1003 att->atthasdef = false;
1004 att->atthasmissing = false;
1005 att->attidentity = '\0';
1006 att->attgenerated = '\0';
1007 att->attisdropped = false;
1008 att->attislocal = true;
1009 att->attinhcount = 0;
1010 /* variable-length fields are not present in tupledescs */
1011
1012 att->atttypid = oidtypeid;
1013
1014 /*
1015 * Our goal here is to support just enough types to let basic builtin
1016 * commands work without catalog access - e.g. so that we can do certain
1017 * things even in processes that are not connected to a database.
1018 */
1019 switch (oidtypeid)
1020 {
1021 case TEXTOID:
1022 case TEXTARRAYOID:
1023 att->attlen = -1;
1024 att->attbyval = false;
1025 att->attalign = TYPALIGN_INT;
1026 att->attstorage = TYPSTORAGE_EXTENDED;
1027 att->attcompression = InvalidCompressionMethod;
1028 att->attcollation = DEFAULT_COLLATION_OID;
1029 break;
1030
1031 case BOOLOID:
1032 att->attlen = 1;
1033 att->attbyval = true;
1034 att->attalign = TYPALIGN_CHAR;
1035 att->attstorage = TYPSTORAGE_PLAIN;
1036 att->attcompression = InvalidCompressionMethod;
1037 att->attcollation = InvalidOid;
1038 break;
1039
1040 case INT4OID:
1041 att->attlen = 4;
1042 att->attbyval = true;
1043 att->attalign = TYPALIGN_INT;
1044 att->attstorage = TYPSTORAGE_PLAIN;
1045 att->attcompression = InvalidCompressionMethod;
1046 att->attcollation = InvalidOid;
1047 break;
1048
1049 case INT8OID:
1050 att->attlen = 8;
1051 att->attbyval = true;
1052 att->attalign = TYPALIGN_DOUBLE;
1053 att->attstorage = TYPSTORAGE_PLAIN;
1054 att->attcompression = InvalidCompressionMethod;
1055 att->attcollation = InvalidOid;
1056 break;
1057
1058 case OIDOID:
1059 att->attlen = 4;
1060 att->attbyval = true;
1061 att->attalign = TYPALIGN_INT;
1062 att->attstorage = TYPSTORAGE_PLAIN;
1063 att->attcompression = InvalidCompressionMethod;
1064 att->attcollation = InvalidOid;
1065 break;
1066
1067 default:
1068 elog(ERROR, "unsupported type %u", oidtypeid);
1069 }
1070
1072}
1073
1074/*
1075 * TupleDescInitEntryCollation
1076 *
1077 * Assign a nondefault collation to a previously initialized tuple descriptor
1078 * entry.
1079 */
1080void
1084{
1085 /*
1086 * sanity checks
1087 */
1088 Assert(desc);
1089 Assert(attributeNumber >= 1);
1091
1092 TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
1093}
1094
1095/*
1096 * BuildDescFromLists
1097 *
1098 * Build a TupleDesc given lists of column names (as String nodes),
1099 * column type OIDs, typmods, and collation OIDs.
1100 *
1101 * No constraints are generated.
1102 *
1103 * This is for use with functions returning RECORD.
1104 */
1106BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
1107{
1108 int natts;
1110 ListCell *l1;
1111 ListCell *l2;
1112 ListCell *l3;
1113 ListCell *l4;
1114 TupleDesc desc;
1115
1116 natts = list_length(names);
1117 Assert(natts == list_length(types));
1118 Assert(natts == list_length(typmods));
1119 Assert(natts == list_length(collations));
1120
1121 /*
1122 * allocate a new tuple descriptor
1123 */
1124 desc = CreateTemplateTupleDesc(natts);
1125
1126 attnum = 0;
1127 forfour(l1, names, l2, types, l3, typmods, l4, collations)
1128 {
1129 char *attname = strVal(lfirst(l1));
1130 Oid atttypid = lfirst_oid(l2);
1131 int32 atttypmod = lfirst_int(l3);
1132 Oid attcollation = lfirst_oid(l4);
1133
1134 attnum++;
1135
1136 TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
1137 TupleDescInitEntryCollation(desc, attnum, attcollation);
1138 }
1139
1140 TupleDescFinalize(desc);
1141
1142 return desc;
1143}
1144
1145/*
1146 * Get default expression (or NULL if none) for the given attribute number.
1147 */
1148Node *
1150{
1151 Node *result = NULL;
1152
1153 if (tupdesc->constr)
1154 {
1155 AttrDefault *attrdef = tupdesc->constr->defval;
1156
1157 for (int i = 0; i < tupdesc->constr->num_defval; i++)
1158 {
1159 if (attrdef[i].adnum == attnum)
1160 {
1161 result = stringToNode(attrdef[i].adbin);
1162 break;
1163 }
1164 }
1165 }
1166
1167 return result;
1168}
1169
1170/* ResourceOwner callbacks */
1171
1172static void
1174{
1175 TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1176
1177 /* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
1178 Assert(tupdesc->tdrefcount > 0);
1179 if (--tupdesc->tdrefcount == 0)
1180 FreeTupleDesc(tupdesc);
1181}
1182
1183static char *
1185{
1186 TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1187
1188 return psprintf("TupleDesc %p (%u,%d)",
1189 tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1190}
int16 AttrNumber
Definition attnum.h:21
#define NameStr(name)
Definition c.h:837
#define Assert(condition)
Definition c.h:945
int16_t int16
Definition c.h:613
int32_t int32
Definition c.h:614
uint32_t uint32
Definition c.h:618
#define MemSet(start, val, len)
Definition c.h:1109
#define PG_INT16_MAX
Definition c.h:672
bool IsCatalogRelationOid(Oid relid)
Definition catalog.c:121
Datum datumCopy(Datum value, bool typByVal, int typLen)
Definition datum.c:132
bool datumIsEqual(Datum value1, Datum value2, bool typByVal, int typLen)
Definition datum.c:223
struct typedefs * types
Definition ecpg.c:30
#define ERROR
Definition elog.h:39
#define elog(elevel,...)
Definition elog.h:226
#define palloc0_object(type)
Definition fe_memutils.h:75
uint32 hash_bytes_uint32(uint32 k)
Definition hashfn.c:610
static uint32 hash_combine(uint32 a, uint32 b)
Definition hashfn.h:68
#define HeapTupleIsValid(tuple)
Definition htup.h:78
static void * GETSTRUCT(const HeapTupleData *tuple)
int i
Definition isn.c:77
char * pstrdup(const char *in)
Definition mcxt.c:1781
void pfree(void *pointer)
Definition mcxt.c:1616
void * palloc(Size size)
Definition mcxt.c:1387
void namestrcpy(Name name, const char *str)
Definition name.c:233
FormData_pg_attribute
NameData attname
#define ATTRIBUTE_FIXED_PART_SIZE
int16 attnum
FormData_pg_attribute * Form_pg_attribute
#define NAMEDATALEN
#define lfirst(lc)
Definition pg_list.h:172
static int list_length(const List *l)
Definition pg_list.h:152
#define lfirst_int(lc)
Definition pg_list.h:173
#define forfour(cell1, list1, cell2, list2, cell3, list3, cell4, list4)
Definition pg_list.h:575
#define lfirst_oid(lc)
Definition pg_list.h:174
END_CATALOG_STRUCT typedef FormData_pg_type * Form_pg_type
Definition pg_type.h:265
static Datum PointerGetDatum(const void *X)
Definition postgres.h:342
static Datum ObjectIdGetDatum(Oid X)
Definition postgres.h:252
uint64_t Datum
Definition postgres.h:70
static Pointer DatumGetPointer(Datum X)
Definition postgres.h:332
#define InvalidOid
unsigned int Oid
static int fb(int x)
char * psprintf(const char *fmt,...)
Definition psprintf.c:43
void * stringToNode(const char *str)
Definition read.c:90
ResourceOwner CurrentResourceOwner
Definition resowner.c:173
void ResourceOwnerForget(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition resowner.c:561
void ResourceOwnerRemember(ResourceOwner owner, Datum value, const ResourceOwnerDesc *kind)
Definition resowner.c:521
void ResourceOwnerEnlarge(ResourceOwner owner)
Definition resowner.c:449
@ RESOURCE_RELEASE_AFTER_LOCKS
Definition resowner.h:56
#define RELEASE_PRIO_TUPDESC_REFS
Definition resowner.h:74
char * adbin
Definition tupdesc.h:25
int16 attcacheoff
Definition tupdesc.h:70
char attnullability
Definition tupdesc.h:80
char * ccname
Definition tupdesc.h:30
bool ccenforced
Definition tupdesc.h:32
bool ccnoinherit
Definition tupdesc.h:34
bool ccvalid
Definition tupdesc.h:33
char * ccbin
Definition tupdesc.h:31
Definition pg_list.h:54
Definition nodes.h:135
const char * name
Definition resowner.h:93
bool has_generated_virtual
Definition tupdesc.h:47
bool has_not_null
Definition tupdesc.h:45
AttrDefault * defval
Definition tupdesc.h:40
bool has_generated_stored
Definition tupdesc.h:46
struct AttrMissing * missing
Definition tupdesc.h:42
ConstrCheck * check
Definition tupdesc.h:41
uint16 num_defval
Definition tupdesc.h:43
uint16 num_check
Definition tupdesc.h:44
int firstNonCachedOffsetAttr
Definition tupdesc.h:154
CompactAttribute compact_attrs[FLEXIBLE_ARRAY_MEMBER]
Definition tupdesc.h:161
TupleConstr * constr
Definition tupdesc.h:159
int32 tdtypmod
Definition tupdesc.h:152
int firstNonGuaranteedAttr
Definition tupdesc.h:156
void ReleaseSysCache(HeapTuple tuple)
Definition syscache.c:264
HeapTuple SearchSysCache1(SysCacheIdentifier cacheId, Datum key1)
Definition syscache.c:220
#define InvalidCompressionMethod
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition tupdesc.c:334
void TupleDescCopy(TupleDesc dst, TupleDesc src)
Definition tupdesc.c:424
static void populate_compact_attribute_internal(Form_pg_attribute src, CompactAttribute *dst)
Definition tupdesc.c:65
Node * TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
Definition tupdesc.c:1149
void DecrTupleDescRefCount(TupleDesc tupdesc)
Definition tupdesc.c:632
void FreeTupleDesc(TupleDesc tupdesc)
Definition tupdesc.c:557
void IncrTupleDescRefCount(TupleDesc tupdesc)
Definition tupdesc.c:614
void verify_compact_attribute(TupleDesc tupdesc, int attnum)
Definition tupdesc.c:125
TupleDesc CreateTemplateTupleDesc(int natts)
Definition tupdesc.c:165
static void ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
Definition tupdesc.c:49
static void ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
Definition tupdesc.c:55
void TupleDescFinalize(TupleDesc tupdesc)
Definition tupdesc.c:508
static void ResOwnerReleaseTupleDesc(Datum res)
Definition tupdesc.c:1173
uint32 hashRowType(TupleDesc desc)
Definition tupdesc.c:868
TupleDesc CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
Definition tupdesc.c:288
TupleDesc CreateTupleDescCopy(TupleDesc tupdesc)
Definition tupdesc.c:242
void TupleDescInitBuiltinEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:973
static const ResourceOwnerDesc tupdesc_resowner_desc
Definition tupdesc.c:38
TupleDesc BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
Definition tupdesc.c:1106
void populate_compact_attribute(TupleDesc tupdesc, int attnum)
Definition tupdesc.c:100
bool equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition tupdesc.c:832
void TupleDescInitEntryCollation(TupleDesc desc, AttrNumber attributeNumber, Oid collationid)
Definition tupdesc.c:1081
TupleDesc CreateTupleDesc(int natts, Form_pg_attribute *attrs)
Definition tupdesc.c:216
void TupleDescInitEntry(TupleDesc desc, AttrNumber attributeNumber, const char *attributeName, Oid oidtypeid, int32 typmod, int attdim)
Definition tupdesc.c:897
bool equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
Definition tupdesc.c:645
void TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno, TupleDesc src, AttrNumber srcAttno)
Definition tupdesc.c:469
static char * ResOwnerPrintTupleDesc(Datum res)
Definition tupdesc.c:1184
#define TupleDescSize(src)
Definition tupdesc.h:216
#define ATTNULLABLE_UNKNOWN
Definition tupdesc.h:85
#define ATTNULLABLE_VALID
Definition tupdesc.h:86
static FormData_pg_attribute * TupleDescAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:178
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition tupdesc.h:193
struct TupleDescData * TupleDesc
Definition tupdesc.h:163
#define ATTNULLABLE_UNRESTRICTED
Definition tupdesc.h:84
#define att_nominal_alignby(cur_offset, attalignby)
Definition tupmacs.h:411
static uint8 typalign_to_alignby(char typalign)
Definition tupmacs.h:302
#define strVal(v)
Definition value.h:82