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 HEAP_TUPLE_INFOMASK_COLS   2
 

Typedefs

typedef struct heap_page_items_state heap_page_items_state
 

Functions

static Oid HeapTupleHeaderGetOidOld (const HeapTupleHeaderData *tup)
 
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

Typedef Documentation

◆ heap_page_items_state

Function Documentation

◆ bits_to_text()

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

Definition at line 62 of file heapfuncs.c.

63{
64 int i;
65 char *str;
66
67 str = palloc(len + 1);
68
69 for (i = 0; i < len; i++)
70 str[i] = (bits[(i / 8)] & (1 << (i % 8))) ? '1' : '0';
71
72 str[i] = '\0';
73
74 return str;
75}
const char * str
int i
Definition: isn.c:77
void * palloc(Size size)
Definition: mcxt.c:1943
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 130 of file heapfuncs.c.

131{
132 bytea *raw_page = PG_GETARG_BYTEA_P(0);
133 heap_page_items_state *inter_call_data = NULL;
134 FuncCallContext *fctx;
135 int raw_page_size;
136
137 if (!superuser())
139 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
140 errmsg("must be superuser to use raw page functions")));
141
142 raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
143
144 if (SRF_IS_FIRSTCALL())
145 {
146 TupleDesc tupdesc;
147 MemoryContext mctx;
148
149 if (raw_page_size < SizeOfPageHeaderData)
151 (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
152 errmsg("input page too small (%d bytes)", raw_page_size)));
153
154 fctx = SRF_FIRSTCALL_INIT();
156
157 inter_call_data = palloc(sizeof(heap_page_items_state));
158
159 /* Build a tuple descriptor for our result type */
160 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
161 elog(ERROR, "return type must be a row type");
162
163 inter_call_data->tupd = tupdesc;
164
165 inter_call_data->offset = FirstOffsetNumber;
166 inter_call_data->page = VARDATA(raw_page);
167
168 fctx->max_calls = PageGetMaxOffsetNumber(inter_call_data->page);
169 fctx->user_fctx = inter_call_data;
170
172 }
173
174 fctx = SRF_PERCALL_SETUP();
175 inter_call_data = fctx->user_fctx;
176
177 if (fctx->call_cntr < fctx->max_calls)
178 {
179 Page page = inter_call_data->page;
180 HeapTuple resultTuple;
181 Datum result;
182 ItemId id;
183 Datum values[14];
184 bool nulls[14];
185 uint16 lp_offset;
186 uint16 lp_flags;
187 uint16 lp_len;
188
189 memset(nulls, 0, sizeof(nulls));
190
191 /* Extract information from the line pointer */
192
193 id = PageGetItemId(page, inter_call_data->offset);
194
195 lp_offset = ItemIdGetOffset(id);
196 lp_flags = ItemIdGetFlags(id);
197 lp_len = ItemIdGetLength(id);
198
199 values[0] = UInt16GetDatum(inter_call_data->offset);
200 values[1] = UInt16GetDatum(lp_offset);
201 values[2] = UInt16GetDatum(lp_flags);
202 values[3] = UInt16GetDatum(lp_len);
203
204 /*
205 * We do just enough validity checking to make sure we don't reference
206 * data outside the page passed to us. The page could be corrupt in
207 * many other ways, but at least we won't crash.
208 */
209 if (ItemIdHasStorage(id) &&
210 lp_len >= MinHeapTupleSize &&
211 lp_offset == MAXALIGN(lp_offset) &&
212 lp_offset + lp_len <= raw_page_size)
213 {
214 HeapTupleHeader tuphdr;
215
216 /* Extract information from the tuple header */
217 tuphdr = (HeapTupleHeader) PageGetItem(page, id);
218
221 /* shared with xvac */
223 values[7] = PointerGetDatum(&tuphdr->t_ctid);
224 values[8] = UInt32GetDatum(tuphdr->t_infomask2);
225 values[9] = UInt32GetDatum(tuphdr->t_infomask);
226 values[10] = UInt8GetDatum(tuphdr->t_hoff);
227
228 /*
229 * We already checked that the item is completely within the raw
230 * page passed to us, with the length given in the line pointer.
231 * But t_hoff could be out of range, so check it before relying on
232 * it to fetch additional info.
233 */
234 if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
235 tuphdr->t_hoff <= lp_len &&
236 tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
237 {
238 int tuple_data_len;
239 bytea *tuple_data_bytea;
240
241 /* Copy null bitmask and OID, if present */
242 if (tuphdr->t_infomask & HEAP_HASNULL)
243 {
244 int bitmaplen;
245
246 bitmaplen = BITMAPLEN(HeapTupleHeaderGetNatts(tuphdr));
247 /* better range-check the attribute count, too */
248 if (bitmaplen <= tuphdr->t_hoff - SizeofHeapTupleHeader)
249 values[11] =
251 bitmaplen * BITS_PER_BYTE));
252 else
253 nulls[11] = true;
254 }
255 else
256 nulls[11] = true;
257
258 if (tuphdr->t_infomask & HEAP_HASOID_OLD)
259 values[12] = HeapTupleHeaderGetOidOld(tuphdr);
260 else
261 nulls[12] = true;
262
263 /* Copy raw tuple data into bytea attribute */
264 tuple_data_len = lp_len - tuphdr->t_hoff;
265 tuple_data_bytea = (bytea *) palloc(tuple_data_len + VARHDRSZ);
266 SET_VARSIZE(tuple_data_bytea, tuple_data_len + VARHDRSZ);
267 if (tuple_data_len > 0)
268 memcpy(VARDATA(tuple_data_bytea),
269 (char *) tuphdr + tuphdr->t_hoff,
270 tuple_data_len);
271 values[13] = PointerGetDatum(tuple_data_bytea);
272 }
273 else
274 {
275 nulls[11] = true;
276 nulls[12] = true;
277 nulls[13] = true;
278 }
279 }
280 else
281 {
282 /*
283 * The line pointer is not used, or it's invalid. Set the rest of
284 * the fields to NULL
285 */
286 int i;
287
288 for (i = 4; i <= 13; i++)
289 nulls[i] = true;
290 }
291
292 /* Build and return the result tuple. */
293 resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
294 result = HeapTupleGetDatum(resultTuple);
295
296 inter_call_data->offset++;
297
298 SRF_RETURN_NEXT(fctx, result);
299 }
300 else
301 SRF_RETURN_DONE(fctx);
302}
static Datum values[MAXATTR]
Definition: bootstrap.c:151
static Item PageGetItem(const PageData *page, const ItemIdData *itemId)
Definition: bufpage.h:354
#define SizeOfPageHeaderData
Definition: bufpage.h:217
static ItemId PageGetItemId(Page page, OffsetNumber offsetNumber)
Definition: bufpage.h:244
PageData * Page
Definition: bufpage.h:82
static OffsetNumber PageGetMaxOffsetNumber(const PageData *page)
Definition: bufpage.h:372
#define CStringGetTextDatum(s)
Definition: builtins.h:97
#define MAXALIGN(LEN)
Definition: c.h:782
#define VARHDRSZ
Definition: c.h:663
uint16_t uint16
Definition: c.h:501
int errcode(int sqlerrcode)
Definition: elog.c:854
int errmsg(const char *fmt,...)
Definition: elog.c:1071
#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
static char * bits_to_text(bits8 *bits, int len)
Definition: heapfuncs.c:62
static Oid HeapTupleHeaderGetOidOld(const HeapTupleHeaderData *tup)
Definition: heapfuncs.c:46
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:577
#define SizeofHeapTupleHeader
Definition: htup_details.h:185
#define MinHeapTupleSize
Definition: htup_details.h:611
#define HEAP_HASNULL
Definition: htup_details.h:190
static int BITMAPLEN(int NATTS)
Definition: htup_details.h:594
static CommandId HeapTupleHeaderGetRawCommandId(const HeapTupleHeaderData *tup)
Definition: htup_details.h:415
#define HEAP_HASOID_OLD
Definition: htup_details.h:193
static TransactionId HeapTupleHeaderGetRawXmax(const HeapTupleHeaderData *tup)
Definition: htup_details.h:377
static TransactionId HeapTupleHeaderGetRawXmin(const HeapTupleHeaderData *tup)
Definition: htup_details.h:318
#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:327
uintptr_t Datum
Definition: postgres.h:69
static Datum UInt16GetDatum(uint16 X)
Definition: postgres.h:197
static Datum UInt8GetDatum(uint8 X)
Definition: postgres.h:157
static Datum UInt32GetDatum(uint32 X)
Definition: postgres.h:237
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:658
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 520 of file heapfuncs.c.

