PostgreSQL Source Code  git master
heapfuncs.c File Reference
#include "postgres.h"
#include "pageinspect.h"
#include "access/htup_details.h"
#include "access/relation.h"
#include "funcapi.h"
#include "catalog/pg_am_d.h"
#include "catalog/pg_type.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))) \
: \
)
unsigned int Oid
Definition: postgres_ext.h:31
#define InvalidOid
Definition: postgres_ext.h:36
#define HEAP_HASOID_OLD
Definition: htup_details.h:192

Definition at line 46 of file heapfuncs.c.

Referenced by heap_page_items().

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.

References i, palloc(), and generate_unaccent_rules::str.

Referenced by heap_page_items().

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 }
void * palloc(Size size)
Definition: mcxt.c:949
int i

◆ heap_page_items()

Datum heap_page_items ( PG_FUNCTION_ARGS  )

Definition at line 129 of file heapfuncs.c.

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.

Referenced by text_to_bits().

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())
137  ereport(ERROR,
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)
149  ereport(ERROR,
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 
170  MemoryContextSwitchTo(mctx);
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 
221  values[4] = UInt32GetDatum(HeapTupleHeaderGetRawXmin(tuphdr));
222  values[5] = UInt32GetDatum(HeapTupleHeaderGetRawXmax(tuphdr));
223  /* shared with xvac */
224  values[6] = UInt32GetDatum(HeapTupleHeaderGetRawCommandId(tuphdr));
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(
255  bits_to_text(tuphdr->t_bits, bits_len));
256  }
257  else
258  nulls[11] = true;
259 
260  if (tuphdr->t_infomask & HEAP_HASOID_OLD)
261  values[12] = HeapTupleHeaderGetOidOld(tuphdr);
262  else
263  nulls[12] = true;
264  }
265  else
266  {
267  nulls[11] = true;
268  nulls[12] = true;
269  }
270  }
271  else
272  {
273  /*
274  * The line pointer is not used, or it's invalid. Set the rest of
275  * the fields to NULL
276  */
277  int i;
278 
279  for (i = 4; i <= 13; i++)
280  nulls[i] = true;
281  }
282 
283  /* Build and return the result tuple. */
284  resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
285  result = HeapTupleGetDatum(resultTuple);
286 
287  inter_call_data->offset++;
288 
289  SRF_RETURN_NEXT(fctx, result);
290  }
291  else
292  SRF_RETURN_DONE(fctx);
293 }
uint64 call_cntr
Definition: funcapi.h:66
#define SizeofHeapTupleHeader
Definition: htup_details.h:184
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define VARDATA(PTR)
Definition: postgres.h:302
#define MinHeapTupleSize
Definition: htup_details.h:561
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:177
#define BITS_PER_BYTE
#define VARSIZE(PTR)
Definition: postgres.h:303
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:283
#define PointerGetDatum(X)
Definition: postgres.h:556
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define VARHDRSZ
Definition: c.h:555
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
#define BITMAPLEN(NATTS)
Definition: htup_details.h:547
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define SizeOfPageHeaderData
Definition: bufpage.h:216
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:357
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:287
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:289
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:329
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:375
unsigned short uint16
Definition: c.h:357
#define ItemIdGetLength(itemId)
Definition: itemid.h:59
#define HEAP_HASNULL
Definition: htup_details.h:189
#define UInt8GetDatum(X)
Definition: postgres.h:437
#define ERROR
Definition: elog.h:43
#define HeapTupleHeaderGetNatts(tup)
Definition: htup_details.h:531
ItemPointerData t_ctid
Definition: htup_details.h:160
#define FirstOffsetNumber
Definition: off.h:27
#define UInt32GetDatum(X)
Definition: postgres.h:493
#define ItemIdGetOffset(itemId)
Definition: itemid.h:65
#define HeapTupleHeaderGetOidOld(tup)
Definition: heapfuncs.c:46
#define ereport(elevel, rest)
Definition: elog.h:141
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:235
#define ItemIdGetFlags(itemId)
Definition: itemid.h:71
uintptr_t Datum
Definition: postgres.h:367
Page page
Definition: btreefuncs.c:246
#define ItemIdHasStorage(itemId)
Definition: itemid.h:120
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:102
#define MAXALIGN(LEN)
Definition: c.h:685
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:308
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
static Datum values[MAXATTR]
Definition: bootstrap.c:167
#define HeapTupleHeaderGetRawCommandId(tup)
Definition: htup_details.h:391
void * user_fctx
Definition: funcapi.h:83
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
int i
#define CStringGetTextDatum(s)
Definition: builtins.h:83
#define HEAP_HASOID_OLD
Definition: htup_details.h:192
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
static char * bits_to_text(bits8 *bits, int len)
Definition: heapfuncs.c:62
#define UInt16GetDatum(X)
Definition: postgres.h:465
uint64 max_calls
Definition: funcapi.h:75
#define PageGetItem(page, itemId)
Definition: bufpage.h:340
Pointer Page
Definition: bufpage.h:78
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:307
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:285

