Line data Source code
1 : /*-------------------------------------------------------------------------
2 : *
3 : * heapfuncs.c
4 : * Functions to investigate heap pages
5 : *
6 : * We check the input to these functions for corrupt pointers etc. that
7 : * might cause crashes, but at the same time we try to print out as much
8 : * information as possible, even if it's nonsense. That's because if a
9 : * page is corrupt, we don't know why and how exactly it is corrupt, so we
10 : * let the user judge it.
11 : *
12 : * These functions are restricted to superusers for the fear of introducing
13 : * security holes if the input checking isn't as water-tight as it should be.
14 : * You'd need to be superuser to obtain a raw page image anyway, so
15 : * there's hardly any use case for using these without superuser-rights
16 : * anyway.
17 : *
18 : * Copyright (c) 2007-2024, PostgreSQL Global Development Group
19 : *
20 : * IDENTIFICATION
21 : * contrib/pageinspect/heapfuncs.c
22 : *
23 : *-------------------------------------------------------------------------
24 : */
25 :
26 : #include "postgres.h"
27 :
28 : #include "access/htup_details.h"
29 : #include "access/relation.h"
30 : #include "catalog/pg_am_d.h"
31 : #include "catalog/pg_type.h"
32 : #include "funcapi.h"
33 : #include "mb/pg_wchar.h"
34 : #include "miscadmin.h"
35 : #include "port/pg_bitutils.h"
36 : #include "utils/array.h"
37 : #include "utils/builtins.h"
38 : #include "utils/rel.h"
39 :
40 : /*
41 : * It's not supported to create tuples with oids anymore, but when pg_upgrade
42 : * was used to upgrade from an older version, tuples might still have an
43 : * oid. Seems worthwhile to display that.
44 : */
45 : #define HeapTupleHeaderGetOidOld(tup) \
46 : ( \
47 : ((tup)->t_infomask & HEAP_HASOID_OLD) ? \
48 : *((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
49 : : \
50 : InvalidOid \
51 : )
52 :
53 :
54 : /*
55 : * bits_to_text
56 : *
57 : * Converts a bits8-array of 'len' bits to a human-readable
58 : * c-string representation.
59 : */
60 : static char *
61 4 : bits_to_text(bits8 *bits, int len)
62 : {
63 : int i;
64 : char *str;
65 :
66 4 : str = palloc(len + 1);
67 :
68 36 : for (i = 0; i < len; i++)
69 32 : str[i] = (bits[(i / 8)] & (1 << (i % 8))) ? '1' : '0';
70 :
71 4 : str[i] = '\0';
72 :
73 4 : return str;
74 : }
75 :
76 :
77 : /*
78 : * text_to_bits
79 : *
80 : * Converts a c-string representation of bits into a bits8-array. This is
81 : * the reverse operation of previous routine.
82 : */
83 : static bits8 *
84 2 : text_to_bits(char *str, int len)
85 : {
86 : bits8 *bits;
87 2 : int off = 0;
88 2 : char byte = 0;
89 :
90 2 : bits = palloc(len + 1);
91 :
92 18 : while (off < len)
93 : {
94 16 : if (off % 8 == 0)
95 2 : byte = 0;
96 :
97 16 : if ((str[off] == '0') || (str[off] == '1'))
98 16 : byte = byte | ((str[off] - '0') << off % 8);
99 : else
100 0 : ereport(ERROR,
101 : (errcode(ERRCODE_DATA_CORRUPTED),
102 : errmsg("invalid character \"%.*s\" in t_bits string",
103 : pg_mblen(str + off), str + off)));
104 :
105 16 : if (off % 8 == 7)
106 2 : bits[off / 8] = byte;
107 :
108 16 : off++;
109 : }
110 :
111 2 : return bits;
112 : }
113 :
114 : /*
115 : * heap_page_items
116 : *
117 : * Allows inspection of line pointers and tuple headers of a heap page.
118 : */
119 16 : PG_FUNCTION_INFO_V1(heap_page_items);
120 :
121 : typedef struct heap_page_items_state
122 : {
123 : TupleDesc tupd;
124 : Page page;
125 : uint16 offset;
126 : } heap_page_items_state;
127 :
128 : Datum
129 110 : heap_page_items(PG_FUNCTION_ARGS)
130 : {
131 110 : bytea *raw_page = PG_GETARG_BYTEA_P(0);
132 110 : heap_page_items_state *inter_call_data = NULL;
133 : FuncCallContext *fctx;
134 : int raw_page_size;
135 :
136 110 : if (!superuser())
137 0 : ereport(ERROR,
138 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
139 : errmsg("must be superuser to use raw page functions")));
140 :
141 110 : raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
142 :
143 110 : if (SRF_IS_FIRSTCALL())
144 : {
145 : TupleDesc tupdesc;
146 : MemoryContext mctx;
147 :
148 12 : if (raw_page_size < SizeOfPageHeaderData)
149 0 : ereport(ERROR,
150 : (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
151 : errmsg("input page too small (%d bytes)", raw_page_size)));
152 :
153 12 : fctx = SRF_FIRSTCALL_INIT();
154 12 : mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
155 :
156 12 : inter_call_data = palloc(sizeof(heap_page_items_state));
157 :
158 : /* Build a tuple descriptor for our result type */
159 12 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
160 0 : elog(ERROR, "return type must be a row type");
161 :
162 12 : inter_call_data->tupd = tupdesc;
163 :
164 12 : inter_call_data->offset = FirstOffsetNumber;
165 12 : inter_call_data->page = VARDATA(raw_page);
166 :
167 12 : fctx->max_calls = PageGetMaxOffsetNumber(inter_call_data->page);
168 12 : fctx->user_fctx = inter_call_data;
169 :
170 12 : MemoryContextSwitchTo(mctx);
171 : }
172 :
173 110 : fctx = SRF_PERCALL_SETUP();
174 110 : inter_call_data = fctx->user_fctx;
175 :
176 110 : if (fctx->call_cntr < fctx->max_calls)
177 : {
178 98 : Page page = inter_call_data->page;
179 : HeapTuple resultTuple;
180 : Datum result;
181 : ItemId id;
182 : Datum values[14];
183 : bool nulls[14];
184 : uint16 lp_offset;
185 : uint16 lp_flags;
186 : uint16 lp_len;
187 :
188 98 : memset(nulls, 0, sizeof(nulls));
189 :
190 : /* Extract information from the line pointer */
191 :
192 98 : id = PageGetItemId(page, inter_call_data->offset);
193 :
194 98 : lp_offset = ItemIdGetOffset(id);
195 98 : lp_flags = ItemIdGetFlags(id);
196 98 : lp_len = ItemIdGetLength(id);
197 :
198 98 : values[0] = UInt16GetDatum(inter_call_data->offset);
199 98 : values[1] = UInt16GetDatum(lp_offset);
200 98 : values[2] = UInt16GetDatum(lp_flags);
201 98 : values[3] = UInt16GetDatum(lp_len);
202 :
203 : /*
204 : * We do just enough validity checking to make sure we don't reference
205 : * data outside the page passed to us. The page could be corrupt in
206 : * many other ways, but at least we won't crash.
207 : */
208 98 : if (ItemIdHasStorage(id) &&
209 88 : lp_len >= MinHeapTupleSize &&
210 88 : lp_offset == MAXALIGN(lp_offset) &&
211 88 : lp_offset + lp_len <= raw_page_size)
212 88 : {
213 : HeapTupleHeader tuphdr;
214 : bytea *tuple_data_bytea;
215 : int tuple_data_len;
216 :
217 : /* Extract information from the tuple header */
218 :
219 88 : tuphdr = (HeapTupleHeader) PageGetItem(page, id);
220 :
221 88 : values[4] = UInt32GetDatum(HeapTupleHeaderGetRawXmin(tuphdr));
222 88 : values[5] = UInt32GetDatum(HeapTupleHeaderGetRawXmax(tuphdr));
223 : /* shared with xvac */
224 88 : values[6] = UInt32GetDatum(HeapTupleHeaderGetRawCommandId(tuphdr));
225 88 : values[7] = PointerGetDatum(&tuphdr->t_ctid);
226 88 : values[8] = UInt32GetDatum(tuphdr->t_infomask2);
227 88 : values[9] = UInt32GetDatum(tuphdr->t_infomask);
228 88 : values[10] = UInt8GetDatum(tuphdr->t_hoff);
229 :
230 : /* Copy raw tuple data into bytea attribute */
231 88 : tuple_data_len = lp_len - tuphdr->t_hoff;
232 88 : tuple_data_bytea = (bytea *) palloc(tuple_data_len + VARHDRSZ);
233 88 : SET_VARSIZE(tuple_data_bytea, tuple_data_len + VARHDRSZ);
234 88 : memcpy(VARDATA(tuple_data_bytea), (char *) tuphdr + tuphdr->t_hoff,
235 : tuple_data_len);
236 88 : values[13] = PointerGetDatum(tuple_data_bytea);
237 :
238 : /*
239 : * We already checked that the item is completely within the raw
240 : * page passed to us, with the length given in the line pointer.
241 : * Let's check that t_hoff doesn't point over lp_len, before using
242 : * it to access t_bits and oid.
243 : */
244 88 : if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
245 88 : tuphdr->t_hoff <= lp_len &&
246 88 : tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
247 : {
248 88 : if (tuphdr->t_infomask & HEAP_HASNULL)
249 : {
250 : int bits_len;
251 :
252 4 : bits_len =
253 4 : BITMAPLEN(HeapTupleHeaderGetNatts(tuphdr)) * BITS_PER_BYTE;
254 4 : values[11] = CStringGetTextDatum(bits_to_text(tuphdr->t_bits, bits_len));
255 : }
256 : else
257 84 : nulls[11] = true;
258 :
259 88 : if (tuphdr->t_infomask & HEAP_HASOID_OLD)
260 0 : values[12] = HeapTupleHeaderGetOidOld(tuphdr);
261 : else
262 88 : nulls[12] = true;
263 : }
264 : else
265 : {
266 0 : nulls[11] = true;
267 0 : nulls[12] = true;
268 : }
269 : }
270 : else
271 : {
272 : /*
273 : * The line pointer is not used, or it's invalid. Set the rest of
274 : * the fields to NULL
275 : */
276 : int i;
277 :
278 110 : for (i = 4; i <= 13; i++)
279 100 : nulls[i] = true;
280 : }
281 :
282 : /* Build and return the result tuple. */
283 98 : resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
284 98 : result = HeapTupleGetDatum(resultTuple);
285 :
286 98 : inter_call_data->offset++;
287 :
288 98 : SRF_RETURN_NEXT(fctx, result);
289 : }
290 : else
291 12 : SRF_RETURN_DONE(fctx);
292 : }
293 :
294 : /*
295 : * tuple_data_split_internal
296 : *
297 : * Split raw tuple data taken directly from a page into an array of bytea
298 : * elements. This routine does a lookup on NULL values and creates array
299 : * elements accordingly. This is a reimplementation of nocachegetattr()
300 : * in heaptuple.c simplified for educational purposes.
301 : */
302 : static Datum
303 6 : tuple_data_split_internal(Oid relid, char *tupdata,
304 : uint16 tupdata_len, uint16 t_infomask,
305 : uint16 t_infomask2, bits8 *t_bits,
306 : bool do_detoast)
307 : {
308 : ArrayBuildState *raw_attrs;
309 : int nattrs;
310 : int i;
311 6 : int off = 0;
312 : Relation rel;
313 : TupleDesc tupdesc;
314 :
315 : /* Get tuple descriptor from relation OID */
316 6 : rel = relation_open(relid, AccessShareLock);
317 6 : tupdesc = RelationGetDescr(rel);
318 :
319 6 : raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
320 6 : nattrs = tupdesc->natts;
321 :
322 : /*
323 : * Sequences always use heap AM, but they don't show that in the catalogs.
324 : */
325 6 : if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
326 4 : rel->rd_rel->relam != HEAP_TABLE_AM_OID)
327 0 : ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
328 : errmsg("only heap AM is supported")));
329 :
330 6 : if (nattrs < (t_infomask2 & HEAP_NATTS_MASK))
331 0 : ereport(ERROR,
332 : (errcode(ERRCODE_DATA_CORRUPTED),
333 : errmsg("number of attributes in tuple header is greater than number of attributes in tuple descriptor")));
334 :
335 32 : for (i = 0; i < nattrs; i++)
336 : {
337 : Form_pg_attribute attr;
338 : bool is_null;
339 26 : bytea *attr_data = NULL;
340 :
341 26 : attr = TupleDescAttr(tupdesc, i);
342 :
343 : /*
344 : * Tuple header can specify fewer attributes than tuple descriptor as
345 : * ALTER TABLE ADD COLUMN without DEFAULT keyword does not actually
346 : * change tuples in pages, so attributes with numbers greater than
347 : * (t_infomask2 & HEAP_NATTS_MASK) should be treated as NULL.
348 : */
349 26 : if (i >= (t_infomask2 & HEAP_NATTS_MASK))
350 0 : is_null = true;
351 : else
352 26 : is_null = (t_infomask & HEAP_HASNULL) && att_isnull(i, t_bits);
353 :
354 26 : if (!is_null)
355 : {
356 : int len;
357 :
358 14 : if (attr->attlen == -1)
359 : {
360 0 : off = att_align_pointer(off, attr->attalign, -1,
361 : tupdata + off);
362 :
363 : /*
364 : * As VARSIZE_ANY throws an exception if it can't properly
365 : * detect the type of external storage in macros VARTAG_SIZE,
366 : * this check is repeated to have a nicer error handling.
367 : */
368 0 : if (VARATT_IS_EXTERNAL(tupdata + off) &&
369 0 : !VARATT_IS_EXTERNAL_ONDISK(tupdata + off) &&
370 0 : !VARATT_IS_EXTERNAL_INDIRECT(tupdata + off))
371 0 : ereport(ERROR,
372 : (errcode(ERRCODE_DATA_CORRUPTED),
373 : errmsg("first byte of varlena attribute is incorrect for attribute %d", i)));
374 :
375 0 : len = VARSIZE_ANY(tupdata + off);
376 : }
377 : else
378 : {
379 14 : off = att_align_nominal(off, attr->attalign);
380 14 : len = attr->attlen;
381 : }
382 :
383 14 : if (tupdata_len < off + len)
384 0 : ereport(ERROR,
385 : (errcode(ERRCODE_DATA_CORRUPTED),
386 : errmsg("unexpected end of tuple data")));
387 :
388 14 : if (attr->attlen == -1 && do_detoast)
389 0 : attr_data = pg_detoast_datum_copy((struct varlena *) (tupdata + off));
390 : else
391 : {
392 14 : attr_data = (bytea *) palloc(len + VARHDRSZ);
393 14 : SET_VARSIZE(attr_data, len + VARHDRSZ);
394 14 : memcpy(VARDATA(attr_data), tupdata + off, len);
395 : }
396 :
397 14 : off = att_addlength_pointer(off, attr->attlen,
398 : tupdata + off);
399 : }
400 :
401 26 : raw_attrs = accumArrayResult(raw_attrs, PointerGetDatum(attr_data),
402 : is_null, BYTEAOID, CurrentMemoryContext);
403 26 : if (attr_data)
404 14 : pfree(attr_data);
405 : }
406 :
407 6 : if (tupdata_len != off)
408 0 : ereport(ERROR,
409 : (errcode(ERRCODE_DATA_CORRUPTED),
410 : errmsg("end of tuple reached without looking at all its data")));
411 :
412 6 : relation_close(rel, AccessShareLock);
413 :
414 6 : return makeArrayResult(raw_attrs, CurrentMemoryContext);
415 : }
416 :
417 : /*
418 : * tuple_data_split
419 : *
420 : * Split raw tuple data taken directly from page into distinct elements
421 : * taking into account null values.
422 : */
423 26 : PG_FUNCTION_INFO_V1(tuple_data_split);
424 :
425 : Datum
426 6 : tuple_data_split(PG_FUNCTION_ARGS)
427 : {
428 : Oid relid;
429 : bytea *raw_data;
430 : uint16 t_infomask;
431 : uint16 t_infomask2;
432 : char *t_bits_str;
433 6 : bool do_detoast = false;
434 6 : bits8 *t_bits = NULL;
435 : Datum res;
436 :
437 6 : relid = PG_GETARG_OID(0);
438 6 : raw_data = PG_ARGISNULL(1) ? NULL : PG_GETARG_BYTEA_P(1);
439 6 : t_infomask = PG_GETARG_INT16(2);
440 6 : t_infomask2 = PG_GETARG_INT16(3);
441 6 : t_bits_str = PG_ARGISNULL(4) ? NULL :
442 2 : text_to_cstring(PG_GETARG_TEXT_PP(4));
443 :
444 6 : if (PG_NARGS() >= 6)
445 0 : do_detoast = PG_GETARG_BOOL(5);
446 :
447 6 : if (!superuser())
448 0 : ereport(ERROR,
449 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
450 : errmsg("must be superuser to use raw page functions")));
451 :
452 6 : if (!raw_data)
453 0 : PG_RETURN_NULL();
454 :
455 : /*
456 : * Convert t_bits string back to the bits8 array as represented in the
457 : * tuple header.
458 : */
459 6 : if (t_infomask & HEAP_HASNULL)
460 : {
461 : size_t bits_str_len;
462 : size_t bits_len;
463 :
464 2 : bits_len = BITMAPLEN(t_infomask2 & HEAP_NATTS_MASK) * BITS_PER_BYTE;
465 2 : if (!t_bits_str)
466 0 : ereport(ERROR,
467 : (errcode(ERRCODE_DATA_CORRUPTED),
468 : errmsg("t_bits string must not be NULL")));
469 :
470 2 : bits_str_len = strlen(t_bits_str);
471 2 : if (bits_len != bits_str_len)
472 0 : ereport(ERROR,
473 : (errcode(ERRCODE_DATA_CORRUPTED),
474 : errmsg("unexpected length of t_bits string: %zu, expected %zu",
475 : bits_str_len, bits_len)));
476 :
477 : /* do the conversion */
478 2 : t_bits = text_to_bits(t_bits_str, bits_str_len);
479 : }
480 : else
481 : {
482 4 : if (t_bits_str)
483 0 : ereport(ERROR,
484 : (errcode(ERRCODE_DATA_CORRUPTED),
485 : errmsg("t_bits string is expected to be NULL, but instead it is %zu bytes long",
486 : strlen(t_bits_str))));
487 : }
488 :
489 : /* Split tuple data */
490 6 : res = tuple_data_split_internal(relid, (char *) raw_data + VARHDRSZ,
491 6 : VARSIZE(raw_data) - VARHDRSZ,
492 : t_infomask, t_infomask2, t_bits,
493 : do_detoast);
494 :
495 6 : if (t_bits)
496 2 : pfree(t_bits);
497 :
498 6 : PG_RETURN_DATUM(res);
499 : }
500 :
501 : /*
502 : * heap_tuple_infomask_flags
503 : *
504 : * Decode into a human-readable format t_infomask and t_infomask2 associated
505 : * to a tuple. All the flags are described in access/htup_details.h.
506 : */
507 14 : PG_FUNCTION_INFO_V1(heap_tuple_infomask_flags);
508 :
509 : Datum
510 18 : heap_tuple_infomask_flags(PG_FUNCTION_ARGS)
511 : {
512 : #define HEAP_TUPLE_INFOMASK_COLS 2
513 18 : Datum values[HEAP_TUPLE_INFOMASK_COLS] = {0};
514 18 : bool nulls[HEAP_TUPLE_INFOMASK_COLS] = {0};
515 18 : uint16 t_infomask = PG_GETARG_INT16(0);
516 18 : uint16 t_infomask2 = PG_GETARG_INT16(1);
517 18 : int cnt = 0;
518 : ArrayType *a;
519 : int bitcnt;
520 : Datum *flags;
521 : TupleDesc tupdesc;
522 : HeapTuple tuple;
523 :
524 18 : if (!superuser())
525 0 : ereport(ERROR,
526 : (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
527 : errmsg("must be superuser to use raw page functions")));
528 :
529 : /* Build a tuple descriptor for our result type */
530 18 : if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
531 0 : elog(ERROR, "return type must be a row type");
532 :
533 18 : bitcnt = pg_popcount((const char *) &t_infomask, sizeof(uint16)) +
534 18 : pg_popcount((const char *) &t_infomask2, sizeof(uint16));
535 :
536 : /* If no flags, return a set of empty arrays */
537 18 : if (bitcnt <= 0)
538 : {
539 2 : values[0] = PointerGetDatum(construct_empty_array(TEXTOID));
540 2 : values[1] = PointerGetDatum(construct_empty_array(TEXTOID));
541 2 : tuple = heap_form_tuple(tupdesc, values, nulls);
542 2 : PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
543 : }
544 :
545 : /* build set of raw flags */
546 16 : flags = (Datum *) palloc0(sizeof(Datum) * bitcnt);
547 :
548 : /* decode t_infomask */
549 16 : if ((t_infomask & HEAP_HASNULL) != 0)
550 4 : flags[cnt++] = CStringGetTextDatum("HEAP_HASNULL");
551 16 : if ((t_infomask & HEAP_HASVARWIDTH) != 0)
552 4 : flags[cnt++] = CStringGetTextDatum("HEAP_HASVARWIDTH");
553 16 : if ((t_infomask & HEAP_HASEXTERNAL) != 0)
554 4 : flags[cnt++] = CStringGetTextDatum("HEAP_HASEXTERNAL");
555 16 : if ((t_infomask & HEAP_HASOID_OLD) != 0)
556 4 : flags[cnt++] = CStringGetTextDatum("HEAP_HASOID_OLD");
557 16 : if ((t_infomask & HEAP_XMAX_KEYSHR_LOCK) != 0)
558 8 : flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_KEYSHR_LOCK");
559 16 : if ((t_infomask & HEAP_COMBOCID) != 0)
560 4 : flags[cnt++] = CStringGetTextDatum("HEAP_COMBOCID");
561 16 : if ((t_infomask & HEAP_XMAX_EXCL_LOCK) != 0)
562 6 : flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_EXCL_LOCK");
563 16 : if ((t_infomask & HEAP_XMAX_LOCK_ONLY) != 0)
564 4 : flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_LOCK_ONLY");
565 16 : if ((t_infomask & HEAP_XMIN_COMMITTED) != 0)
566 8 : flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_COMMITTED");
567 16 : if ((t_infomask & HEAP_XMIN_INVALID) != 0)
568 8 : flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_INVALID");
569 16 : if ((t_infomask & HEAP_XMAX_COMMITTED) != 0)
570 4 : flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_COMMITTED");
571 16 : if ((t_infomask & HEAP_XMAX_INVALID) != 0)
572 6 : flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_INVALID");
573 16 : if ((t_infomask & HEAP_XMAX_IS_MULTI) != 0)
574 4 : flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_IS_MULTI");
575 16 : if ((t_infomask & HEAP_UPDATED) != 0)
576 4 : flags[cnt++] = CStringGetTextDatum("HEAP_UPDATED");
577 16 : if ((t_infomask & HEAP_MOVED_OFF) != 0)
578 8 : flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_OFF");
579 16 : if ((t_infomask & HEAP_MOVED_IN) != 0)
580 8 : flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_IN");
581 :
582 : /* decode t_infomask2 */
583 16 : if ((t_infomask2 & HEAP_KEYS_UPDATED) != 0)
584 4 : flags[cnt++] = CStringGetTextDatum("HEAP_KEYS_UPDATED");
585 16 : if ((t_infomask2 & HEAP_HOT_UPDATED) != 0)
586 4 : flags[cnt++] = CStringGetTextDatum("HEAP_HOT_UPDATED");
587 16 : if ((t_infomask2 & HEAP_ONLY_TUPLE) != 0)
588 4 : flags[cnt++] = CStringGetTextDatum("HEAP_ONLY_TUPLE");
589 :
590 : /* build value */
591 : Assert(cnt <= bitcnt);
592 16 : a = construct_array_builtin(flags, cnt, TEXTOID);
593 16 : values[0] = PointerGetDatum(a);
594 :
595 : /*
596 : * Build set of combined flags. Use the same array as previously, this
597 : * keeps the code simple.
598 : */
599 16 : cnt = 0;
600 170 : MemSet(flags, 0, sizeof(Datum) * bitcnt);
601 :
602 : /* decode combined masks of t_infomask */
603 16 : if ((t_infomask & HEAP_XMAX_SHR_LOCK) == HEAP_XMAX_SHR_LOCK)
604 6 : flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_SHR_LOCK");
605 16 : if ((t_infomask & HEAP_XMIN_FROZEN) == HEAP_XMIN_FROZEN)
606 8 : flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_FROZEN");
607 16 : if ((t_infomask & HEAP_MOVED) == HEAP_MOVED)
608 8 : flags[cnt++] = CStringGetTextDatum("HEAP_MOVED");
609 :
610 : /* Build an empty array if there are no combined flags */
611 16 : if (cnt == 0)
612 2 : a = construct_empty_array(TEXTOID);
613 : else
614 14 : a = construct_array_builtin(flags, cnt, TEXTOID);
615 16 : pfree(flags);
616 16 : values[1] = PointerGetDatum(a);
617 :
618 : /* Returns the record as Datum */
619 16 : tuple = heap_form_tuple(tupdesc, values, nulls);
620 16 : PG_RETURN_DATUM(HeapTupleGetDatum(tuple));
621 : }
|