521{
522#define HEAP_TUPLE_INFOMASK_COLS 2
524 bool nulls[HEAP_TUPLE_INFOMASK_COLS] = {0};
525 uint16 t_infomask = PG_GETARG_INT16(0);
526 uint16 t_infomask2 = PG_GETARG_INT16(1);
527 int cnt = 0;
528 ArrayType *a;
529 int bitcnt;
530 Datum *flags;
531 TupleDesc tupdesc;
532 HeapTuple tuple;
533
534 if (!superuser())
536 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
537 errmsg("must be superuser to use raw page functions")));
538
539 /* Build a tuple descriptor for our result type */
540 if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
541 elog(ERROR, "return type must be a row type");
542
543 bitcnt = pg_popcount((const char *) &t_infomask, sizeof(uint16)) +
544 pg_popcount((const char *) &t_infomask2, sizeof(uint16));
545
546 /* If no flags, return a set of empty arrays */
547 if (bitcnt <= 0)
548 {
551 tuple = heap_form_tuple(tupdesc, values, nulls);
553 }
554
555 /* build set of raw flags */
556 flags = (Datum *) palloc0(sizeof(Datum) * bitcnt);
557
558 /* decode t_infomask */
559 if ((t_infomask & HEAP_HASNULL) != 0)
560 flags[cnt++] = CStringGetTextDatum("HEAP_HASNULL");
561 if ((t_infomask & HEAP_HASVARWIDTH) != 0)
562 flags[cnt++] = CStringGetTextDatum("HEAP_HASVARWIDTH");
563 if ((t_infomask & HEAP_HASEXTERNAL) != 0)
564 flags[cnt++] = CStringGetTextDatum("HEAP_HASEXTERNAL");
565 if ((t_infomask & HEAP_HASOID_OLD) != 0)
566 flags[cnt++] = CStringGetTextDatum("HEAP_HASOID_OLD");
567 if ((t_infomask & HEAP_XMAX_KEYSHR_LOCK) != 0)
568 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_KEYSHR_LOCK");
569 if ((t_infomask & HEAP_COMBOCID) != 0)
570 flags[cnt++] = CStringGetTextDatum("HEAP_COMBOCID");
571 if ((t_infomask & HEAP_XMAX_EXCL_LOCK) != 0)
572 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_EXCL_LOCK");
573 if ((t_infomask & HEAP_XMAX_LOCK_ONLY) != 0)
574 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_LOCK_ONLY");
575 if ((t_infomask & HEAP_XMIN_COMMITTED) != 0)
576 flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_COMMITTED");
577 if ((t_infomask & HEAP_XMIN_INVALID) != 0)
578 flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_INVALID");
579 if ((t_infomask & HEAP_XMAX_COMMITTED) != 0)
580 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_COMMITTED");
581 if ((t_infomask & HEAP_XMAX_INVALID) != 0)
582 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_INVALID");
583 if ((t_infomask & HEAP_XMAX_IS_MULTI) != 0)
584 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_IS_MULTI");
585 if ((t_infomask & HEAP_UPDATED) != 0)
586 flags[cnt++] = CStringGetTextDatum("HEAP_UPDATED");
587 if ((t_infomask & HEAP_MOVED_OFF) != 0)
588 flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_OFF");
589 if ((t_infomask & HEAP_MOVED_IN) != 0)
590 flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_IN");
591
592 /* decode t_infomask2 */
593 if ((t_infomask2 & HEAP_KEYS_UPDATED) != 0)
594 flags[cnt++] = CStringGetTextDatum("HEAP_KEYS_UPDATED");
595 if ((t_infomask2 & HEAP_HOT_UPDATED) != 0)
596 flags[cnt++] = CStringGetTextDatum("HEAP_HOT_UPDATED");
597 if ((t_infomask2 & HEAP_ONLY_TUPLE) != 0)
598 flags[cnt++] = CStringGetTextDatum("HEAP_ONLY_TUPLE");
599
600 /* build value */
601 Assert(cnt <= bitcnt);
602 a = construct_array_builtin(flags, cnt, TEXTOID);
604
605 /*
606 * Build set of combined flags. Use the same array as previously, this
607 * keeps the code simple.
608 */
609 cnt = 0;
610 MemSet(flags, 0, sizeof(Datum) * bitcnt);
611
612 /* decode combined masks of t_infomask */
613 if ((t_infomask & HEAP_XMAX_SHR_LOCK) == HEAP_XMAX_SHR_LOCK)
614 flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_SHR_LOCK");
615 if ((t_infomask & HEAP_XMIN_FROZEN) == HEAP_XMIN_FROZEN)
616 flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_FROZEN");
617 if ((t_infomask & HEAP_MOVED) == HEAP_MOVED)
618 flags[cnt++] = CStringGetTextDatum("HEAP_MOVED");
619
620 /* Build an empty array if there are no combined flags */
621 if (cnt == 0)
622 a = construct_empty_array(TEXTOID);
623 else
624 a = construct_array_builtin(flags, cnt, TEXTOID);
625 pfree(flags);
627
628 /* Returns the record as Datum */
629 tuple = heap_form_tuple(tupdesc, values, nulls);
631}
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 MemSet(start, val, len)
Definition: c.h:991
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:353
#define PG_GETARG_INT16(n)
Definition: fmgr.h:271
Assert(PointerIsAligned(start, uint64))
#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:289
#define HEAP_HOT_UPDATED
Definition: htup_details.h:290
#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:291
int a
Definition: isn.c:73
void pfree(void *pointer)
Definition: mcxt.c:2150
void * palloc0(Size size)
Definition: mcxt.c:1973
static uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.h:363

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.

