Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * tupdesc.c
4 : * POSTGRES tuple descriptor support code
5 : *
6 : * Portions Copyright (c) 1996-2025, 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 "access/toast_compression.h"
24 : #include "access/tupdesc_details.h"
25 : #include "catalog/pg_collation.h"
26 : #include "catalog/pg_type.h"
27 : #include "common/hashfn.h"
28 : #include "utils/builtins.h"
29 : #include "utils/datum.h"
30 : #include "utils/resowner.h"
31 : #include "utils/syscache.h"
32 :
33 : /* ResourceOwner callbacks to hold tupledesc references */
34 : static void ResOwnerReleaseTupleDesc(Datum res);
35 : static char *ResOwnerPrintTupleDesc(Datum res);
36 :
37 : static const ResourceOwnerDesc tupdesc_resowner_desc =
38 : {
39 : .name = "tupdesc reference",
40 : .release_phase = RESOURCE_RELEASE_AFTER_LOCKS,
41 : .release_priority = RELEASE_PRIO_TUPDESC_REFS,
42 : .ReleaseResource = ResOwnerReleaseTupleDesc,
43 : .DebugPrint = ResOwnerPrintTupleDesc
44 : };
45 :
46 : /* Convenience wrappers over ResourceOwnerRemember/Forget */
47 : static inline void
48 30020244 : ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
49 : {
50 30020244 : ResourceOwnerRemember(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
51 30020244 : }
52 :
53 : static inline void
54 30006870 : ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
55 : {
56 30006870 : ResourceOwnerForget(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
57 30006870 : }
58 :
59 : /*
60 : * populate_compact_attribute_internal
61 : * Helper function for populate_compact_attribute()
62 : */
63 : static inline void
64 65848580 : populate_compact_attribute_internal(Form_pg_attribute src,
65 : CompactAttribute *dst)
66 : {
67 65848580 : memset(dst, 0, sizeof(CompactAttribute));
68 :
69 65848580 : dst->attcacheoff = -1;
70 65848580 : dst->attlen = src->attlen;
71 :
72 65848580 : dst->attbyval = src->attbyval;
73 65848580 : dst->attispackable = (src->attstorage != TYPSTORAGE_PLAIN);
74 65848580 : dst->atthasmissing = src->atthasmissing;
75 65848580 : dst->attisdropped = src->attisdropped;
76 65848580 : dst->attgenerated = (src->attgenerated != '\0');
77 65848580 : dst->attnotnull = src->attnotnull;
78 :
79 65848580 : switch (src->attalign)
80 : {
81 41721020 : case TYPALIGN_INT:
82 41721020 : dst->attalignby = ALIGNOF_INT;
83 41721020 : break;
84 16766340 : case TYPALIGN_CHAR:
85 16766340 : dst->attalignby = sizeof(char);
86 16766340 : break;
87 4166046 : case TYPALIGN_DOUBLE:
88 4166046 : dst->attalignby = ALIGNOF_DOUBLE;
89 4166046 : break;
90 3195174 : case TYPALIGN_SHORT:
91 3195174 : dst->attalignby = ALIGNOF_SHORT;
92 3195174 : break;
93 0 : default:
94 0 : dst->attalignby = 0;
95 0 : elog(ERROR, "invalid attalign value: %c", src->attalign);
96 : break;
97 : }
98 65848580 : }
99 :
100 : /*
101 : * populate_compact_attribute
102 : * Fill in the corresponding CompactAttribute element from the
103 : * Form_pg_attribute for the given attribute number. This must be called
104 : * whenever a change is made to a Form_pg_attribute in the TupleDesc.
105 : */
106 : void
107 65848580 : populate_compact_attribute(TupleDesc tupdesc, int attnum)
108 : {
109 65848580 : Form_pg_attribute src = TupleDescAttr(tupdesc, attnum);
110 : CompactAttribute *dst;
111 :
112 : /*
113 : * Don't use TupleDescCompactAttr to prevent infinite recursion in assert
114 : * builds.
115 : */
116 65848580 : dst = &tupdesc->compact_attrs[attnum];
117 :
118 65848580 : populate_compact_attribute_internal(src, dst);
119 65848580 : }
120 :
121 : /*
122 : * verify_compact_attribute
123 : * In Assert enabled builds, we verify that the CompactAttribute is
124 : * populated correctly. This helps find bugs in places such as ALTER
125 : * TABLE where code makes changes to the FormData_pg_attribute but
126 : * forgets to call populate_compact_attribute().
127 : *
128 : * This is used in TupleDescCompactAttr(), but declared here to allow access
129 : * to populate_compact_attribute_internal().
130 : */
131 : void
132 0 : verify_compact_attribute(TupleDesc tupdesc, int attnum)
133 : {
134 : #ifdef USE_ASSERT_CHECKING
135 : CompactAttribute *cattr = &tupdesc->compact_attrs[attnum];
136 : Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum);
137 : CompactAttribute tmp;
138 :
139 : /*
140 : * Populate the temporary CompactAttribute from the corresponding
141 : * Form_pg_attribute
142 : */
143 : populate_compact_attribute_internal(attr, &tmp);
144 :
145 : /*
146 : * Make the attcacheoff match since it's been reset to -1 by
147 : * populate_compact_attribute_internal.
148 : */
149 : tmp.attcacheoff = cattr->attcacheoff;
150 :
151 : /* Check the freshly populated CompactAttribute matches the TupleDesc's */
152 : Assert(memcmp(&tmp, cattr, sizeof(CompactAttribute)) == 0);
153 : #endif
154 0 : }
155 :
156 : /*
157 : * CreateTemplateTupleDesc
158 : * This function allocates an empty tuple descriptor structure.
159 : *
160 : * Tuple type ID information is initially set for an anonymous record type;
161 : * caller can overwrite this if needed.
162 : */
163 : TupleDesc
164 9445866 : CreateTemplateTupleDesc(int natts)
165 : {
166 : TupleDesc desc;
167 :
168 : /*
169 : * sanity checks
170 : */
171 : Assert(natts >= 0);
172 :
173 : /*
174 : * Allocate enough memory for the tuple descriptor, the CompactAttribute
175 : * array and also an array of FormData_pg_attribute.
176 : *
177 : * Note: the FormData_pg_attribute array stride is
178 : * sizeof(FormData_pg_attribute), since we declare the array elements as
179 : * FormData_pg_attribute for notational convenience. However, we only
180 : * guarantee that the first ATTRIBUTE_FIXED_PART_SIZE bytes of each entry
181 : * are valid; most code that copies tupdesc entries around copies just
182 : * that much. In principle that could be less due to trailing padding,
183 : * although with the current definition of pg_attribute there probably
184 : * isn't any padding.
185 : */
186 9445866 : desc = (TupleDesc) palloc(offsetof(struct TupleDescData, compact_attrs) +
187 9445866 : natts * sizeof(CompactAttribute) +
188 : natts * sizeof(FormData_pg_attribute));
189 :
190 : /*
191 : * Initialize other fields of the tupdesc.
192 : */
193 9445866 : desc->natts = natts;
194 9445866 : desc->constr = NULL;
195 9445866 : desc->tdtypeid = RECORDOID;
196 9445866 : desc->tdtypmod = -1;
197 9445866 : desc->tdrefcount = -1; /* assume not reference-counted */
198 :
199 9445866 : return desc;
200 : }
201 :
202 : /*
203 : * CreateTupleDesc
204 : * This function allocates a new TupleDesc by copying a given
205 : * Form_pg_attribute array.
206 : *
207 : * Tuple type ID information is initially set for an anonymous record type;
208 : * caller can overwrite this if needed.
209 : */
210 : TupleDesc
211 1034670 : CreateTupleDesc(int natts, Form_pg_attribute *attrs)
212 : {
213 : TupleDesc desc;
214 : int i;
215 :
216 1034670 : desc = CreateTemplateTupleDesc(natts);
217 :
218 15797550 : for (i = 0; i < natts; ++i)
219 : {
220 14762880 : memcpy(TupleDescAttr(desc, i), attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
221 14762880 : populate_compact_attribute(desc, i);
222 : }
223 1034670 : return desc;
224 : }
225 :
226 : /*
227 : * CreateTupleDescCopy
228 : * This function creates a new TupleDesc by copying from an existing
229 : * TupleDesc.
230 : *
231 : * !!! Constraints and defaults are not copied !!!
232 : */
233 : TupleDesc
234 395482 : CreateTupleDescCopy(TupleDesc tupdesc)
235 : {
236 : TupleDesc desc;
237 : int i;
238 :
239 395482 : desc = CreateTemplateTupleDesc(tupdesc->natts);
240 :
241 : /* Flat-copy the attribute array */
242 395482 : memcpy(TupleDescAttr(desc, 0),
243 395482 : TupleDescAttr(tupdesc, 0),
244 395482 : desc->natts * sizeof(FormData_pg_attribute));
245 :
246 : /*
247 : * Since we're not copying constraints and defaults, clear fields
248 : * associated with them.
249 : */
250 2019294 : for (i = 0; i < desc->natts; i++)
251 : {
252 1623812 : Form_pg_attribute att = TupleDescAttr(desc, i);
253 :
254 1623812 : att->attnotnull = false;
255 1623812 : att->atthasdef = false;
256 1623812 : att->atthasmissing = false;
257 1623812 : att->attidentity = '\0';
258 1623812 : att->attgenerated = '\0';
259 :
260 1623812 : populate_compact_attribute(desc, i);
261 : }
262 :
263 : /* We can copy the tuple type identification, too */
264 395482 : desc->tdtypeid = tupdesc->tdtypeid;
265 395482 : desc->tdtypmod = tupdesc->tdtypmod;
266 :
267 395482 : return desc;
268 : }
269 :
270 : /*
271 : * CreateTupleDescTruncatedCopy
272 : * This function creates a new TupleDesc with only the first 'natts'
273 : * attributes from an existing TupleDesc
274 : *
275 : * !!! Constraints and defaults are not copied !!!
276 : */
277 : TupleDesc
278 35346 : CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
279 : {
280 : TupleDesc desc;
281 : int i;
282 :
283 : Assert(natts <= tupdesc->natts);
284 :
285 35346 : desc = CreateTemplateTupleDesc(natts);
286 :
287 : /* Flat-copy the attribute array */
288 35346 : memcpy(TupleDescAttr(desc, 0),
289 35346 : TupleDescAttr(tupdesc, 0),
290 35346 : desc->natts * sizeof(FormData_pg_attribute));
291 :
292 : /*
293 : * Since we're not copying constraints and defaults, clear fields
294 : * associated with them.
295 : */
296 82780 : for (i = 0; i < desc->natts; i++)
297 : {
298 47434 : Form_pg_attribute att = TupleDescAttr(desc, i);
299 :
300 47434 : att->attnotnull = false;
301 47434 : att->atthasdef = false;
302 47434 : att->atthasmissing = false;
303 47434 : att->attidentity = '\0';
304 47434 : att->attgenerated = '\0';
305 :
306 47434 : populate_compact_attribute(desc, i);
307 : }
308 :
309 : /* We can copy the tuple type identification, too */
310 35346 : desc->tdtypeid = tupdesc->tdtypeid;
311 35346 : desc->tdtypmod = tupdesc->tdtypmod;
312 :
313 35346 : return desc;
314 : }
315 :
316 : /*
317 : * CreateTupleDescCopyConstr
318 : * This function creates a new TupleDesc by copying from an existing
319 : * TupleDesc (including its constraints and defaults).
320 : */
321 : TupleDesc
322 749130 : CreateTupleDescCopyConstr(TupleDesc tupdesc)
323 : {
324 : TupleDesc desc;
325 749130 : TupleConstr *constr = tupdesc->constr;
326 : int i;
327 :
328 749130 : desc = CreateTemplateTupleDesc(tupdesc->natts);
329 :
330 : /* Flat-copy the attribute array */
331 749130 : memcpy(TupleDescAttr(desc, 0),
332 749130 : TupleDescAttr(tupdesc, 0),
333 749130 : desc->natts * sizeof(FormData_pg_attribute));
334 :
335 11048820 : for (i = 0; i < desc->natts; i++)
336 10299690 : populate_compact_attribute(desc, i);
337 :
338 : /* Copy the TupleConstr data structure, if any */
339 749130 : if (constr)
340 : {
341 693618 : TupleConstr *cpy = (TupleConstr *) palloc0(sizeof(TupleConstr));
342 :
343 693618 : cpy->has_not_null = constr->has_not_null;
344 693618 : cpy->has_generated_stored = constr->has_generated_stored;
345 693618 : cpy->has_generated_virtual = constr->has_generated_virtual;
346 :
347 693618 : if ((cpy->num_defval = constr->num_defval) > 0)
348 : {
349 3404 : cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));
350 3404 : memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
351 8354 : for (i = cpy->num_defval - 1; i >= 0; i--)
352 4950 : cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
353 : }
354 :
355 693618 : if (constr->missing)
356 : {
357 596 : cpy->missing = (AttrMissing *) palloc(tupdesc->natts * sizeof(AttrMissing));
358 596 : memcpy(cpy->missing, constr->missing, tupdesc->natts * sizeof(AttrMissing));
359 4724 : for (i = tupdesc->natts - 1; i >= 0; i--)
360 : {
361 4128 : if (constr->missing[i].am_present)
362 : {
363 1100 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
364 :
365 1100 : cpy->missing[i].am_value = datumCopy(constr->missing[i].am_value,
366 1100 : attr->attbyval,
367 1100 : attr->attlen);
368 : }
369 : }
370 : }
371 :
372 693618 : if ((cpy->num_check = constr->num_check) > 0)
373 : {
374 2078 : cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));
375 2078 : memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
376 5432 : for (i = cpy->num_check - 1; i >= 0; i--)
377 : {
378 3354 : cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
379 3354 : cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
380 3354 : cpy->check[i].ccenforced = constr->check[i].ccenforced;
381 3354 : cpy->check[i].ccvalid = constr->check[i].ccvalid;
382 3354 : cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
383 : }
384 : }
385 :
386 693618 : desc->constr = cpy;
387 : }
388 :
389 : /* We can copy the tuple type identification, too */
390 749130 : desc->tdtypeid = tupdesc->tdtypeid;
391 749130 : desc->tdtypmod = tupdesc->tdtypmod;
392 :
393 749130 : return desc;
394 : }
395 :
396 : /*
397 : * TupleDescCopy
398 : * Copy a tuple descriptor into caller-supplied memory.
399 : * The memory may be shared memory mapped at any address, and must
400 : * be sufficient to hold TupleDescSize(src) bytes.
401 : *
402 : * !!! Constraints and defaults are not copied !!!
403 : */
404 : void
405 168 : TupleDescCopy(TupleDesc dst, TupleDesc src)
406 : {
407 : int i;
408 :
409 : /* Flat-copy the header and attribute arrays */
410 168 : memcpy(dst, src, TupleDescSize(src));
411 :
412 : /*
413 : * Since we're not copying constraints and defaults, clear fields
414 : * associated with them.
415 : */
416 686 : for (i = 0; i < dst->natts; i++)
417 : {
418 518 : Form_pg_attribute att = TupleDescAttr(dst, i);
419 :
420 518 : att->attnotnull = false;
421 518 : att->atthasdef = false;
422 518 : att->atthasmissing = false;
423 518 : att->attidentity = '\0';
424 518 : att->attgenerated = '\0';
425 :
426 518 : populate_compact_attribute(dst, i);
427 : }
428 168 : dst->constr = NULL;
429 :
430 : /*
431 : * Also, assume the destination is not to be ref-counted. (Copying the
432 : * source's refcount would be wrong in any case.)
433 : */
434 168 : dst->tdrefcount = -1;
435 168 : }
436 :
437 : /*
438 : * TupleDescCopyEntry
439 : * This function copies a single attribute structure from one tuple
440 : * descriptor to another.
441 : *
442 : * !!! Constraints and defaults are not copied !!!
443 : */
444 : void
445 3618 : TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
446 : TupleDesc src, AttrNumber srcAttno)
447 : {
448 3618 : Form_pg_attribute dstAtt = TupleDescAttr(dst, dstAttno - 1);
449 3618 : Form_pg_attribute srcAtt = TupleDescAttr(src, srcAttno - 1);
450 :
451 : /*
452 : * sanity checks
453 : */
454 : Assert(PointerIsValid(src));
455 : Assert(PointerIsValid(dst));
456 : Assert(srcAttno >= 1);
457 : Assert(srcAttno <= src->natts);
458 : Assert(dstAttno >= 1);
459 : Assert(dstAttno <= dst->natts);
460 :
461 3618 : memcpy(dstAtt, srcAtt, ATTRIBUTE_FIXED_PART_SIZE);
462 :
463 3618 : dstAtt->attnum = dstAttno;
464 :
465 : /* since we're not copying constraints or defaults, clear these */
466 3618 : dstAtt->attnotnull = false;
467 3618 : dstAtt->atthasdef = false;
468 3618 : dstAtt->atthasmissing = false;
469 3618 : dstAtt->attidentity = '\0';
470 3618 : dstAtt->attgenerated = '\0';
471 :
472 3618 : populate_compact_attribute(dst, dstAttno - 1);
473 3618 : }
474 :
475 : /*
476 : * Free a TupleDesc including all substructure
477 : */
478 : void
479 1347160 : FreeTupleDesc(TupleDesc tupdesc)
480 : {
481 : int i;
482 :
483 : /*
484 : * Possibly this should assert tdrefcount == 0, to disallow explicit
485 : * freeing of un-refcounted tupdescs?
486 : */
487 : Assert(tupdesc->tdrefcount <= 0);
488 :
489 1347160 : if (tupdesc->constr)
490 : {
491 401582 : if (tupdesc->constr->num_defval > 0)
492 : {
493 27260 : AttrDefault *attrdef = tupdesc->constr->defval;
494 :
495 66110 : for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
496 38850 : pfree(attrdef[i].adbin);
497 27260 : pfree(attrdef);
498 : }
499 401582 : if (tupdesc->constr->missing)
500 : {
501 3380 : AttrMissing *attrmiss = tupdesc->constr->missing;
502 :
503 24710 : for (i = tupdesc->natts - 1; i >= 0; i--)
504 : {
505 21330 : if (attrmiss[i].am_present
506 7208 : && !TupleDescAttr(tupdesc, i)->attbyval)
507 2684 : pfree(DatumGetPointer(attrmiss[i].am_value));
508 : }
509 3380 : pfree(attrmiss);
510 : }
511 401582 : if (tupdesc->constr->num_check > 0)
512 : {
513 10182 : ConstrCheck *check = tupdesc->constr->check;
514 :
515 25584 : for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
516 : {
517 15402 : pfree(check[i].ccname);
518 15402 : pfree(check[i].ccbin);
519 : }
520 10182 : pfree(check);
521 : }
522 401582 : pfree(tupdesc->constr);
523 : }
524 :
525 1347160 : pfree(tupdesc);
526 1347160 : }
527 :
528 : /*
529 : * Increment the reference count of a tupdesc, and log the reference in
530 : * CurrentResourceOwner.
531 : *
532 : * Do not apply this to tupdescs that are not being refcounted. (Use the
533 : * macro PinTupleDesc for tupdescs of uncertain status.)
534 : */
535 : void
536 30020244 : IncrTupleDescRefCount(TupleDesc tupdesc)
537 : {
538 : Assert(tupdesc->tdrefcount >= 0);
539 :
540 30020244 : ResourceOwnerEnlarge(CurrentResourceOwner);
541 30020244 : tupdesc->tdrefcount++;
542 30020244 : ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
543 30020244 : }
544 :
545 : /*
546 : * Decrement the reference count of a tupdesc, remove the corresponding
547 : * reference from CurrentResourceOwner, and free the tupdesc if no more
548 : * references remain.
549 : *
550 : * Do not apply this to tupdescs that are not being refcounted. (Use the
551 : * macro ReleaseTupleDesc for tupdescs of uncertain status.)
552 : */
553 : void
554 30006870 : DecrTupleDescRefCount(TupleDesc tupdesc)
555 : {
556 : Assert(tupdesc->tdrefcount > 0);
557 :
558 30006870 : ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
559 30006870 : if (--tupdesc->tdrefcount == 0)
560 4 : FreeTupleDesc(tupdesc);
561 30006870 : }
562 :
563 : /*
564 : * Compare two TupleDesc structures for logical equality
565 : */
566 : bool
567 394502 : equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
568 : {
569 : int i,
570 : n;
571 :
572 394502 : if (tupdesc1->natts != tupdesc2->natts)
573 2528 : return false;
574 391974 : if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
575 0 : return false;
576 :
577 : /* tdtypmod and tdrefcount are not checked */
578 :
579 1872020 : for (i = 0; i < tupdesc1->natts; i++)
580 : {
581 1492496 : Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
582 1492496 : Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
583 :
584 : /*
585 : * We do not need to check every single field here: we can disregard
586 : * attrelid and attnum (which were used to place the row in the attrs
587 : * array in the first place). It might look like we could dispense
588 : * with checking attlen/attbyval/attalign, since these are derived
589 : * from atttypid; but in the case of dropped columns we must check
590 : * them (since atttypid will be zero for all dropped columns) and in
591 : * general it seems safer to check them always.
592 : *
593 : * We intentionally ignore atthasmissing, since that's not very
594 : * relevant in tupdescs, which lack the attmissingval field.
595 : */
596 1492496 : if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
597 1284 : return false;
598 1491212 : if (attr1->atttypid != attr2->atttypid)
599 1032 : return false;
600 1490180 : if (attr1->attlen != attr2->attlen)
601 10 : return false;
602 1490170 : if (attr1->attndims != attr2->attndims)
603 0 : return false;
604 1490170 : if (attr1->atttypmod != attr2->atttypmod)
605 42 : return false;
606 1490128 : if (attr1->attbyval != attr2->attbyval)
607 68 : return false;
608 1490060 : if (attr1->attalign != attr2->attalign)
609 0 : return false;
610 1490060 : if (attr1->attstorage != attr2->attstorage)
611 214 : return false;
612 1489846 : if (attr1->attcompression != attr2->attcompression)
613 62 : return false;
614 1489784 : if (attr1->attnotnull != attr2->attnotnull)
615 1438 : return false;
616 1488346 : if (attr1->atthasdef != attr2->atthasdef)
617 4714 : return false;
618 1483632 : if (attr1->attidentity != attr2->attidentity)
619 178 : return false;
620 1483454 : if (attr1->attgenerated != attr2->attgenerated)
621 20 : return false;
622 1483434 : if (attr1->attisdropped != attr2->attisdropped)
623 0 : return false;
624 1483434 : if (attr1->attislocal != attr2->attislocal)
625 2634 : return false;
626 1480800 : if (attr1->attinhcount != attr2->attinhcount)
627 754 : return false;
628 1480046 : if (attr1->attcollation != attr2->attcollation)
629 0 : return false;
630 : /* variable-length fields are not even present... */
631 : }
632 :
633 379524 : if (tupdesc1->constr != NULL)
634 : {
635 131986 : TupleConstr *constr1 = tupdesc1->constr;
636 131986 : TupleConstr *constr2 = tupdesc2->constr;
637 :
638 131986 : if (constr2 == NULL)
639 212 : return false;
640 131774 : if (constr1->has_not_null != constr2->has_not_null)
641 0 : return false;
642 131774 : if (constr1->has_generated_stored != constr2->has_generated_stored)
643 540 : return false;
644 131234 : if (constr1->has_generated_virtual != constr2->has_generated_virtual)
645 338 : return false;
646 130896 : n = constr1->num_defval;
647 130896 : if (n != (int) constr2->num_defval)
648 0 : return false;
649 : /* We assume here that both AttrDefault arrays are in adnum order */
650 144776 : for (i = 0; i < n; i++)
651 : {
652 13880 : AttrDefault *defval1 = constr1->defval + i;
653 13880 : AttrDefault *defval2 = constr2->defval + i;
654 :
655 13880 : if (defval1->adnum != defval2->adnum)
656 0 : return false;
657 13880 : if (strcmp(defval1->adbin, defval2->adbin) != 0)
658 0 : return false;
659 : }
660 130896 : if (constr1->missing)
661 : {
662 526 : if (!constr2->missing)
663 72 : return false;
664 2600 : for (i = 0; i < tupdesc1->natts; i++)
665 : {
666 2146 : AttrMissing *missval1 = constr1->missing + i;
667 2146 : AttrMissing *missval2 = constr2->missing + i;
668 :
669 2146 : if (missval1->am_present != missval2->am_present)
670 0 : return false;
671 2146 : if (missval1->am_present)
672 : {
673 572 : CompactAttribute *missatt1 = TupleDescCompactAttr(tupdesc1, i);
674 :
675 572 : if (!datumIsEqual(missval1->am_value, missval2->am_value,
676 572 : missatt1->attbyval, missatt1->attlen))
677 0 : return false;
678 : }
679 : }
680 : }
681 130370 : else if (constr2->missing)
682 4 : return false;
683 130820 : n = constr1->num_check;
684 130820 : if (n != (int) constr2->num_check)
685 1392 : return false;
686 :
687 : /*
688 : * Similarly, we rely here on the ConstrCheck entries being sorted by
689 : * name. If there are duplicate names, the outcome of the comparison
690 : * is uncertain, but that should not happen.
691 : */
692 132392 : for (i = 0; i < n; i++)
693 : {
694 3060 : ConstrCheck *check1 = constr1->check + i;
695 3060 : ConstrCheck *check2 = constr2->check + i;
696 :
697 3060 : if (!(strcmp(check1->ccname, check2->ccname) == 0 &&
698 3060 : strcmp(check1->ccbin, check2->ccbin) == 0 &&
699 3060 : check1->ccenforced == check2->ccenforced &&
700 3048 : check1->ccvalid == check2->ccvalid &&
701 2964 : check1->ccnoinherit == check2->ccnoinherit))
702 96 : return false;
703 : }
704 : }
705 247538 : else if (tupdesc2->constr != NULL)
706 1740 : return false;
707 375130 : return true;
708 : }
709 :
710 : /*
711 : * equalRowTypes
712 : *
713 : * This determines whether two tuple descriptors have equal row types. This
714 : * only checks those fields in pg_attribute that are applicable for row types,
715 : * while ignoring those fields that define the physical row storage or those
716 : * that define table column metadata.
717 : *
718 : * Specifically, this checks:
719 : *
720 : * - same number of attributes
721 : * - same composite type ID (but could both be zero)
722 : * - corresponding attributes (in order) have same the name, type, typmod,
723 : * collation
724 : *
725 : * This is used to check whether two record types are compatible, whether
726 : * function return row types are the same, and other similar situations.
727 : *
728 : * (XXX There was some discussion whether attndims should be checked here, but
729 : * for now it has been decided not to.)
730 : *
731 : * Note: We deliberately do not check the tdtypmod field. This allows
732 : * typcache.c to use this routine to see if a cached record type matches a
733 : * requested type.
734 : */
735 : bool
736 323544 : equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
737 : {
738 323544 : if (tupdesc1->natts != tupdesc2->natts)
739 166 : return false;
740 323378 : if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
741 1742 : return false;
742 :
743 5096878 : for (int i = 0; i < tupdesc1->natts; i++)
744 : {
745 4778926 : Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
746 4778926 : Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
747 :
748 4778926 : if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
749 3658 : return false;
750 4775268 : if (attr1->atttypid != attr2->atttypid)
751 22 : return false;
752 4775246 : if (attr1->atttypmod != attr2->atttypmod)
753 4 : return false;
754 4775242 : if (attr1->attcollation != attr2->attcollation)
755 0 : return false;
756 :
757 : /* Record types derived from tables could have dropped fields. */
758 4775242 : if (attr1->attisdropped != attr2->attisdropped)
759 0 : return false;
760 : }
761 :
762 317952 : return true;
763 : }
764 :
765 : /*
766 : * hashRowType
767 : *
768 : * If two tuple descriptors would be considered equal by equalRowTypes()
769 : * then their hash value will be equal according to this function.
770 : */
771 : uint32
772 340928 : hashRowType(TupleDesc desc)
773 : {
774 : uint32 s;
775 : int i;
776 :
777 340928 : s = hash_combine(0, hash_uint32(desc->natts));
778 340928 : s = hash_combine(s, hash_uint32(desc->tdtypeid));
779 5382140 : for (i = 0; i < desc->natts; ++i)
780 5041212 : s = hash_combine(s, hash_uint32(TupleDescAttr(desc, i)->atttypid));
781 :
782 340928 : return s;
783 : }
784 :
785 : /*
786 : * TupleDescInitEntry
787 : * This function initializes a single attribute structure in
788 : * a previously allocated tuple descriptor.
789 : *
790 : * If attributeName is NULL, the attname field is set to an empty string
791 : * (this is for cases where we don't know or need a name for the field).
792 : * Also, some callers use this function to change the datatype-related fields
793 : * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
794 : * to indicate that the attname field shouldn't be modified.
795 : *
796 : * Note that attcollation is set to the default for the specified datatype.
797 : * If a nondefault collation is needed, insert it afterwards using
798 : * TupleDescInitEntryCollation.
799 : */
800 : void
801 14287210 : TupleDescInitEntry(TupleDesc desc,
802 : AttrNumber attributeNumber,
803 : const char *attributeName,
804 : Oid oidtypeid,
805 : int32 typmod,
806 : int attdim)
807 : {
808 : HeapTuple tuple;
809 : Form_pg_type typeForm;
810 : Form_pg_attribute att;
811 :
812 : /*
813 : * sanity checks
814 : */
815 : Assert(PointerIsValid(desc));
816 : Assert(attributeNumber >= 1);
817 : Assert(attributeNumber <= desc->natts);
818 : Assert(attdim >= 0);
819 : Assert(attdim <= PG_INT16_MAX);
820 :
821 : /*
822 : * initialize the attribute fields
823 : */
824 14287210 : att = TupleDescAttr(desc, attributeNumber - 1);
825 :
826 14287210 : att->attrelid = 0; /* dummy value */
827 :
828 : /*
829 : * Note: attributeName can be NULL, because the planner doesn't always
830 : * fill in valid resname values in targetlists, particularly for resjunk
831 : * attributes. Also, do nothing if caller wants to re-use the old attname.
832 : */
833 14287210 : if (attributeName == NULL)
834 30754744 : MemSet(NameStr(att->attname), 0, NAMEDATALEN);
835 7835506 : else if (attributeName != NameStr(att->attname))
836 7832566 : namestrcpy(&(att->attname), attributeName);
837 :
838 14287210 : att->atttypmod = typmod;
839 :
840 14287210 : att->attnum = attributeNumber;
841 14287210 : att->attndims = attdim;
842 :
843 14287210 : att->attnotnull = false;
844 14287210 : att->atthasdef = false;
845 14287210 : att->atthasmissing = false;
846 14287210 : att->attidentity = '\0';
847 14287210 : att->attgenerated = '\0';
848 14287210 : att->attisdropped = false;
849 14287210 : att->attislocal = true;
850 14287210 : att->attinhcount = 0;
851 : /* variable-length fields are not present in tupledescs */
852 :
853 14287210 : tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
854 14287210 : if (!HeapTupleIsValid(tuple))
855 0 : elog(ERROR, "cache lookup failed for type %u", oidtypeid);
856 14287210 : typeForm = (Form_pg_type) GETSTRUCT(tuple);
857 :
858 14287210 : att->atttypid = oidtypeid;
859 14287210 : att->attlen = typeForm->typlen;
860 14287210 : att->attbyval = typeForm->typbyval;
861 14287210 : att->attalign = typeForm->typalign;
862 14287210 : att->attstorage = typeForm->typstorage;
863 14287210 : att->attcompression = InvalidCompressionMethod;
864 14287210 : att->attcollation = typeForm->typcollation;
865 :
866 14287210 : populate_compact_attribute(desc, attributeNumber - 1);
867 :
868 14287210 : ReleaseSysCache(tuple);
869 14287210 : }
870 :
871 : /*
872 : * TupleDescInitBuiltinEntry
873 : * Initialize a tuple descriptor without catalog access. Only
874 : * a limited range of builtin types are supported.
875 : */
876 : void
877 12944 : TupleDescInitBuiltinEntry(TupleDesc desc,
878 : AttrNumber attributeNumber,
879 : const char *attributeName,
880 : Oid oidtypeid,
881 : int32 typmod,
882 : int attdim)
883 : {
884 : Form_pg_attribute att;
885 :
886 : /* sanity checks */
887 : Assert(PointerIsValid(desc));
888 : Assert(attributeNumber >= 1);
889 : Assert(attributeNumber <= desc->natts);
890 : Assert(attdim >= 0);
891 : Assert(attdim <= PG_INT16_MAX);
892 :
893 : /* initialize the attribute fields */
894 12944 : att = TupleDescAttr(desc, attributeNumber - 1);
895 12944 : att->attrelid = 0; /* dummy value */
896 :
897 : /* unlike TupleDescInitEntry, we require an attribute name */
898 : Assert(attributeName != NULL);
899 12944 : namestrcpy(&(att->attname), attributeName);
900 :
901 12944 : att->atttypmod = typmod;
902 :
903 12944 : att->attnum = attributeNumber;
904 12944 : att->attndims = attdim;
905 :
906 12944 : att->attnotnull = false;
907 12944 : att->atthasdef = false;
908 12944 : att->atthasmissing = false;
909 12944 : att->attidentity = '\0';
910 12944 : att->attgenerated = '\0';
911 12944 : att->attisdropped = false;
912 12944 : att->attislocal = true;
913 12944 : att->attinhcount = 0;
914 : /* variable-length fields are not present in tupledescs */
915 :
916 12944 : att->atttypid = oidtypeid;
917 :
918 : /*
919 : * Our goal here is to support just enough types to let basic builtin
920 : * commands work without catalog access - e.g. so that we can do certain
921 : * things even in processes that are not connected to a database.
922 : */
923 12944 : switch (oidtypeid)
924 : {
925 10386 : case TEXTOID:
926 : case TEXTARRAYOID:
927 10386 : att->attlen = -1;
928 10386 : att->attbyval = false;
929 10386 : att->attalign = TYPALIGN_INT;
930 10386 : att->attstorage = TYPSTORAGE_EXTENDED;
931 10386 : att->attcompression = InvalidCompressionMethod;
932 10386 : att->attcollation = DEFAULT_COLLATION_OID;
933 10386 : break;
934 :
935 0 : case BOOLOID:
936 0 : att->attlen = 1;
937 0 : att->attbyval = true;
938 0 : att->attalign = TYPALIGN_CHAR;
939 0 : att->attstorage = TYPSTORAGE_PLAIN;
940 0 : att->attcompression = InvalidCompressionMethod;
941 0 : att->attcollation = InvalidOid;
942 0 : break;
943 :
944 0 : case INT4OID:
945 0 : att->attlen = 4;
946 0 : att->attbyval = true;
947 0 : att->attalign = TYPALIGN_INT;
948 0 : att->attstorage = TYPSTORAGE_PLAIN;
949 0 : att->attcompression = InvalidCompressionMethod;
950 0 : att->attcollation = InvalidOid;
951 0 : break;
952 :
953 2250 : case INT8OID:
954 2250 : att->attlen = 8;
955 2250 : att->attbyval = FLOAT8PASSBYVAL;
956 2250 : att->attalign = TYPALIGN_DOUBLE;
957 2250 : att->attstorage = TYPSTORAGE_PLAIN;
958 2250 : att->attcompression = InvalidCompressionMethod;
959 2250 : att->attcollation = InvalidOid;
960 2250 : break;
961 :
962 308 : case OIDOID:
963 308 : att->attlen = 4;
964 308 : att->attbyval = true;
965 308 : att->attalign = TYPALIGN_INT;
966 308 : att->attstorage = TYPSTORAGE_PLAIN;
967 308 : att->attcompression = InvalidCompressionMethod;
968 308 : att->attcollation = InvalidOid;
969 308 : break;
970 :
971 0 : default:
972 0 : elog(ERROR, "unsupported type %u", oidtypeid);
973 : }
974 :
975 12944 : populate_compact_attribute(desc, attributeNumber - 1);
976 12944 : }
977 :
978 : /*
979 : * TupleDescInitEntryCollation
980 : *
981 : * Assign a nondefault collation to a previously initialized tuple descriptor
982 : * entry.
983 : */
984 : void
985 9102952 : TupleDescInitEntryCollation(TupleDesc desc,
986 : AttrNumber attributeNumber,
987 : Oid collationid)
988 : {
989 : /*
990 : * sanity checks
991 : */
992 : Assert(PointerIsValid(desc));
993 : Assert(attributeNumber >= 1);
994 : Assert(attributeNumber <= desc->natts);
995 :
996 9102952 : TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
997 9102952 : }
998 :
999 : /*
1000 : * BuildDescFromLists
1001 : *
1002 : * Build a TupleDesc given lists of column names (as String nodes),
1003 : * column type OIDs, typmods, and collation OIDs.
1004 : *
1005 : * No constraints are generated.
1006 : *
1007 : * This is for use with functions returning RECORD.
1008 : */
1009 : TupleDesc
1010 1410 : BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
1011 : {
1012 : int natts;
1013 : AttrNumber attnum;
1014 : ListCell *l1;
1015 : ListCell *l2;
1016 : ListCell *l3;
1017 : ListCell *l4;
1018 : TupleDesc desc;
1019 :
1020 1410 : natts = list_length(names);
1021 : Assert(natts == list_length(types));
1022 : Assert(natts == list_length(typmods));
1023 : Assert(natts == list_length(collations));
1024 :
1025 : /*
1026 : * allocate a new tuple descriptor
1027 : */
1028 1410 : desc = CreateTemplateTupleDesc(natts);
1029 :
1030 1410 : attnum = 0;
1031 4974 : forfour(l1, names, l2, types, l3, typmods, l4, collations)
1032 : {
1033 3564 : char *attname = strVal(lfirst(l1));
1034 3564 : Oid atttypid = lfirst_oid(l2);
1035 3564 : int32 atttypmod = lfirst_int(l3);
1036 3564 : Oid attcollation = lfirst_oid(l4);
1037 :
1038 3564 : attnum++;
1039 :
1040 3564 : TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
1041 3564 : TupleDescInitEntryCollation(desc, attnum, attcollation);
1042 : }
1043 :
1044 1410 : return desc;
1045 : }
1046 :
1047 : /*
1048 : * Get default expression (or NULL if none) for the given attribute number.
1049 : */
1050 : Node *
1051 150648 : TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
1052 : {
1053 150648 : Node *result = NULL;
1054 :
1055 150648 : if (tupdesc->constr)
1056 : {
1057 150648 : AttrDefault *attrdef = tupdesc->constr->defval;
1058 :
1059 227208 : for (int i = 0; i < tupdesc->constr->num_defval; i++)
1060 : {
1061 227208 : if (attrdef[i].adnum == attnum)
1062 : {
1063 150648 : result = stringToNode(attrdef[i].adbin);
1064 150648 : break;
1065 : }
1066 : }
1067 : }
1068 :
1069 150648 : return result;
1070 : }
1071 :
1072 : /* ResourceOwner callbacks */
1073 :
1074 : static void
1075 13374 : ResOwnerReleaseTupleDesc(Datum res)
1076 : {
1077 13374 : TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1078 :
1079 : /* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
1080 : Assert(tupdesc->tdrefcount > 0);
1081 13374 : if (--tupdesc->tdrefcount == 0)
1082 436 : FreeTupleDesc(tupdesc);
1083 13374 : }
1084 :
1085 : static char *
1086 0 : ResOwnerPrintTupleDesc(Datum res)
1087 : {
1088 0 : TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1089 :
1090 0 : return psprintf("TupleDesc %p (%u,%d)",
1091 : tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1092 : }
|