PostgreSQL Source Code git master
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
heapfuncs.c File Reference
#include "postgres.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "port/pg_bitutils.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/rel.h"
Include dependency graph for heapfuncs.c:

Go to the source code of this file.

Data Structures

struct  heap_page_items_state
 

Macros

#define HeapTupleHeaderGetOidOld(tup)
 
#define HEAP_TUPLE_INFOMASK_COLS   2
 

Typedefs

typedef struct heap_page_items_state heap_page_items_state
 

Functions

static char * bits_to_text (bits8 *bits, int len)
 
static bits8text_to_bits (char *str, int len)
 
 PG_FUNCTION_INFO_V1 (heap_page_items)
 
Datum heap_page_items (PG_FUNCTION_ARGS)
 
static Datum tuple_data_split_internal (Oid relid, char *tupdata, uint16 tupdata_len, uint16 t_infomask, uint16 t_infomask2, bits8 *t_bits, bool do_detoast)
 
 PG_FUNCTION_INFO_V1 (tuple_data_split)
 
Datum tuple_data_split (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (heap_tuple_infomask_flags)
 
Datum heap_tuple_infomask_flags (PG_FUNCTION_ARGS)
 

Macro Definition Documentation

◆ HEAP_TUPLE_INFOMASK_COLS

#define HEAP_TUPLE_INFOMASK_COLS   2

◆ HeapTupleHeaderGetOidOld

#define HeapTupleHeaderGetOidOld (   tup)
Value:
( \
((tup)->t_infomask & HEAP_HASOID_OLD) ? \
*((Oid *) ((char *)(tup) + (tup)->t_hoff - sizeof(Oid))) \
: \
)
#define HEAP_HASOID_OLD
Definition: htup_details.h:193
#define InvalidOid
Definition: postgres_ext.h:36
unsigned int Oid
Definition: postgres_ext.h:31

Definition at line 45 of file heapfuncs.c.

Typedef Documentation

◆ heap_page_items_state

Function Documentation

◆ bits_to_text()

static char * bits_to_text ( bits8 bits,
int  len 
)
static

Definition at line 61 of file heapfuncs.c.

62{
63 int i;
64 char *str;
65
66 str = palloc(len + 1);
67
68 for (i = 0; i < len; i++)
69 str[i] = (bits[(i / 8)] & (1 << (i % 8))) ? '1' : '0';
70
71 str[i] = '\0';
72
73 return str;
74}
const char * str
int i
Definition: isn.c:72
void * palloc(Size size)
Definition: mcxt.c:1317
const void size_t len

References i, len, palloc(), and str.

Referenced by heap_page_items().

◆ heap_page_items()

Datum heap_page_items ( PG_FUNCTION_ARGS  )

Definition at line 129 of file heapfuncs.c.

130{
131 bytea *raw_page = PG_GETARG_BYTEA_P(0);
132 heap_page_items_state *inter_call_data = NULL;
133 FuncCallContext *fctx;
134 int raw_page_size;
135
136 if (!superuser())
138 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
139 errmsg("must be superuser to use raw page functions")));
140
141 raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
142
143 if (SRF_IS_FIRSTCALL())
144 {
145 TupleDesc tupdesc;
146 MemoryContext mctx;
147
148 if (raw_page_size < SizeOfPageHeaderData)
150 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
151 errmsg("input page too small (%d bytes)", raw_page_size)));
152
153 fctx = SRF_FIRSTCALL_INIT();
155
156 inter_call_data = palloc(sizeof(heap_page_items_state));
157
158 /* Build a tuple descriptor for our result type */
159 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
160 elog(ERROR, "return type must be a row type");
161
162 inter_call_data->tupd = tupdesc;
163
164 inter_call_data->offset = FirstOffsetNumber;
165 inter_call_data->page = VARDATA(raw_page);
166
167 fctx->max_calls = PageGetMaxOffsetNumber(inter_call_data->page);
168 fctx->user_fctx = inter_call_data;
169
171 }
172
173 fctx = SRF_PERCALL_SETUP();
174 inter_call_data = fctx->user_fctx;
175
176 if (fctx->call_cntr < fctx->max_calls)
177 {
178 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 memset(nulls, 0, sizeof(nulls));
189
190 /* Extract information from the line pointer */
191
192 id = PageGetItemId(page, inter_call_data->offset);
193
194 lp_offset = ItemIdGetOffset(id);
195 lp_flags = ItemIdGetFlags(id);
196 lp_len = ItemIdGetLength(id);
197
198 values[0] = UInt16GetDatum(inter_call_data->offset);
199 values[1] = UInt16GetDatum(lp_offset);
200 values[2] = UInt16GetDatum(lp_flags);
201 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 if (ItemIdHasStorage(id) &&
209 lp_len >= MinHeapTupleSize &&
210 lp_offset == MAXALIGN(lp_offset) &&
211 lp_offset + lp_len <= raw_page_size)
212 {
213 HeapTupleHeader tuphdr;
214 bytea *tuple_data_bytea;
215 int tuple_data_len;
216
217 /* Extract information from the tuple header */
218
219 tuphdr = (HeapTupleHeader) PageGetItem(page, id);
220
223 /* shared with xvac */
225 values[7] = PointerGetDatum(&tuphdr->t_ctid);
226 values[8] = UInt32GetDatum(tuphdr->t_infomask2);
227 values[9] = UInt32GetDatum(tuphdr->t_infomask);
228 values[10] = UInt8GetDatum(tuphdr->t_hoff);
229
230 /* Copy raw tuple data into bytea attribute */
231 tuple_data_len = lp_len - tuphdr->t_hoff;
232 tuple_data_bytea = (bytea *) palloc(tuple_data_len + VARHDRSZ);
233 SET_VARSIZE(tuple_data_bytea, tuple_data_len + VARHDRSZ);
234 memcpy(VARDATA(tuple_data_bytea), (char *) tuphdr + tuphdr->t_hoff,
235 tuple_data_len);
236 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 if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
245 tuphdr->t_hoff <= lp_len &&
246 tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
247 {
248 if (tuphdr->t_infomask & HEAP_HASNULL)
249 {
250 int bits_len;
251
252 bits_len =
254 values[11] = CStringGetTextDatum(bits_to_text(tuphdr->t_bits, bits_len));
255 }
256 else
257 nulls[11] = true;
258
259 if (tuphdr->t_infomask & HEAP_HASOID_OLD)
260 values[12] = HeapTupleHeaderGetOidOld(tuphdr);
261 else
262 nulls[12] = true;
263 }
264 else
265 {
266 nulls[11] = true;
267 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 for (i = 4; i <= 13; i++)
279 nulls[i] = true;
280 }
281
282 /* Build and return the result tuple. */
283 resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
284 result = HeapTupleGetDatum(resultTuple);
285
286 inter_call_data->offset++;
287
288 SRF_RETURN_NEXT(fctx, result);
289 }
290 else
291 SRF_RETURN_DONE(fctx);
292}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
Pointer Page
Definition: bufpage.h:81
static Item PageGetItem(Page page, ItemId itemId)
Definition: bufpage.h:354
#define SizeOfPageHeaderData
Definition: bufpage.h:216
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:243
static OffsetNumber PageGetMaxOffsetNumber(Page page)
Definition: bufpage.h:372
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define MAXALIGN(LEN)
Definition: c.h:765
#define VARHDRSZ
Definition: c.h:646
uint16_t uint16
Definition: c.h:484
int errcode(int sqlerrcode)
Definition: elog.c:853
int errmsg(const char *fmt,...)
Definition: elog.c:1070
#define ERROR
Definition: elog.h:39
#define elog(elevel,...)
Definition: elog.h:225
#define ereport(elevel,...)
Definition: elog.h:149
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:335
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:276
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:304
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:308
@ TYPEFUNC_COMPOSITE
Definition: funcapi.h:149
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:310
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:306
static Datum HeapTupleGetDatum(const HeapTupleData *tuple)
Definition: funcapi.h:230
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:328
#define HeapTupleHeaderGetOidOld(tup)
Definition: heapfuncs.c:45
static char * bits_to_text(bits8 *bits, int len)
Definition: heapfuncs.c:61
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, const Datum *values, const bool *isnull)
Definition: heaptuple.c:1117
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:529
#define SizeofHeapTupleHeader
Definition: htup_details.h:185
#define MinHeapTupleSize
Definition: htup_details.h:559
#define HEAP_HASNULL
Definition: htup_details.h:190
#define BITMAPLEN(NATTS)
Definition: htup_details.h:545
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:304
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:371
#define HeapTupleHeaderGetRawCommandId(tup)
Definition: htup_details.h:387
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define ItemIdGetOffset(itemId)
Definition: itemid.h:65
#define ItemIdGetFlags(itemId)
Definition: itemid.h:71
#define ItemIdHasStorage(itemId)
Definition: itemid.h:120
#define FirstOffsetNumber
Definition: off.h:27
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:124
#define BITS_PER_BYTE
static Datum PointerGetDatum(const void *X)
Definition: postgres.h:322
uintptr_t Datum
Definition: postgres.h:64
static Datum UInt16GetDatum(uint16 X)
Definition: postgres.h:192
static Datum UInt8GetDatum(uint8 X)
Definition: postgres.h:152
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:232
void * user_fctx
Definition: funcapi.h:82
uint64 max_calls
Definition: funcapi.h:74
uint64 call_cntr
Definition: funcapi.h:65
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:101
ItemPointerData t_ctid
Definition: htup_details.h:161
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:178
Definition: c.h:641
bool superuser(void)
Definition: superuser.c:46
#define VARDATA(PTR)
Definition: varatt.h:278
#define SET_VARSIZE(PTR, len)
Definition: varatt.h:305
#define VARSIZE(PTR)
Definition: varatt.h:279

