Branch data Line data Source code
1 : : /*-------------------------------------------------------------------------
2 : : *
3 : : * expandedrecord.c
4 : : * Functions for manipulating composite expanded objects.
5 : : *
6 : : * This module supports "expanded objects" (cf. expandeddatum.h) that can
7 : : * store values of named composite types, domains over named composite types,
8 : : * and record types (registered or anonymous).
9 : : *
10 : : * Portions Copyright (c) 1996-2026, PostgreSQL Global Development Group
11 : : * Portions Copyright (c) 1994, Regents of the University of California
12 : : *
13 : : *
14 : : * IDENTIFICATION
15 : : * src/backend/utils/adt/expandedrecord.c
16 : : *
17 : : *-------------------------------------------------------------------------
18 : : */
19 : : #include "postgres.h"
20 : :
21 : : #include "access/detoast.h"
22 : : #include "access/heaptoast.h"
23 : : #include "access/htup_details.h"
24 : : #include "catalog/heap.h"
25 : : #include "catalog/pg_type.h"
26 : : #include "utils/builtins.h"
27 : : #include "utils/datum.h"
28 : : #include "utils/expandedrecord.h"
29 : : #include "utils/memutils.h"
30 : : #include "utils/typcache.h"
31 : :
32 : :
33 : : /* "Methods" required for an expanded object */
34 : : static Size ER_get_flat_size(ExpandedObjectHeader *eohptr);
35 : : static void ER_flatten_into(ExpandedObjectHeader *eohptr,
36 : : void *result, Size allocated_size);
37 : :
38 : : static const ExpandedObjectMethods ER_methods =
39 : : {
40 : : ER_get_flat_size,
41 : : ER_flatten_into
42 : : };
43 : :
44 : : /* Other local functions */
45 : : static void ER_mc_callback(void *arg);
46 : : static MemoryContext get_short_term_cxt(ExpandedRecordHeader *erh);
47 : : static void build_dummy_expanded_header(ExpandedRecordHeader *main_erh);
48 : : static pg_noinline void check_domain_for_new_field(ExpandedRecordHeader *erh,
49 : : int fnumber,
50 : : Datum newValue, bool isnull);
51 : : static pg_noinline void check_domain_for_new_tuple(ExpandedRecordHeader *erh,
52 : : HeapTuple tuple);
53 : :
54 : :
55 : : /*
56 : : * Build an expanded record of the specified composite type
57 : : *
58 : : * type_id can be RECORDOID, but only if a positive typmod is given.
59 : : *
60 : : * The expanded record is initially "empty", having a state logically
61 : : * equivalent to a NULL composite value (not ROW(NULL, NULL, ...)).
62 : : * Note that this might not be a valid state for a domain type;
63 : : * if the caller needs to check that, call
64 : : * expanded_record_set_tuple(erh, NULL, false, false).
65 : : *
66 : : * The expanded object will be a child of parentcontext.
67 : : */
68 : : ExpandedRecordHeader *
69 : 1201 : make_expanded_record_from_typeid(Oid type_id, int32 typmod,
70 : : MemoryContext parentcontext)
71 : : {
72 : : ExpandedRecordHeader *erh;
73 : 1201 : int flags = 0;
74 : : TupleDesc tupdesc;
75 : : uint64 tupdesc_id;
76 : : MemoryContext objcxt;
77 : : char *chunk;
78 : :
79 [ + + ]: 1201 : if (type_id != RECORDOID)
80 : : {
81 : : /*
82 : : * Consult the typcache to see if it's a domain over composite, and in
83 : : * any case to get the tupdesc and tupdesc identifier.
84 : : */
85 : : TypeCacheEntry *typentry;
86 : :
87 : 1149 : typentry = lookup_type_cache(type_id,
88 : : TYPECACHE_TUPDESC |
89 : : TYPECACHE_DOMAIN_BASE_INFO);
90 [ + + ]: 1149 : if (typentry->typtype == TYPTYPE_DOMAIN)
91 : : {
92 : 58 : flags |= ER_FLAG_IS_DOMAIN;
93 : 58 : typentry = lookup_type_cache(typentry->domainBaseType,
94 : : TYPECACHE_TUPDESC);
95 : : }
96 [ - + ]: 1149 : if (typentry->tupDesc == NULL)
97 [ # # ]: 0 : ereport(ERROR,
98 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
99 : : errmsg("type %s is not composite",
100 : : format_type_be(type_id))));
101 : 1149 : tupdesc = typentry->tupDesc;
102 : 1149 : tupdesc_id = typentry->tupDesc_identifier;
103 : : }
104 : : else
105 : : {
106 : : /*
107 : : * For RECORD types, get the tupdesc and identifier from typcache.
108 : : */
109 : 52 : tupdesc = lookup_rowtype_tupdesc(type_id, typmod);
110 : 52 : tupdesc_id = assign_record_type_identifier(type_id, typmod);
111 : : }
112 : :
113 : : /*
114 : : * Allocate private context for expanded object. We use a regular-size
115 : : * context, not a small one, to improve the odds that we can fit a tupdesc
116 : : * into it without needing an extra malloc block. (This code path doesn't
117 : : * ever need to copy a tupdesc into the expanded record, but let's be
118 : : * consistent with the other ways of making an expanded record.)
119 : : */
120 : 1201 : objcxt = AllocSetContextCreate(parentcontext,
121 : : "expanded record",
122 : : ALLOCSET_DEFAULT_SIZES);
123 : :
124 : : /*
125 : : * Since we already know the number of fields in the tupdesc, we can
126 : : * allocate the dvalues/dnulls arrays along with the record header. This
127 : : * is useless if we never need those arrays, but it costs almost nothing,
128 : : * and it will save a palloc cycle if we do need them.
129 : : */
130 : : erh = (ExpandedRecordHeader *)
131 : 1201 : MemoryContextAlloc(objcxt, MAXALIGN(sizeof(ExpandedRecordHeader))
132 : 1201 : + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
133 : :
134 : : /* Ensure all header fields are initialized to 0/null */
135 : 1201 : memset(erh, 0, sizeof(ExpandedRecordHeader));
136 : :
137 : 1201 : EOH_init_header(&erh->hdr, &ER_methods, objcxt);
138 : 1201 : erh->er_magic = ER_MAGIC;
139 : :
140 : : /* Set up dvalues/dnulls, with no valid contents as yet */
141 : 1201 : chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
142 : 1201 : erh->dvalues = (Datum *) chunk;
143 : 1201 : erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
144 : 1201 : erh->nfields = tupdesc->natts;
145 : :
146 : : /* Fill in composite-type identification info */
147 : 1201 : erh->er_decltypeid = type_id;
148 : 1201 : erh->er_typeid = tupdesc->tdtypeid;
149 : 1201 : erh->er_typmod = tupdesc->tdtypmod;
150 : 1201 : erh->er_tupdesc_id = tupdesc_id;
151 : :
152 : 1201 : erh->flags = flags;
153 : :
154 : : /*
155 : : * If what we got from the typcache is a refcounted tupdesc, we need to
156 : : * acquire our own refcount on it. We manage the refcount with a memory
157 : : * context callback rather than assuming that the CurrentResourceOwner is
158 : : * longer-lived than this expanded object.
159 : : */
160 [ + - ]: 1201 : if (tupdesc->tdrefcount >= 0)
161 : : {
162 : : /* Register callback to release the refcount */
163 : 1201 : erh->er_mcb.func = ER_mc_callback;
164 : 1201 : erh->er_mcb.arg = erh;
165 : 1201 : MemoryContextRegisterResetCallback(erh->hdr.eoh_context,
166 : : &erh->er_mcb);
167 : :
168 : : /* And save the pointer */
169 : 1201 : erh->er_tupdesc = tupdesc;
170 : 1201 : tupdesc->tdrefcount++;
171 : :
172 : : /* If we called lookup_rowtype_tupdesc, release the pin it took */
173 [ + + ]: 1201 : if (type_id == RECORDOID)
174 [ + - ]: 52 : ReleaseTupleDesc(tupdesc);
175 : : }
176 : : else
177 : : {
178 : : /*
179 : : * If it's not refcounted, just assume it will outlive the expanded
180 : : * object. (This can happen for shared record types, for instance.)
181 : : */
182 : 0 : erh->er_tupdesc = tupdesc;
183 : : }
184 : :
185 : : /*
186 : : * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
187 : : * record remains logically empty.
188 : : */
189 : :
190 : 1201 : return erh;
191 : : }
192 : :
193 : : /*
194 : : * Build an expanded record of the rowtype defined by the tupdesc
195 : : *
196 : : * The tupdesc is copied if necessary (i.e., if we can't just bump its
197 : : * reference count instead).
198 : : *
199 : : * The expanded record is initially "empty", having a state logically
200 : : * equivalent to a NULL composite value (not ROW(NULL, NULL, ...)).
201 : : *
202 : : * The expanded object will be a child of parentcontext.
203 : : */
204 : : ExpandedRecordHeader *
205 : 13969 : make_expanded_record_from_tupdesc(TupleDesc tupdesc,
206 : : MemoryContext parentcontext)
207 : : {
208 : : ExpandedRecordHeader *erh;
209 : : uint64 tupdesc_id;
210 : : MemoryContext objcxt;
211 : : MemoryContext oldcxt;
212 : : char *chunk;
213 : :
214 [ + + ]: 13969 : if (tupdesc->tdtypeid != RECORDOID)
215 : : {
216 : : /*
217 : : * If it's a named composite type (not RECORD), we prefer to reference
218 : : * the typcache's copy of the tupdesc, which is guaranteed to be
219 : : * refcounted (the given tupdesc might not be). In any case, we need
220 : : * to consult the typcache to get the correct tupdesc identifier.
221 : : *
222 : : * Note that tdtypeid couldn't be a domain type, so we need not
223 : : * consider that case here.
224 : : */
225 : : TypeCacheEntry *typentry;
226 : :
227 : 10295 : typentry = lookup_type_cache(tupdesc->tdtypeid, TYPECACHE_TUPDESC);
228 [ - + ]: 10295 : if (typentry->tupDesc == NULL)
229 [ # # ]: 0 : ereport(ERROR,
230 : : (errcode(ERRCODE_WRONG_OBJECT_TYPE),
231 : : errmsg("type %s is not composite",
232 : : format_type_be(tupdesc->tdtypeid))));
233 : 10295 : tupdesc = typentry->tupDesc;
234 : 10295 : tupdesc_id = typentry->tupDesc_identifier;
235 : : }
236 : : else
237 : : {
238 : : /*
239 : : * For RECORD types, get the appropriate unique identifier (possibly
240 : : * freshly assigned).
241 : : */
242 : 3674 : tupdesc_id = assign_record_type_identifier(tupdesc->tdtypeid,
243 : : tupdesc->tdtypmod);
244 : : }
245 : :
246 : : /*
247 : : * Allocate private context for expanded object. We use a regular-size
248 : : * context, not a small one, to improve the odds that we can fit a tupdesc
249 : : * into it without needing an extra malloc block.
250 : : */
251 : 13969 : objcxt = AllocSetContextCreate(parentcontext,
252 : : "expanded record",
253 : : ALLOCSET_DEFAULT_SIZES);
254 : :
255 : : /*
256 : : * Since we already know the number of fields in the tupdesc, we can
257 : : * allocate the dvalues/dnulls arrays along with the record header. This
258 : : * is useless if we never need those arrays, but it costs almost nothing,
259 : : * and it will save a palloc cycle if we do need them.
260 : : */
261 : : erh = (ExpandedRecordHeader *)
262 : 13969 : MemoryContextAlloc(objcxt, MAXALIGN(sizeof(ExpandedRecordHeader))
263 : 13969 : + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
264 : :
265 : : /* Ensure all header fields are initialized to 0/null */
266 : 13969 : memset(erh, 0, sizeof(ExpandedRecordHeader));
267 : :
268 : 13969 : EOH_init_header(&erh->hdr, &ER_methods, objcxt);
269 : 13969 : erh->er_magic = ER_MAGIC;
270 : :
271 : : /* Set up dvalues/dnulls, with no valid contents as yet */
272 : 13969 : chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
273 : 13969 : erh->dvalues = (Datum *) chunk;
274 : 13969 : erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
275 : 13969 : erh->nfields = tupdesc->natts;
276 : :
277 : : /* Fill in composite-type identification info */
278 : 13969 : erh->er_decltypeid = erh->er_typeid = tupdesc->tdtypeid;
279 : 13969 : erh->er_typmod = tupdesc->tdtypmod;
280 : 13969 : erh->er_tupdesc_id = tupdesc_id;
281 : :
282 : : /*
283 : : * Copy tupdesc if needed, but we prefer to bump its refcount if possible.
284 : : * We manage the refcount with a memory context callback rather than
285 : : * assuming that the CurrentResourceOwner is longer-lived than this
286 : : * expanded object.
287 : : */
288 [ + + ]: 13969 : if (tupdesc->tdrefcount >= 0)
289 : : {
290 : : /* Register callback to release the refcount */
291 : 10295 : erh->er_mcb.func = ER_mc_callback;
292 : 10295 : erh->er_mcb.arg = erh;
293 : 10295 : MemoryContextRegisterResetCallback(erh->hdr.eoh_context,
294 : : &erh->er_mcb);
295 : :
296 : : /* And save the pointer */
297 : 10295 : erh->er_tupdesc = tupdesc;
298 : 10295 : tupdesc->tdrefcount++;
299 : : }
300 : : else
301 : : {
302 : : /* Just copy it */
303 : 3674 : oldcxt = MemoryContextSwitchTo(objcxt);
304 : 3674 : erh->er_tupdesc = CreateTupleDescCopy(tupdesc);
305 : 3674 : erh->flags |= ER_FLAG_TUPDESC_ALLOCED;
306 : 3674 : MemoryContextSwitchTo(oldcxt);
307 : : }
308 : :
309 : : /*
310 : : * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
311 : : * record remains logically empty.
312 : : */
313 : :
314 : 13969 : return erh;
315 : : }
316 : :
317 : : /*
318 : : * Build an expanded record of the same rowtype as the given expanded record
319 : : *
320 : : * This is faster than either of the above routines because we can bypass
321 : : * typcache lookup(s).
322 : : *
323 : : * The expanded record is initially "empty" --- we do not copy whatever
324 : : * tuple might be in the source expanded record.
325 : : *
326 : : * The expanded object will be a child of parentcontext.
327 : : */
328 : : ExpandedRecordHeader *
329 : 10456 : make_expanded_record_from_exprecord(ExpandedRecordHeader *olderh,
330 : : MemoryContext parentcontext)
331 : : {
332 : : ExpandedRecordHeader *erh;
333 : 10456 : TupleDesc tupdesc = expanded_record_get_tupdesc(olderh);
334 : : MemoryContext objcxt;
335 : : MemoryContext oldcxt;
336 : : char *chunk;
337 : :
338 : : /*
339 : : * Allocate private context for expanded object. We use a regular-size
340 : : * context, not a small one, to improve the odds that we can fit a tupdesc
341 : : * into it without needing an extra malloc block.
342 : : */
343 : 10456 : objcxt = AllocSetContextCreate(parentcontext,
344 : : "expanded record",
345 : : ALLOCSET_DEFAULT_SIZES);
346 : :
347 : : /*
348 : : * Since we already know the number of fields in the tupdesc, we can
349 : : * allocate the dvalues/dnulls arrays along with the record header. This
350 : : * is useless if we never need those arrays, but it costs almost nothing,
351 : : * and it will save a palloc cycle if we do need them.
352 : : */
353 : : erh = (ExpandedRecordHeader *)
354 : 10456 : MemoryContextAlloc(objcxt, MAXALIGN(sizeof(ExpandedRecordHeader))
355 : 10456 : + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
356 : :
357 : : /* Ensure all header fields are initialized to 0/null */
358 : 10456 : memset(erh, 0, sizeof(ExpandedRecordHeader));
359 : :
360 : 10456 : EOH_init_header(&erh->hdr, &ER_methods, objcxt);
361 : 10456 : erh->er_magic = ER_MAGIC;
362 : :
363 : : /* Set up dvalues/dnulls, with no valid contents as yet */
364 : 10456 : chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
365 : 10456 : erh->dvalues = (Datum *) chunk;
366 : 10456 : erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
367 : 10456 : erh->nfields = tupdesc->natts;
368 : :
369 : : /* Fill in composite-type identification info */
370 : 10456 : erh->er_decltypeid = olderh->er_decltypeid;
371 : 10456 : erh->er_typeid = olderh->er_typeid;
372 : 10456 : erh->er_typmod = olderh->er_typmod;
373 : 10456 : erh->er_tupdesc_id = olderh->er_tupdesc_id;
374 : :
375 : : /* The only flag bit that transfers over is IS_DOMAIN */
376 : 10456 : erh->flags = olderh->flags & ER_FLAG_IS_DOMAIN;
377 : :
378 : : /*
379 : : * Copy tupdesc if needed, but we prefer to bump its refcount if possible.
380 : : * We manage the refcount with a memory context callback rather than
381 : : * assuming that the CurrentResourceOwner is longer-lived than this
382 : : * expanded object.
383 : : */
384 [ + - ]: 10456 : if (tupdesc->tdrefcount >= 0)
385 : : {
386 : : /* Register callback to release the refcount */
387 : 10456 : erh->er_mcb.func = ER_mc_callback;
388 : 10456 : erh->er_mcb.arg = erh;
389 : 10456 : MemoryContextRegisterResetCallback(erh->hdr.eoh_context,
390 : : &erh->er_mcb);
391 : :
392 : : /* And save the pointer */
393 : 10456 : erh->er_tupdesc = tupdesc;
394 : 10456 : tupdesc->tdrefcount++;
395 : : }
396 [ # # ]: 0 : else if (olderh->flags & ER_FLAG_TUPDESC_ALLOCED)
397 : : {
398 : : /* We need to make our own copy of the tupdesc */
399 : 0 : oldcxt = MemoryContextSwitchTo(objcxt);
400 : 0 : erh->er_tupdesc = CreateTupleDescCopy(tupdesc);
401 : 0 : erh->flags |= ER_FLAG_TUPDESC_ALLOCED;
402 : 0 : MemoryContextSwitchTo(oldcxt);
403 : : }
404 : : else
405 : : {
406 : : /*
407 : : * Assume the tupdesc will outlive this expanded object, just like
408 : : * we're assuming it will outlive the source object.
409 : : */
410 : 0 : erh->er_tupdesc = tupdesc;
411 : : }
412 : :
413 : : /*
414 : : * We don't set ER_FLAG_DVALUES_VALID or ER_FLAG_FVALUE_VALID, so the
415 : : * record remains logically empty.
416 : : */
417 : :
418 : 10456 : return erh;
419 : : }
420 : :
421 : : /*
422 : : * Insert given tuple as the value of the expanded record
423 : : *
424 : : * It is caller's responsibility that the tuple matches the record's
425 : : * previously-assigned rowtype. (However domain constraints, if any,
426 : : * will be checked here.) A versioned datum tuple is checked against the
427 : : * record's descriptor here, other tuples can't be. The match matters beyond
428 : : * deforming: the tuple becomes the record's flat value, which
429 : : * ER_flatten_into() copies and stamps with the version of the record's
430 : : * descriptor, so a mismatched tuple would pass later version checks.
431 : : *
432 : : * The tuple is physically copied into the expanded record's local storage
433 : : * if "copy" is true, otherwise it's caller's responsibility that the tuple
434 : : * will live as long as the expanded record does.
435 : : *
436 : : * Out-of-line field values in the tuple are automatically inlined if
437 : : * "expand_external" is true, otherwise not. (The combination copy = false,
438 : : * expand_external = true is not sensible and not supported.)
439 : : *
440 : : * Alternatively, tuple can be NULL, in which case we just set the expanded
441 : : * record to be empty.
442 : : */
443 : : void
444 : 21297 : expanded_record_set_tuple(ExpandedRecordHeader *erh,
445 : : HeapTuple tuple,
446 : : bool copy,
447 : : bool expand_external)
448 : : {
449 : : int oldflags;
450 : : HeapTuple oldtuple;
451 : : char *oldfstartptr;
452 : : char *oldfendptr;
453 : : int newflags;
454 : : HeapTuple newtuple;
455 : : MemoryContext oldcxt;
456 : :
457 : : /* Shouldn't ever be trying to assign new data to a dummy header */
458 : : Assert(!(erh->flags & ER_FLAG_IS_DUMMY));
459 : :
460 : : /*
461 : : * XXX: This is needed right now because some sources of caller tuples
462 : : * don't match the tupledesc. But arguably we should have caught the
463 : : * problem earlier.
464 : : */
465 [ + + + + ]: 21297 : if (tuple && HeapTupleHeaderIsVersionedDatum(tuple->t_data))
466 : 4548 : heap_datum_check(tuple->t_data, erh->er_tupdesc);
467 : :
468 : : /*
469 : : * Before performing the assignment, see if result will satisfy domain.
470 : : */
471 [ + + ]: 21273 : if (erh->flags & ER_FLAG_IS_DOMAIN)
472 : 59 : check_domain_for_new_tuple(erh, tuple);
473 : :
474 : : /*
475 : : * If we need to get rid of out-of-line field values, do so, using the
476 : : * short-term context to avoid leaking whatever cruft the toast fetch
477 : : * might generate.
478 : : */
479 [ + + + - ]: 21265 : if (expand_external && tuple)
480 : : {
481 : : /* Assert caller didn't ask for unsupported case */
482 : : Assert(copy);
483 [ + + ]: 3407 : if (HeapTupleHasExternal(tuple))
484 : : {
485 : 7 : oldcxt = MemoryContextSwitchTo(get_short_term_cxt(erh));
486 : 7 : tuple = toast_flatten_tuple(tuple, erh->er_tupdesc);
487 : 7 : MemoryContextSwitchTo(oldcxt);
488 : : }
489 : : else
490 : 3400 : expand_external = false; /* need not clean up below */
491 : : }
492 : :
493 : : /*
494 : : * Initialize new flags, keeping only non-data status bits.
495 : : */
496 : 21265 : oldflags = erh->flags;
497 : 21265 : newflags = oldflags & ER_FLAGS_NON_DATA;
498 : :
499 : : /*
500 : : * Copy tuple into local storage if needed. We must be sure this succeeds
501 : : * before we start to modify the expanded record's state.
502 : : */
503 [ + + + - ]: 21265 : if (copy && tuple)
504 : : {
505 : 7865 : oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
506 : 7865 : newtuple = heap_copytuple(tuple);
507 : 7865 : newflags |= ER_FLAG_FVALUE_ALLOCED;
508 : 7865 : MemoryContextSwitchTo(oldcxt);
509 : :
510 : : /* We can now flush anything that detoasting might have leaked. */
511 [ + + ]: 7865 : if (expand_external)
512 : 7 : MemoryContextReset(erh->er_short_term_cxt);
513 : : }
514 : : else
515 : 13400 : newtuple = tuple;
516 : :
517 : : /* Make copies of fields we're about to overwrite */
518 : 21265 : oldtuple = erh->fvalue;
519 : 21265 : oldfstartptr = erh->fstartptr;
520 : 21265 : oldfendptr = erh->fendptr;
521 : :
522 : : /*
523 : : * It's now safe to update the expanded record's state.
524 : : */
525 [ + + ]: 21265 : if (newtuple)
526 : : {
527 : : /* Save flat representation */
528 : 21239 : erh->fvalue = newtuple;
529 : 21239 : erh->fstartptr = (char *) newtuple->t_data;
530 : 21239 : erh->fendptr = ((char *) newtuple->t_data) + newtuple->t_len;
531 : 21239 : newflags |= ER_FLAG_FVALUE_VALID;
532 : :
533 : : /* Remember if we have any out-of-line field values */
534 [ + + ]: 21239 : if (HeapTupleHasExternal(newtuple))
535 : 134 : newflags |= ER_FLAG_HAVE_EXTERNAL;
536 : : }
537 : : else
538 : : {
539 : 26 : erh->fvalue = NULL;
540 : 26 : erh->fstartptr = erh->fendptr = NULL;
541 : : }
542 : :
543 : 21265 : erh->flags = newflags;
544 : :
545 : : /* Reset flat-size info; we don't bother to make it valid now */
546 : 21265 : erh->flat_size = 0;
547 : :
548 : : /*
549 : : * Now, release any storage belonging to old field values. It's safe to
550 : : * do this because ER_FLAG_DVALUES_VALID is no longer set in erh->flags;
551 : : * even if we fail partway through, the record is valid, and at worst
552 : : * we've failed to reclaim some space.
553 : : */
554 [ + + ]: 21265 : if (oldflags & ER_FLAG_DVALUES_ALLOCED)
555 : : {
556 : 32 : TupleDesc tupdesc = erh->er_tupdesc;
557 : : int i;
558 : :
559 [ + + ]: 160 : for (i = 0; i < erh->nfields; i++)
560 : : {
561 [ + + ]: 128 : if (!erh->dnulls[i] &&
562 [ + + ]: 112 : !(TupleDescCompactAttr(tupdesc, i)->attbyval))
563 : : {
564 : 80 : char *oldValue = (char *) DatumGetPointer(erh->dvalues[i]);
565 : :
566 [ + + + + ]: 80 : if (oldValue < oldfstartptr || oldValue >= oldfendptr)
567 : 40 : pfree(oldValue);
568 : : }
569 : : }
570 : : }
571 : :
572 : : /* Likewise free the old tuple, if it was locally allocated */
573 [ + + ]: 21265 : if (oldflags & ER_FLAG_FVALUE_ALLOCED)
574 : 4003 : heap_freetuple(oldtuple);
575 : :
576 : : /* We won't make a new deconstructed representation until/unless needed */
577 : 21265 : }
578 : :
579 : : /*
580 : : * make_expanded_record_from_datum: build expanded record from composite Datum
581 : : *
582 : : * This combines the functions of make_expanded_record_from_typeid and
583 : : * expanded_record_set_tuple.
584 : : *
585 : : * The expanded object will be a child of parentcontext.
586 : : *
587 : : * Note: a composite datum cannot self-identify as being of a domain type,
588 : : * so we need not consider domain cases here.
589 : : */
590 : : Datum
591 : 36 : make_expanded_record_from_datum(Datum recorddatum, MemoryContext parentcontext)
592 : : {
593 : : ExpandedRecordHeader *erh;
594 : : HeapTupleHeader tuphdr;
595 : : HeapTupleData tmptup;
596 : :
597 : 36 : tuphdr = DatumGetHeapTupleHeader(recorddatum);
598 : :
599 : 36 : erh = make_expanded_record_from_typeid(HeapTupleHeaderGetTypeId(tuphdr),
600 : : HeapTupleHeaderGetTypMod(tuphdr),
601 : : parentcontext);
602 : :
603 : 36 : tmptup.t_len = HeapTupleHeaderGetDatumLength(tuphdr);
604 : 36 : ItemPointerSetInvalid(&(tmptup.t_self));
605 : 36 : tmptup.t_tableOid = InvalidOid;
606 : 36 : tmptup.t_data = tuphdr;
607 : :
608 : : /* Composite datums must not contain out-of-line field values */
609 : : Assert(!HeapTupleHeaderHasExternal(tuphdr));
610 : :
611 : 36 : expanded_record_set_tuple(erh, &tmptup, true, false);
612 : :
613 : : /* return a R/W pointer to the expanded record */
614 : 32 : return EOHPGetRWDatum(&erh->hdr);
615 : : }
616 : :
617 : : /*
618 : : * get_flat_size method for expanded records
619 : : *
620 : : * Note: call this in a reasonably short-lived memory context, in case of
621 : : * memory leaks from activities such as detoasting.
622 : : */
623 : : static Size
624 : 2555 : ER_get_flat_size(ExpandedObjectHeader *eohptr)
625 : : {
626 : 2555 : ExpandedRecordHeader *erh = (ExpandedRecordHeader *) eohptr;
627 : : TupleDesc tupdesc;
628 : : Size len;
629 : : Size data_len;
630 : : int hoff;
631 : : bool hasnull;
632 : : int i;
633 : :
634 : : Assert(erh->er_magic == ER_MAGIC);
635 : :
636 : : /*
637 : : * The flat representation has to be a valid composite datum. Make sure
638 : : * that we have a registered, not anonymous, RECORD type.
639 : : */
640 [ + + ]: 2555 : if (erh->er_typeid == RECORDOID &&
641 [ + + ]: 98 : erh->er_typmod < 0)
642 : : {
643 : 39 : tupdesc = expanded_record_get_tupdesc(erh);
644 : 39 : assign_record_type_typmod(tupdesc);
645 : 39 : erh->er_typmod = tupdesc->tdtypmod;
646 : : }
647 : :
648 : : /*
649 : : * If we have a valid flattened value without out-of-line fields, we can
650 : : * just use it as-is.
651 : : */
652 [ + + ]: 2555 : if (erh->flags & ER_FLAG_FVALUE_VALID &&
653 [ + + ]: 2293 : !(erh->flags & ER_FLAG_HAVE_EXTERNAL))
654 : 2261 : return erh->fvalue->t_len;
655 : :
656 : : /* If we have a cached size value, believe that */
657 [ + + ]: 294 : if (erh->flat_size)
658 : 55 : return erh->flat_size;
659 : :
660 : : /* If we haven't yet deconstructed the tuple, do that */
661 [ + + ]: 239 : if (!(erh->flags & ER_FLAG_DVALUES_VALID))
662 : 32 : deconstruct_expanded_record(erh);
663 : :
664 : 239 : tupdesc = erh->er_tupdesc;
665 : :
666 : : /*
667 : : * Composite datums mustn't contain any out-of-line values.
668 : : */
669 [ + + ]: 239 : if (erh->flags & ER_FLAG_HAVE_EXTERNAL)
670 : : {
671 [ + + ]: 160 : for (i = 0; i < erh->nfields; i++)
672 : : {
673 : 128 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, i);
674 : :
675 [ + + ]: 128 : if (!erh->dnulls[i] &&
676 [ + + + - : 192 : !attr->attbyval && attr->attlen == -1 &&
+ + ]
677 : 80 : VARATT_IS_EXTERNAL(DatumGetPointer(erh->dvalues[i])))
678 : : {
679 : : /*
680 : : * expanded_record_set_field_internal can do the actual work
681 : : * of detoasting. It needn't recheck domain constraints.
682 : : */
683 : 40 : expanded_record_set_field_internal(erh, i + 1,
684 : 40 : erh->dvalues[i], false,
685 : : true,
686 : : false);
687 : : }
688 : : }
689 : :
690 : : /*
691 : : * We have now removed all external field values, so we can clear the
692 : : * flag about them. This won't cause ER_flatten_into() to mistakenly
693 : : * take the fast path, since expanded_record_set_field() will have
694 : : * cleared ER_FLAG_FVALUE_VALID.
695 : : */
696 : 32 : erh->flags &= ~ER_FLAG_HAVE_EXTERNAL;
697 : : }
698 : :
699 : : /* Test if we currently have any null values */
700 : 239 : hasnull = false;
701 [ + + ]: 703 : for (i = 0; i < erh->nfields; i++)
702 : : {
703 [ + + ]: 499 : if (erh->dnulls[i])
704 : : {
705 : 35 : hasnull = true;
706 : 35 : break;
707 : : }
708 : : }
709 : :
710 : : /* Determine total space needed */
711 : 239 : len = offsetof(HeapTupleHeaderData, t_bits);
712 : :
713 [ + + ]: 239 : if (hasnull)
714 : 35 : len += BITMAPLEN(tupdesc->natts);
715 : :
716 : 239 : hoff = len = MAXALIGN(len); /* align user data safely */
717 : :
718 : 239 : data_len = heap_compute_data_size(tupdesc, erh->dvalues, erh->dnulls,
719 : : false);
720 : :
721 : 239 : len += data_len;
722 : :
723 : : /* Cache for next time */
724 : 239 : erh->flat_size = len;
725 : 239 : erh->data_len = data_len;
726 : 239 : erh->hoff = hoff;
727 : 239 : erh->hasnull = hasnull;
728 : :
729 : 239 : return len;
730 : : }
731 : :
732 : : /*
733 : : * flatten_into method for expanded records
734 : : */
735 : : static void
736 : 2521 : ER_flatten_into(ExpandedObjectHeader *eohptr,
737 : : void *result, Size allocated_size)
738 : : {
739 : 2521 : ExpandedRecordHeader *erh = (ExpandedRecordHeader *) eohptr;
740 : 2521 : HeapTupleHeader tuphdr = (HeapTupleHeader) result;
741 : 2521 : TupleDesc tupdesc = expanded_record_get_tupdesc(erh);
742 : :
743 : : Assert(erh->er_magic == ER_MAGIC);
744 : :
745 : : /* Easy if we have a valid flattened value without out-of-line fields */
746 [ + + ]: 2521 : if (erh->flags & ER_FLAG_FVALUE_VALID &&
747 [ + - ]: 2245 : !(erh->flags & ER_FLAG_HAVE_EXTERNAL))
748 : : {
749 : : Assert(allocated_size == erh->fvalue->t_len);
750 : 2245 : memcpy(tuphdr, erh->fvalue->t_data, allocated_size);
751 : : /* The original flattened value might not have datum header fields */
752 : 2245 : heap_tuple_set_datum(tuphdr, tupdesc, allocated_size);
753 : 2245 : return;
754 : : }
755 : :
756 : : /* Else allocation should match previous get_flat_size result */
757 : : Assert(allocated_size == erh->flat_size);
758 : :
759 : : /* We must ensure that any pad space is zero-filled */
760 : 276 : memset(tuphdr, 0, allocated_size);
761 : :
762 : : /* Set up header fields of composite Datum */
763 : 276 : HeapTupleHeaderSetNatts(tuphdr, tupdesc->natts);
764 : 276 : tuphdr->t_hoff = erh->hoff;
765 : 276 : heap_tuple_set_datum(tuphdr, tupdesc, allocated_size);
766 : :
767 : : /* And fill the data area from dvalues/dnulls */
768 : 276 : heap_fill_tuple(tupdesc,
769 : 276 : erh->dvalues,
770 : 276 : erh->dnulls,
771 : 276 : (char *) tuphdr + erh->hoff,
772 : : erh->data_len,
773 : : &tuphdr->t_infomask,
774 [ + + ]: 276 : (erh->hasnull ? tuphdr->t_bits : NULL));
775 : : }
776 : :
777 : : /*
778 : : * Get the tupdesc for the expanded record's actual type
779 : : *
780 : : * Kept for ABI compatibility with callers compiled when tuple descriptor
781 : : * lookup could be deferred. New code should use expanded_record_get_tupdesc.
782 : : */
783 : : TupleDesc
784 : 0 : expanded_record_fetch_tupdesc(ExpandedRecordHeader *erh)
785 : : {
786 : 0 : return expanded_record_get_tupdesc(erh);
787 : : }
788 : :
789 : : /*
790 : : * Get a HeapTuple representing the current value of the expanded record
791 : : *
792 : : * If valid, the originally stored tuple is returned, so caller must not
793 : : * scribble on it. Otherwise, we return a HeapTuple created in the current
794 : : * memory context. In either case, no attempt has been made to inline
795 : : * out-of-line toasted values, so the tuple isn't usable as a composite
796 : : * datum.
797 : : *
798 : : * Returns NULL if expanded record is empty.
799 : : */
800 : : HeapTuple
801 : 8370 : expanded_record_get_tuple(ExpandedRecordHeader *erh)
802 : : {
803 : : /* Easy case if we still have original tuple */
804 [ + + ]: 8370 : if (erh->flags & ER_FLAG_FVALUE_VALID)
805 : 7148 : return erh->fvalue;
806 : :
807 : : /* Else just build a tuple from datums */
808 [ + - ]: 1222 : if (erh->flags & ER_FLAG_DVALUES_VALID)
809 : 1222 : return heap_form_tuple(erh->er_tupdesc, erh->dvalues, erh->dnulls);
810 : :
811 : : /* Expanded record is empty */
812 : 0 : return NULL;
813 : : }
814 : :
815 : : /*
816 : : * Memory context reset callback for cleaning up external resources
817 : : */
818 : : static void
819 : 21952 : ER_mc_callback(void *arg)
820 : : {
821 : 21952 : ExpandedRecordHeader *erh = (ExpandedRecordHeader *) arg;
822 : 21952 : TupleDesc tupdesc = erh->er_tupdesc;
823 : :
824 : : /* Release our privately-managed tupdesc refcount, if any */
825 [ + - ]: 21952 : if (tupdesc)
826 : : {
827 : 21952 : erh->er_tupdesc = NULL; /* just for luck */
828 [ + - ]: 21952 : if (tupdesc->tdrefcount > 0)
829 : : {
830 [ + + ]: 21952 : if (--tupdesc->tdrefcount == 0)
831 : 192 : FreeTupleDesc(tupdesc);
832 : : }
833 : : }
834 : 21952 : }
835 : :
836 : : /*
837 : : * DatumGetExpandedRecord: get a writable expanded record from an input argument
838 : : *
839 : : * Caution: if the input is a read/write pointer, this returns the input
840 : : * argument; so callers must be sure that their changes are "safe", that is
841 : : * they cannot leave the record in a corrupt state.
842 : : */
843 : : ExpandedRecordHeader *
844 : 40 : DatumGetExpandedRecord(Datum d)
845 : : {
846 : : /* If it's a writable expanded record already, just return it */
847 [ + + ]: 40 : if (VARATT_IS_EXTERNAL_EXPANDED_RW(DatumGetPointer(d)))
848 : : {
849 : 4 : ExpandedRecordHeader *erh = (ExpandedRecordHeader *) DatumGetEOHP(d);
850 : :
851 : : Assert(erh->er_magic == ER_MAGIC);
852 : 4 : return erh;
853 : : }
854 : :
855 : : /* Else expand the hard way */
856 : 36 : d = make_expanded_record_from_datum(d, CurrentMemoryContext);
857 : 32 : return (ExpandedRecordHeader *) DatumGetEOHP(d);
858 : : }
859 : :
860 : : /*
861 : : * Create the Datum/isnull representation of an expanded record object
862 : : * if we didn't do so already. After calling this, it's OK to read the
863 : : * dvalues/dnulls arrays directly, rather than going through get_field.
864 : : *
865 : : * Note that if the object is currently empty ("null"), this will change
866 : : * it to represent a row of nulls.
867 : : */
868 : : void
869 : 16423 : deconstruct_expanded_record(ExpandedRecordHeader *erh)
870 : : {
871 : 16423 : TupleDesc tupdesc = erh->er_tupdesc;
872 : 16423 : Datum *dvalues = erh->dvalues;
873 : 16423 : bool *dnulls = erh->dnulls;
874 : 16423 : int nfields = erh->nfields;
875 : :
876 [ + + ]: 16423 : if (erh->flags & ER_FLAG_DVALUES_VALID)
877 : 15 : return; /* already valid, nothing to do */
878 : :
879 : : Assert(dvalues != NULL && dnulls != NULL);
880 : : Assert(nfields == tupdesc->natts);
881 : :
882 [ + + ]: 16408 : if (erh->flags & ER_FLAG_FVALUE_VALID)
883 : : {
884 : : /* Deconstruct tuple */
885 : 15209 : heap_deform_tuple(erh->fvalue, tupdesc, dvalues, dnulls);
886 : : }
887 : : else
888 : : {
889 : : /* If record was empty, instantiate it as a row of nulls */
890 : 1199 : memset(dvalues, 0, nfields * sizeof(Datum));
891 : 1199 : memset(dnulls, true, nfields * sizeof(bool));
892 : : }
893 : :
894 : : /* Mark the dvalues as valid */
895 : 16408 : erh->flags |= ER_FLAG_DVALUES_VALID;
896 : : }
897 : :
898 : : /*
899 : : * Look up a record field by name
900 : : *
901 : : * If there is a field named "fieldname", fill in the contents of finfo
902 : : * and return "true". Else return "false" without changing *finfo.
903 : : */
904 : : bool
905 : 4833 : expanded_record_lookup_field(ExpandedRecordHeader *erh, const char *fieldname,
906 : : ExpandedRecordFieldInfo *finfo)
907 : : {
908 : : TupleDesc tupdesc;
909 : : int fno;
910 : : Form_pg_attribute attr;
911 : : const FormData_pg_attribute *sysattr;
912 : :
913 : 4833 : tupdesc = expanded_record_get_tupdesc(erh);
914 : :
915 : : /* First, check user-defined attributes */
916 [ + + ]: 18637 : for (fno = 0; fno < tupdesc->natts; fno++)
917 : : {
918 : 18607 : attr = TupleDescAttr(tupdesc, fno);
919 [ + + ]: 18607 : if (namestrcmp(&attr->attname, fieldname) == 0 &&
920 [ + - ]: 4803 : !attr->attisdropped)
921 : : {
922 : 4803 : finfo->fnumber = attr->attnum;
923 : 4803 : finfo->ftypeid = attr->atttypid;
924 : 4803 : finfo->ftypmod = attr->atttypmod;
925 : 4803 : finfo->fcollation = attr->attcollation;
926 : 4803 : return true;
927 : : }
928 : : }
929 : :
930 : : /* How about system attributes? */
931 : 30 : sysattr = SystemAttributeByName(fieldname);
932 [ + + ]: 30 : if (sysattr != NULL)
933 : : {
934 : 18 : finfo->fnumber = sysattr->attnum;
935 : 18 : finfo->ftypeid = sysattr->atttypid;
936 : 18 : finfo->ftypmod = sysattr->atttypmod;
937 : 18 : finfo->fcollation = sysattr->attcollation;
938 : 18 : return true;
939 : : }
940 : :
941 : 12 : return false;
942 : : }
943 : :
944 : : /*
945 : : * Fetch value of record field
946 : : *
947 : : * expanded_record_get_field is the frontend for this; it handles the
948 : : * easy inline-able cases.
949 : : */
950 : : Datum
951 : 15032 : expanded_record_fetch_field(ExpandedRecordHeader *erh, int fnumber,
952 : : bool *isnull)
953 : : {
954 [ + + ]: 15032 : if (fnumber > 0)
955 : : {
956 : : /* Empty record has null fields */
957 [ + + ]: 15011 : if (ExpandedRecordIsEmpty(erh))
958 : : {
959 : 32 : *isnull = true;
960 : 32 : return (Datum) 0;
961 : : }
962 : : /* Make sure we have deconstructed form */
963 : 14979 : deconstruct_expanded_record(erh);
964 : : /* Out-of-range field number reads as null */
965 [ - + ]: 14979 : if (unlikely(fnumber > erh->nfields))
966 : : {
967 : 0 : *isnull = true;
968 : 0 : return (Datum) 0;
969 : : }
970 : 14979 : *isnull = erh->dnulls[fnumber - 1];
971 : 14979 : return erh->dvalues[fnumber - 1];
972 : : }
973 : : else
974 : : {
975 : : /* System columns read as null if we haven't got flat tuple */
976 [ + + ]: 21 : if (erh->fvalue == NULL)
977 : : {
978 : 1 : *isnull = true;
979 : 1 : return (Datum) 0;
980 : : }
981 : : /* heap_getsysattr doesn't actually use tupdesc, so just pass null */
982 : 20 : return heap_getsysattr(erh->fvalue, fnumber, NULL, isnull);
983 : : }
984 : : }
985 : :
986 : : /*
987 : : * Set value of record field
988 : : *
989 : : * If the expanded record is of domain type, the assignment will be rejected
990 : : * (without changing the record's state) if the domain's constraints would
991 : : * be violated.
992 : : *
993 : : * If expand_external is true and newValue is an out-of-line value, we'll
994 : : * forcibly detoast it so that the record does not depend on external storage.
995 : : *
996 : : * Internal callers can pass check_constraints = false to skip application
997 : : * of domain constraints. External callers should never do that.
998 : : */
999 : : void
1000 : 1579 : expanded_record_set_field_internal(ExpandedRecordHeader *erh, int fnumber,
1001 : : Datum newValue, bool isnull,
1002 : : bool expand_external,
1003 : : bool check_constraints)
1004 : : {
1005 : : TupleDesc tupdesc;
1006 : : CompactAttribute *attr;
1007 : : Datum *dvalues;
1008 : : bool *dnulls;
1009 : : char *oldValue;
1010 : :
1011 : : /*
1012 : : * Shouldn't ever be trying to assign new data to a dummy header, except
1013 : : * in the case of an internal call for field inlining.
1014 : : */
1015 : : Assert(!(erh->flags & ER_FLAG_IS_DUMMY) || !check_constraints);
1016 : :
1017 : : /* Before performing the assignment, see if result will satisfy domain */
1018 [ + + + - ]: 1579 : if ((erh->flags & ER_FLAG_IS_DOMAIN) && check_constraints)
1019 : 12 : check_domain_for_new_field(erh, fnumber, newValue, isnull);
1020 : :
1021 : : /* If we haven't yet deconstructed the tuple, do that */
1022 [ + + ]: 1576 : if (!(erh->flags & ER_FLAG_DVALUES_VALID))
1023 : 272 : deconstruct_expanded_record(erh);
1024 : :
1025 : 1576 : tupdesc = erh->er_tupdesc;
1026 : : Assert(erh->nfields == tupdesc->natts);
1027 : :
1028 : : /* Caller error if fnumber is system column or nonexistent column */
1029 [ + - - + : 1576 : if (unlikely(fnumber <= 0 || fnumber > erh->nfields))
- + ]
1030 [ # # ]: 0 : elog(ERROR, "cannot assign to field %d of expanded record", fnumber);
1031 : :
1032 : : /*
1033 : : * Copy new field value into record's context, and deal with detoasting,
1034 : : * if needed.
1035 : : */
1036 : 1576 : attr = TupleDescCompactAttr(tupdesc, fnumber - 1);
1037 [ + + + + ]: 1576 : if (!isnull && !attr->attbyval)
1038 : : {
1039 : : MemoryContext oldcxt;
1040 : :
1041 : : /* If requested, detoast any external value */
1042 [ + + ]: 1001 : if (expand_external)
1043 : : {
1044 [ + - + + ]: 122 : if (attr->attlen == -1 &&
1045 : 61 : VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
1046 : : {
1047 : : /* Detoasting should be done in short-lived context. */
1048 : 41 : oldcxt = MemoryContextSwitchTo(get_short_term_cxt(erh));
1049 : 41 : newValue = PointerGetDatum(detoast_external_attr((varlena *) DatumGetPointer(newValue)));
1050 : 41 : MemoryContextSwitchTo(oldcxt);
1051 : : }
1052 : : else
1053 : 20 : expand_external = false; /* need not clean up below */
1054 : : }
1055 : :
1056 : : /* Copy value into record's context */
1057 : 1001 : oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
1058 : 1001 : newValue = datumCopy(newValue, false, attr->attlen);
1059 : 1001 : MemoryContextSwitchTo(oldcxt);
1060 : :
1061 : : /* We can now flush anything that detoasting might have leaked */
1062 [ + + ]: 1001 : if (expand_external)
1063 : 41 : MemoryContextReset(erh->er_short_term_cxt);
1064 : :
1065 : : /* Remember that we have field(s) that may need to be pfree'd */
1066 : 1001 : erh->flags |= ER_FLAG_DVALUES_ALLOCED;
1067 : :
1068 : : /*
1069 : : * While we're here, note whether it's an external toasted value,
1070 : : * because that could mean we need to inline it later. (Think not to
1071 : : * merge this into the previous expand_external logic: datumCopy could
1072 : : * by itself have made the value non-external.)
1073 : : */
1074 [ + - - + ]: 2002 : if (attr->attlen == -1 &&
1075 : 1001 : VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
1076 : 0 : erh->flags |= ER_FLAG_HAVE_EXTERNAL;
1077 : : }
1078 : :
1079 : : /*
1080 : : * We're ready to make irreversible changes.
1081 : : */
1082 : 1576 : dvalues = erh->dvalues;
1083 : 1576 : dnulls = erh->dnulls;
1084 : :
1085 : : /* Flattened value will no longer represent record accurately */
1086 : 1576 : erh->flags &= ~ER_FLAG_FVALUE_VALID;
1087 : : /* And we don't know the flattened size either */
1088 : 1576 : erh->flat_size = 0;
1089 : :
1090 : : /* Grab old field value for pfree'ing, if needed. */
1091 [ + + + + ]: 1576 : if (!attr->attbyval && !dnulls[fnumber - 1])
1092 : 863 : oldValue = (char *) DatumGetPointer(dvalues[fnumber - 1]);
1093 : : else
1094 : 713 : oldValue = NULL;
1095 : :
1096 : : /* And finally we can insert the new field. */
1097 : 1576 : dvalues[fnumber - 1] = newValue;
1098 : 1576 : dnulls[fnumber - 1] = isnull;
1099 : :
1100 : : /*
1101 : : * Free old field if needed; this keeps repeated field replacements from
1102 : : * bloating the record's storage. If the pfree somehow fails, it won't
1103 : : * corrupt the record.
1104 : : *
1105 : : * If we're updating a dummy header, we can't risk pfree'ing the old
1106 : : * value, because most likely the expanded record's main header still has
1107 : : * a pointer to it. This won't result in any sustained memory leak, since
1108 : : * whatever we just allocated here is in the short-lived domain check
1109 : : * context.
1110 : : */
1111 [ + + + - ]: 1576 : if (oldValue && !(erh->flags & ER_FLAG_IS_DUMMY))
1112 : : {
1113 : : /* Don't try to pfree a part of the original flat record */
1114 [ + - + + ]: 863 : if (oldValue < erh->fstartptr || oldValue >= erh->fendptr)
1115 : 14 : pfree(oldValue);
1116 : : }
1117 : 1576 : }
1118 : :
1119 : : /*
1120 : : * Set all record field(s)
1121 : : *
1122 : : * Caller must ensure that the provided datums are of the right types
1123 : : * to match the record's previously assigned rowtype.
1124 : : *
1125 : : * If expand_external is true, we'll forcibly detoast out-of-line field values
1126 : : * so that the record does not depend on external storage.
1127 : : *
1128 : : * Unlike repeated application of expanded_record_set_field(), this does not
1129 : : * guarantee to leave the expanded record in a non-corrupt state in event
1130 : : * of an error. Typically it would only be used for initializing a new
1131 : : * expanded record. Also, because we expect this to be applied at most once
1132 : : * in the lifespan of an expanded record, we do not worry about any cruft
1133 : : * that detoasting might leak.
1134 : : */
1135 : : void
1136 : 84 : expanded_record_set_fields(ExpandedRecordHeader *erh,
1137 : : const Datum *newValues, const bool *isnulls,
1138 : : bool expand_external)
1139 : : {
1140 : : TupleDesc tupdesc;
1141 : : Datum *dvalues;
1142 : : bool *dnulls;
1143 : : int fnumber;
1144 : : MemoryContext oldcxt;
1145 : :
1146 : : /* Shouldn't ever be trying to assign new data to a dummy header */
1147 : : Assert(!(erh->flags & ER_FLAG_IS_DUMMY));
1148 : :
1149 : : /* If we haven't yet deconstructed the tuple, do that */
1150 [ + - ]: 84 : if (!(erh->flags & ER_FLAG_DVALUES_VALID))
1151 : 84 : deconstruct_expanded_record(erh);
1152 : :
1153 : 84 : tupdesc = erh->er_tupdesc;
1154 : : Assert(erh->nfields == tupdesc->natts);
1155 : :
1156 : : /* Flattened value will no longer represent record accurately */
1157 : 84 : erh->flags &= ~ER_FLAG_FVALUE_VALID;
1158 : : /* And we don't know the flattened size either */
1159 : 84 : erh->flat_size = 0;
1160 : :
1161 : 84 : oldcxt = MemoryContextSwitchTo(erh->hdr.eoh_context);
1162 : :
1163 : 84 : dvalues = erh->dvalues;
1164 : 84 : dnulls = erh->dnulls;
1165 : :
1166 [ + + ]: 235 : for (fnumber = 0; fnumber < erh->nfields; fnumber++)
1167 : : {
1168 : 151 : CompactAttribute *attr = TupleDescCompactAttr(tupdesc, fnumber);
1169 : : Datum newValue;
1170 : : bool isnull;
1171 : :
1172 : : /* Ignore dropped columns */
1173 [ + + ]: 151 : if (attr->attisdropped)
1174 : 5 : continue;
1175 : :
1176 : 146 : newValue = newValues[fnumber];
1177 : 146 : isnull = isnulls[fnumber];
1178 : :
1179 [ + + ]: 146 : if (!attr->attbyval)
1180 : : {
1181 : : /*
1182 : : * Copy new field value into record's context, and deal with
1183 : : * detoasting, if needed.
1184 : : */
1185 [ + - ]: 51 : if (!isnull)
1186 : : {
1187 : : /* Is it an external toasted value? */
1188 [ + - + + ]: 102 : if (attr->attlen == -1 &&
1189 : 51 : VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
1190 : : {
1191 [ + - ]: 1 : if (expand_external)
1192 : : {
1193 : : /* Detoast as requested while copying the value */
1194 : 1 : newValue = PointerGetDatum(detoast_external_attr((varlena *) DatumGetPointer(newValue)));
1195 : : }
1196 : : else
1197 : : {
1198 : : /* Just copy the value */
1199 : 0 : newValue = datumCopy(newValue, false, -1);
1200 : : /* If it's still external, remember that */
1201 [ # # ]: 0 : if (VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
1202 : 0 : erh->flags |= ER_FLAG_HAVE_EXTERNAL;
1203 : : }
1204 : : }
1205 : : else
1206 : : {
1207 : : /* Not an external value, just copy it */
1208 : 50 : newValue = datumCopy(newValue, false, attr->attlen);
1209 : : }
1210 : :
1211 : : /* Remember that we have field(s) that need to be pfree'd */
1212 : 51 : erh->flags |= ER_FLAG_DVALUES_ALLOCED;
1213 : : }
1214 : :
1215 : : /*
1216 : : * Free old field value, if any (not likely, since really we ought
1217 : : * to be inserting into an empty record).
1218 : : */
1219 [ - + ]: 51 : if (unlikely(!dnulls[fnumber]))
1220 : : {
1221 : : char *oldValue;
1222 : :
1223 : 0 : oldValue = (char *) DatumGetPointer(dvalues[fnumber]);
1224 : : /* Don't try to pfree a part of the original flat record */
1225 [ # # # # ]: 0 : if (oldValue < erh->fstartptr || oldValue >= erh->fendptr)
1226 : 0 : pfree(oldValue);
1227 : : }
1228 : : }
1229 : :
1230 : : /* And finally we can insert the new field. */
1231 : 146 : dvalues[fnumber] = newValue;
1232 : 146 : dnulls[fnumber] = isnull;
1233 : : }
1234 : :
1235 : : /*
1236 : : * Because we don't guarantee atomicity of set_fields(), we can just leave
1237 : : * checking of domain constraints to occur as the final step; if it throws
1238 : : * an error, too bad.
1239 : : */
1240 [ + + ]: 84 : if (erh->flags & ER_FLAG_IS_DOMAIN)
1241 : : {
1242 : : /* We run domain_check in a short-lived context to limit cruft */
1243 : 9 : MemoryContextSwitchTo(get_short_term_cxt(erh));
1244 : :
1245 : 9 : domain_check(ExpandedRecordGetRODatum(erh), false,
1246 : : erh->er_decltypeid,
1247 : : &erh->er_domaininfo,
1248 : : erh->hdr.eoh_context);
1249 : : }
1250 : :
1251 : 80 : MemoryContextSwitchTo(oldcxt);
1252 : 80 : }
1253 : :
1254 : : /*
1255 : : * Construct (or reset) working memory context for short-term operations.
1256 : : *
1257 : : * This context is used for domain check evaluation and for detoasting.
1258 : : *
1259 : : * If we don't have a short-lived memory context, make one; if we have one,
1260 : : * reset it to get rid of any leftover cruft. (It is a tad annoying to need a
1261 : : * whole context for this, since it will often go unused --- but it's hard to
1262 : : * avoid memory leaks otherwise. We can make the context small, at least.)
1263 : : */
1264 : : static MemoryContext
1265 : 128 : get_short_term_cxt(ExpandedRecordHeader *erh)
1266 : : {
1267 [ + + ]: 128 : if (erh->er_short_term_cxt == NULL)
1268 : 101 : erh->er_short_term_cxt =
1269 : 101 : AllocSetContextCreate(erh->hdr.eoh_context,
1270 : : "expanded record short-term context",
1271 : : ALLOCSET_SMALL_SIZES);
1272 : : else
1273 : 27 : MemoryContextReset(erh->er_short_term_cxt);
1274 : 128 : return erh->er_short_term_cxt;
1275 : : }
1276 : :
1277 : : /*
1278 : : * Construct "dummy header" for checking domain constraints.
1279 : : *
1280 : : * Since we don't want to modify the state of the expanded record until
1281 : : * we've validated the constraints, our approach is to set up a dummy
1282 : : * record header containing the new field value(s) and then pass that to
1283 : : * domain_check. We retain the dummy header as part of the expanded
1284 : : * record's state to save palloc cycles, but reinitialize (most of)
1285 : : * its contents on each use.
1286 : : */
1287 : : static void
1288 : 41 : build_dummy_expanded_header(ExpandedRecordHeader *main_erh)
1289 : : {
1290 : : ExpandedRecordHeader *erh;
1291 : 41 : TupleDesc tupdesc = expanded_record_get_tupdesc(main_erh);
1292 : :
1293 : : /* Ensure we have a short-lived context */
1294 : 41 : (void) get_short_term_cxt(main_erh);
1295 : :
1296 : : /*
1297 : : * Allocate dummy header on first time through. Include space for its
1298 : : * field values in the request.
1299 : : */
1300 : 41 : erh = main_erh->er_dummy_header;
1301 [ + + ]: 41 : if (erh == NULL)
1302 : : {
1303 : : char *chunk;
1304 : :
1305 : : erh = (ExpandedRecordHeader *)
1306 : 31 : MemoryContextAlloc(main_erh->hdr.eoh_context,
1307 : : MAXALIGN(sizeof(ExpandedRecordHeader))
1308 : 31 : + tupdesc->natts * (sizeof(Datum) + sizeof(bool)));
1309 : :
1310 : : /* Ensure all header fields are initialized to 0/null */
1311 : 31 : memset(erh, 0, sizeof(ExpandedRecordHeader));
1312 : :
1313 : : /*
1314 : : * We set up the dummy header with an indication that its memory
1315 : : * context is the short-lived context. This is so that, if any
1316 : : * detoasting of out-of-line values happens due to an attempt to
1317 : : * extract a composite datum from the dummy header, the detoasted
1318 : : * stuff will end up in the short-lived context and not cause a leak.
1319 : : * This is cheating a bit on the expanded-object protocol; but since
1320 : : * we never pass a R/W pointer to the dummy object to any other code,
1321 : : * nothing else is authorized to delete or transfer ownership of the
1322 : : * object's context, so it should be safe enough.
1323 : : */
1324 : 31 : EOH_init_header(&erh->hdr, &ER_methods, main_erh->er_short_term_cxt);
1325 : 31 : erh->er_magic = ER_MAGIC;
1326 : :
1327 : : /* Set up dvalues/dnulls, with no valid contents as yet */
1328 : 31 : chunk = (char *) erh + MAXALIGN(sizeof(ExpandedRecordHeader));
1329 : 31 : erh->dvalues = (Datum *) chunk;
1330 : 31 : erh->dnulls = (bool *) (chunk + tupdesc->natts * sizeof(Datum));
1331 : 31 : erh->nfields = tupdesc->natts;
1332 : :
1333 : : /*
1334 : : * The fields we just set are assumed to remain constant through
1335 : : * multiple uses of the dummy header to check domain constraints. All
1336 : : * other dummy header fields should be explicitly reset below, to
1337 : : * ensure there's not accidental effects of one check on the next one.
1338 : : */
1339 : :
1340 : 31 : main_erh->er_dummy_header = erh;
1341 : : }
1342 : :
1343 : : /*
1344 : : * If anything inquires about the dummy header's declared type, it should
1345 : : * report the composite base type, not the domain type (since the VALUE in
1346 : : * a domain check constraint is of the base type not the domain). Hence
1347 : : * we do not transfer over the IS_DOMAIN flag, nor indeed any of the main
1348 : : * header's flags, since the dummy header is empty of data at this point.
1349 : : * But don't forget to mark header as dummy.
1350 : : */
1351 : 41 : erh->flags = ER_FLAG_IS_DUMMY;
1352 : :
1353 : : /* Copy composite-type identification info */
1354 : 41 : erh->er_decltypeid = erh->er_typeid = main_erh->er_typeid;
1355 : 41 : erh->er_typmod = main_erh->er_typmod;
1356 : :
1357 : : /* Dummy header does not need its own tupdesc refcount */
1358 : 41 : erh->er_tupdesc = tupdesc;
1359 : 41 : erh->er_tupdesc_id = main_erh->er_tupdesc_id;
1360 : :
1361 : : /*
1362 : : * It's tempting to copy over whatever we know about the flat size, but
1363 : : * there's no point since we're surely about to modify the dummy record's
1364 : : * field(s). Instead just clear anything left over from a previous usage
1365 : : * cycle.
1366 : : */
1367 : 41 : erh->flat_size = 0;
1368 : :
1369 : : /* Copy over fvalue if we have it, so that system columns are available */
1370 : 41 : erh->fvalue = main_erh->fvalue;
1371 : 41 : erh->fstartptr = main_erh->fstartptr;
1372 : 41 : erh->fendptr = main_erh->fendptr;
1373 : :
1374 : : /*
1375 : : * We must create a new short-term context (if needed) in each cycle of
1376 : : * usage of the dummy header. This is because if we create one, it will
1377 : : * be a child of the main header's short-term context, as per above, and
1378 : : * therefore will go away anytime something resets that context. The
1379 : : * immediate callers of this function do so at their ends, so no long-term
1380 : : * leakage is implied, but the next time we come through here the dummy's
1381 : : * er_short_term_cxt will be a dangling pointer.
1382 : : */
1383 : 41 : erh->er_short_term_cxt = NULL;
1384 : :
1385 : : /*
1386 : : * Needn't worry about the dummy header's er_dummy_header, er_domaininfo,
1387 : : * or er_mcb; none of them will be used.
1388 : : */
1389 : 41 : }
1390 : :
1391 : : /*
1392 : : * Precheck domain constraints for a set_field operation
1393 : : */
1394 : : static pg_noinline void
1395 : 12 : check_domain_for_new_field(ExpandedRecordHeader *erh, int fnumber,
1396 : : Datum newValue, bool isnull)
1397 : : {
1398 : : ExpandedRecordHeader *dummy_erh;
1399 : : MemoryContext oldcxt;
1400 : :
1401 : : /* Construct dummy header to contain proposed new field set */
1402 : 12 : build_dummy_expanded_header(erh);
1403 : 12 : dummy_erh = erh->er_dummy_header;
1404 : :
1405 : : /*
1406 : : * If record isn't empty, just deconstruct it (if needed) and copy over
1407 : : * the existing field values. If it is empty, just fill fields with nulls
1408 : : * manually --- don't call deconstruct_expanded_record prematurely.
1409 : : */
1410 [ + + ]: 12 : if (!ExpandedRecordIsEmpty(erh))
1411 : : {
1412 : 8 : deconstruct_expanded_record(erh);
1413 : 8 : memcpy(dummy_erh->dvalues, erh->dvalues,
1414 : 8 : dummy_erh->nfields * sizeof(Datum));
1415 : 8 : memcpy(dummy_erh->dnulls, erh->dnulls,
1416 : 8 : dummy_erh->nfields * sizeof(bool));
1417 : : /* There might be some external values in there... */
1418 : 8 : dummy_erh->flags |= erh->flags & ER_FLAG_HAVE_EXTERNAL;
1419 : : }
1420 : : else
1421 : : {
1422 : 4 : memset(dummy_erh->dvalues, 0, dummy_erh->nfields * sizeof(Datum));
1423 : 4 : memset(dummy_erh->dnulls, true, dummy_erh->nfields * sizeof(bool));
1424 : : }
1425 : :
1426 : : /* Either way, we now have valid dvalues */
1427 : 12 : dummy_erh->flags |= ER_FLAG_DVALUES_VALID;
1428 : :
1429 : : /* Caller error if fnumber is system column or nonexistent column */
1430 [ + - - + : 12 : if (unlikely(fnumber <= 0 || fnumber > dummy_erh->nfields))
- + ]
1431 [ # # ]: 0 : elog(ERROR, "cannot assign to field %d of expanded record", fnumber);
1432 : :
1433 : : /* Insert proposed new value into dummy field array */
1434 : 12 : dummy_erh->dvalues[fnumber - 1] = newValue;
1435 : 12 : dummy_erh->dnulls[fnumber - 1] = isnull;
1436 : :
1437 : : /*
1438 : : * The proposed new value might be external, in which case we'd better set
1439 : : * the flag for that in dummy_erh. (This matters in case something in the
1440 : : * domain check expressions tries to extract a flat value from the dummy
1441 : : * header.)
1442 : : */
1443 [ + + ]: 12 : if (!isnull)
1444 : : {
1445 : 11 : CompactAttribute *attr = TupleDescCompactAttr(erh->er_tupdesc, fnumber - 1);
1446 : :
1447 [ + + + - : 17 : if (!attr->attbyval && attr->attlen == -1 &&
- + ]
1448 : 6 : VARATT_IS_EXTERNAL(DatumGetPointer(newValue)))
1449 : 0 : dummy_erh->flags |= ER_FLAG_HAVE_EXTERNAL;
1450 : : }
1451 : :
1452 : : /*
1453 : : * We call domain_check in the short-lived context, so that any cruft
1454 : : * leaked by expression evaluation can be reclaimed.
1455 : : */
1456 : 12 : oldcxt = MemoryContextSwitchTo(erh->er_short_term_cxt);
1457 : :
1458 : : /*
1459 : : * And now we can apply the check. Note we use main header's domain cache
1460 : : * space, so that caching carries across repeated uses.
1461 : : */
1462 : 12 : domain_check(ExpandedRecordGetRODatum(dummy_erh), false,
1463 : : erh->er_decltypeid,
1464 : : &erh->er_domaininfo,
1465 : : erh->hdr.eoh_context);
1466 : :
1467 : 9 : MemoryContextSwitchTo(oldcxt);
1468 : :
1469 : : /* We might as well clean up cruft immediately. */
1470 : 9 : MemoryContextReset(erh->er_short_term_cxt);
1471 : 9 : }
1472 : :
1473 : : /*
1474 : : * Precheck domain constraints for a set_tuple operation
1475 : : */
1476 : : static pg_noinline void
1477 : 59 : check_domain_for_new_tuple(ExpandedRecordHeader *erh, HeapTuple tuple)
1478 : : {
1479 : : ExpandedRecordHeader *dummy_erh;
1480 : : MemoryContext oldcxt;
1481 : :
1482 : : /* If we're being told to set record to empty, just see if NULL is OK */
1483 [ + + ]: 59 : if (tuple == NULL)
1484 : : {
1485 : : /* We run domain_check in a short-lived context to limit cruft */
1486 : 30 : oldcxt = MemoryContextSwitchTo(get_short_term_cxt(erh));
1487 : :
1488 : 30 : domain_check((Datum) 0, true,
1489 : : erh->er_decltypeid,
1490 : : &erh->er_domaininfo,
1491 : : erh->hdr.eoh_context);
1492 : :
1493 : 26 : MemoryContextSwitchTo(oldcxt);
1494 : :
1495 : : /* We might as well clean up cruft immediately. */
1496 : 26 : MemoryContextReset(erh->er_short_term_cxt);
1497 : :
1498 : 26 : return;
1499 : : }
1500 : :
1501 : : /* Construct dummy header to contain replacement tuple */
1502 : 29 : build_dummy_expanded_header(erh);
1503 : 29 : dummy_erh = erh->er_dummy_header;
1504 : :
1505 : : /* Insert tuple, but don't bother to deconstruct its fields for now */
1506 : 29 : dummy_erh->fvalue = tuple;
1507 : 29 : dummy_erh->fstartptr = (char *) tuple->t_data;
1508 : 29 : dummy_erh->fendptr = ((char *) tuple->t_data) + tuple->t_len;
1509 : 29 : dummy_erh->flags |= ER_FLAG_FVALUE_VALID;
1510 : :
1511 : : /* Remember if we have any out-of-line field values */
1512 [ - + ]: 29 : if (HeapTupleHasExternal(tuple))
1513 : 0 : dummy_erh->flags |= ER_FLAG_HAVE_EXTERNAL;
1514 : :
1515 : : /*
1516 : : * We call domain_check in the short-lived context, so that any cruft
1517 : : * leaked by expression evaluation can be reclaimed.
1518 : : */
1519 : 29 : oldcxt = MemoryContextSwitchTo(erh->er_short_term_cxt);
1520 : :
1521 : : /*
1522 : : * And now we can apply the check. Note we use main header's domain cache
1523 : : * space, so that caching carries across repeated uses.
1524 : : */
1525 : 29 : domain_check(ExpandedRecordGetRODatum(dummy_erh), false,
1526 : : erh->er_decltypeid,
1527 : : &erh->er_domaininfo,
1528 : : erh->hdr.eoh_context);
1529 : :
1530 : 25 : MemoryContextSwitchTo(oldcxt);
1531 : :
1532 : : /* We might as well clean up cruft immediately. */
1533 : 25 : MemoryContextReset(erh->er_short_term_cxt);
1534 : : }
|