◆ HeapTupleHeaderGetOidOld()

static Oid HeapTupleHeaderGetOidOld ( const HeapTupleHeaderData tup)
inlinestatic

Definition at line 46 of file heapfuncs.c.

47{
48 if (tup->t_infomask & HEAP_HASOID_OLD)
49 return *((Oid *) ((char *) (tup) + (tup)->t_hoff - sizeof(Oid)));
50 else
51 return InvalidOid;
52}
#define InvalidOid
Definition: postgres_ext.h:35
unsigned int Oid
Definition: postgres_ext.h:30

References HEAP_HASOID_OLD, InvalidOid, and HeapTupleHeaderData::t_infomask.

Referenced by heap_page_items().

◆ 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 85 of file heapfuncs.c.

86{
87 bits8 *bits;
88 int off = 0;
89 char byte = 0;
90
91 bits = palloc(len + 1);
92
93 while (off < len)
94 {
95 if (off % 8 == 0)
96 byte = 0;
97
98 if ((str[off] == '0') || (str[off] == '1'))
99 byte = byte | ((str[off] - '0') << off % 8);
100 else
103 errmsg("invalid character \"%.*s\" in t_bits string",
104 pg_mblen(str + off), str + off)));
105
106 if (off % 8 == 7)
107 bits[off / 8] = byte;
108
109 off++;
110 }
111
112 return bits;
113}
uint8 bits8
Definition: c.h:509
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 436 of file heapfuncs.c.