◆ heap_tuple_infomask_flags()

Datum heap_tuple_infomask_flags ( PG_FUNCTION_ARGS  )

Definition at line 508 of file heapfuncs.c.

References Assert, construct_array(), 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.

Referenced by tuple_data_split().

509 {
510 #define HEAP_TUPLE_INFOMASK_COLS 2
512  bool nulls[HEAP_TUPLE_INFOMASK_COLS];
513  uint16 t_infomask = PG_GETARG_INT16(0);
514  uint16 t_infomask2 = PG_GETARG_INT16(1);
515  int cnt = 0;
516  ArrayType *a;
517  int bitcnt;
518  Datum *flags;
519  TupleDesc tupdesc;
520  HeapTuple tuple;
521 
522  if (!superuser())
523  ereport(ERROR,
524  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
525  errmsg("must be superuser to use raw page functions")));
526 
527  /* Build a tuple descriptor for our result type */
528  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
529  elog(ERROR, "return type must be a row type");
530 
531  bitcnt = pg_popcount((const char *) &t_infomask, sizeof(uint16)) +
532  pg_popcount((const char *) &t_infomask2, sizeof(uint16));
533 
534  /* Initialize values and NULL flags arrays */
535  MemSet(values, 0, sizeof(values));
536  MemSet(nulls, 0, sizeof(nulls));
537 
538  /* If no flags, return a set of empty arrays */
539  if (bitcnt <= 0)
540  {
541  values[0] = PointerGetDatum(construct_empty_array(TEXTOID));
542  values[1] = PointerGetDatum(construct_empty_array(TEXTOID));
543  tuple = heap_form_tuple(tupdesc, values, nulls);
545  }
546 
547  /* build set of raw flags */
548  flags = (Datum *) palloc0(sizeof(Datum) * bitcnt);
549 
550  /* decode t_infomask */
551  if ((t_infomask & HEAP_HASNULL) != 0)
552  flags[cnt++] = CStringGetTextDatum("HEAP_HASNULL");
553  if ((t_infomask & HEAP_HASVARWIDTH) != 0)
554  flags[cnt++] = CStringGetTextDatum("HEAP_HASVARWIDTH");
555  if ((t_infomask & HEAP_HASEXTERNAL) != 0)
556  flags[cnt++] = CStringGetTextDatum("HEAP_HASEXTERNAL");
557  if ((t_infomask & HEAP_HASOID_OLD) != 0)
558  flags[cnt++] = CStringGetTextDatum("HEAP_HASOID_OLD");
559  if ((t_infomask & HEAP_XMAX_KEYSHR_LOCK) != 0)
560  flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_KEYSHR_LOCK");
561  if ((t_infomask & HEAP_COMBOCID) != 0)
562  flags[cnt++] = CStringGetTextDatum("HEAP_COMBOCID");
563  if ((t_infomask & HEAP_XMAX_EXCL_LOCK) != 0)
564  flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_EXCL_LOCK");
565  if ((t_infomask & HEAP_XMAX_LOCK_ONLY) != 0)
566  flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_LOCK_ONLY");
567  if ((t_infomask & HEAP_XMIN_COMMITTED) != 0)
568  flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_COMMITTED");
569  if ((t_infomask & HEAP_XMIN_INVALID) != 0)
570  flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_INVALID");
571  if ((t_infomask & HEAP_XMAX_COMMITTED) != 0)
572  flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_COMMITTED");
573  if ((t_infomask & HEAP_XMAX_INVALID) != 0)
574  flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_INVALID");
575  if ((t_infomask & HEAP_XMAX_IS_MULTI) != 0)
576  flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_IS_MULTI");
577  if ((t_infomask & HEAP_UPDATED) != 0)
578  flags[cnt++] = CStringGetTextDatum("HEAP_UPDATED");
579  if ((t_infomask & HEAP_MOVED_OFF) != 0)
580  flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_OFF");
581  if ((t_infomask & HEAP_MOVED_IN) != 0)
582  flags[cnt++] = CStringGetTextDatum("HEAP_MOVED_IN");
583 
584  /* decode t_infomask2 */
585  if ((t_infomask2 & HEAP_KEYS_UPDATED) != 0)
586  flags[cnt++] = CStringGetTextDatum("HEAP_KEYS_UPDATED");
587  if ((t_infomask2 & HEAP_HOT_UPDATED) != 0)
588  flags[cnt++] = CStringGetTextDatum("HEAP_HOT_UPDATED");
589  if ((t_infomask2 & HEAP_ONLY_TUPLE) != 0)
590  flags[cnt++] = CStringGetTextDatum("HEAP_ONLY_TUPLE");
591 
592  /* build value */
593  Assert(cnt <= bitcnt);
594  a = construct_array(flags, cnt, TEXTOID, -1, false, 'i');
595  values[0] = PointerGetDatum(a);
596 
597  /*
598  * Build set of combined flags. Use the same array as previously, this
599  * keeps the code simple.
600  */
601  cnt = 0;
602  MemSet(flags, 0, sizeof(Datum) * bitcnt);
603 
604  /* decode combined masks of t_infomask */
605  if ((t_infomask & HEAP_XMAX_SHR_LOCK) == HEAP_XMAX_SHR_LOCK)
606  flags[cnt++] = CStringGetTextDatum("HEAP_XMAX_SHR_LOCK");
607  if ((t_infomask & HEAP_XMIN_FROZEN) == HEAP_XMIN_FROZEN)
608  flags[cnt++] = CStringGetTextDatum("HEAP_XMIN_FROZEN");
609  if ((t_infomask & HEAP_MOVED) == HEAP_MOVED)
610  flags[cnt++] = CStringGetTextDatum("HEAP_MOVED");
611 
612  /* Build an empty array if there are no combined flags */
613  if (cnt == 0)
614  a = construct_empty_array(TEXTOID);
615  else
616  a = construct_array(flags, cnt, TEXTOID, -1, false, 'i');
617  pfree(flags);
618  values[1] = PointerGetDatum(a);
619 
620  /* Returns the record as Datum */
621  tuple = heap_form_tuple(tupdesc, values, nulls);
623 }
#define HEAP_XMAX_KEYSHR_LOCK
Definition: htup_details.h:193
#define HEAP_ONLY_TUPLE
Definition: htup_details.h:281
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define HEAP_XMAX_LOCK_ONLY
Definition: htup_details.h:196
#define HEAP_TUPLE_INFOMASK_COLS
#define PointerGetDatum(X)
Definition: postgres.h:556
#define HEAP_XMIN_FROZEN
Definition: htup_details.h:205
ArrayType * construct_array(Datum *elems, int nelems, Oid elmtype, int elmlen, bool elmbyval, char elmalign)
Definition: arrayfuncs.c:3291
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
#define MemSet(start, val, len)
Definition: c.h:955
#define HEAP_XMAX_COMMITTED
Definition: htup_details.h:206
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
ArrayType * construct_empty_array(Oid elmtype)
Definition: arrayfuncs.c:3410
#define HEAP_UPDATED
Definition: htup_details.h:209
#define HEAP_XMAX_SHR_LOCK
Definition: htup_details.h:199
uint64 pg_popcount(const char *buf, int bytes)
Definition: pg_bitutils.c:282
#define HEAP_XMIN_INVALID
Definition: htup_details.h:204
unsigned short uint16
Definition: c.h:357
void pfree(void *pointer)
Definition: mcxt.c:1056
#define HEAP_HASNULL
Definition: htup_details.h:189
#define ERROR
Definition: elog.h:43
#define HEAP_XMAX_INVALID
Definition: htup_details.h:207
#define HEAP_XMAX_EXCL_LOCK
Definition: htup_details.h:195
#define HEAP_MOVED_IN
Definition: htup_details.h:213
#define HEAP_HASVARWIDTH
Definition: htup_details.h:190
#define ereport(elevel, rest)
Definition: elog.h:141
void * palloc0(Size size)
Definition: mcxt.c:980
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
#define HEAP_KEYS_UPDATED
Definition: htup_details.h:278
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
#define HEAP_XMAX_IS_MULTI
Definition: htup_details.h:208
#define HEAP_XMIN_COMMITTED
Definition: htup_details.h:203
#define HEAP_MOVED
Definition: htup_details.h:216
#define HEAP_COMBOCID
Definition: htup_details.h:194
#define HEAP_MOVED_OFF
Definition: htup_details.h:210
#define Assert(condition)
Definition: c.h:732
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
static Datum values[MAXATTR]
Definition: bootstrap.c:167
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define elog(elevel,...)
Definition: elog.h:226
#define HEAP_HOT_UPDATED
Definition: htup_details.h:280
#define CStringGetTextDatum(s)
Definition: builtins.h:83
#define HEAP_HASEXTERNAL
Definition: htup_details.h:191
#define HEAP_HASOID_OLD
Definition: htup_details.h:192