References BITMAPLEN, BITS_PER_BYTE, bits_to_text(), FuncCallContext::call_cntr, CStringGetTextDatum, elog, ereport, errcode(), errmsg(), ERROR, FirstOffsetNumber, get_call_result_type(), heap_form_tuple(), HEAP_HASNULL, HEAP_HASOID_OLD, HeapTupleGetDatum(), HeapTupleHeaderGetNatts, HeapTupleHeaderGetOidOld, HeapTupleHeaderGetRawCommandId, HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetRawXmin, i, ItemIdGetFlags, ItemIdGetLength, ItemIdGetOffset, ItemIdHasStorage, FuncCallContext::max_calls, MAXALIGN, MemoryContextSwitchTo(), MinHeapTupleSize, FuncCallContext::multi_call_memory_ctx, heap_page_items_state::offset, heap_page_items_state::page, PageGetItem(), PageGetItemId(), PageGetMaxOffsetNumber(), palloc(), PG_GETARG_BYTEA_P, PointerGetDatum(), SET_VARSIZE, SizeofHeapTupleHeader, SizeOfPageHeaderData, SRF_FIRSTCALL_INIT, SRF_IS_FIRSTCALL, SRF_PERCALL_SETUP, SRF_RETURN_DONE, SRF_RETURN_NEXT, superuser(), HeapTupleHeaderData::t_bits, HeapTupleHeaderData::t_ctid, HeapTupleHeaderData::t_hoff, HeapTupleHeaderData::t_infomask, HeapTupleHeaderData::t_infomask2, heap_page_items_state::tupd, TYPEFUNC_COMPOSITE, UInt16GetDatum(), UInt32GetDatum(), UInt8GetDatum(), FuncCallContext::user_fctx, values, VARDATA, VARHDRSZ, and VARSIZE.

