Age Owner Branch data TLA Line data Source code
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"
23 : : #include "access/toast_compression.h"
24 : : #include "access/tupdesc_details.h"
25 : : #include "catalog/catalog.h"
26 : : #include "catalog/pg_collation.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 */
35 : : static void ResOwnerReleaseTupleDesc(Datum res);
36 : : static char *ResOwnerPrintTupleDesc(Datum res);
37 : :
38 : : static const ResourceOwnerDesc tupdesc_resowner_desc =
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 */
48 : : static inline void
1069 heikki.linnakangas@i 49 :CBC 12844008 : ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
50 : : {
51 : 12844008 : ResourceOwnerRemember(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
52 : 12844008 : }
53 : :
54 : : static inline void
55 : 12833192 : ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
56 : : {
57 : 12833192 : ResourceOwnerForget(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
58 : 12833192 : }
59 : :
60 : : /*
61 : : * populate_compact_attribute_internal
62 : : * Helper function for populate_compact_attribute()
63 : : */
64 : : static inline void
657 drowley@postgresql.o 65 : 1321764877 : populate_compact_attribute_internal(Form_pg_attribute src,
66 : : CompactAttribute *dst)
67 : : {
661 68 : 1321764877 : memset(dst, 0, sizeof(CompactAttribute));
69 : :
70 : 1321764877 : dst->attcacheoff = -1;
71 : 1321764877 : dst->attlen = src->attlen;
72 : :
73 : 1321764877 : dst->attbyval = src->attbyval;
74 : 1321764877 : dst->attispackable = (src->attstorage != TYPSTORAGE_PLAIN);
75 : 1321764877 : dst->atthasmissing = src->atthasmissing;
76 : 1321764877 : dst->attisdropped = src->attisdropped;
77 : 1321764877 : 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 : : */
553 alvherre@alvh.no-ip. 85 [ + + + + ]: 1670780711 : dst->attnullability = !src->attnotnull ? ATTNULLABLE_UNRESTRICTED :
86 : 349015834 : IsCatalogRelationOid(src->attrelid) ? ATTNULLABLE_VALID :
87 : : ATTNULLABLE_UNKNOWN;
88 : :
89 : : /* Compute numeric alignment requirement, too */
252 tgl@sss.pgh.pa.us 90 : 1321764877 : dst->attalignby = typalign_to_alignby(src->attalign);
661 drowley@postgresql.o 91 : 1321764877 : }
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 : : */
99 : : void
657 100 : 42214712 : populate_compact_attribute(TupleDesc tupdesc, int attnum)
101 : : {
102 : 42214712 : Form_pg_attribute src = TupleDescAttr(tupdesc, attnum);
103 : : CompactAttribute *dst;
104 : :
105 : : /*
106 : : * Don't use TupleDescCompactAttr to prevent infinite recursion in assert
107 : : * builds.
108 : : */
109 : 42214712 : dst = &tupdesc->compact_attrs[attnum];
110 : :
111 : 42214712 : populate_compact_attribute_internal(src, dst);
112 : 42214712 : }
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 : : */
124 : : void
125 : 1279550165 : verify_compact_attribute(TupleDesc tupdesc, int attnum)
126 : : {
127 : : #ifdef USE_ASSERT_CHECKING
128 : : CompactAttribute cattr;
129 : 1279550165 : Form_pg_attribute attr = TupleDescAttr(tupdesc, attnum);
130 : : CompactAttribute tmp;
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 : : */
482 137 : 1279550165 : memcpy(&cattr, &tupdesc->compact_attrs[attnum], sizeof(CompactAttribute));
138 : :
139 : : /*
140 : : * Populate the temporary CompactAttribute from the corresponding
141 : : * Form_pg_attribute
142 : : */
657 143 : 1279550165 : 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. Same with attnullability.
148 : : */
482 149 : 1279550165 : tmp.attcacheoff = cattr.attcacheoff;
150 : 1279550165 : tmp.attnullability = cattr.attnullability;
151 : :
152 : : /* Check the freshly populated CompactAttribute matches the TupleDesc's */
153 [ - + ]: 1279550165 : Assert(memcmp(&tmp, &cattr, sizeof(CompactAttribute)) == 0);
154 : : #endif
639 155 : 1279550165 : }
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 : : */
164 : : TupleDesc
2883 andres@anarazel.de 165 : 6067113 : CreateTemplateTupleDesc(int natts)
166 : : {
167 : : TupleDesc desc;
168 : :
169 : : /*
170 : : * sanity checks
171 : : */
1445 peter@eisentraut.org 172 [ - + ]: 6067113 : 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 : : */
661 drowley@postgresql.o 187 : 6067113 : desc = (TupleDesc) palloc(offsetof(struct TupleDescData, compact_attrs) +
188 : : natts * sizeof(CompactAttribute) +
3340 andres@anarazel.de 189 : 6067113 : natts * sizeof(FormData_pg_attribute));
190 : :
191 : : /*
192 : : * Initialize other fields of the tupdesc.
193 : : */
8229 tgl@sss.pgh.pa.us 194 : 6067113 : desc->natts = natts;
8780 195 : 6067113 : desc->constr = NULL;
8229 196 : 6067113 : desc->tdtypeid = RECORDOID;
197 : 6067113 : desc->tdtypmod = -1;
7423 198 : 6067113 : desc->tdrefcount = -1; /* assume not reference-counted */
199 : :
200 : : /* This will be set to the correct value by TupleDescFinalize() */
210 drowley@postgresql.o 201 : 6067113 : desc->firstNonCachedOffsetAttr = -1;
202 : 6067113 : desc->firstNonGuaranteedAttr = -1;
203 : :
10268 bruce@momjian.us 204 : 6067113 : 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 : : */
215 : : TupleDesc
2883 andres@anarazel.de 216 : 677626 : CreateTupleDesc(int natts, Form_pg_attribute *attrs)
217 : : {
218 : : TupleDesc desc;
219 : : int i;
220 : :
221 : 677626 : desc = CreateTemplateTupleDesc(natts);
222 : :
3340 223 [ + + ]: 10359870 : for (i = 0; i < natts; ++i)
224 : : {
225 : 9682244 : memcpy(TupleDescAttr(desc, i), attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
661 drowley@postgresql.o 226 : 9682244 : populate_compact_attribute(desc, i);
227 : : }
228 : :
210 229 : 677626 : TupleDescFinalize(desc);
230 : :
10268 bruce@momjian.us 231 : 677626 : 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 : : */
241 : : TupleDesc
11052 scrappy@hub.org 242 : 256807 : CreateTupleDescCopy(TupleDesc tupdesc)
243 : : {
244 : : TupleDesc desc;
245 : : int i;
246 : :
2883 andres@anarazel.de 247 : 256807 : desc = CreateTemplateTupleDesc(tupdesc->natts);
248 : :
249 : : /* Flat-copy the attribute array (unless there are no attributes) */
202 rhaas@postgresql.org 250 [ + + ]: 256807 : if (desc->natts > 0)
251 : 254550 : memcpy(TupleDescAttr(desc, 0),
252 : 254550 : TupleDescAttr(tupdesc, 0),
253 : 254550 : desc->natts * sizeof(FormData_pg_attribute));
254 : :
255 : : /*
256 : : * Since we're not copying constraints and defaults, clear fields
257 : : * associated with them.
258 : : */
7889 tgl@sss.pgh.pa.us 259 [ + + ]: 1271040 : for (i = 0; i < desc->natts; i++)
260 : : {
3340 andres@anarazel.de 261 : 1014233 : Form_pg_attribute att = TupleDescAttr(desc, i);
262 : :
263 : 1014233 : att->attnotnull = false;
264 : 1014233 : att->atthasdef = false;
3120 andrew@dunslane.net 265 : 1014233 : att->atthasmissing = false;
3340 andres@anarazel.de 266 : 1014233 : att->attidentity = '\0';
2753 peter@eisentraut.org 267 : 1014233 : att->attgenerated = '\0';
268 : :
661 drowley@postgresql.o 269 : 1014233 : populate_compact_attribute(desc, i);
270 : : }
271 : :
272 : : /* We can copy the tuple type identification, too */
273 : 256807 : desc->tdtypeid = tupdesc->tdtypeid;
274 : 256807 : desc->tdtypmod = tupdesc->tdtypmod;
275 : :
210 276 : 256807 : TupleDescFinalize(desc);
277 : :
661 278 : 256807 : return desc;
279 : : }
280 : :
281 : : /*
282 : : * CreateTupleDescTruncatedCopy
283 : : * This function creates a new TupleDesc with only the first 'natts'
284 : : * attributes from an existing TupleDesc
285 : : *
286 : : * !!! Constraints and defaults are not copied !!!
287 : : */
288 : : TupleDesc
289 : 21941 : CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
290 : : {
291 : : TupleDesc desc;
292 : : int i;
293 : :
294 [ - + ]: 21941 : Assert(natts <= tupdesc->natts);
295 : :
296 : 21941 : desc = CreateTemplateTupleDesc(natts);
297 : :
298 : : /* Flat-copy the attribute array (unless there are no attributes) */
202 rhaas@postgresql.org 299 [ + - ]: 21941 : if (desc->natts > 0)
300 : 21941 : memcpy(TupleDescAttr(desc, 0),
301 : 21941 : TupleDescAttr(tupdesc, 0),
302 : 21941 : desc->natts * sizeof(FormData_pg_attribute));
303 : :
304 : : /*
305 : : * Since we're not copying constraints and defaults, clear fields
306 : : * associated with them.
307 : : */
661 drowley@postgresql.o 308 [ + + ]: 51398 : for (i = 0; i < desc->natts; i++)
309 : : {
310 : 29457 : Form_pg_attribute att = TupleDescAttr(desc, i);
311 : :
312 : 29457 : att->attnotnull = false;
313 : 29457 : att->atthasdef = false;
314 : 29457 : att->atthasmissing = false;
315 : 29457 : att->attidentity = '\0';
316 : 29457 : att->attgenerated = '\0';
317 : :
318 : 29457 : populate_compact_attribute(desc, i);
319 : : }
320 : :
321 : : /* We can copy the tuple type identification, too */
8229 tgl@sss.pgh.pa.us 322 : 21941 : desc->tdtypeid = tupdesc->tdtypeid;
323 : 21941 : desc->tdtypmod = tupdesc->tdtypmod;
324 : :
210 drowley@postgresql.o 325 : 21941 : TupleDescFinalize(desc);
326 : :
10627 bruce@momjian.us 327 : 21941 : return desc;
328 : : }
329 : :
330 : : /*
331 : : * CreateTupleDescCopyConstr
332 : : * This function creates a new TupleDesc by copying from an existing
333 : : * TupleDesc (including its constraints and defaults).
334 : : */
335 : : TupleDesc
10643 vadim4o@yahoo.com 336 : 499365 : CreateTupleDescCopyConstr(TupleDesc tupdesc)
337 : : {
338 : : TupleDesc desc;
10626 bruce@momjian.us 339 : 499365 : TupleConstr *constr = tupdesc->constr;
340 : : int i;
341 : :
2883 andres@anarazel.de 342 : 499365 : desc = CreateTemplateTupleDesc(tupdesc->natts);
343 : :
344 : : /* Flat-copy the attribute array (unless there are no attributes) */
202 rhaas@postgresql.org 345 [ + + ]: 499365 : if (desc->natts > 0)
346 : 499303 : memcpy(TupleDescAttr(desc, 0),
347 : 499303 : TupleDescAttr(tupdesc, 0),
348 : 499303 : desc->natts * sizeof(FormData_pg_attribute));
349 : :
661 drowley@postgresql.o 350 [ + + ]: 7358032 : for (i = 0; i < desc->natts; i++)
351 : : {
352 : 6858667 : populate_compact_attribute(desc, i);
353 : :
553 alvherre@alvh.no-ip. 354 : 6858667 : TupleDescCompactAttr(desc, i)->attnullability =
355 : 13717334 : TupleDescCompactAttr(tupdesc, i)->attnullability;
356 : : }
357 : :
358 : : /* Copy the TupleConstr data structure, if any */
10627 bruce@momjian.us 359 [ + + ]: 499365 : if (constr)
360 : : {
306 michael@paquier.xyz 361 : 451995 : TupleConstr *cpy = palloc0_object(TupleConstr);
362 : :
10627 bruce@momjian.us 363 : 451995 : cpy->has_not_null = constr->has_not_null;
2753 peter@eisentraut.org 364 : 451995 : cpy->has_generated_stored = constr->has_generated_stored;
612 365 : 451995 : cpy->has_generated_virtual = constr->has_generated_virtual;
366 : :
10627 bruce@momjian.us 367 [ + + ]: 451995 : if ((cpy->num_defval = constr->num_defval) > 0)
368 : : {
56 michael@paquier.xyz 369 :GNC 2785 : cpy->defval = palloc_array(AttrDefault, cpy->num_defval);
10627 bruce@momjian.us 370 :CBC 2785 : memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
371 [ + + ]: 6709 : for (i = cpy->num_defval - 1; i >= 0; i--)
2015 tgl@sss.pgh.pa.us 372 : 3924 : cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
373 : : }
374 : :
3120 andrew@dunslane.net 375 [ + + ]: 451995 : if (constr->missing)
376 : : {
56 michael@paquier.xyz 377 :GNC 424 : cpy->missing = palloc_array(AttrMissing, tupdesc->natts);
3120 andrew@dunslane.net 378 :CBC 424 : memcpy(cpy->missing, constr->missing, tupdesc->natts * sizeof(AttrMissing));
379 [ + + ]: 3255 : for (i = tupdesc->natts - 1; i >= 0; i--)
380 : : {
3029 akapila@postgresql.o 381 [ + + ]: 2831 : if (constr->missing[i].am_present)
382 : : {
661 drowley@postgresql.o 383 : 760 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
384 : :
3029 akapila@postgresql.o 385 : 760 : cpy->missing[i].am_value = datumCopy(constr->missing[i].am_value,
386 : 760 : attr->attbyval,
387 : 760 : attr->attlen);
388 : : }
389 : : }
390 : : }
391 : :
10627 bruce@momjian.us 392 [ + + ]: 451995 : if ((cpy->num_check = constr->num_check) > 0)
393 : : {
56 michael@paquier.xyz 394 :GNC 1727 : cpy->check = palloc_array(ConstrCheck, cpy->num_check);
10627 bruce@momjian.us 395 :CBC 1727 : memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
396 [ + + ]: 5127 : for (i = cpy->num_check - 1; i >= 0; i--)
397 : : {
2015 tgl@sss.pgh.pa.us 398 : 3400 : cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
399 : 3400 : cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
639 peter@eisentraut.org 400 : 3400 : cpy->check[i].ccenforced = constr->check[i].ccenforced;
5612 alvherre@alvh.no-ip. 401 : 3400 : cpy->check[i].ccvalid = constr->check[i].ccvalid;
4586 noah@leadboat.com 402 : 3400 : cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
403 : : }
404 : : }
405 : :
10627 bruce@momjian.us 406 : 451995 : desc->constr = cpy;
407 : : }
408 : :
409 : : /* We can copy the tuple type identification, too */
8229 tgl@sss.pgh.pa.us 410 : 499365 : desc->tdtypeid = tupdesc->tdtypeid;
411 : 499365 : desc->tdtypmod = tupdesc->tdtypmod;
412 : :
210 drowley@postgresql.o 413 : 499365 : TupleDescFinalize(desc);
414 : :
10627 bruce@momjian.us 415 : 499365 : return desc;
416 : : }
417 : :
418 : : /*
419 : : * TupleDescCopy
420 : : * Copy a tuple descriptor into caller-supplied memory.
421 : : * The memory may be shared memory mapped at any address, and must
422 : : * be sufficient to hold TupleDescSize(src) bytes.
423 : : *
424 : : * !!! Constraints and defaults are not copied !!!
425 : : */
426 : : void
3315 andres@anarazel.de 427 : 233 : TupleDescCopy(TupleDesc dst, TupleDesc src)
428 : : {
429 : : int i;
430 : :
431 : : /* Flat-copy the header and attribute arrays */
432 : 233 : memcpy(dst, src, TupleDescSize(src));
433 : :
434 : : /*
435 : : * Since we're not copying constraints and defaults, clear fields
436 : : * associated with them.
437 : : */
3204 tgl@sss.pgh.pa.us 438 [ + + ]: 924 : for (i = 0; i < dst->natts; i++)
439 : : {
440 : 691 : Form_pg_attribute att = TupleDescAttr(dst, i);
441 : :
442 : 691 : att->attnotnull = false;
443 : 691 : att->atthasdef = false;
3120 andrew@dunslane.net 444 : 691 : att->atthasmissing = false;
3204 tgl@sss.pgh.pa.us 445 : 691 : att->attidentity = '\0';
2753 peter@eisentraut.org 446 : 691 : att->attgenerated = '\0';
447 : :
661 drowley@postgresql.o 448 : 691 : populate_compact_attribute(dst, i);
449 : : }
3315 andres@anarazel.de 450 : 233 : dst->constr = NULL;
451 : :
452 : : /*
453 : : * Also, assume the destination is not to be ref-counted. (Copying the
454 : : * source's refcount would be wrong in any case.)
455 : : */
456 : 233 : dst->tdrefcount = -1;
457 : :
210 drowley@postgresql.o 458 : 233 : TupleDescFinalize(dst);
3315 andres@anarazel.de 459 : 233 : }
460 : :
461 : : /*
462 : : * TupleDescCopyEntry
463 : : * This function copies a single attribute structure from one tuple
464 : : * descriptor to another.
465 : : *
466 : : * !!! Constraints and defaults are not copied !!!
467 : : *
468 : : * The caller must take care of calling TupleDescFinalize() on 'dst' once all
469 : : * TupleDesc changes have been made.
470 : : */
471 : : void
4708 tgl@sss.pgh.pa.us 472 : 2669 : TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
473 : : TupleDesc src, AttrNumber srcAttno)
474 : : {
3340 andres@anarazel.de 475 : 2669 : Form_pg_attribute dstAtt = TupleDescAttr(dst, dstAttno - 1);
476 : 2669 : Form_pg_attribute srcAtt = TupleDescAttr(src, srcAttno - 1);
477 : :
478 : : /*
479 : : * sanity checks
480 : : */
383 peter@eisentraut.org 481 [ - + ]: 2669 : Assert(src);
482 [ - + ]: 2669 : Assert(dst);
1445 483 [ - + ]: 2669 : Assert(srcAttno >= 1);
484 [ - + ]: 2669 : Assert(srcAttno <= src->natts);
485 [ - + ]: 2669 : Assert(dstAttno >= 1);
486 [ - + ]: 2669 : Assert(dstAttno <= dst->natts);
487 : :
3340 andres@anarazel.de 488 : 2669 : memcpy(dstAtt, srcAtt, ATTRIBUTE_FIXED_PART_SIZE);
489 : :
490 : 2669 : dstAtt->attnum = dstAttno;
491 : :
492 : : /* since we're not copying constraints or defaults, clear these */
493 : 2669 : dstAtt->attnotnull = false;
494 : 2669 : dstAtt->atthasdef = false;
3120 andrew@dunslane.net 495 : 2669 : dstAtt->atthasmissing = false;
3340 andres@anarazel.de 496 : 2669 : dstAtt->attidentity = '\0';
2753 peter@eisentraut.org 497 : 2669 : dstAtt->attgenerated = '\0';
498 : :
661 drowley@postgresql.o 499 : 2669 : populate_compact_attribute(dst, dstAttno - 1);
4708 tgl@sss.pgh.pa.us 500 : 2669 : }
501 : :
502 : : /*
503 : : * TupleDescFinalize
504 : : * Finalize the given TupleDesc. This must be called after the
505 : : * attributes arrays have been populated or adjusted by any code.
506 : : *
507 : : * Must be called after populate_compact_attribute() and before
508 : : * BlessTupleDesc().
509 : : */
510 : : void
210 drowley@postgresql.o 511 : 6196324 : TupleDescFinalize(TupleDesc tupdesc)
512 : : {
513 : 6196324 : int firstNonCachedOffsetAttr = 0;
514 : 6196324 : int firstNonGuaranteedAttr = tupdesc->natts;
515 : 6196324 : int off = 0;
516 : :
517 [ + + ]: 35443089 : for (int i = 0; i < tupdesc->natts; i++)
518 : : {
519 : 32424869 : CompactAttribute *cattr = TupleDescCompactAttr(tupdesc, i);
128 520 : 32424869 : Form_pg_attribute attr = TupleDescAttr(tupdesc, i);
521 : :
522 : : /*
523 : : * Find the highest attnum which is guaranteed to exist in all tuples
524 : : * in the table. We currently only pay attention to byval attributes
525 : : * to allow additional optimizations during tuple deformation.
526 : : */
210 527 [ + + ]: 32424869 : if (firstNonGuaranteedAttr == tupdesc->natts &&
528 [ + + + + : 13151277 : (cattr->attnullability != ATTNULLABLE_VALID || !cattr->attbyval ||
+ + ]
128 529 [ + - ]: 7471253 : cattr->atthasmissing || cattr->attisdropped ||
530 [ + - ]: 7471253 : cattr->attlen <= 0 ||
531 [ + + ]: 7471253 : attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL))
210 532 : 5680407 : firstNonGuaranteedAttr = i;
533 : :
534 : : /*
535 : : * Don't cache offsets beyond fixed-width attributes. Virtual
536 : : * generated attributes are stored as NULLs in the tuple, so we don't
537 : : * cache offsets beyond these.
538 : : */
128 539 [ + + ]: 32424869 : if (cattr->attlen <= 0 ||
540 [ + + ]: 29252254 : attr->attgenerated == ATTRIBUTE_GENERATED_VIRTUAL)
541 : : break;
542 : :
210 543 : 29246765 : off = att_nominal_alignby(off, cattr->attalignby);
544 : :
545 : : /*
546 : : * attcacheoff is an int16, so don't try to cache any offsets larger
547 : : * than will fit in that type. Any attributes which are offset more
548 : : * than 2^15 are likely due to variable-length attributes. Since we
549 : : * don't cache offsets for or beyond variable-length attributes, using
550 : : * an int16 rather than an int32 here is unlikely to cost us anything.
551 : : */
209 552 [ - + ]: 29246765 : if (off > PG_INT16_MAX)
209 drowley@postgresql.o 553 :UBC 0 : break;
554 : :
209 drowley@postgresql.o 555 :CBC 29246765 : cattr->attcacheoff = (int16) off;
556 : :
210 557 : 29246765 : off += cattr->attlen;
558 : 29246765 : firstNonCachedOffsetAttr = i + 1;
559 : : }
560 : :
561 : 6196324 : tupdesc->firstNonCachedOffsetAttr = firstNonCachedOffsetAttr;
562 : 6196324 : tupdesc->firstNonGuaranteedAttr = firstNonGuaranteedAttr;
563 : 6196324 : }
564 : :
565 : : /*
566 : : * Free a TupleDesc including all substructure
567 : : */
568 : : void
10627 bruce@momjian.us 569 : 899357 : FreeTupleDesc(TupleDesc tupdesc)
570 : : {
571 : : int i;
572 : :
573 : : /*
574 : : * Possibly this should assert tdrefcount == 0, to disallow explicit
575 : : * freeing of un-refcounted tupdescs?
576 : : */
7423 tgl@sss.pgh.pa.us 577 [ - + ]: 899357 : Assert(tupdesc->tdrefcount <= 0);
578 : :
10627 bruce@momjian.us 579 [ + + ]: 899357 : if (tupdesc->constr)
580 : : {
581 [ + + ]: 261270 : if (tupdesc->constr->num_defval > 0)
582 : : {
10626 583 : 21875 : AttrDefault *attrdef = tupdesc->constr->defval;
584 : :
10627 585 [ + + ]: 52961 : for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
2015 tgl@sss.pgh.pa.us 586 : 31086 : pfree(attrdef[i].adbin);
10627 bruce@momjian.us 587 : 21875 : pfree(attrdef);
588 : : }
3120 andrew@dunslane.net 589 [ + + ]: 261270 : if (tupdesc->constr->missing)
590 : : {
591 : 2579 : AttrMissing *attrmiss = tupdesc->constr->missing;
592 : :
593 [ + + ]: 18126 : for (i = tupdesc->natts - 1; i >= 0; i--)
594 : : {
3029 akapila@postgresql.o 595 [ + + ]: 15547 : if (attrmiss[i].am_present
3120 andrew@dunslane.net 596 [ + + ]: 5380 : && !TupleDescAttr(tupdesc, i)->attbyval)
3029 akapila@postgresql.o 597 : 1984 : pfree(DatumGetPointer(attrmiss[i].am_value));
598 : : }
3120 andrew@dunslane.net 599 : 2579 : pfree(attrmiss);
600 : : }
10627 bruce@momjian.us 601 [ + + ]: 261270 : if (tupdesc->constr->num_check > 0)
602 : : {
10626 603 : 7686 : ConstrCheck *check = tupdesc->constr->check;
604 : :
10627 605 [ + + ]: 21173 : for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
606 : : {
2015 tgl@sss.pgh.pa.us 607 : 13487 : pfree(check[i].ccname);
608 : 13487 : pfree(check[i].ccbin);
609 : : }
10627 bruce@momjian.us 610 : 7686 : pfree(check);
611 : : }
612 : 261270 : pfree(tupdesc->constr);
613 : : }
614 : :
615 : 899357 : pfree(tupdesc);
10643 vadim4o@yahoo.com 616 : 899357 : }
617 : :
618 : : /*
619 : : * Increment the reference count of a tupdesc, and log the reference in
620 : : * CurrentResourceOwner.
621 : : *
622 : : * Do not apply this to tupdescs that are not being refcounted. (Use the
623 : : * macro PinTupleDesc for tupdescs of uncertain status.)
624 : : */
625 : : void
7423 tgl@sss.pgh.pa.us 626 : 12844008 : IncrTupleDescRefCount(TupleDesc tupdesc)
627 : : {
628 [ - + ]: 12844008 : Assert(tupdesc->tdrefcount >= 0);
629 : :
1069 heikki.linnakangas@i 630 : 12844008 : ResourceOwnerEnlarge(CurrentResourceOwner);
7423 tgl@sss.pgh.pa.us 631 : 12844008 : tupdesc->tdrefcount++;
632 : 12844008 : ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
633 : 12844008 : }
634 : :
635 : : /*
636 : : * Decrement the reference count of a tupdesc, remove the corresponding
637 : : * reference from CurrentResourceOwner, and free the tupdesc if no more
638 : : * references remain.
639 : : *
640 : : * Do not apply this to tupdescs that are not being refcounted. (Use the
641 : : * macro ReleaseTupleDesc for tupdescs of uncertain status.)
642 : : */
643 : : void
644 : 12833192 : DecrTupleDescRefCount(TupleDesc tupdesc)
645 : : {
646 [ - + ]: 12833192 : Assert(tupdesc->tdrefcount > 0);
647 : :
648 : 12833192 : ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
649 [ - + ]: 12833192 : if (--tupdesc->tdrefcount == 0)
7423 tgl@sss.pgh.pa.us 650 :UBC 0 : FreeTupleDesc(tupdesc);
7423 tgl@sss.pgh.pa.us 651 :CBC 12833192 : }
652 : :
653 : : /*
654 : : * Compare two TupleDesc structures for logical equality
655 : : */
656 : : bool
9751 657 : 254390 : equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
658 : : {
659 : : int i,
660 : : n;
661 : :
662 [ + + ]: 254390 : if (tupdesc1->natts != tupdesc2->natts)
663 : 1947 : return false;
8229 664 [ - + ]: 252443 : if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
8229 tgl@sss.pgh.pa.us 665 :UBC 0 : return false;
666 : :
667 : : /* tdtypmod and tdrefcount are not checked */
668 : :
9751 tgl@sss.pgh.pa.us 669 [ + + ]:CBC 1153887 : for (i = 0; i < tupdesc1->natts; i++)
670 : : {
3340 andres@anarazel.de 671 : 910693 : Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
672 : 910693 : Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
673 : :
674 : : /*
675 : : * We do not need to check every single field here: we can disregard
676 : : * attrelid and attnum (which were used to place the row in the attrs
677 : : * array in the first place). It might look like we could dispense
678 : : * with checking attlen/attbyval/attalign, since these are derived
679 : : * from atttypid; but in the case of dropped columns we must check
680 : : * them (since atttypid will be zero for all dropped columns) and in
681 : : * general it seems safer to check them always.
682 : : *
683 : : * We intentionally ignore atthasmissing, since that's not very
684 : : * relevant in tupdescs, which lack the attmissingval field.
685 : : */
9751 tgl@sss.pgh.pa.us 686 [ + + ]: 910693 : if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
687 : 884 : return false;
688 [ + + ]: 909809 : if (attr1->atttypid != attr2->atttypid)
689 : 767 : return false;
7851 690 [ + + ]: 909042 : if (attr1->attlen != attr2->attlen)
691 : 5 : return false;
8229 692 [ - + ]: 909037 : if (attr1->attndims != attr2->attndims)
8229 tgl@sss.pgh.pa.us 693 :UBC 0 : return false;
9751 tgl@sss.pgh.pa.us 694 [ + + ]:CBC 909037 : if (attr1->atttypmod != attr2->atttypmod)
695 : 28 : return false;
7851 696 [ + + ]: 909009 : if (attr1->attbyval != attr2->attbyval)
697 : 34 : return false;
1968 698 [ - + ]: 908975 : if (attr1->attalign != attr2->attalign)
1968 tgl@sss.pgh.pa.us 699 :UBC 0 : return false;
9751 tgl@sss.pgh.pa.us 700 [ + + ]:CBC 908975 : if (attr1->attstorage != attr2->attstorage)
701 : 214 : return false;
1968 702 [ + + ]: 908761 : if (attr1->attcompression != attr2->attcompression)
7851 703 : 45 : return false;
9751 704 [ + + ]: 908716 : if (attr1->attnotnull != attr2->attnotnull)
705 : 1202 : return false;
706 : :
707 : : /*
708 : : * When the column has a not-null constraint, we also need to consider
709 : : * its validity aspect, which only manifests in CompactAttribute->
710 : : * attnullability, so verify that.
711 : : */
553 alvherre@alvh.no-ip. 712 [ + + ]: 907514 : if (attr1->attnotnull)
713 : : {
714 : 267016 : CompactAttribute *cattr1 = TupleDescCompactAttr(tupdesc1, i);
715 : 267016 : CompactAttribute *cattr2 = TupleDescCompactAttr(tupdesc2, i);
716 : :
717 [ - + ]: 267016 : Assert(cattr1->attnullability != ATTNULLABLE_UNKNOWN);
718 [ - + ]: 267016 : Assert((cattr1->attnullability == ATTNULLABLE_UNKNOWN) ==
719 : : (cattr2->attnullability == ATTNULLABLE_UNKNOWN));
720 : :
721 [ + + ]: 267016 : if (cattr1->attnullability != cattr2->attnullability)
722 : 62 : return false;
723 : : }
8809 tgl@sss.pgh.pa.us 724 [ + + ]: 907452 : if (attr1->atthasdef != attr2->atthasdef)
725 : 3631 : return false;
3476 peter_e@gmx.net 726 [ + + ]: 903821 : if (attr1->attidentity != attr2->attidentity)
727 : 110 : return false;
2753 peter@eisentraut.org 728 [ + + ]: 903711 : if (attr1->attgenerated != attr2->attgenerated)
729 : 37 : return false;
8837 tgl@sss.pgh.pa.us 730 [ - + ]: 903674 : if (attr1->attisdropped != attr2->attisdropped)
8837 tgl@sss.pgh.pa.us 731 :UBC 0 : return false;
8786 tgl@sss.pgh.pa.us 732 [ + + ]:CBC 903674 : if (attr1->attislocal != attr2->attislocal)
733 : 1736 : return false;
734 [ + + ]: 901938 : if (attr1->attinhcount != attr2->attinhcount)
8809 735 : 494 : return false;
5652 736 [ - + ]: 901444 : if (attr1->attcollation != attr2->attcollation)
5652 tgl@sss.pgh.pa.us 737 :UBC 0 : return false;
738 : : /* variable-length fields are not even present... */
739 : : }
740 : :
9751 tgl@sss.pgh.pa.us 741 [ + + ]:CBC 243194 : if (tupdesc1->constr != NULL)
742 : : {
9679 bruce@momjian.us 743 : 83858 : TupleConstr *constr1 = tupdesc1->constr;
744 : 83858 : TupleConstr *constr2 = tupdesc2->constr;
745 : :
9751 tgl@sss.pgh.pa.us 746 [ + + ]: 83858 : if (constr2 == NULL)
747 : 151 : return false;
9469 748 [ - + ]: 83707 : if (constr1->has_not_null != constr2->has_not_null)
9469 tgl@sss.pgh.pa.us 749 :UBC 0 : return false;
2753 peter@eisentraut.org 750 [ + + ]:CBC 83707 : if (constr1->has_generated_stored != constr2->has_generated_stored)
751 : 431 : return false;
612 752 [ + + ]: 83276 : if (constr1->has_generated_virtual != constr2->has_generated_virtual)
753 : 302 : return false;
9469 tgl@sss.pgh.pa.us 754 : 82974 : n = constr1->num_defval;
755 [ - + ]: 82974 : if (n != (int) constr2->num_defval)
9751 tgl@sss.pgh.pa.us 756 :UBC 0 : return false;
757 : : /* We assume here that both AttrDefault arrays are in adnum order */
9469 tgl@sss.pgh.pa.us 758 [ + + ]:CBC 95534 : for (i = 0; i < n; i++)
759 : : {
9679 bruce@momjian.us 760 : 12560 : AttrDefault *defval1 = constr1->defval + i;
2015 tgl@sss.pgh.pa.us 761 : 12560 : AttrDefault *defval2 = constr2->defval + i;
762 : :
763 [ - + ]: 12560 : if (defval1->adnum != defval2->adnum)
9751 tgl@sss.pgh.pa.us 764 :UBC 0 : return false;
9751 tgl@sss.pgh.pa.us 765 [ - + ]:CBC 12560 : if (strcmp(defval1->adbin, defval2->adbin) != 0)
9751 tgl@sss.pgh.pa.us 766 :UBC 0 : return false;
767 : : }
3120 andrew@dunslane.net 768 [ + + ]:CBC 82974 : if (constr1->missing)
769 : : {
770 [ + + ]: 543 : if (!constr2->missing)
771 : 66 : return false;
772 [ + + ]: 2760 : for (i = 0; i < tupdesc1->natts; i++)
773 : : {
774 : 2388 : AttrMissing *missval1 = constr1->missing + i;
775 : 2388 : AttrMissing *missval2 = constr2->missing + i;
776 : :
3029 akapila@postgresql.o 777 [ + + ]: 2388 : if (missval1->am_present != missval2->am_present)
3120 andrew@dunslane.net 778 : 105 : return false;
3029 akapila@postgresql.o 779 [ + + ]: 2283 : if (missval1->am_present)
780 : : {
661 drowley@postgresql.o 781 : 783 : CompactAttribute *missatt1 = TupleDescCompactAttr(tupdesc1, i);
782 : :
3029 akapila@postgresql.o 783 [ - + ]: 783 : if (!datumIsEqual(missval1->am_value, missval2->am_value,
3120 andrew@dunslane.net 784 : 783 : missatt1->attbyval, missatt1->attlen))
3120 andrew@dunslane.net 785 :UBC 0 : return false;
786 : : }
787 : : }
788 : : }
3120 andrew@dunslane.net 789 [ + + ]:CBC 82431 : else if (constr2->missing)
790 : 263 : return false;
9469 tgl@sss.pgh.pa.us 791 : 82540 : n = constr1->num_check;
792 [ + + ]: 82540 : if (n != (int) constr2->num_check)
9751 793 : 1103 : return false;
794 : :
795 : : /*
796 : : * Similarly, we rely here on the ConstrCheck entries being sorted by
797 : : * name. If there are duplicate names, the outcome of the comparison
798 : : * is uncertain, but that should not happen.
799 : : */
9469 800 [ + + ]: 83797 : for (i = 0; i < n; i++)
801 : : {
9679 bruce@momjian.us 802 : 2516 : ConstrCheck *check1 = constr1->check + i;
2015 tgl@sss.pgh.pa.us 803 : 2516 : ConstrCheck *check2 = constr2->check + i;
804 : :
805 [ + - ]: 2516 : if (!(strcmp(check1->ccname, check2->ccname) == 0 &&
806 [ + - ]: 2516 : strcmp(check1->ccbin, check2->ccbin) == 0 &&
639 peter@eisentraut.org 807 [ + + ]: 2516 : check1->ccenforced == check2->ccenforced &&
2015 tgl@sss.pgh.pa.us 808 [ + + ]: 2416 : check1->ccvalid == check2->ccvalid &&
809 [ - + ]: 2360 : check1->ccnoinherit == check2->ccnoinherit))
9751 810 : 156 : return false;
811 : : }
812 : : }
813 [ + + ]: 159336 : else if (tupdesc2->constr != NULL)
814 : 1231 : return false;
815 : 239386 : return true;
816 : : }
817 : :
818 : : /*
819 : : * equalRowTypes
820 : : *
821 : : * This determines whether two tuple descriptors have equal row types. This
822 : : * only checks those fields in pg_attribute that are applicable for row types,
823 : : * while ignoring those fields that define the physical row storage or those
824 : : * that define table column metadata.
825 : : *
826 : : * Specifically, this checks:
827 : : *
828 : : * - same number of attributes
829 : : * - same composite type ID (but could both be zero)
830 : : * - corresponding attributes (in order) have same the name, type, typmod,
831 : : * collation
832 : : *
833 : : * This is used to check whether two record types are compatible, whether
834 : : * function return row types are the same, and other similar situations.
835 : : *
836 : : * (XXX There was some discussion whether attndims should be checked here, but
837 : : * for now it has been decided not to.)
838 : : *
839 : : * Note: We deliberately do not check the tdtypmod field. This allows
840 : : * typcache.c to use this routine to see if a cached record type matches a
841 : : * requested type.
842 : : */
843 : : bool
939 peter@eisentraut.org 844 : 318002 : equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
845 : : {
846 [ + + ]: 318002 : if (tupdesc1->natts != tupdesc2->natts)
847 : 113 : return false;
848 [ + + ]: 317889 : if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
849 : 1320 : return false;
850 : :
851 [ + + ]: 4687254 : for (int i = 0; i < tupdesc1->natts; i++)
852 : : {
853 : 4376848 : Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
854 : 4376848 : Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
855 : :
856 [ + + ]: 4376848 : if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
857 : 6145 : return false;
858 [ + + ]: 4370703 : if (attr1->atttypid != attr2->atttypid)
859 : 14 : return false;
860 [ + + ]: 4370689 : if (attr1->atttypmod != attr2->atttypmod)
861 : 4 : return false;
862 [ - + ]: 4370685 : if (attr1->attcollation != attr2->attcollation)
939 peter@eisentraut.org 863 :UBC 0 : return false;
864 : :
865 : : /* Record types derived from tables could have dropped fields. */
939 peter@eisentraut.org 866 [ - + ]:CBC 4370685 : if (attr1->attisdropped != attr2->attisdropped)
939 peter@eisentraut.org 867 :UBC 0 : return false;
868 : : }
869 : :
939 peter@eisentraut.org 870 :CBC 310406 : return true;
871 : : }
872 : :
873 : : /*
874 : : * hashRowType
875 : : *
876 : : * If two tuple descriptors would be considered equal by equalRowTypes()
877 : : * then their hash value will be equal according to this function.
878 : : */
879 : : uint32
880 : 327548 : hashRowType(TupleDesc desc)
881 : : {
882 : : uint32 s;
883 : : int i;
884 : :
433 885 : 327548 : s = hash_combine(0, hash_bytes_uint32(desc->natts));
886 : 327548 : s = hash_combine(s, hash_bytes_uint32(desc->tdtypeid));
3338 andres@anarazel.de 887 [ + + ]: 4886661 : for (i = 0; i < desc->natts; ++i)
433 peter@eisentraut.org 888 : 4559113 : s = hash_combine(s, hash_bytes_uint32(TupleDescAttr(desc, i)->atttypid));
889 : :
3338 andres@anarazel.de 890 : 327548 : return s;
891 : : }
892 : :
893 : : /*
894 : : * TupleDescInitEntry
895 : : * This function initializes a single attribute structure in
896 : : * a previously allocated tuple descriptor.
897 : : *
898 : : * If attributeName is NULL, the attname field is set to an empty string
899 : : * (this is for cases where we don't know or need a name for the field).
900 : : * Also, some callers use this function to change the datatype-related fields
901 : : * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
902 : : * to indicate that the attname field shouldn't be modified.
903 : : *
904 : : * Note that attcollation is set to the default for the specified datatype.
905 : : * If a nondefault collation is needed, insert it afterwards using
906 : : * TupleDescInitEntryCollation.
907 : : */
908 : : void
11052 scrappy@hub.org 909 : 8789965 : TupleDescInitEntry(TupleDesc desc,
910 : : AttrNumber attributeNumber,
911 : : const char *attributeName,
912 : : Oid oidtypeid,
913 : : int32 typmod,
914 : : int attdim)
915 : : {
916 : : HeapTuple tuple;
917 : : Form_pg_type typeForm;
918 : : Form_pg_attribute att;
919 : :
920 : : /*
921 : : * sanity checks
922 : : */
383 peter@eisentraut.org 923 [ - + ]: 8789965 : Assert(desc);
1445 924 [ - + ]: 8789965 : Assert(attributeNumber >= 1);
925 [ - + ]: 8789965 : Assert(attributeNumber <= desc->natts);
1294 926 [ - + ]: 8789965 : Assert(attdim >= 0);
927 [ - + ]: 8789965 : Assert(attdim <= PG_INT16_MAX);
928 : :
929 : : /*
930 : : * initialize the attribute fields
931 : : */
3340 andres@anarazel.de 932 : 8789965 : att = TupleDescAttr(desc, attributeNumber - 1);
933 : :
10627 bruce@momjian.us 934 : 8789965 : att->attrelid = 0; /* dummy value */
935 : :
936 : : /*
937 : : * Note: attributeName can be NULL, because the planner doesn't always
938 : : * fill in valid resname values in targetlists, particularly for resjunk
939 : : * attributes. Also, do nothing if caller wants to re-use the old attname.
940 : : */
4732 tgl@sss.pgh.pa.us 941 [ + + ]: 8789965 : if (attributeName == NULL)
9836 bruce@momjian.us 942 [ + + + - : 12571670 : MemSet(NameStr(att->attname), 0, NAMEDATALEN);
+ - + - +
+ ]
4732 tgl@sss.pgh.pa.us 943 [ + + ]: 6102615 : else if (attributeName != NameStr(att->attname))
944 : 6100490 : namestrcpy(&(att->attname), attributeName);
945 : :
10471 bruce@momjian.us 946 : 8789965 : att->atttypmod = typmod;
947 : :
10627 948 : 8789965 : att->attnum = attributeNumber;
9289 tgl@sss.pgh.pa.us 949 : 8789965 : att->attndims = attdim;
950 : :
10627 bruce@momjian.us 951 : 8789965 : att->attnotnull = false;
952 : 8789965 : att->atthasdef = false;
3120 andrew@dunslane.net 953 : 8789965 : att->atthasmissing = false;
3476 peter_e@gmx.net 954 : 8789965 : att->attidentity = '\0';
2753 peter@eisentraut.org 955 : 8789965 : att->attgenerated = '\0';
8837 tgl@sss.pgh.pa.us 956 : 8789965 : att->attisdropped = false;
8786 957 : 8789965 : att->attislocal = true;
958 : 8789965 : att->attinhcount = 0;
959 : : /* variable-length fields are not present in tupledescs */
960 : :
6084 rhaas@postgresql.org 961 : 8789965 : tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
10627 bruce@momjian.us 962 [ - + ]: 8789965 : if (!HeapTupleIsValid(tuple))
8484 tgl@sss.pgh.pa.us 963 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", oidtypeid);
10268 bruce@momjian.us 964 :CBC 8789965 : typeForm = (Form_pg_type) GETSTRUCT(tuple);
965 : :
8229 tgl@sss.pgh.pa.us 966 : 8789965 : att->atttypid = oidtypeid;
967 : 8789965 : att->attlen = typeForm->typlen;
968 : 8789965 : att->attbyval = typeForm->typbyval;
969 : 8789965 : att->attalign = typeForm->typalign;
970 : 8789965 : att->attstorage = typeForm->typstorage;
1964 971 : 8789965 : att->attcompression = InvalidCompressionMethod;
1968 972 : 8789965 : att->attcollation = typeForm->typcollation;
973 : :
661 drowley@postgresql.o 974 : 8789965 : populate_compact_attribute(desc, attributeNumber - 1);
975 : :
9461 tgl@sss.pgh.pa.us 976 : 8789965 : ReleaseSysCache(tuple);
11052 scrappy@hub.org 977 : 8789965 : }
978 : :
979 : : /*
980 : : * TupleDescInitBuiltinEntry
981 : : * Initialize a tuple descriptor without catalog access. Only
982 : : * a limited range of builtin types are supported.
983 : : */
984 : : void
3548 rhaas@postgresql.org 985 : 8818 : TupleDescInitBuiltinEntry(TupleDesc desc,
986 : : AttrNumber attributeNumber,
987 : : const char *attributeName,
988 : : Oid oidtypeid,
989 : : int32 typmod,
990 : : int attdim)
991 : : {
992 : : Form_pg_attribute att;
993 : :
994 : : /* sanity checks */
383 peter@eisentraut.org 995 [ - + ]: 8818 : Assert(desc);
1445 996 [ - + ]: 8818 : Assert(attributeNumber >= 1);
997 [ - + ]: 8818 : Assert(attributeNumber <= desc->natts);
1294 998 [ - + ]: 8818 : Assert(attdim >= 0);
999 [ - + ]: 8818 : Assert(attdim <= PG_INT16_MAX);
1000 : :
1001 : : /* initialize the attribute fields */
3340 andres@anarazel.de 1002 : 8818 : att = TupleDescAttr(desc, attributeNumber - 1);
3548 rhaas@postgresql.org 1003 : 8818 : att->attrelid = 0; /* dummy value */
1004 : :
1005 : : /* unlike TupleDescInitEntry, we require an attribute name */
1006 [ - + ]: 8818 : Assert(attributeName != NULL);
1007 : 8818 : namestrcpy(&(att->attname), attributeName);
1008 : :
1009 : 8818 : att->atttypmod = typmod;
1010 : :
1011 : 8818 : att->attnum = attributeNumber;
1012 : 8818 : att->attndims = attdim;
1013 : :
1014 : 8818 : att->attnotnull = false;
1015 : 8818 : att->atthasdef = false;
3120 andrew@dunslane.net 1016 : 8818 : att->atthasmissing = false;
3476 peter_e@gmx.net 1017 : 8818 : att->attidentity = '\0';
2753 peter@eisentraut.org 1018 : 8818 : att->attgenerated = '\0';
3548 rhaas@postgresql.org 1019 : 8818 : att->attisdropped = false;
1020 : 8818 : att->attislocal = true;
1021 : 8818 : att->attinhcount = 0;
1022 : : /* variable-length fields are not present in tupledescs */
1023 : :
1024 : 8818 : att->atttypid = oidtypeid;
1025 : :
1026 : : /*
1027 : : * Our goal here is to support just enough types to let basic builtin
1028 : : * commands work without catalog access - e.g. so that we can do certain
1029 : : * things even in processes that are not connected to a database.
1030 : : */
1031 [ + - - + : 8818 : switch (oidtypeid)
+ - ]
1032 : : {
1033 : 7175 : case TEXTOID:
1034 : : case TEXTARRAYOID:
1035 : 7175 : att->attlen = -1;
1036 : 7175 : att->attbyval = false;
2413 tgl@sss.pgh.pa.us 1037 : 7175 : att->attalign = TYPALIGN_INT;
1038 : 7175 : att->attstorage = TYPSTORAGE_EXTENDED;
1964 1039 : 7175 : att->attcompression = InvalidCompressionMethod;
3548 rhaas@postgresql.org 1040 : 7175 : att->attcollation = DEFAULT_COLLATION_OID;
1041 : 7175 : break;
1042 : :
3548 rhaas@postgresql.org 1043 :UBC 0 : case BOOLOID:
1044 : 0 : att->attlen = 1;
1045 : 0 : att->attbyval = true;
2413 tgl@sss.pgh.pa.us 1046 : 0 : att->attalign = TYPALIGN_CHAR;
1047 : 0 : att->attstorage = TYPSTORAGE_PLAIN;
1968 1048 : 0 : att->attcompression = InvalidCompressionMethod;
3548 rhaas@postgresql.org 1049 : 0 : att->attcollation = InvalidOid;
1050 : 0 : break;
1051 : :
1052 : 0 : case INT4OID:
1053 : 0 : att->attlen = 4;
1054 : 0 : att->attbyval = true;
2413 tgl@sss.pgh.pa.us 1055 : 0 : att->attalign = TYPALIGN_INT;
1056 : 0 : att->attstorage = TYPSTORAGE_PLAIN;
1968 1057 : 0 : att->attcompression = InvalidCompressionMethod;
3548 rhaas@postgresql.org 1058 : 0 : att->attcollation = InvalidOid;
3540 1059 : 0 : break;
1060 : :
3540 rhaas@postgresql.org 1061 :CBC 1452 : case INT8OID:
1062 : 1452 : att->attlen = 8;
425 tgl@sss.pgh.pa.us 1063 : 1452 : att->attbyval = true;
2413 1064 : 1452 : att->attalign = TYPALIGN_DOUBLE;
1065 : 1452 : att->attstorage = TYPSTORAGE_PLAIN;
1968 1066 : 1452 : att->attcompression = InvalidCompressionMethod;
3540 rhaas@postgresql.org 1067 : 1452 : att->attcollation = InvalidOid;
3548 1068 : 1452 : break;
1069 : :
1561 peter@eisentraut.org 1070 : 191 : case OIDOID:
1071 : 191 : att->attlen = 4;
1072 : 191 : att->attbyval = true;
1073 : 191 : att->attalign = TYPALIGN_INT;
1074 : 191 : att->attstorage = TYPSTORAGE_PLAIN;
1075 : 191 : att->attcompression = InvalidCompressionMethod;
1076 : 191 : att->attcollation = InvalidOid;
1077 : 191 : break;
1078 : :
2863 tgl@sss.pgh.pa.us 1079 :UBC 0 : default:
1080 [ # # ]: 0 : elog(ERROR, "unsupported type %u", oidtypeid);
1081 : : }
1082 : :
661 drowley@postgresql.o 1083 :CBC 8818 : populate_compact_attribute(desc, attributeNumber - 1);
3548 rhaas@postgresql.org 1084 : 8818 : }
1085 : :
1086 : : /*
1087 : : * TupleDescInitEntryCollation
1088 : : *
1089 : : * Assign a nondefault collation to a previously initialized tuple descriptor
1090 : : * entry.
1091 : : */
1092 : : void
5725 peter_e@gmx.net 1093 : 4142822 : TupleDescInitEntryCollation(TupleDesc desc,
1094 : : AttrNumber attributeNumber,
1095 : : Oid collationid)
1096 : : {
1097 : : /*
1098 : : * sanity checks
1099 : : */
383 peter@eisentraut.org 1100 [ - + ]: 4142822 : Assert(desc);
1445 1101 [ - + ]: 4142822 : Assert(attributeNumber >= 1);
1102 [ - + ]: 4142822 : Assert(attributeNumber <= desc->natts);
1103 : :
3340 andres@anarazel.de 1104 : 4142822 : TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
5725 peter_e@gmx.net 1105 : 4142822 : }
1106 : :
1107 : : /*
1108 : : * BuildDescFromLists
1109 : : *
1110 : : * Build a TupleDesc given lists of column names (as String nodes),
1111 : : * column type OIDs, typmods, and collation OIDs.
1112 : : *
1113 : : * No constraints are generated.
1114 : : *
1115 : : * This is for use with functions returning RECORD.
1116 : : */
1117 : : TupleDesc
1112 peter@eisentraut.org 1118 : 970 : BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
1119 : : {
1120 : : int natts;
1121 : : AttrNumber attnum;
1122 : : ListCell *l1;
1123 : : ListCell *l2;
1124 : : ListCell *l3;
1125 : : ListCell *l4;
1126 : : TupleDesc desc;
1127 : :
7515 tgl@sss.pgh.pa.us 1128 : 970 : natts = list_length(names);
1129 [ - + ]: 970 : Assert(natts == list_length(types));
1130 [ - + ]: 970 : Assert(natts == list_length(typmods));
5725 peter_e@gmx.net 1131 [ - + ]: 970 : Assert(natts == list_length(collations));
1132 : :
1133 : : /*
1134 : : * allocate a new tuple descriptor
1135 : : */
2883 andres@anarazel.de 1136 : 970 : desc = CreateTemplateTupleDesc(natts);
1137 : :
7515 tgl@sss.pgh.pa.us 1138 : 970 : attnum = 0;
2783 1139 [ + - + + : 3553 : forfour(l1, names, l2, types, l3, typmods, l4, collations)
+ - + + +
- + + + -
+ + + + +
- + - + -
+ + ]
1140 : : {
7515 1141 : 2583 : char *attname = strVal(lfirst(l1));
2783 1142 : 2583 : Oid atttypid = lfirst_oid(l2);
1143 : 2583 : int32 atttypmod = lfirst_int(l3);
1144 : 2583 : Oid attcollation = lfirst_oid(l4);
1145 : :
7515 1146 : 2583 : attnum++;
1147 : :
1148 : 2583 : TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
5725 peter_e@gmx.net 1149 : 2583 : TupleDescInitEntryCollation(desc, attnum, attcollation);
1150 : : }
1151 : :
210 drowley@postgresql.o 1152 : 970 : TupleDescFinalize(desc);
1153 : :
7515 tgl@sss.pgh.pa.us 1154 : 970 : return desc;
1155 : : }
1156 : :
1157 : : /*
1158 : : * Get default expression (or NULL if none) for the given attribute number.
1159 : : */
1160 : : Node *
1111 peter@eisentraut.org 1161 : 77896 : TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
1162 : : {
1163 : 77896 : Node *result = NULL;
1164 : :
1165 [ + - ]: 77896 : if (tupdesc->constr)
1166 : : {
1167 : 77896 : AttrDefault *attrdef = tupdesc->constr->defval;
1168 : :
1169 [ + - ]: 118042 : for (int i = 0; i < tupdesc->constr->num_defval; i++)
1170 : : {
1171 [ + + ]: 118042 : if (attrdef[i].adnum == attnum)
1172 : : {
1173 : 77896 : result = stringToNode(attrdef[i].adbin);
1174 : 77896 : break;
1175 : : }
1176 : : }
1177 : : }
1178 : :
1179 : 77896 : return result;
1180 : : }
1181 : :
1182 : : /* ResourceOwner callbacks */
1183 : :
1184 : : static void
1069 heikki.linnakangas@i 1185 : 10816 : ResOwnerReleaseTupleDesc(Datum res)
1186 : : {
1187 : 10816 : TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1188 : :
1189 : : /* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
1190 [ - + ]: 10816 : Assert(tupdesc->tdrefcount > 0);
1191 [ + + ]: 10816 : if (--tupdesc->tdrefcount == 0)
1192 : 414 : FreeTupleDesc(tupdesc);
1193 : 10816 : }
1194 : :
1195 : : static char *
1069 heikki.linnakangas@i 1196 :UBC 0 : ResOwnerPrintTupleDesc(Datum res)
1197 : : {
1198 : 0 : TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1199 : :
1200 : 0 : return psprintf("TupleDesc %p (%u,%d)",
1201 : : tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1202 : : }
|