◆ PG_FUNCTION_INFO_V1() [1/3]

PG_FUNCTION_INFO_V1 ( heap_page_items  )

◆ PG_FUNCTION_INFO_V1() [2/3]

PG_FUNCTION_INFO_V1 ( tuple_data_split  )

◆ PG_FUNCTION_INFO_V1() [3/3]

PG_FUNCTION_INFO_V1 ( heap_tuple_infomask_flags  )

◆ text_to_bits()

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

Definition at line 85 of file heapfuncs.c.

References byte, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), ERROR, heap_page_items(), palloc(), and PG_FUNCTION_INFO_V1().

Referenced by tuple_data_split().

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
101  ereport(ERROR,
103  errmsg("illegal character '%c' in t_bits string", str[off])));
104 
105  if (off % 8 == 7)
106  bits[off / 8] = byte;
107 
108  off++;
109  }
110 
111  return bits;
112 }
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:141
#define byte(x, n)
Definition: rijndael.c:68
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
uint8 bits8
Definition: c.h:365
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784

◆ tuple_data_split()

Datum tuple_data_split ( PG_FUNCTION_ARGS  )

Definition at line 423 of file heapfuncs.c.

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

Referenced by tuple_data_split_internal().

424 {
425  Oid relid;
426  bytea *raw_data;
427  uint16 t_infomask;
428  uint16 t_infomask2;
429  char *t_bits_str;
430  bool do_detoast = false;
431  bits8 *t_bits = NULL;
432  Datum res;
433 
434  relid = PG_GETARG_OID(0);
435  raw_data = PG_ARGISNULL(1) ? NULL : PG_GETARG_BYTEA_P(1);
436  t_infomask = PG_GETARG_INT16(2);
437  t_infomask2 = PG_GETARG_INT16(3);
438  t_bits_str = PG_ARGISNULL(4) ? NULL :
440 
441  if (PG_NARGS() >= 6)
442  do_detoast = PG_GETARG_BOOL(5);
443 
444  if (!superuser())
445  ereport(ERROR,
446  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
447  errmsg("must be superuser to use raw page functions")));
448 
449  if (!raw_data)
450  PG_RETURN_NULL();
451 
452  /*
453  * Convert t_bits string back to the bits8 array as represented in the
454  * tuple header.
455  */
456  if (t_infomask & HEAP_HASNULL)
457  {
458  int bits_str_len;
459  int bits_len;
460 
461  bits_len = BITMAPLEN(t_infomask2 & HEAP_NATTS_MASK) * BITS_PER_BYTE;
462  if (!t_bits_str)
463  ereport(ERROR,
465  errmsg("argument of t_bits is null, but it is expected to be null and %d character long",
466  bits_len)));
467 
468  bits_str_len = strlen(t_bits_str);
469  if (bits_len != bits_str_len)
470  ereport(ERROR,
472  errmsg("unexpected length of t_bits %u, expected %d",
473  bits_str_len, bits_len)));
474 
475  /* do the conversion */
476  t_bits = text_to_bits(t_bits_str, bits_str_len);
477  }
478  else
479  {
480  if (t_bits_str)
481  ereport(ERROR,
483  errmsg("t_bits string is expected to be NULL, but instead it is %zu bytes length",
484  strlen(t_bits_str))));
485  }
486 
487  /* Split tuple data */
488  res = tuple_data_split_internal(relid, (char *) raw_data + VARHDRSZ,
489  VARSIZE(raw_data) - VARHDRSZ,
490  t_infomask, t_infomask2, t_bits,
491  do_detoast);
492 
493  if (t_bits)
494  pfree(t_bits);
495 
497 }
#define BITS_PER_BYTE
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:555
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
#define BITMAPLEN(NATTS)
Definition: htup_details.h:547
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:269
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:329
unsigned short uint16
Definition: c.h:357
void pfree(void *pointer)
Definition: mcxt.c:1056
#define HEAP_HASNULL
Definition: htup_details.h:189
#define ERROR
Definition: elog.h:43
#define PG_GETARG_OID(n)
Definition: fmgr.h:270
static bits8 * text_to_bits(char *str, int len)
Definition: heapfuncs.c:85
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:253
#define ereport(elevel, rest)
Definition: elog.h:141
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
uint8 bits8
Definition: c.h:365
uintptr_t Datum
Definition: postgres.h:367
#define PG_GETARG_INT16(n)
Definition: fmgr.h:266
#define PG_ARGISNULL(n)
Definition: fmgr.h:204
#define HEAP_NATTS_MASK
Definition: htup_details.h:276
#define PG_NARGS()
Definition: fmgr.h:198
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:304
char * text_to_cstring(const text *t)
Definition: varlena.c:204
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549
#define PG_RETURN_NULL()
Definition: fmgr.h:335

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