◆ heap_tuple_infomask_flags()

Datum heap_tuple_infomask_flags ( PG_FUNCTION_ARGS  )

Definition at line 510 of file heapfuncs.c.

511{
512#define HEAP_TUPLE_INFOMASK_COLS 2
514 bool nulls[HEAP_TUPLE_INFOMASK_COLS] = {0};
515 uint16 t_infomask = PG_GETARG_INT16(0);
516 uint16 t_infomask2 = PG_GETARG_INT16(1);
517 int cnt = 0;
518 ArrayType *a;
519 int bitcnt;
520 Datum *flags;
521 TupleDesc tupdesc;
522 HeapTuple tuple;
523
524 if (!superuser())
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 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
531 elog(ERROR, "return type must be a row type");
532
533 bitcnt = pg_popcount((const char *) &t_infomask, sizeof(uint16)) +
534 pg_popcount((const char *) &t_infomask2, sizeof(uint16));
535
536 /* If no flags, return a set of empty arrays */
537 if (bitcnt <= 0)
538 {
541 tuple = heap_form_tuple(tupdesc, values, nulls);
543 }
544
545 /* build set of raw flags */
546 flags = (Datum *) palloc0(sizeof(Datum) * bitcnt);
547
548 /* decode t_infomask */
549 if ((t_infomask & HEAP_HASNULL) != 0)
550 flags[cnt++] = CStringGetTextDatum("HEAP_HASNULL");
551 if ((t_infomask & HEAP_HASVARWIDTH) != 0)
552 flags[cnt++] = CStringGetTextDatum("HEAP_HASVARWIDTH");
553 if ((t_infomask & HEAP_HASEXTERNAL) != 0)
554 flags[cnt++] = CStringGetTextDatum("HEAP_HASEXTERNAL");
555 if ((t_infomask & HEAP_HASOID_OLD) != 0)
556 flags[cnt++] = CStringGetTextDatum("HEAP_HASOID_OLD");
557 if ((t_infomask & HEAP_XMAX_KEYSHR_LOCK) != 0)
558 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_KEYSHR_LOCK");
559 if ((t_infomask & HEAP_COMBOCID) != 0)
560 flags[cnt++] = CStringGetTextDatum("HEAP_COMBOCID");
561 if ((t_infomask & HEAP_XMAX_EXCL_LOCK) != 0)
562 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_EXCL_LOCK");
563 if ((t_infomask & HEAP_XMAX_LOCK_ONLY) != 0)
564 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_LOCK_ONLY");
565 if ((t_infomask & HEAP_XMIN_COMMITTED) != 0)
566 flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_COMMITTED");
567 if ((t_infomask & HEAP_XMIN_INVALID) != 0)
568 flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_INVALID");
569 if ((t_infomask & HEAP_XMAX_COMMITTED) != 0)
570 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_COMMITTED");
571 if ((t_infomask & HEAP_XMAX_INVALID) != 0)
572 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_INVALID");
573 if ((t_infomask & HEAP_XMAX_IS_MULTI) != 0)
574 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_IS_MULTI");
575 if ((t_infomask & HEAP_UPDATED) != 0)
576 flags[cnt++] = CStringGetTextDatum("HEAP_UPDATED");
577 if ((t_infomask & HEAP_MOVED_OFF) != 0)
578 flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_OFF");
579 if ((t_infomask & HEAP_MOVED_IN) != 0)
580 flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_IN");
581
582 /* decode t_infomask2 */
583 if ((t_infomask2 & HEAP_KEYS_UPDATED) != 0)
584 flags[cnt++] = CStringGetTextDatum("HEAP_KEYS_UPDATED");
585 if ((t_infomask2 & HEAP_HOT_UPDATED) != 0)
586 flags[cnt++] = CStringGetTextDatum("HEAP_HOT_UPDATED");
587 if ((t_infomask2 & HEAP_ONLY_TUPLE) != 0)
588 flags[cnt++] = CStringGetTextDatum("HEAP_ONLY_TUPLE");
589
590 /* build value */
591 Assert(cnt <= bitcnt);
592 a = construct_array_builtin(flags, cnt, TEXTOID);
594
595 /*
596 * Build set of combined flags. Use the same array as previously, this
597 * keeps the code simple.
598 */
599 cnt = 0;
600 MemSet(flags, 0, sizeof(Datum) * bitcnt);
601
602 /* decode combined masks of t_infomask */
603 if ((t_infomask & HEAP_XMAX_SHR_LOCK) == HEAP_XMAX_SHR_LOCK)
604 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_SHR_LOCK");
605 if ((t_infomask & HEAP_XMIN_FROZEN) == HEAP_XMIN_FROZEN)
606 flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_FROZEN");
607 if ((t_infomask & HEAP_MOVED) == HEAP_MOVED)
608 flags[cnt++] = CStringGetTextDatum("HEAP_MOVED");
609
610 /* Build an empty array if there are no combined flags */
611 if (cnt == 0)
612 a = construct_empty_array(TEXTOID);
613 else
614 a = construct_array_builtin(flags, cnt, TEXTOID);
615 pfree(flags);
617
618 /* Returns the record as Datum */
619 tuple = heap_form_tuple(tupdesc, values, nulls);
621}
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3580
ArrayType * construct_array_builtin(Datum *elems, int nelems, Oid elmtype)
Definition: arrayfuncs.c:3381
#define Assert(condition)
Definition: c.h:812
#define MemSet(start, val, len)
Definition: c.h:974
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
#define HEAP_TUPLE_INFOMASK_COLS
#define HEAP_HASVARWIDTH
Definition: htup_details.h:191
#define HEAP_MOVED_OFF
Definition: htup_details.h:211
#define HEAP_XMAX_SHR_LOCK
Definition: htup_details.h:200
#define HEAP_XMIN_FROZEN
Definition: htup_details.h:206
#define HEAP_XMIN_COMMITTED
Definition: htup_details.h:204
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:275
#define HEAP_HOT_UPDATED
Definition: htup_details.h:276
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:197
#define HEAP_MOVED_IN
Definition: htup_details.h:212
#define HEAP_MOVED
Definition: htup_details.h:213
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:209
#define HEAP_XMAX_COMMITTED
Definition: htup_details.h:207
#define HEAP_COMBOCID
Definition: htup_details.h:195
#define HEAP_XMIN_INVALID
Definition: htup_details.h:205
#define HEAP_HASEXTERNAL
Definition: htup_details.h:192
#define HEAP_XMAX_EXCL_LOCK
Definition: htup_details.h:196
#define HEAP_XMAX_INVALID
Definition: htup_details.h:208
#define HEAP_UPDATED
Definition: htup_details.h:210
#define HEAP_XMAX_KEYSHR_LOCK
Definition: htup_details.h:194
#define HEAP_ONLY_TUPLE
Definition: htup_details.h:277
int a
Definition: isn.c:68
void pfree(void *pointer)
Definition: mcxt.c:1521
void * palloc0(Size size)
Definition: mcxt.c:1347
static uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.h:339