437{
438 Oid relid;
439 bytea *raw_data;
440 uint16 t_infomask;
441 uint16 t_infomask2;
442 char *t_bits_str;
443 bool do_detoast = false;
444 bits8 *t_bits = NULL;
445 Datum res;
446
447 relid = PG_GETARG_OID(0);
448 raw_data = PG_ARGISNULL(1) ? NULL : PG_GETARG_BYTEA_P(1);
449 t_infomask = PG_GETARG_INT16(2);
450 t_infomask2 = PG_GETARG_INT16(3);
451 t_bits_str = PG_ARGISNULL(4) ? NULL :
453
454 if (PG_NARGS() >= 6)
455 do_detoast = PG_GETARG_BOOL(5);
456
457 if (!superuser())
459 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
460 errmsg("must be superuser to use raw page functions")));
461
462 if (!raw_data)
464
465 /*
466 * Convert t_bits string back to the bits8 array as represented in the
467 * tuple header.
468 */
469 if (t_infomask & HEAP_HASNULL)
470 {
471 size_t bits_str_len;
472 size_t bits_len;
473
474 bits_len = BITMAPLEN(t_infomask2 & HEAP_NATTS_MASK) * BITS_PER_BYTE;
475 if (!t_bits_str)
478 errmsg("t_bits string must not be NULL")));
479
480 bits_str_len = strlen(t_bits_str);
481 if (bits_len != bits_str_len)
484 errmsg("unexpected length of t_bits string: %zu, expected %zu",
485 bits_str_len, bits_len)));
486
487 /* do the conversion */
488 t_bits = text_to_bits(t_bits_str, bits_str_len);
489 }
490 else
491 {
492 if (t_bits_str)
495 errmsg("t_bits string is expected to be NULL, but instead it is %zu bytes long",
496 strlen(t_bits_str))));
497 }
498
499 /* Split tuple data */
500 res = tuple_data_split_internal(relid, (char *) raw_data + VARHDRSZ,
501 VARSIZE(raw_data) - VARHDRSZ,
502 t_infomask, t_infomask2, t_bits,
503 do_detoast);
504
505 if (t_bits)
506 pfree(t_bits);
507
508 PG_RETURN_DATUM(res);
509}
#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:313
static bits8 * text_to_bits(char *str, int len)
Definition: heapfuncs.c:85
#define HEAP_NATTS_MASK
Definition: htup_details.h:287
char * text_to_cstring(const text *t)
Definition: varlena.c:225

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, 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 313 of file heapfuncs.c.

