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
909 heikki.linnakangas@i 49 :CBC 20497612 : ResourceOwnerRememberTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
50 : : {
51 : 20497612 : ResourceOwnerRemember(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
52 : 20497612 : }
53 : :
54 : : static inline void
55 : 20485978 : ResourceOwnerForgetTupleDesc(ResourceOwner owner, TupleDesc tupdesc)
56 : : {
57 : 20485978 : ResourceOwnerForget(owner, PointerGetDatum(tupdesc), &tupdesc_resowner_desc);
58 : 20485978 : }
59 : :
60 : : /*
61 : : * populate_compact_attribute_internal
62 : : * Helper function for populate_compact_attribute()
63 : : */
64 : : static inline void
497 drowley@postgresql.o 65 : 1374938656 : populate_compact_attribute_internal(Form_pg_attribute src,
66 : : CompactAttribute *dst)
67 : : {
501 68 : 1374938656 : memset(dst, 0, sizeof(CompactAttribute));
69 : :
70 : 1374938656 : dst->attcacheoff = -1;
71 : 1374938656 : dst->attlen = src->attlen;
72 : :
73 : 1374938656 : dst->attbyval = src->attbyval;
74 : 1374938656 : dst->attispackable = (src->attstorage != TYPSTORAGE_PLAIN);
75 : 1374938656 : dst->atthasmissing = src->atthasmissing;
76 : 1374938656 : dst->attisdropped = src->attisdropped;
77 : 1374938656 : 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 : : */
393 alvherre@alvh.no-ip. 85 [ + + + + ]: 1727609860 : dst->attnullability = !src->attnotnull ? ATTNULLABLE_UNRESTRICTED :
86 : 352671204 : IsCatalogRelationOid(src->attrelid) ? ATTNULLABLE_VALID :
87 : : ATTNULLABLE_UNKNOWN;
88 : :
89 : : /* Compute numeric alignment requirement, too */
92 tgl@sss.pgh.pa.us 90 :GNC 1374938656 : dst->attalignby = typalign_to_alignby(src->attalign);
501 drowley@postgresql.o 91 :CBC 1374938656 : }
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
497 100 : 41201882 : populate_compact_attribute(TupleDesc tupdesc, int attnum)
101 : : {
102 : 41201882 : 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 : 41201882 : dst = &tupdesc->compact_attrs[attnum];
110 : :
111 : 41201882 : populate_compact_attribute_internal(src, dst);
112 : 41201882 : }
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 : 1333736774 : verify_compact_attribute(TupleDesc tupdesc, int attnum)
126 : : {
127 : : #ifdef USE_ASSERT_CHECKING
128 : : CompactAttribute cattr;
129 : 1333736774 : 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 : : */
322 137 : 1333736774 : memcpy(&cattr, &tupdesc->compact_attrs[attnum], sizeof(CompactAttribute));
138 : :
139 : : /*
140 : : * Populate the temporary CompactAttribute from the corresponding
141 : : * Form_pg_attribute
142 : : */
497 143 : 1333736774 : 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 : : */
322 149 : 1333736774 : tmp.attcacheoff = cattr.attcacheoff;
150 : 1333736774 : tmp.attnullability = cattr.attnullability;
151 : :
152 : : /* Check the freshly populated CompactAttribute matches the TupleDesc's */
153 [ - + ]: 1333736774 : Assert(memcmp(&tmp, &cattr, sizeof(CompactAttribute)) == 0);
154 : : #endif
479 155 : 1333736774 : }
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
2723 andres@anarazel.de 165 : 6123366 : CreateTemplateTupleDesc(int natts)
166 : : {
167 : : TupleDesc desc;
168 : :
169 : : /*
170 : : * sanity checks
171 : : */
1285 peter@eisentraut.org 172 [ - + ]: 6123366 : 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 : : */
501 drowley@postgresql.o 187 : 6123366 : desc = (TupleDesc) palloc(offsetof(struct TupleDescData, compact_attrs) +
501 drowley@postgresql.o 188 :ECB (4678311) : natts * sizeof(CompactAttribute) +
3180 andres@anarazel.de 189 :GIC 6123366 : natts * sizeof(FormData_pg_attribute));
190 : :
191 : : /*
192 : : * Initialize other fields of the tupdesc.
193 : : */
8069 tgl@sss.pgh.pa.us 194 :CBC 6123366 : desc->natts = natts;
8620 195 : 6123366 : desc->constr = NULL;
8069 196 : 6123366 : desc->tdtypeid = RECORDOID;
197 : 6123366 : desc->tdtypmod = -1;
7263 198 : 6123366 : desc->tdrefcount = -1; /* assume not reference-counted */
199 : :
200 : : /* This will be set to the correct value by TupleDescFinalize() */
50 drowley@postgresql.o 201 :GNC 6123366 : desc->firstNonCachedOffsetAttr = -1;
202 : 6123366 : desc->firstNonGuaranteedAttr = -1;
203 : :
10108 bruce@momjian.us 204 :CBC 6123366 : 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
2723 andres@anarazel.de 216 : 666327 : CreateTupleDesc(int natts, Form_pg_attribute *attrs)
217 : : {
218 : : TupleDesc desc;
219 : : int i;
220 : :
221 : 666327 : desc = CreateTemplateTupleDesc(natts);
222 : :
3180 223 [ + + ]: 10159659 : for (i = 0; i < natts; ++i)
224 : : {
225 : 9493332 : memcpy(TupleDescAttr(desc, i), attrs[i], ATTRIBUTE_FIXED_PART_SIZE);
501 drowley@postgresql.o 226 : 9493332 : populate_compact_attribute(desc, i);
227 : : }
228 : :
50 drowley@postgresql.o 229 :GNC 666327 : TupleDescFinalize(desc);
230 : :
10108 bruce@momjian.us 231 :CBC 666327 : 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
10892 scrappy@hub.org 242 : 259670 : CreateTupleDescCopy(TupleDesc tupdesc)
243 : : {
244 : : TupleDesc desc;
245 : : int i;
246 : :
2723 andres@anarazel.de 247 : 259670 : desc = CreateTemplateTupleDesc(tupdesc->natts);
248 : :
249 : : /* Flat-copy the attribute array (unless there are no attributes) */
42 rhaas@postgresql.org 250 [ + + ]:GNC 259670 : if (desc->natts > 0)
251 : 257291 : memcpy(TupleDescAttr(desc, 0),
252 : 257291 : TupleDescAttr(tupdesc, 0),
253 : 257291 : desc->natts * sizeof(FormData_pg_attribute));
254 : :
255 : : /*
256 : : * Since we're not copying constraints and defaults, clear fields
257 : : * associated with them.
258 : : */
7729 tgl@sss.pgh.pa.us 259 [ + + ]:CBC 1272897 : for (i = 0; i < desc->natts; i++)
260 : : {
3180 andres@anarazel.de 261 : 1013227 : Form_pg_attribute att = TupleDescAttr(desc, i);
262 : :
263 : 1013227 : att->attnotnull = false;
264 : 1013227 : att->atthasdef = false;
2960 andrew@dunslane.net 265 : 1013227 : att->atthasmissing = false;
3180 andres@anarazel.de 266 : 1013227 : att->attidentity = '\0';
2593 peter@eisentraut.org 267 : 1013227 : att->attgenerated = '\0';
268 : :
501 drowley@postgresql.o 269 : 1013227 : populate_compact_attribute(desc, i);
270 : : }
271 : :
272 : : /* We can copy the tuple type identification, too */
273 : 259670 : desc->tdtypeid = tupdesc->tdtypeid;
274 : 259670 : desc->tdtypmod = tupdesc->tdtypmod;
275 : :
50 drowley@postgresql.o 276 :GNC 259670 : TupleDescFinalize(desc);
277 : :
501 drowley@postgresql.o 278 :CBC 259670 : 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 : 25820 : CreateTupleDescTruncatedCopy(TupleDesc tupdesc, int natts)
290 : : {
291 : : TupleDesc desc;
292 : : int i;
293 : :
294 [ - + ]: 25820 : Assert(natts <= tupdesc->natts);
295 : :
296 : 25820 : desc = CreateTemplateTupleDesc(natts);
297 : :
298 : : /* Flat-copy the attribute array (unless there are no attributes) */
42 rhaas@postgresql.org 299 [ + - ]:GNC 25820 : if (desc->natts > 0)
300 : 25820 : memcpy(TupleDescAttr(desc, 0),
301 : 25820 : TupleDescAttr(tupdesc, 0),
302 : 25820 : desc->natts * sizeof(FormData_pg_attribute));
303 : :
304 : : /*
305 : : * Since we're not copying constraints and defaults, clear fields
306 : : * associated with them.
307 : : */
501 drowley@postgresql.o 308 [ + + ]:CBC 62763 : for (i = 0; i < desc->natts; i++)
309 : : {
310 : 36943 : Form_pg_attribute att = TupleDescAttr(desc, i);
311 : :
312 : 36943 : att->attnotnull = false;
313 : 36943 : att->atthasdef = false;
314 : 36943 : att->atthasmissing = false;
315 : 36943 : att->attidentity = '\0';
316 : 36943 : att->attgenerated = '\0';
317 : :
318 : 36943 : populate_compact_attribute(desc, i);
319 : : }
320 : :
321 : : /* We can copy the tuple type identification, too */
8069 tgl@sss.pgh.pa.us 322 : 25820 : desc->tdtypeid = tupdesc->tdtypeid;
323 : 25820 : desc->tdtypmod = tupdesc->tdtypmod;
324 : :
50 drowley@postgresql.o 325 :GNC 25820 : TupleDescFinalize(desc);
326 : :
10467 bruce@momjian.us 327 :CBC 25820 : 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
10483 vadim4o@yahoo.com 336 : 498772 : CreateTupleDescCopyConstr(TupleDesc tupdesc)
337 : : {
338 : : TupleDesc desc;
10466 bruce@momjian.us 339 : 498772 : TupleConstr *constr = tupdesc->constr;
340 : : int i;
341 : :
2723 andres@anarazel.de 342 : 498772 : desc = CreateTemplateTupleDesc(tupdesc->natts);
343 : :
344 : : /* Flat-copy the attribute array (unless there are no attributes) */
42 rhaas@postgresql.org 345 [ + + ]:GNC 498772 : if (desc->natts > 0)
346 : 498710 : memcpy(TupleDescAttr(desc, 0),
347 : 498710 : TupleDescAttr(tupdesc, 0),
348 : 498710 : desc->natts * sizeof(FormData_pg_attribute));
349 : :
501 drowley@postgresql.o 350 [ + + ]:CBC 7168591 : for (i = 0; i < desc->natts; i++)
351 : : {
352 : 6669819 : populate_compact_attribute(desc, i);
353 : :
393 alvherre@alvh.no-ip. 354 : 6669819 : TupleDescCompactAttr(desc, i)->attnullability =
355 : 13339638 : TupleDescCompactAttr(tupdesc, i)->attnullability;
356 : : }
357 : :
358 : : /* Copy the TupleConstr data structure, if any */
10467 bruce@momjian.us 359 [ + + ]: 498772 : if (constr)
360 : : {
146 michael@paquier.xyz 361 :GNC 453710 : TupleConstr *cpy = palloc0_object(TupleConstr);
362 : :
10467 bruce@momjian.us 363 :CBC 453710 : cpy->has_not_null = constr->has_not_null;
2593 peter@eisentraut.org 364 : 453710 : cpy->has_generated_stored = constr->has_generated_stored;
452 365 : 453710 : cpy->has_generated_virtual = constr->has_generated_virtual;
366 : :
10467 bruce@momjian.us 367 [ + + ]: 453710 : if ((cpy->num_defval = constr->num_defval) > 0)
368 : : {
369 : 2935 : cpy->defval = (AttrDefault *) palloc(cpy->num_defval * sizeof(AttrDefault));
370 : 2935 : memcpy(cpy->defval, constr->defval, cpy->num_defval * sizeof(AttrDefault));
371 [ + + ]: 7183 : for (i = cpy->num_defval - 1; i >= 0; i--)
1855 tgl@sss.pgh.pa.us 372 : 4248 : cpy->defval[i].adbin = pstrdup(constr->defval[i].adbin);
373 : : }
374 : :
2960 andrew@dunslane.net 375 [ + + ]: 453710 : if (constr->missing)
376 : : {
377 : 434 : cpy->missing = (AttrMissing *) palloc(tupdesc->natts * sizeof(AttrMissing));
378 : 434 : memcpy(cpy->missing, constr->missing, tupdesc->natts * sizeof(AttrMissing));
379 [ + + ]: 3326 : for (i = tupdesc->natts - 1; i >= 0; i--)
380 : : {
2869 akapila@postgresql.o 381 [ + + ]: 2892 : if (constr->missing[i].am_present)
382 : : {
501 drowley@postgresql.o 383 : 782 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
384 : :
2869 akapila@postgresql.o 385 : 782 : cpy->missing[i].am_value = datumCopy(constr->missing[i].am_value,
386 : 782 : attr->attbyval,
387 : 782 : attr->attlen);
388 : : }
389 : : }
390 : : }
391 : :
10467 bruce@momjian.us 392 [ + + ]: 453710 : if ((cpy->num_check = constr->num_check) > 0)
393 : : {
394 : 1779 : cpy->check = (ConstrCheck *) palloc(cpy->num_check * sizeof(ConstrCheck));
395 : 1779 : memcpy(cpy->check, constr->check, cpy->num_check * sizeof(ConstrCheck));
396 [ + + ]: 5303 : for (i = cpy->num_check - 1; i >= 0; i--)
397 : : {
1855 tgl@sss.pgh.pa.us 398 : 3524 : cpy->check[i].ccname = pstrdup(constr->check[i].ccname);
399 : 3524 : cpy->check[i].ccbin = pstrdup(constr->check[i].ccbin);
479 peter@eisentraut.org 400 : 3524 : cpy->check[i].ccenforced = constr->check[i].ccenforced;
5452 alvherre@alvh.no-ip. 401 : 3524 : cpy->check[i].ccvalid = constr->check[i].ccvalid;
4426 noah@leadboat.com 402 : 3524 : cpy->check[i].ccnoinherit = constr->check[i].ccnoinherit;
403 : : }
404 : : }
405 : :
10467 bruce@momjian.us 406 : 453710 : desc->constr = cpy;
407 : : }
408 : :
409 : : /* We can copy the tuple type identification, too */
8069 tgl@sss.pgh.pa.us 410 : 498772 : desc->tdtypeid = tupdesc->tdtypeid;
411 : 498772 : desc->tdtypmod = tupdesc->tdtypmod;
412 : :
50 drowley@postgresql.o 413 :GNC 498772 : TupleDescFinalize(desc);
414 : :
10467 bruce@momjian.us 415 :CBC 498772 : 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
3155 andres@anarazel.de 427 : 236 : TupleDescCopy(TupleDesc dst, TupleDesc src)
428 : : {
429 : : int i;
430 : :
431 : : /* Flat-copy the header and attribute arrays */
432 : 236 : memcpy(dst, src, TupleDescSize(src));
433 : :
434 : : /*
435 : : * Since we're not copying constraints and defaults, clear fields
436 : : * associated with them.
437 : : */
3044 tgl@sss.pgh.pa.us 438 [ + + ]: 933 : for (i = 0; i < dst->natts; i++)
439 : : {
440 : 697 : Form_pg_attribute att = TupleDescAttr(dst, i);
441 : :
442 : 697 : att->attnotnull = false;
443 : 697 : att->atthasdef = false;
2960 andrew@dunslane.net 444 : 697 : att->atthasmissing = false;
3044 tgl@sss.pgh.pa.us 445 : 697 : att->attidentity = '\0';
2593 peter@eisentraut.org 446 : 697 : att->attgenerated = '\0';
447 : :
501 drowley@postgresql.o 448 : 697 : populate_compact_attribute(dst, i);
449 : : }
3155 andres@anarazel.de 450 : 236 : 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 : 236 : dst->tdrefcount = -1;
457 : :
50 drowley@postgresql.o 458 :GNC 236 : TupleDescFinalize(dst);
3155 andres@anarazel.de 459 :CBC 236 : }
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
4548 tgl@sss.pgh.pa.us 472 : 2869 : TupleDescCopyEntry(TupleDesc dst, AttrNumber dstAttno,
473 : : TupleDesc src, AttrNumber srcAttno)
474 : : {
3180 andres@anarazel.de 475 : 2869 : Form_pg_attribute dstAtt = TupleDescAttr(dst, dstAttno - 1);
476 : 2869 : Form_pg_attribute srcAtt = TupleDescAttr(src, srcAttno - 1);
477 : :
478 : : /*
479 : : * sanity checks
480 : : */
223 peter@eisentraut.org 481 [ - + ]:GNC 2869 : Assert(src);
482 [ - + ]: 2869 : Assert(dst);
1285 peter@eisentraut.org 483 [ - + ]:CBC 2869 : Assert(srcAttno >= 1);
484 [ - + ]: 2869 : Assert(srcAttno <= src->natts);
485 [ - + ]: 2869 : Assert(dstAttno >= 1);
486 [ - + ]: 2869 : Assert(dstAttno <= dst->natts);
487 : :
3180 andres@anarazel.de 488 : 2869 : memcpy(dstAtt, srcAtt, ATTRIBUTE_FIXED_PART_SIZE);
489 : :
490 : 2869 : dstAtt->attnum = dstAttno;
491 : :
492 : : /* since we're not copying constraints or defaults, clear these */
493 : 2869 : dstAtt->attnotnull = false;
494 : 2869 : dstAtt->atthasdef = false;
2960 andrew@dunslane.net 495 : 2869 : dstAtt->atthasmissing = false;
3180 andres@anarazel.de 496 : 2869 : dstAtt->attidentity = '\0';
2593 peter@eisentraut.org 497 : 2869 : dstAtt->attgenerated = '\0';
498 : :
501 drowley@postgresql.o 499 : 2869 : populate_compact_attribute(dst, dstAttno - 1);
4548 tgl@sss.pgh.pa.us 500 : 2869 : }
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
50 drowley@postgresql.o 511 :GNC 6256730 : TupleDescFinalize(TupleDesc tupdesc)
512 : : {
513 : 6256730 : int firstNonCachedOffsetAttr = 0;
514 : 6256730 : int firstNonGuaranteedAttr = tupdesc->natts;
515 : 6256730 : int off = 0;
516 : :
517 [ + + ]: 35043260 : for (int i = 0; i < tupdesc->natts; i++)
518 : : {
519 : 31963284 : CompactAttribute *cattr = TupleDescCompactAttr(tupdesc, i);
520 : :
521 : : /*
522 : : * Find the highest attnum which is guaranteed to exist in all tuples
523 : : * in the table. We currently only pay attention to byval attributes
524 : : * to allow additional optimizations during tuple deformation.
525 : : */
526 [ + + ]: 31963284 : if (firstNonGuaranteedAttr == tupdesc->natts &&
527 [ + + + + : 13208310 : (cattr->attnullability != ATTNULLABLE_VALID || !cattr->attbyval ||
+ + ]
528 [ + - - + ]: 7466649 : cattr->atthasmissing || cattr->attisdropped || cattr->attlen <= 0))
529 : 5741661 : firstNonGuaranteedAttr = i;
530 : :
531 [ + + ]: 31963284 : if (cattr->attlen <= 0)
532 : 3176754 : break;
533 : :
534 : 28786530 : off = att_nominal_alignby(off, cattr->attalignby);
535 : :
536 : : /*
537 : : * attcacheoff is an int16, so don't try to cache any offsets larger
538 : : * than will fit in that type. Any attributes which are offset more
539 : : * than 2^15 are likely due to variable-length attributes. Since we
540 : : * don't cache offsets for or beyond variable-length attributes, using
541 : : * an int16 rather than an int32 here is unlikely to cost us anything.
542 : : */
49 543 [ - + ]: 28786530 : if (off > PG_INT16_MAX)
49 drowley@postgresql.o 544 :UNC 0 : break;
545 : :
49 drowley@postgresql.o 546 :GNC 28786530 : cattr->attcacheoff = (int16) off;
547 : :
50 548 : 28786530 : off += cattr->attlen;
549 : 28786530 : firstNonCachedOffsetAttr = i + 1;
550 : : }
551 : :
552 : 6256730 : tupdesc->firstNonCachedOffsetAttr = firstNonCachedOffsetAttr;
553 : 6256730 : tupdesc->firstNonGuaranteedAttr = firstNonGuaranteedAttr;
554 : 6256730 : }
555 : :
556 : : /*
557 : : * Free a TupleDesc including all substructure
558 : : */
559 : : void
10467 bruce@momjian.us 560 :CBC 953912 : FreeTupleDesc(TupleDesc tupdesc)
561 : : {
562 : : int i;
563 : :
564 : : /*
565 : : * Possibly this should assert tdrefcount == 0, to disallow explicit
566 : : * freeing of un-refcounted tupdescs?
567 : : */
7263 tgl@sss.pgh.pa.us 568 [ - + ]: 953912 : Assert(tupdesc->tdrefcount <= 0);
569 : :
10467 bruce@momjian.us 570 [ + + ]: 953912 : if (tupdesc->constr)
571 : : {
572 [ + + ]: 285383 : if (tupdesc->constr->num_defval > 0)
573 : : {
10466 574 : 23078 : AttrDefault *attrdef = tupdesc->constr->defval;
575 : :
10467 576 [ + + ]: 56462 : for (i = tupdesc->constr->num_defval - 1; i >= 0; i--)
1855 tgl@sss.pgh.pa.us 577 : 33384 : pfree(attrdef[i].adbin);
10467 bruce@momjian.us 578 : 23078 : pfree(attrdef);
579 : : }
2960 andrew@dunslane.net 580 [ + + ]: 285383 : if (tupdesc->constr->missing)
581 : : {
582 : 2506 : AttrMissing *attrmiss = tupdesc->constr->missing;
583 : :
584 [ + + ]: 17865 : for (i = tupdesc->natts - 1; i >= 0; i--)
585 : : {
2869 akapila@postgresql.o 586 [ + + ]: 15359 : if (attrmiss[i].am_present
2960 andrew@dunslane.net 587 [ + + ]: 5311 : && !TupleDescAttr(tupdesc, i)->attbyval)
2869 akapila@postgresql.o 588 : 2002 : pfree(DatumGetPointer(attrmiss[i].am_value));
589 : : }
2960 andrew@dunslane.net 590 : 2506 : pfree(attrmiss);
591 : : }
10467 bruce@momjian.us 592 [ + + ]: 285383 : if (tupdesc->constr->num_check > 0)
593 : : {
10466 594 : 8351 : ConstrCheck *check = tupdesc->constr->check;
595 : :
10467 596 [ + + ]: 23324 : for (i = tupdesc->constr->num_check - 1; i >= 0; i--)
597 : : {
1855 tgl@sss.pgh.pa.us 598 : 14973 : pfree(check[i].ccname);
599 : 14973 : pfree(check[i].ccbin);
600 : : }
10467 bruce@momjian.us 601 : 8351 : pfree(check);
602 : : }
603 : 285383 : pfree(tupdesc->constr);
604 : : }
605 : :
606 : 953912 : pfree(tupdesc);
10483 vadim4o@yahoo.com 607 : 953912 : }
608 : :
609 : : /*
610 : : * Increment the reference count of a tupdesc, and log the reference in
611 : : * CurrentResourceOwner.
612 : : *
613 : : * Do not apply this to tupdescs that are not being refcounted. (Use the
614 : : * macro PinTupleDesc for tupdescs of uncertain status.)
615 : : */
616 : : void
7263 tgl@sss.pgh.pa.us 617 : 20497612 : IncrTupleDescRefCount(TupleDesc tupdesc)
618 : : {
619 [ - + ]: 20497612 : Assert(tupdesc->tdrefcount >= 0);
620 : :
909 heikki.linnakangas@i 621 : 20497612 : ResourceOwnerEnlarge(CurrentResourceOwner);
7263 tgl@sss.pgh.pa.us 622 : 20497612 : tupdesc->tdrefcount++;
623 : 20497612 : ResourceOwnerRememberTupleDesc(CurrentResourceOwner, tupdesc);
624 : 20497612 : }
625 : :
626 : : /*
627 : : * Decrement the reference count of a tupdesc, remove the corresponding
628 : : * reference from CurrentResourceOwner, and free the tupdesc if no more
629 : : * references remain.
630 : : *
631 : : * Do not apply this to tupdescs that are not being refcounted. (Use the
632 : : * macro ReleaseTupleDesc for tupdescs of uncertain status.)
633 : : */
634 : : void
635 : 20485978 : DecrTupleDescRefCount(TupleDesc tupdesc)
636 : : {
637 [ - + ]: 20485978 : Assert(tupdesc->tdrefcount > 0);
638 : :
639 : 20485978 : ResourceOwnerForgetTupleDesc(CurrentResourceOwner, tupdesc);
640 [ - + ]: 20485978 : if (--tupdesc->tdrefcount == 0)
7263 tgl@sss.pgh.pa.us 641 :LBC (2) : FreeTupleDesc(tupdesc);
7263 tgl@sss.pgh.pa.us 642 :CBC 20485978 : }
643 : :
644 : : /*
645 : : * Compare two TupleDesc structures for logical equality
646 : : */
647 : : bool
9591 648 : 264191 : equalTupleDescs(TupleDesc tupdesc1, TupleDesc tupdesc2)
649 : : {
650 : : int i,
651 : : n;
652 : :
653 [ + + ]: 264191 : if (tupdesc1->natts != tupdesc2->natts)
654 : 1965 : return false;
8069 655 [ - + ]: 262226 : if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
8069 tgl@sss.pgh.pa.us 656 :UBC 0 : return false;
657 : :
658 : : /* tdtypmod and tdrefcount are not checked */
659 : :
9591 tgl@sss.pgh.pa.us 660 [ + + ]:CBC 1198406 : for (i = 0; i < tupdesc1->natts; i++)
661 : : {
3180 andres@anarazel.de 662 : 946255 : Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
663 : 946255 : Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
664 : :
665 : : /*
666 : : * We do not need to check every single field here: we can disregard
667 : : * attrelid and attnum (which were used to place the row in the attrs
668 : : * array in the first place). It might look like we could dispense
669 : : * with checking attlen/attbyval/attalign, since these are derived
670 : : * from atttypid; but in the case of dropped columns we must check
671 : : * them (since atttypid will be zero for all dropped columns) and in
672 : : * general it seems safer to check them always.
673 : : *
674 : : * We intentionally ignore atthasmissing, since that's not very
675 : : * relevant in tupdescs, which lack the attmissingval field.
676 : : */
9591 tgl@sss.pgh.pa.us 677 [ + + ]: 946255 : if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
678 : 918 : return false;
679 [ + + ]: 945337 : if (attr1->atttypid != attr2->atttypid)
680 : 756 : return false;
7691 681 [ + + ]: 944581 : if (attr1->attlen != attr2->attlen)
682 : 6 : return false;
8069 683 [ - + ]: 944575 : if (attr1->attndims != attr2->attndims)
8069 tgl@sss.pgh.pa.us 684 :UBC 0 : return false;
9591 tgl@sss.pgh.pa.us 685 [ + + ]:CBC 944575 : if (attr1->atttypmod != attr2->atttypmod)
686 : 28 : return false;
7691 687 [ + + ]: 944547 : if (attr1->attbyval != attr2->attbyval)
688 : 34 : return false;
1808 689 [ - + ]: 944513 : if (attr1->attalign != attr2->attalign)
1808 tgl@sss.pgh.pa.us 690 :UBC 0 : return false;
9591 tgl@sss.pgh.pa.us 691 [ + + ]:CBC 944513 : if (attr1->attstorage != attr2->attstorage)
692 : 157 : return false;
1808 693 [ + + ]: 944356 : if (attr1->attcompression != attr2->attcompression)
7691 694 : 44 : return false;
9591 695 [ + + ]: 944312 : if (attr1->attnotnull != attr2->attnotnull)
696 : 1166 : return false;
697 : :
698 : : /*
699 : : * When the column has a not-null constraint, we also need to consider
700 : : * its validity aspect, which only manifests in CompactAttribute->
701 : : * attnullability, so verify that.
702 : : */
393 alvherre@alvh.no-ip. 703 [ + + ]: 943146 : if (attr1->attnotnull)
704 : : {
705 : 279756 : CompactAttribute *cattr1 = TupleDescCompactAttr(tupdesc1, i);
706 : 279756 : CompactAttribute *cattr2 = TupleDescCompactAttr(tupdesc2, i);
707 : :
708 [ - + ]: 279756 : Assert(cattr1->attnullability != ATTNULLABLE_UNKNOWN);
709 [ - + ]: 279756 : Assert((cattr1->attnullability == ATTNULLABLE_UNKNOWN) ==
710 : : (cattr2->attnullability == ATTNULLABLE_UNKNOWN));
711 : :
712 [ + + ]: 279756 : if (cattr1->attnullability != cattr2->attnullability)
713 : 70 : return false;
714 : : }
8649 tgl@sss.pgh.pa.us 715 [ + + ]: 943076 : if (attr1->atthasdef != attr2->atthasdef)
716 : 3636 : return false;
3316 peter_e@gmx.net 717 [ + + ]: 939440 : if (attr1->attidentity != attr2->attidentity)
718 : 118 : return false;
2593 peter@eisentraut.org 719 [ + + ]: 939322 : if (attr1->attgenerated != attr2->attgenerated)
720 : 13 : return false;
8677 tgl@sss.pgh.pa.us 721 [ - + ]: 939309 : if (attr1->attisdropped != attr2->attisdropped)
8677 tgl@sss.pgh.pa.us 722 :UBC 0 : return false;
8626 tgl@sss.pgh.pa.us 723 [ + + ]:CBC 939309 : if (attr1->attislocal != attr2->attislocal)
724 : 2624 : return false;
725 [ + + ]: 936685 : if (attr1->attinhcount != attr2->attinhcount)
8649 726 : 505 : return false;
5492 727 [ - + ]: 936180 : if (attr1->attcollation != attr2->attcollation)
5492 tgl@sss.pgh.pa.us 728 :UBC 0 : return false;
729 : : /* variable-length fields are not even present... */
730 : : }
731 : :
9591 tgl@sss.pgh.pa.us 732 [ + + ]:CBC 252151 : if (tupdesc1->constr != NULL)
733 : : {
9519 bruce@momjian.us 734 : 89466 : TupleConstr *constr1 = tupdesc1->constr;
735 : 89466 : TupleConstr *constr2 = tupdesc2->constr;
736 : :
9591 tgl@sss.pgh.pa.us 737 [ + + ]: 89466 : if (constr2 == NULL)
738 : 175 : return false;
9309 739 [ - + ]: 89291 : if (constr1->has_not_null != constr2->has_not_null)
9309 tgl@sss.pgh.pa.us 740 :UBC 0 : return false;
2593 peter@eisentraut.org 741 [ + + ]:CBC 89291 : if (constr1->has_generated_stored != constr2->has_generated_stored)
742 : 460 : return false;
452 743 [ + + ]: 88831 : if (constr1->has_generated_virtual != constr2->has_generated_virtual)
744 : 290 : return false;
9309 tgl@sss.pgh.pa.us 745 : 88541 : n = constr1->num_defval;
746 [ - + ]: 88541 : if (n != (int) constr2->num_defval)
9591 tgl@sss.pgh.pa.us 747 :UBC 0 : return false;
748 : : /* We assume here that both AttrDefault arrays are in adnum order */
9309 tgl@sss.pgh.pa.us 749 [ + + ]:CBC 101632 : for (i = 0; i < n; i++)
750 : : {
9519 bruce@momjian.us 751 : 13091 : AttrDefault *defval1 = constr1->defval + i;
1855 tgl@sss.pgh.pa.us 752 : 13091 : AttrDefault *defval2 = constr2->defval + i;
753 : :
754 [ - + ]: 13091 : if (defval1->adnum != defval2->adnum)
9591 tgl@sss.pgh.pa.us 755 :UBC 0 : return false;
9591 tgl@sss.pgh.pa.us 756 [ - + ]:CBC 13091 : if (strcmp(defval1->adbin, defval2->adbin) != 0)
9591 tgl@sss.pgh.pa.us 757 :UBC 0 : return false;
758 : : }
2960 andrew@dunslane.net 759 [ + + ]:CBC 88541 : if (constr1->missing)
760 : : {
761 [ + + ]: 486 : if (!constr2->missing)
762 : 60 : return false;
763 [ + + ]: 2547 : for (i = 0; i < tupdesc1->natts; i++)
764 : : {
765 : 2226 : AttrMissing *missval1 = constr1->missing + i;
766 : 2226 : AttrMissing *missval2 = constr2->missing + i;
767 : :
2869 akapila@postgresql.o 768 [ + + ]: 2226 : if (missval1->am_present != missval2->am_present)
2960 andrew@dunslane.net 769 : 105 : return false;
2869 akapila@postgresql.o 770 [ + + ]: 2121 : if (missval1->am_present)
771 : : {
501 drowley@postgresql.o 772 : 724 : CompactAttribute *missatt1 = TupleDescCompactAttr(tupdesc1, i);
773 : :
2869 akapila@postgresql.o 774 [ - + ]: 724 : if (!datumIsEqual(missval1->am_value, missval2->am_value,
2960 andrew@dunslane.net 775 : 724 : missatt1->attbyval, missatt1->attlen))
2960 andrew@dunslane.net 776 :UBC 0 : return false;
777 : : }
778 : : }
779 : : }
2960 andrew@dunslane.net 780 [ + + ]:CBC 88055 : else if (constr2->missing)
781 : 242 : return false;
9309 tgl@sss.pgh.pa.us 782 : 88134 : n = constr1->num_check;
783 [ + + ]: 88134 : if (n != (int) constr2->num_check)
9591 784 : 1175 : return false;
785 : :
786 : : /*
787 : : * Similarly, we rely here on the ConstrCheck entries being sorted by
788 : : * name. If there are duplicate names, the outcome of the comparison
789 : : * is uncertain, but that should not happen.
790 : : */
9309 791 [ + + ]: 89765 : for (i = 0; i < n; i++)
792 : : {
9519 bruce@momjian.us 793 : 2950 : ConstrCheck *check1 = constr1->check + i;
1855 tgl@sss.pgh.pa.us 794 : 2950 : ConstrCheck *check2 = constr2->check + i;
795 : :
796 [ + - ]: 2950 : if (!(strcmp(check1->ccname, check2->ccname) == 0 &&
797 [ + - ]: 2950 : strcmp(check1->ccbin, check2->ccbin) == 0 &&
479 peter@eisentraut.org 798 [ + + ]: 2950 : check1->ccenforced == check2->ccenforced &&
1855 tgl@sss.pgh.pa.us 799 [ + + ]: 2862 : check1->ccvalid == check2->ccvalid &&
800 [ - + ]: 2806 : check1->ccnoinherit == check2->ccnoinherit))
9591 801 : 144 : return false;
802 : : }
803 : : }
804 [ + + ]: 162685 : else if (tupdesc2->constr != NULL)
805 : 1297 : return false;
806 : 248203 : return true;
807 : : }
808 : :
809 : : /*
810 : : * equalRowTypes
811 : : *
812 : : * This determines whether two tuple descriptors have equal row types. This
813 : : * only checks those fields in pg_attribute that are applicable for row types,
814 : : * while ignoring those fields that define the physical row storage or those
815 : : * that define table column metadata.
816 : : *
817 : : * Specifically, this checks:
818 : : *
819 : : * - same number of attributes
820 : : * - same composite type ID (but could both be zero)
821 : : * - corresponding attributes (in order) have same the name, type, typmod,
822 : : * collation
823 : : *
824 : : * This is used to check whether two record types are compatible, whether
825 : : * function return row types are the same, and other similar situations.
826 : : *
827 : : * (XXX There was some discussion whether attndims should be checked here, but
828 : : * for now it has been decided not to.)
829 : : *
830 : : * Note: We deliberately do not check the tdtypmod field. This allows
831 : : * typcache.c to use this routine to see if a cached record type matches a
832 : : * requested type.
833 : : */
834 : : bool
779 peter@eisentraut.org 835 : 277044 : equalRowTypes(TupleDesc tupdesc1, TupleDesc tupdesc2)
836 : : {
837 [ + + ]: 277044 : if (tupdesc1->natts != tupdesc2->natts)
838 : 113 : return false;
839 [ + + ]: 276931 : if (tupdesc1->tdtypeid != tupdesc2->tdtypeid)
840 : 1277 : return false;
841 : :
842 [ + + ]: 3833314 : for (int i = 0; i < tupdesc1->natts; i++)
843 : : {
844 : 3563425 : Form_pg_attribute attr1 = TupleDescAttr(tupdesc1, i);
845 : 3563425 : Form_pg_attribute attr2 = TupleDescAttr(tupdesc2, i);
846 : :
847 [ + + ]: 3563425 : if (strcmp(NameStr(attr1->attname), NameStr(attr2->attname)) != 0)
848 : 5749 : return false;
849 [ + + ]: 3557676 : if (attr1->atttypid != attr2->atttypid)
850 : 14 : return false;
851 [ + + ]: 3557662 : if (attr1->atttypmod != attr2->atttypmod)
852 : 2 : return false;
853 [ - + ]: 3557660 : if (attr1->attcollation != attr2->attcollation)
779 peter@eisentraut.org 854 :UBC 0 : return false;
855 : :
856 : : /* Record types derived from tables could have dropped fields. */
779 peter@eisentraut.org 857 [ - + ]:CBC 3557660 : if (attr1->attisdropped != attr2->attisdropped)
779 peter@eisentraut.org 858 :UBC 0 : return false;
859 : : }
860 : :
779 peter@eisentraut.org 861 :CBC 269889 : return true;
862 : : }
863 : :
864 : : /*
865 : : * hashRowType
866 : : *
867 : : * If two tuple descriptors would be considered equal by equalRowTypes()
868 : : * then their hash value will be equal according to this function.
869 : : */
870 : : uint32
871 : 286128 : hashRowType(TupleDesc desc)
872 : : {
873 : : uint32 s;
874 : : int i;
875 : :
273 peter@eisentraut.org 876 :GNC 286128 : s = hash_combine(0, hash_bytes_uint32(desc->natts));
877 : 286128 : s = hash_combine(s, hash_bytes_uint32(desc->tdtypeid));
3178 andres@anarazel.de 878 [ + + ]:CBC 4025297 : for (i = 0; i < desc->natts; ++i)
273 peter@eisentraut.org 879 :GNC 3739169 : s = hash_combine(s, hash_bytes_uint32(TupleDescAttr(desc, i)->atttypid));
880 : :
3178 andres@anarazel.de 881 :CBC 286128 : return s;
882 : : }
883 : :
884 : : /*
885 : : * TupleDescInitEntry
886 : : * This function initializes a single attribute structure in
887 : : * a previously allocated tuple descriptor.
888 : : *
889 : : * If attributeName is NULL, the attname field is set to an empty string
890 : : * (this is for cases where we don't know or need a name for the field).
891 : : * Also, some callers use this function to change the datatype-related fields
892 : : * in an existing tupdesc; they pass attributeName = NameStr(att->attname)
893 : : * to indicate that the attname field shouldn't be modified.
894 : : *
895 : : * Note that attcollation is set to the default for the specified datatype.
896 : : * If a nondefault collation is needed, insert it afterwards using
897 : : * TupleDescInitEntryCollation.
898 : : */
899 : : void
10892 scrappy@hub.org 900 : 8095952 : TupleDescInitEntry(TupleDesc desc,
901 : : AttrNumber attributeNumber,
902 : : const char *attributeName,
903 : : Oid oidtypeid,
904 : : int32 typmod,
905 : : int attdim)
906 : : {
907 : : HeapTuple tuple;
908 : : Form_pg_type typeForm;
909 : : Form_pg_attribute att;
910 : :
911 : : /*
912 : : * sanity checks
913 : : */
223 peter@eisentraut.org 914 [ - + ]:GNC 8095952 : Assert(desc);
1285 peter@eisentraut.org 915 [ - + ]:CBC 8095952 : Assert(attributeNumber >= 1);
916 [ - + ]: 8095952 : Assert(attributeNumber <= desc->natts);
1134 917 [ - + ]: 8095952 : Assert(attdim >= 0);
918 [ - + ]: 8095952 : Assert(attdim <= PG_INT16_MAX);
919 : :
920 : : /*
921 : : * initialize the attribute fields
922 : : */
3180 andres@anarazel.de 923 : 8095952 : att = TupleDescAttr(desc, attributeNumber - 1);
924 : :
10467 bruce@momjian.us 925 : 8095952 : att->attrelid = 0; /* dummy value */
926 : :
927 : : /*
928 : : * Note: attributeName can be NULL, because the planner doesn't always
929 : : * fill in valid resname values in targetlists, particularly for resjunk
930 : : * attributes. Also, do nothing if caller wants to re-use the old attname.
931 : : */
4572 tgl@sss.pgh.pa.us 932 [ + + ]: 8095952 : if (attributeName == NULL)
9676 bruce@momjian.us 933 [ + + + - : 12889288 : MemSet(NameStr(att->attname), 0, NAMEDATALEN);
+ - + - +
+ ]
4572 tgl@sss.pgh.pa.us 934 [ + + ]: 5343144 : else if (attributeName != NameStr(att->attname))
935 : 5340722 : namestrcpy(&(att->attname), attributeName);
936 : :
10311 bruce@momjian.us 937 : 8095952 : att->atttypmod = typmod;
938 : :
10467 939 : 8095952 : att->attnum = attributeNumber;
9129 tgl@sss.pgh.pa.us 940 : 8095952 : att->attndims = attdim;
941 : :
10467 bruce@momjian.us 942 : 8095952 : att->attnotnull = false;
943 : 8095952 : att->atthasdef = false;
2960 andrew@dunslane.net 944 : 8095952 : att->atthasmissing = false;
3316 peter_e@gmx.net 945 : 8095952 : att->attidentity = '\0';
2593 peter@eisentraut.org 946 : 8095952 : att->attgenerated = '\0';
8677 tgl@sss.pgh.pa.us 947 : 8095952 : att->attisdropped = false;
8626 948 : 8095952 : att->attislocal = true;
949 : 8095952 : att->attinhcount = 0;
950 : : /* variable-length fields are not present in tupledescs */
951 : :
5924 rhaas@postgresql.org 952 : 8095952 : tuple = SearchSysCache1(TYPEOID, ObjectIdGetDatum(oidtypeid));
10467 bruce@momjian.us 953 [ - + ]: 8095952 : if (!HeapTupleIsValid(tuple))
8324 tgl@sss.pgh.pa.us 954 [ # # ]:UBC 0 : elog(ERROR, "cache lookup failed for type %u", oidtypeid);
10108 bruce@momjian.us 955 :CBC 8095952 : typeForm = (Form_pg_type) GETSTRUCT(tuple);
956 : :
8069 tgl@sss.pgh.pa.us 957 : 8095952 : att->atttypid = oidtypeid;
958 : 8095952 : att->attlen = typeForm->typlen;
959 : 8095952 : att->attbyval = typeForm->typbyval;
960 : 8095952 : att->attalign = typeForm->typalign;
961 : 8095952 : att->attstorage = typeForm->typstorage;
1804 962 : 8095952 : att->attcompression = InvalidCompressionMethod;
1808 963 : 8095952 : att->attcollation = typeForm->typcollation;
964 : :
501 drowley@postgresql.o 965 : 8095952 : populate_compact_attribute(desc, attributeNumber - 1);
966 : :
9301 tgl@sss.pgh.pa.us 967 : 8095952 : ReleaseSysCache(tuple);
10892 scrappy@hub.org 968 : 8095952 : }
969 : :
970 : : /*
971 : : * TupleDescInitBuiltinEntry
972 : : * Initialize a tuple descriptor without catalog access. Only
973 : : * a limited range of builtin types are supported.
974 : : */
975 : : void
3388 rhaas@postgresql.org 976 : 8218 : TupleDescInitBuiltinEntry(TupleDesc desc,
977 : : AttrNumber attributeNumber,
978 : : const char *attributeName,
979 : : Oid oidtypeid,
980 : : int32 typmod,
981 : : int attdim)
982 : : {
983 : : Form_pg_attribute att;
984 : :
985 : : /* sanity checks */
223 peter@eisentraut.org 986 [ - + ]:GNC 8218 : Assert(desc);
1285 peter@eisentraut.org 987 [ - + ]:CBC 8218 : Assert(attributeNumber >= 1);
988 [ - + ]: 8218 : Assert(attributeNumber <= desc->natts);
1134 989 [ - + ]: 8218 : Assert(attdim >= 0);
990 [ - + ]: 8218 : Assert(attdim <= PG_INT16_MAX);
991 : :
992 : : /* initialize the attribute fields */
3180 andres@anarazel.de 993 : 8218 : att = TupleDescAttr(desc, attributeNumber - 1);
3388 rhaas@postgresql.org 994 : 8218 : att->attrelid = 0; /* dummy value */
995 : :
996 : : /* unlike TupleDescInitEntry, we require an attribute name */
997 [ - + ]: 8218 : Assert(attributeName != NULL);
998 : 8218 : namestrcpy(&(att->attname), attributeName);
999 : :
1000 : 8218 : att->atttypmod = typmod;
1001 : :
1002 : 8218 : att->attnum = attributeNumber;
1003 : 8218 : att->attndims = attdim;
1004 : :
1005 : 8218 : att->attnotnull = false;
1006 : 8218 : att->atthasdef = false;
2960 andrew@dunslane.net 1007 : 8218 : att->atthasmissing = false;
3316 peter_e@gmx.net 1008 : 8218 : att->attidentity = '\0';
2593 peter@eisentraut.org 1009 : 8218 : att->attgenerated = '\0';
3388 rhaas@postgresql.org 1010 : 8218 : att->attisdropped = false;
1011 : 8218 : att->attislocal = true;
1012 : 8218 : att->attinhcount = 0;
1013 : : /* variable-length fields are not present in tupledescs */
1014 : :
1015 : 8218 : att->atttypid = oidtypeid;
1016 : :
1017 : : /*
1018 : : * Our goal here is to support just enough types to let basic builtin
1019 : : * commands work without catalog access - e.g. so that we can do certain
1020 : : * things even in processes that are not connected to a database.
1021 : : */
1022 [ + - - + : 8218 : switch (oidtypeid)
+ - ]
1023 : : {
1024 : 6680 : case TEXTOID:
1025 : : case TEXTARRAYOID:
1026 : 6680 : att->attlen = -1;
1027 : 6680 : att->attbyval = false;
2253 tgl@sss.pgh.pa.us 1028 : 6680 : att->attalign = TYPALIGN_INT;
1029 : 6680 : att->attstorage = TYPSTORAGE_EXTENDED;
1804 1030 : 6680 : att->attcompression = InvalidCompressionMethod;
3388 rhaas@postgresql.org 1031 : 6680 : att->attcollation = DEFAULT_COLLATION_OID;
1032 : 6680 : break;
1033 : :
3388 rhaas@postgresql.org 1034 :UBC 0 : case BOOLOID:
1035 : 0 : att->attlen = 1;
1036 : 0 : att->attbyval = true;
2253 tgl@sss.pgh.pa.us 1037 : 0 : att->attalign = TYPALIGN_CHAR;
1038 : 0 : att->attstorage = TYPSTORAGE_PLAIN;
1808 1039 : 0 : att->attcompression = InvalidCompressionMethod;
3388 rhaas@postgresql.org 1040 : 0 : att->attcollation = InvalidOid;
1041 : 0 : break;
1042 : :
1043 : 0 : case INT4OID:
1044 : 0 : att->attlen = 4;
1045 : 0 : att->attbyval = true;
2253 tgl@sss.pgh.pa.us 1046 : 0 : att->attalign = TYPALIGN_INT;
1047 : 0 : att->attstorage = TYPSTORAGE_PLAIN;
1808 1048 : 0 : att->attcompression = InvalidCompressionMethod;
3388 rhaas@postgresql.org 1049 : 0 : att->attcollation = InvalidOid;
3380 1050 : 0 : break;
1051 : :
3380 rhaas@postgresql.org 1052 :CBC 1355 : case INT8OID:
1053 : 1355 : att->attlen = 8;
265 tgl@sss.pgh.pa.us 1054 :GNC 1355 : att->attbyval = true;
2253 tgl@sss.pgh.pa.us 1055 :CBC 1355 : att->attalign = TYPALIGN_DOUBLE;
1056 : 1355 : att->attstorage = TYPSTORAGE_PLAIN;
1808 1057 : 1355 : att->attcompression = InvalidCompressionMethod;
3380 rhaas@postgresql.org 1058 : 1355 : att->attcollation = InvalidOid;
3388 1059 : 1355 : break;
1060 : :
1401 peter@eisentraut.org 1061 : 183 : case OIDOID:
1062 : 183 : att->attlen = 4;
1063 : 183 : att->attbyval = true;
1064 : 183 : att->attalign = TYPALIGN_INT;
1065 : 183 : att->attstorage = TYPSTORAGE_PLAIN;
1066 : 183 : att->attcompression = InvalidCompressionMethod;
1067 : 183 : att->attcollation = InvalidOid;
1068 : 183 : break;
1069 : :
2703 tgl@sss.pgh.pa.us 1070 :UBC 0 : default:
1071 [ # # ]: 0 : elog(ERROR, "unsupported type %u", oidtypeid);
1072 : : }
1073 : :
501 drowley@postgresql.o 1074 :CBC 8218 : populate_compact_attribute(desc, attributeNumber - 1);
3388 rhaas@postgresql.org 1075 : 8218 : }
1076 : :
1077 : : /*
1078 : : * TupleDescInitEntryCollation
1079 : : *
1080 : : * Assign a nondefault collation to a previously initialized tuple descriptor
1081 : : * entry.
1082 : : */
1083 : : void
5565 peter_e@gmx.net 1084 : 4264815 : TupleDescInitEntryCollation(TupleDesc desc,
1085 : : AttrNumber attributeNumber,
1086 : : Oid collationid)
1087 : : {
1088 : : /*
1089 : : * sanity checks
1090 : : */
223 peter@eisentraut.org 1091 [ - + ]:GNC 4264815 : Assert(desc);
1285 peter@eisentraut.org 1092 [ - + ]:CBC 4264815 : Assert(attributeNumber >= 1);
1093 [ - + ]: 4264815 : Assert(attributeNumber <= desc->natts);
1094 : :
3180 andres@anarazel.de 1095 : 4264815 : TupleDescAttr(desc, attributeNumber - 1)->attcollation = collationid;
5565 peter_e@gmx.net 1096 : 4264815 : }
1097 : :
1098 : : /*
1099 : : * BuildDescFromLists
1100 : : *
1101 : : * Build a TupleDesc given lists of column names (as String nodes),
1102 : : * column type OIDs, typmods, and collation OIDs.
1103 : : *
1104 : : * No constraints are generated.
1105 : : *
1106 : : * This is for use with functions returning RECORD.
1107 : : */
1108 : : TupleDesc
952 peter@eisentraut.org 1109 : 904 : BuildDescFromLists(const List *names, const List *types, const List *typmods, const List *collations)
1110 : : {
1111 : : int natts;
1112 : : AttrNumber attnum;
1113 : : ListCell *l1;
1114 : : ListCell *l2;
1115 : : ListCell *l3;
1116 : : ListCell *l4;
1117 : : TupleDesc desc;
1118 : :
7355 tgl@sss.pgh.pa.us 1119 : 904 : natts = list_length(names);
1120 [ - + ]: 904 : Assert(natts == list_length(types));
1121 [ - + ]: 904 : Assert(natts == list_length(typmods));
5565 peter_e@gmx.net 1122 [ - + ]: 904 : Assert(natts == list_length(collations));
1123 : :
1124 : : /*
1125 : : * allocate a new tuple descriptor
1126 : : */
2723 andres@anarazel.de 1127 : 904 : desc = CreateTemplateTupleDesc(natts);
1128 : :
7355 tgl@sss.pgh.pa.us 1129 : 904 : attnum = 0;
2623 1130 [ + - + + : 3160 : forfour(l1, names, l2, types, l3, typmods, l4, collations)
+ - + + +
- + + + -
+ + + + +
- + - + -
+ + ]
1131 : : {
7355 1132 : 2256 : char *attname = strVal(lfirst(l1));
2623 1133 : 2256 : Oid atttypid = lfirst_oid(l2);
1134 : 2256 : int32 atttypmod = lfirst_int(l3);
1135 : 2256 : Oid attcollation = lfirst_oid(l4);
1136 : :
7355 1137 : 2256 : attnum++;
1138 : :
1139 : 2256 : TupleDescInitEntry(desc, attnum, attname, atttypid, atttypmod, 0);
5565 peter_e@gmx.net 1140 : 2256 : TupleDescInitEntryCollation(desc, attnum, attcollation);
1141 : : }
1142 : :
50 drowley@postgresql.o 1143 :GNC 904 : TupleDescFinalize(desc);
1144 : :
7355 tgl@sss.pgh.pa.us 1145 :CBC 904 : return desc;
1146 : : }
1147 : :
1148 : : /*
1149 : : * Get default expression (or NULL if none) for the given attribute number.
1150 : : */
1151 : : Node *
951 peter@eisentraut.org 1152 : 78126 : TupleDescGetDefault(TupleDesc tupdesc, AttrNumber attnum)
1153 : : {
1154 : 78126 : Node *result = NULL;
1155 : :
1156 [ + - ]: 78126 : if (tupdesc->constr)
1157 : : {
1158 : 78126 : AttrDefault *attrdef = tupdesc->constr->defval;
1159 : :
1160 [ + - ]: 118409 : for (int i = 0; i < tupdesc->constr->num_defval; i++)
1161 : : {
1162 [ + + ]: 118409 : if (attrdef[i].adnum == attnum)
1163 : : {
1164 : 78126 : result = stringToNode(attrdef[i].adbin);
1165 : 78126 : break;
1166 : : }
1167 : : }
1168 : : }
1169 : :
1170 : 78126 : return result;
1171 : : }
1172 : :
1173 : : /* ResourceOwner callbacks */
1174 : :
1175 : : static void
909 heikki.linnakangas@i 1176 : 11634 : ResOwnerReleaseTupleDesc(Datum res)
1177 : : {
1178 : 11634 : TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1179 : :
1180 : : /* Like DecrTupleDescRefCount, but don't call ResourceOwnerForget() */
1181 [ - + ]: 11634 : Assert(tupdesc->tdrefcount > 0);
1182 [ + + ]: 11634 : if (--tupdesc->tdrefcount == 0)
1183 : 406 : FreeTupleDesc(tupdesc);
1184 : 11634 : }
1185 : :
1186 : : static char *
909 heikki.linnakangas@i 1187 :UBC 0 : ResOwnerPrintTupleDesc(Datum res)
1188 : : {
1189 : 0 : TupleDesc tupdesc = (TupleDesc) DatumGetPointer(res);
1190 : :
1191 : 0 : return psprintf("TupleDesc %p (%u,%d)",
1192 : : tupdesc, tupdesc->tdtypeid, tupdesc->tdtypmod);
1193 : : }
|