References a, Assert, construct_array_builtin(), construct_empty_array(), CStringGetTextDatum, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), HEAP_COMBOCID, heap_form_tuple(), HEAP_HASEXTERNAL, HEAP_HASNULL, HEAP_HASOID_OLD, HEAP_HASVARWIDTH, HEAP_HOT_UPDATED, HEAP_KEYS_UPDATED, HEAP_MOVED, HEAP_MOVED_IN, HEAP_MOVED_OFF, HEAP_ONLY_TUPLE, HEAP_TUPLE_INFOMASK_COLS, HEAP_UPDATED, HEAP_XMAX_COMMITTED, HEAP_XMAX_EXCL_LOCK, HEAP_XMAX_INVALID, HEAP_XMAX_IS_MULTI, HEAP_XMAX_KEYSHR_LOCK, HEAP_XMAX_LOCK_ONLY, HEAP_XMAX_SHR_LOCK, HEAP_XMIN_COMMITTED, HEAP_XMIN_FROZEN, HEAP_XMIN_INVALID, HeapTupleGetDatum(), MemSet, palloc0(), pfree(), PG_GETARG_INT16, pg_popcount(), PG_RETURN_DATUM, PointerGetDatum(), superuser(), TYPEFUNC_COMPOSITE, and values.

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( heap_page_items  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( heap_tuple_infomask_flags  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( tuple_data_split  )

◆ text_to_bits()

static bits8 * text_to_bits ( char *  str,
int  len 
)
static

Definition at line 84 of file heapfuncs.c.

85{
86 bits8 *bits;
87 int off = 0;
88 char byte = 0;
89
90 bits = palloc(len + 1);
91
92 while (off < len)
93 {
94 if (off % 8 == 0)
95 byte = 0;
96
97 if ((str[off] == '0') || (str[off] == '1'))
98 byte = byte | ((str[off] - '0') << off % 8);
99 else
102 errmsg("invalid character \"%.*s\" in t_bits string",
103 pg_mblen(str + off), str + off)));
104
105 if (off % 8 == 7)
106 bits[off / 8] = byte;
107
108 off++;
109 }
110
111 return bits;
112}
uint8 bits8
Definition: c.h:492
int pg_mblen(const char *mbstr)
Definition: mbutils.c:1023
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:41

References ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), ERROR, len, palloc(), pg_mblen(), and str.

Referenced by tuple_data_split().

◆ tuple_data_split()

Datum tuple_data_split ( PG_FUNCTION_ARGS  )

Definition at line 426 of file heapfuncs.c.

427{
428 Oid relid;
429 bytea *raw_data;
430 uint16 t_infomask;
431 uint16 t_infomask2;
432 char *t_bits_str;
433 bool do_detoast = false;
434 bits8 *t_bits = NULL;
435 Datum res;
436
437 relid = PG_GETARG_OID(0);
438 raw_data = PG_ARGISNULL(1) ? NULL : PG_GETARG_BYTEA_P(1);
439 t_infomask = PG_GETARG_INT16(2);
440 t_infomask2 = PG_GETARG_INT16(3);
441 t_bits_str = PG_ARGISNULL(4) ? NULL :
443
444 if (PG_NARGS() >= 6)
445 do_detoast = PG_GETARG_BOOL(5);
446
447 if (!superuser())
449 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
450 errmsg("must be superuser to use raw page functions")));
451
452 if (!raw_data)
454
455 /*
456 * Convert t_bits string back to the bits8 array as represented in the
457 * tuple header.
458 */
459 if (t_infomask & HEAP_HASNULL)
460 {
461 size_t bits_str_len;
462 size_t bits_len;
463
464 bits_len = BITMAPLEN(t_infomask2 & HEAP_NATTS_MASK) * BITS_PER_BYTE;
465 if (!t_bits_str)
468 errmsg("t_bits string must not be NULL")));
469
470 bits_str_len = strlen(t_bits_str);
471 if (bits_len != bits_str_len)
474 errmsg("unexpected length of t_bits string: %zu, expected %zu",
475 bits_str_len, bits_len)));
476
477 /* do the conversion */
478 t_bits = text_to_bits(t_bits_str, bits_str_len);
479 }
480 else
481 {
482 if (t_bits_str)
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 res = tuple_data_split_internal(relid, (char *) raw_data + VARHDRSZ,
491 VARSIZE(raw_data) - VARHDRSZ,
492 t_infomask, t_infomask2, t_bits,
493 do_detoast);
494
495 if (t_bits)
496 pfree(t_bits);
497
499}
#define PG_GETARG_OID(n)
Definition: fmgr.h:275
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:309
#define PG_ARGISNULL(n)
Definition: fmgr.h:209
#define PG_NARGS()
Definition: fmgr.h:203
#define PG_RETURN_NULL()
Definition: fmgr.h:345
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:274
static Datum tuple_data_split_internal(Oid relid, char *tupdata, uint16 tupdata_len, uint16 t_infomask, uint16 t_infomask2, bits8 *t_bits, bool do_detoast)
Definition: heapfuncs.c:303
static bits8 * text_to_bits(char *str, int len)
Definition: heapfuncs.c:84
#define HEAP_NATTS_MASK
Definition: htup_details.h:273
char * text_to_cstring(const text *t)
Definition: varlena.c:217