References AccessShareLock, accumArrayResult(), att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, CurrentMemoryContext, DatumGetByteaPCopy, ereport, errcode(), ERRCODE_DATA_CORRUPTED, errmsg(), ERROR, HEAP_HASNULL, HEAP_NATTS_MASK, i, initArrayResult(), makeArrayResult(), TupleDescData::natts, palloc(), pfree(), PG_FUNCTION_INFO_V1(), PointerGetDatum, RelationData::rd_rel, relation_close(), relation_open(), RelationGetDescr, SET_VARSIZE, tuple_data_split(), TupleDescAttr, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARDATA, VARHDRSZ, and VARSIZE_ANY.

Referenced by tuple_data_split().

308 {
309  ArrayBuildState *raw_attrs;
310  int nattrs;
311  int i;
312  int off = 0;
313  Relation rel;
314  TupleDesc tupdesc;
315 
316  /* Get tuple descriptor from relation OID */
317  rel = relation_open(relid, AccessShareLock);
318  tupdesc = RelationGetDescr(rel);
319 
320  raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
321  nattrs = tupdesc->natts;
322 
323  if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
324  ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
325  errmsg("only heap AM is supported")));
326 
327  if (nattrs < (t_infomask2 & HEAP_NATTS_MASK))
328  ereport(ERROR,
330  errmsg("number of attributes in tuple header is greater than number of attributes in tuple descriptor")));
331 
332  for (i = 0; i < nattrs; i++)
333  {
334  Form_pg_attribute attr;
335  bool is_null;
336  bytea *attr_data = NULL;
337 
338  attr = TupleDescAttr(tupdesc, i);
339 
340  /*
341  * Tuple header can specify less attributes than tuple descriptor as
342  * ALTER TABLE ADD COLUMN without DEFAULT keyword does not actually
343  * change tuples in pages, so attributes with numbers greater than
344  * (t_infomask2 & HEAP_NATTS_MASK) should be treated as NULL.
345  */
346  if (i >= (t_infomask2 & HEAP_NATTS_MASK))
347  is_null = true;
348  else
349  is_null = (t_infomask & HEAP_HASNULL) && att_isnull(i, t_bits);
350 
351  if (!is_null)
352  {
353  int len;
354 
355  if (attr->attlen == -1)
356  {
357  off = att_align_pointer(off, attr->attalign, -1,
358  tupdata + off);
359 
360  /*
361  * As VARSIZE_ANY throws an exception if it can't properly
362  * detect the type of external storage in macros VARTAG_SIZE,
363  * this check is repeated to have a nicer error handling.
364  */
365  if (VARATT_IS_EXTERNAL(tupdata + off) &&
366  !VARATT_IS_EXTERNAL_ONDISK(tupdata + off) &&
367  !VARATT_IS_EXTERNAL_INDIRECT(tupdata + off))
368  ereport(ERROR,
370  errmsg("first byte of varlena attribute is incorrect for attribute %d", i)));
371 
372  len = VARSIZE_ANY(tupdata + off);
373  }
374  else
375  {
376  off = att_align_nominal(off, attr->attalign);
377  len = attr->attlen;
378  }
379 
380  if (tupdata_len < off + len)
381  ereport(ERROR,
383  errmsg("unexpected end of tuple data")));
384 
385  if (attr->attlen == -1 && do_detoast)
386  attr_data = DatumGetByteaPCopy(tupdata + off);
387  else
388  {
389  attr_data = (bytea *) palloc(len + VARHDRSZ);
390  SET_VARSIZE(attr_data, len + VARHDRSZ);
391  memcpy(VARDATA(attr_data), tupdata + off, len);
392  }
393 
394  off = att_addlength_pointer(off, attr->attlen,
395  tupdata + off);
396  }
397 
398  raw_attrs = accumArrayResult(raw_attrs, PointerGetDatum(attr_data),
399  is_null, BYTEAOID, CurrentMemoryContext);
400  if (attr_data)
401  pfree(attr_data);
402  }
403 
404  if (tupdata_len != off)
405  ereport(ERROR,
407  errmsg("end of tuple reached without looking at all its data")));
408 
410 
411  return makeArrayResult(raw_attrs, CurrentMemoryContext);
412 }
#define DatumGetByteaPCopy(X)
Definition: fmgr.h:291
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:314
#define VARDATA(PTR)
Definition: postgres.h:302
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:146
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:5014
#define RelationGetDescr(relation)
Definition: rel.h:445
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:23
#define PointerGetDatum(X)
Definition: postgres.h:556
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define VARHDRSZ
Definition: c.h:555
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
Form_pg_class rd_rel
Definition: rel.h:83
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:313
void pfree(void *pointer)
Definition: mcxt.c:1056
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:316
#define HEAP_HASNULL
Definition: htup_details.h:189
#define ERROR
Definition: elog.h:43
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: relation.c:48
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:200
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:174
#define ereport(elevel, rest)
Definition: elog.h:141
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5117
#define ERRCODE_DATA_CORRUPTED
Definition: pg_basebackup.c:45
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:124
#define VARSIZE_ANY(PTR)
Definition: postgres.h:335
#define HEAP_NATTS_MASK
Definition: htup_details.h:276
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:5053
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
int i
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329