317{
318 ArrayBuildState *raw_attrs;
319 int nattrs;
320 int i;
321 int off = 0;
322 Relation rel;
323 TupleDesc tupdesc;
324
325 /* Get tuple descriptor from relation OID */
326 rel = relation_open(relid, AccessShareLock);
327 tupdesc = RelationGetDescr(rel);
328
329 raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
330 nattrs = tupdesc->natts;
331
332 /*
333 * Sequences always use heap AM, but they don't show that in the catalogs.
334 */
335 if (rel->rd_rel->relkind != RELKIND_SEQUENCE &&
336 rel->rd_rel->relam != HEAP_TABLE_AM_OID)
337 ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
338 errmsg("only heap AM is supported")));
339
340 if (nattrs < (t_infomask2 & HEAP_NATTS_MASK))
343 errmsg("number of attributes in tuple header is greater than number of attributes in tuple descriptor")));
344
345 for (i = 0; i < nattrs; i++)
346 {
347 CompactAttribute *attr;
348 bool is_null;
349 bytea *attr_data = NULL;
350
351 attr = TupleDescCompactAttr(tupdesc, i);
352
353 /*
354 * Tuple header can specify fewer attributes than tuple descriptor as
355 * ALTER TABLE ADD COLUMN without DEFAULT keyword does not actually
356 * change tuples in pages, so attributes with numbers greater than
357 * (t_infomask2 & HEAP_NATTS_MASK) should be treated as NULL.
358 */
359 if (i >= (t_infomask2 & HEAP_NATTS_MASK))
360 is_null = true;
361 else
362 is_null = (t_infomask & HEAP_HASNULL) && att_isnull(i, t_bits);
363
364 if (!is_null)
365 {
366 int len;
367
368 if (attr->attlen == -1)
369 {
370 off = att_pointer_alignby(off, attr->attalignby, -1,
371 tupdata + off);
372
373 /*
374 * As VARSIZE_ANY throws an exception if it can't properly
375 * detect the type of external storage in macros VARTAG_SIZE,
376 * this check is repeated to have a nicer error handling.
377 */
378 if (VARATT_IS_EXTERNAL(tupdata + off) &&
379 !VARATT_IS_EXTERNAL_ONDISK(tupdata + off) &&
380 !VARATT_IS_EXTERNAL_INDIRECT(tupdata + off))
383 errmsg("first byte of varlena attribute is incorrect for attribute %d", i)));
384
385 len = VARSIZE_ANY(tupdata + off);
386 }
387 else
388 {
389 off = att_nominal_alignby(off, attr->attalignby);
390 len = attr->attlen;
391 }
392
393 if (tupdata_len < off + len)
396 errmsg("unexpected end of tuple data")));
397
398 if (attr->attlen == -1 && do_detoast)
399 attr_data = pg_detoast_datum_copy((struct varlena *) (tupdata + off));
400 else
401 {
402 attr_data = (bytea *) palloc(len + VARHDRSZ);
403 SET_VARSIZE(attr_data, len + VARHDRSZ);
404 memcpy(VARDATA(attr_data), tupdata + off, len);
405 }
406
407 off = att_addlength_pointer(off, attr->attlen,
408 tupdata + off);
409 }
410
411 raw_attrs = accumArrayResult(raw_attrs, PointerGetDatum(attr_data),
412 is_null, BYTEAOID, CurrentMemoryContext);
413 if (attr_data)
414 pfree(attr_data);
415 }
416
417 if (tupdata_len != off)
420 errmsg("end of tuple reached without looking at all its data")));
421
423
424 return makeArrayResult(raw_attrs, CurrentMemoryContext);
425}
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:159
#define RelationGetDescr(relation)
Definition: rel.h:542
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:80
int16 attlen
Definition: tupdesc.h:71
Form_pg_class rd_rel
Definition: rel.h:111
static CompactAttribute * TupleDescCompactAttr(TupleDesc tupdesc, int i)
Definition: tupdesc.h:175
#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().