References BITMAPLEN, BITS_PER_BYTE, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), ERROR, HEAP_HASNULL, HEAP_NATTS_MASK, pfree(), PG_ARGISNULL, PG_GETARG_BOOL, PG_GETARG_BYTEA_P, PG_GETARG_INT16, PG_GETARG_OID, PG_GETARG_TEXT_PP, PG_NARGS, PG_RETURN_DATUM, PG_RETURN_NULL, res, superuser(), text_to_bits(), text_to_cstring(), tuple_data_split_internal(), VARHDRSZ, and VARSIZE.

◆ tuple_data_split_internal()

static Datum tuple_data_split_internal ( Oid  relid,
char *  tupdata,
uint16  tupdata_len,
uint16  t_infomask,
uint16  t_infomask2,
bits8 t_bits,
bool  do_detoast 
)
static

Definition at line 303 of file heapfuncs.c.

307{
308 ArrayBuildState *raw_attrs;
309 int nattrs;
310 int i;
311 int off = 0;
312 Relation rel;
313 TupleDesc tupdesc;
314
315 /* Get tuple descriptor from relation OID */
316 rel = relation_open(relid, AccessShareLock);
317 tupdesc = RelationGetDescr(rel);
318
319 raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
320 nattrs = tupdesc->natts;
321
322 /*
323 * Sequences always use heap AM, but they don't show that in the catalogs.
324 */
325 if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
326 rel->rd_rel->relam != HEAP_TABLE_AM_OID)
327 ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
328 errmsg("only heap AM is supported")));
329
330 if (nattrs < (t_infomask2 & HEAP_NATTS_MASK))
333 errmsg("number of attributes in tuple header is greater than number of attributes in tuple descriptor")));
334
335 for (i = 0; i < nattrs; i++)
336 {
337 CompactAttribute *attr;
338 bool is_null;
339 bytea *attr_data = NULL;
340
341 attr = TupleDescCompactAttr(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 if (i >= (t_infomask2 & HEAP_NATTS_MASK))
350 is_null = true;
351 else
352 is_null = (t_infomask & HEAP_HASNULL) && att_isnull(i, t_bits);
353
354 if (!is_null)
355 {
356 int len;
357
358 if (attr->attlen == -1)
359 {
360 off = att_pointer_alignby(off, attr->attalignby, -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 if (VARATT_IS_EXTERNAL(tupdata + off) &&
369 !VARATT_IS_EXTERNAL_ONDISK(tupdata + off) &&
370 !VARATT_IS_EXTERNAL_INDIRECT(tupdata + off))
373 errmsg("first byte of varlena attribute is incorrect for attribute %d", i)));
374
375 len = VARSIZE_ANY(tupdata + off);
376 }
377 else
378 {
379 off = att_nominal_alignby(off, attr->attalignby);
380 len = attr->attlen;
381 }
382
383 if (tupdata_len < off + len)
386 errmsg("unexpected end of tuple data")));
387
388 if (attr->attlen == -1 && do_detoast)
389 attr_data = pg_detoast_datum_copy((struct varlena *) (tupdata + off));
390 else
391 {
392 attr_data = (bytea *) palloc(len + VARHDRSZ);
393 SET_VARSIZE(attr_data, len + VARHDRSZ);
394 memcpy(VARDATA(attr_data), tupdata + off, len);
395 }
396
397 off = att_addlength_pointer(off, attr->attlen,
398 tupdata + off);
399 }
400
401 raw_attrs = accumArrayResult(raw_attrs, PointerGetDatum(attr_data),
402 is_null, BYTEAOID, CurrentMemoryContext);
403 if (attr_data)
404 pfree(attr_data);
405 }
406
407 if (tupdata_len != off)
410 errmsg("end of tuple reached without looking at all its data")));
411
413
414 return makeArrayResult(raw_attrs, CurrentMemoryContext);
415}
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5350
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5293
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5420
struct varlena * pg_detoast_datum_copy(struct varlena *datum)
Definition: fmgr.c:1841
#define AccessShareLock
Definition: lockdefs.h:36
MemoryContext CurrentMemoryContext
Definition: mcxt.c:143
#define RelationGetDescr(relation)
Definition: rel.h:531
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:205
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:47
uint8 attalignby
Definition: tupdesc.h:78
int16 attlen
Definition: tupdesc.h:69
Form_pg_class rd_rel
Definition: rel.h:111
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:169
#define att_nominal_alignby(cur_offset, attalignby)
Definition: tupmacs.h:165
static bool att_isnull(int ATT, const bits8 *BITS)
Definition: tupmacs.h:26
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:185
#define att_pointer_alignby(cur_offset, attalignby, attlen, attptr)
Definition: tupmacs.h:129
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: varatt.h:290
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: varatt.h:292
#define VARSIZE_ANY(PTR)
Definition: varatt.h:311
#define VARATT_IS_EXTERNAL(PTR)
Definition: varatt.h:289

References AccessShareLock, accumArrayResult(), att_addlength_pointer, att_isnull(), att_nominal_alignby, att_pointer_alignby, CompactAttribute::attalignby, CompactAttribute::attlen, CurrentMemoryContext, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), ERROR, HEAP_HASNULL, HEAP_NATTS_MASK, i, initArrayResult(), len, makeArrayResult(), TupleDescData::natts, palloc(), pfree(), pg_detoast_datum_copy(), PointerGetDatum(), RelationData::rd_rel, relation_close(), relation_open(), RelationGetDescr, SET_VARSIZE, TupleDescCompactAttr(), VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARDATA, VARHDRSZ, and VARSIZE_ANY.

Referenced by tuple_data_split().