PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
heapfuncs.c File Reference
#include "postgres.h"
#include "pageinspect.h"
#include "access/htup_details.h"
#include "funcapi.h"
#include "catalog/pg_type.h"
#include "miscadmin.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
 

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)
 

Typedef Documentation

Function Documentation

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

Definition at line 46 of file heapfuncs.c.

References i, and palloc().

Referenced by heap_page_items().

47 {
48  int i;
49  char *str;
50 
51  str = palloc(len + 1);
52 
53  for (i = 0; i < len; i++)
54  str[i] = (bits[(i / 8)] & (1 << (i % 8))) ? '1' : '0';
55 
56  str[i] = '\0';
57 
58  return str;
59 }
void * palloc(Size size)
Definition: mcxt.c:849
int i
Datum heap_page_items ( PG_FUNCTION_ARGS  )

Definition at line 113 of file heapfuncs.c.

References bits_to_text(), FuncCallContext::call_cntr, CStringGetTextDatum, elog, ereport, errcode(), errmsg(), ERROR, FirstOffsetNumber, get_call_result_type(), heap_form_tuple(), HEAP_HASNULL, HEAP_HASOID, HEAP_NATTS_MASK, HeapTupleGetDatum, HeapTupleHeaderGetOid, HeapTupleHeaderGetRawCommandId, HeapTupleHeaderGetRawXmax, HeapTupleHeaderGetRawXmin, i, ItemIdGetFlags, ItemIdGetLength, ItemIdGetOffset, ItemIdHasStorage, FuncCallContext::max_calls, MAXALIGN, MemoryContextSwitchTo(), MinHeapTupleSize, FuncCallContext::multi_call_memory_ctx, NULL, heap_page_items_state::offset, heap_page_items_state::page, PageGetItem, PageGetItemId, PageGetMaxOffsetNumber, palloc(), PG_GETARG_BYTEA_P, PointerGetDatum, result, 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.

114 {
115  bytea *raw_page = PG_GETARG_BYTEA_P(0);
116  heap_page_items_state *inter_call_data = NULL;
117  FuncCallContext *fctx;
118  int raw_page_size;
119 
120  if (!superuser())
121  ereport(ERROR,
122  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
123  (errmsg("must be superuser to use raw page functions"))));
124 
125  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
126 
127  if (SRF_IS_FIRSTCALL())
128  {
129  TupleDesc tupdesc;
130  MemoryContext mctx;
131 
132  if (raw_page_size < SizeOfPageHeaderData)
133  ereport(ERROR,
134  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
135  errmsg("input page too small (%d bytes)", raw_page_size)));
136 
137  fctx = SRF_FIRSTCALL_INIT();
139 
140  inter_call_data = palloc(sizeof(heap_page_items_state));
141 
142  /* Build a tuple descriptor for our result type */
143  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
144  elog(ERROR, "return type must be a row type");
145 
146  inter_call_data->tupd = tupdesc;
147 
148  inter_call_data->offset = FirstOffsetNumber;
149  inter_call_data->page = VARDATA(raw_page);
150 
151  fctx->max_calls = PageGetMaxOffsetNumber(inter_call_data->page);
152  fctx->user_fctx = inter_call_data;
153 
154  MemoryContextSwitchTo(mctx);
155  }
156 
157  fctx = SRF_PERCALL_SETUP();
158  inter_call_data = fctx->user_fctx;
159 
160  if (fctx->call_cntr < fctx->max_calls)
161  {
162  Page page = inter_call_data->page;
163  HeapTuple resultTuple;
164  Datum result;
165  ItemId id;
166  Datum values[14];
167  bool nulls[14];
168  uint16 lp_offset;
169  uint16 lp_flags;
170  uint16 lp_len;
171 
172  memset(nulls, 0, sizeof(nulls));
173 
174  /* Extract information from the line pointer */
175 
176  id = PageGetItemId(page, inter_call_data->offset);
177 
178  lp_offset = ItemIdGetOffset(id);
179  lp_flags = ItemIdGetFlags(id);
180  lp_len = ItemIdGetLength(id);
181 
182  values[0] = UInt16GetDatum(inter_call_data->offset);
183  values[1] = UInt16GetDatum(lp_offset);
184  values[2] = UInt16GetDatum(lp_flags);
185  values[3] = UInt16GetDatum(lp_len);
186 
187  /*
188  * We do just enough validity checking to make sure we don't reference
189  * data outside the page passed to us. The page could be corrupt in
190  * many other ways, but at least we won't crash.
191  */
192  if (ItemIdHasStorage(id) &&
193  lp_len >= MinHeapTupleSize &&
194  lp_offset == MAXALIGN(lp_offset) &&
195  lp_offset + lp_len <= raw_page_size)
196  {
197  HeapTupleHeader tuphdr;
198  bytea *tuple_data_bytea;
199  int tuple_data_len;
200 
201  /* Extract information from the tuple header */
202 
203  tuphdr = (HeapTupleHeader) PageGetItem(page, id);
204 
205  values[4] = UInt32GetDatum(HeapTupleHeaderGetRawXmin(tuphdr));
206  values[5] = UInt32GetDatum(HeapTupleHeaderGetRawXmax(tuphdr));
207  /* shared with xvac */
208  values[6] = UInt32GetDatum(HeapTupleHeaderGetRawCommandId(tuphdr));
209  values[7] = PointerGetDatum(&tuphdr->t_ctid);
210  values[8] = UInt32GetDatum(tuphdr->t_infomask2);
211  values[9] = UInt32GetDatum(tuphdr->t_infomask);
212  values[10] = UInt8GetDatum(tuphdr->t_hoff);
213 
214  /* Copy raw tuple data into bytea attribute */
215  tuple_data_len = lp_len - tuphdr->t_hoff;
216  tuple_data_bytea = (bytea *) palloc(tuple_data_len + VARHDRSZ);
217  SET_VARSIZE(tuple_data_bytea, tuple_data_len + VARHDRSZ);
218  memcpy(VARDATA(tuple_data_bytea), (char *) tuphdr + tuphdr->t_hoff,
219  tuple_data_len);
220  values[13] = PointerGetDatum(tuple_data_bytea);
221 
222  /*
223  * We already checked that the item is completely within the raw
224  * page passed to us, with the length given in the line pointer.
225  * Let's check that t_hoff doesn't point over lp_len, before using
226  * it to access t_bits and oid.
227  */
228  if (tuphdr->t_hoff >= SizeofHeapTupleHeader &&
229  tuphdr->t_hoff <= lp_len &&
230  tuphdr->t_hoff == MAXALIGN(tuphdr->t_hoff))
231  {
232  if (tuphdr->t_infomask & HEAP_HASNULL)
233  {
234  int bits_len;
235 
236  bits_len =
237  ((tuphdr->t_infomask2 & HEAP_NATTS_MASK) / 8 + 1) * 8;
238  values[11] = CStringGetTextDatum(
239  bits_to_text(tuphdr->t_bits, bits_len));
240  }
241  else
242  nulls[11] = true;
243 
244  if (tuphdr->t_infomask & HEAP_HASOID)
245  values[12] = HeapTupleHeaderGetOid(tuphdr);
246  else
247  nulls[12] = true;
248  }
249  else
250  {
251  nulls[11] = true;
252  nulls[12] = true;
253  }
254  }
255  else
256  {
257  /*
258  * The line pointer is not used, or it's invalid. Set the rest of
259  * the fields to NULL
260  */
261  int i;
262 
263  for (i = 4; i <= 13; i++)
264  nulls[i] = true;
265  }
266 
267  /* Build and return the result tuple. */
268  resultTuple = heap_form_tuple(inter_call_data->tupd, values, nulls);
269  result = HeapTupleGetDatum(resultTuple);
270 
271  inter_call_data->offset++;
272 
273  SRF_RETURN_NEXT(fctx, result);
274  }
275  else
276  SRF_RETURN_DONE(fctx);
277 }
uint64 call_cntr
Definition: funcapi.h:65
#define SizeofHeapTupleHeader
Definition: htup_details.h:170
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define VARDATA(PTR)
Definition: postgres.h:303
#define MinHeapTupleSize
Definition: htup_details.h:562
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:163
#define VARSIZE(PTR)
Definition: postgres.h:304
#define SRF_IS_FIRSTCALL()
Definition: funcapi.h:285
#define PointerGetDatum(X)
Definition: postgres.h:562
HeapTupleHeaderData * HeapTupleHeader
Definition: htup.h:23
#define VARHDRSZ
Definition: c.h:445
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
return result
Definition: formatting.c:1632
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define SizeOfPageHeaderData
Definition: bufpage.h:213
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
#define HeapTupleHeaderGetRawXmax(tup)
Definition: htup_details.h:369
unsigned short uint16
Definition: c.h:267
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
#define HEAP_HASNULL
Definition: htup_details.h:175
#define UInt8GetDatum(X)
Definition: postgres.h:443
#define ERROR
Definition: elog.h:43
ItemPointerData t_ctid
Definition: htup_details.h:150
#define FirstOffsetNumber
Definition: off.h:27
#define UInt32GetDatum(X)
Definition: postgres.h:499
#define ItemIdGetOffset(itemId)
Definition: itemid.h:64
#define ereport(elevel, rest)
Definition: elog.h:122
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
#define ItemIdGetFlags(itemId)
Definition: itemid.h:70
uintptr_t Datum
Definition: postgres.h:372
Page page
Definition: btreefuncs.c:245
#define ItemIdHasStorage(itemId)
Definition: itemid.h:119
#define HEAP_NATTS_MASK
Definition: htup_details.h:262
#define NULL
Definition: c.h:229
MemoryContext multi_call_memory_ctx
Definition: funcapi.h:109
#define MAXALIGN(LEN)
Definition: c.h:588
#define HeapTupleHeaderGetRawXmin(tup)
Definition: htup_details.h:302
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
static Datum values[MAXATTR]
Definition: bootstrap.c:163
#define HeapTupleHeaderGetOid(tup)
Definition: htup_details.h:465
#define HeapTupleHeaderGetRawCommandId(tup)
Definition: htup_details.h:385
void * user_fctx
Definition: funcapi.h:90
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
#define HEAP_HASOID
Definition: htup_details.h:178
#define CStringGetTextDatum(s)
Definition: builtins.h:91
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define elog
Definition: elog.h:219
static char * bits_to_text(bits8 *bits, int len)
Definition: heapfuncs.c:46
#define UInt16GetDatum(X)
Definition: postgres.h:471
uint64 max_calls
Definition: funcapi.h:74
#define PageGetItem(page, itemId)
Definition: bufpage.h:337
Pointer Page
Definition: bufpage.h:74
#define SRF_RETURN_DONE(_funcctx)
Definition: funcapi.h:309
#define SRF_FIRSTCALL_INIT()
Definition: funcapi.h:287
PG_FUNCTION_INFO_V1 ( heap_page_items  )
PG_FUNCTION_INFO_V1 ( tuple_data_split  )
static bits8* text_to_bits ( char *  str,
int  len 
)
static

Definition at line 69 of file heapfuncs.c.

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

Referenced by tuple_data_split().

70 {
71  bits8 *bits;
72  int off = 0;
73  char byte = 0;
74 
75  bits = palloc(len + 1);
76 
77  while (off < len)
78  {
79  if (off % 8 == 0)
80  byte = 0;
81 
82  if ((str[off] == '0') || (str[off] == '1'))
83  byte = byte | ((str[off] - '0') << off % 8);
84  else
85  ereport(ERROR,
86  (errcode(ERRCODE_DATA_CORRUPTED),
87  errmsg("illegal character '%c' in t_bits string", str[off])));
88 
89  if (off % 8 == 7)
90  bits[off / 8] = byte;
91 
92  off++;
93  }
94 
95  return bits;
96 }
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
#define ereport(elevel, rest)
Definition: elog.h:122
#define byte(x, n)
Definition: rijndael.c:68
uint8 bits8
Definition: c.h:275
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Datum tuple_data_split ( PG_FUNCTION_ARGS  )

Definition at line 401 of file heapfuncs.c.

References ereport, errcode(), errmsg(), ERROR, HEAP_HASNULL, HEAP_NATTS_MASK, NULL, pfree(), PG_ARGISNULL, 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.

402 {
403  Oid relid;
404  bytea *raw_data;
405  uint16 t_infomask;
406  uint16 t_infomask2;
407  char *t_bits_str;
408  bool do_detoast = false;
409  bits8 *t_bits = NULL;
410  Datum res;
411 
412  relid = PG_GETARG_OID(0);
413  raw_data = PG_ARGISNULL(1) ? NULL : PG_GETARG_BYTEA_P(1);
414  t_infomask = PG_GETARG_INT16(2);
415  t_infomask2 = PG_GETARG_INT16(3);
416  t_bits_str = PG_ARGISNULL(4) ? NULL :
418 
419  if (PG_NARGS() >= 6)
420  do_detoast = PG_GETARG_BOOL(5);
421 
422  if (!superuser())
423  ereport(ERROR,
424  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
425  errmsg("must be superuser to use raw page functions")));
426 
427  if (!raw_data)
428  PG_RETURN_NULL();
429 
430  /*
431  * Convert t_bits string back to the bits8 array as represented in the
432  * tuple header.
433  */
434  if (t_infomask & HEAP_HASNULL)
435  {
436  int bits_str_len;
437  int bits_len;
438 
439  bits_len = (t_infomask2 & HEAP_NATTS_MASK) / 8 + 1;
440  if (!t_bits_str)
441  ereport(ERROR,
442  (errcode(ERRCODE_DATA_CORRUPTED),
443  errmsg("argument of t_bits is null, but it is expected to be null and %d character long",
444  bits_len * 8)));
445 
446  bits_str_len = strlen(t_bits_str);
447  if ((bits_str_len % 8) != 0)
448  ereport(ERROR,
449  (errcode(ERRCODE_DATA_CORRUPTED),
450  errmsg("length of t_bits is not a multiple of eight")));
451 
452  if (bits_len * 8 != bits_str_len)
453  ereport(ERROR,
454  (errcode(ERRCODE_DATA_CORRUPTED),
455  errmsg("unexpected length of t_bits %u, expected %d",
456  bits_str_len, bits_len * 8)));
457 
458  /* do the conversion */
459  t_bits = text_to_bits(t_bits_str, bits_str_len);
460  }
461  else
462  {
463  if (t_bits_str)
464  ereport(ERROR,
465  (errcode(ERRCODE_DATA_CORRUPTED),
466  errmsg("t_bits string is expected to be NULL, but instead it is %zu bytes length",
467  strlen(t_bits_str))));
468  }
469 
470  /* Split tuple data */
471  res = tuple_data_split_internal(relid, (char *) raw_data + VARHDRSZ,
472  VARSIZE(raw_data) - VARHDRSZ,
473  t_infomask, t_infomask2, t_bits,
474  do_detoast);
475 
476  if (t_bits)
477  pfree(t_bits);
478 
480 }
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
unsigned int Oid
Definition: postgres_ext.h:31
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
unsigned short uint16
Definition: c.h:267
void pfree(void *pointer)
Definition: mcxt.c:950
#define HEAP_HASNULL
Definition: htup_details.h:175
#define ERROR
Definition: elog.h:43
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
static bits8 * text_to_bits(char *str, int len)
Definition: heapfuncs.c:69
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#define ereport(elevel, rest)
Definition: elog.h:122
uint8 bits8
Definition: c.h:275
uintptr_t Datum
Definition: postgres.h:372
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#define HEAP_NATTS_MASK
Definition: htup_details.h:262
#define NULL
Definition: c.h:229
#define PG_NARGS()
Definition: fmgr.h:168
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:288
char * text_to_cstring(const text *t)
Definition: varlena.c:182
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
#define PG_RETURN_NULL()
Definition: fmgr.h:305
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 288 of file heapfuncs.c.

References accumArrayResult(), att_addlength_pointer, att_align_nominal, att_align_pointer, att_isnull, tupleDesc::attrs, BYTEAOID, CreateTupleDescCopyConstr(), CurrentMemoryContext, DatumGetByteaPCopy, ereport, errcode(), errmsg(), ERROR, HEAP_HASNULL, HEAP_NATTS_MASK, i, initArrayResult(), makeArrayResult(), tupleDesc::natts, NoLock, NULL, palloc(), pfree(), PointerGetDatum, RelationData::rd_att, relation_close(), relation_open(), SET_VARSIZE, VARATT_IS_EXTERNAL, VARATT_IS_EXTERNAL_INDIRECT, VARATT_IS_EXTERNAL_ONDISK, VARDATA, VARHDRSZ, and VARSIZE_ANY.

Referenced by tuple_data_split().

292 {
293  ArrayBuildState *raw_attrs;
294  int nattrs;
295  int i;
296  int off = 0;
297  Relation rel;
298  TupleDesc tupdesc;
299 
300  /* Get tuple descriptor from relation OID */
301  rel = relation_open(relid, NoLock);
302  tupdesc = CreateTupleDescCopyConstr(rel->rd_att);
303  relation_close(rel, NoLock);
304 
305  raw_attrs = initArrayResult(BYTEAOID, CurrentMemoryContext, false);
306  nattrs = tupdesc->natts;
307 
308  if (nattrs < (t_infomask2 & HEAP_NATTS_MASK))
309  ereport(ERROR,
310  (errcode(ERRCODE_DATA_CORRUPTED),
311  errmsg("number of attributes in tuple header is greater than number of attributes in tuple descriptor")));
312 
313  for (i = 0; i < nattrs; i++)
314  {
315  Form_pg_attribute attr;
316  bool is_null;
317  bytea *attr_data = NULL;
318 
319  attr = tupdesc->attrs[i];
320  is_null = (t_infomask & HEAP_HASNULL) && att_isnull(i, t_bits);
321 
322  /*
323  * Tuple header can specify less attributes than tuple descriptor as
324  * ALTER TABLE ADD COLUMN without DEFAULT keyword does not actually
325  * change tuples in pages, so attributes with numbers greater than
326  * (t_infomask2 & HEAP_NATTS_MASK) should be treated as NULL.
327  */
328  if (i >= (t_infomask2 & HEAP_NATTS_MASK))
329  is_null = true;
330 
331  if (!is_null)
332  {
333  int len;
334 
335  if (attr->attlen == -1)
336  {
337  off = att_align_pointer(off, tupdesc->attrs[i]->attalign, -1,
338  tupdata + off);
339 
340  /*
341  * As VARSIZE_ANY throws an exception if it can't properly
342  * detect the type of external storage in macros VARTAG_SIZE,
343  * this check is repeated to have a nicer error handling.
344  */
345  if (VARATT_IS_EXTERNAL(tupdata + off) &&
346  !VARATT_IS_EXTERNAL_ONDISK(tupdata + off) &&
347  !VARATT_IS_EXTERNAL_INDIRECT(tupdata + off))
348  ereport(ERROR,
349  (errcode(ERRCODE_DATA_CORRUPTED),
350  errmsg("first byte of varlena attribute is incorrect for attribute %d", i)));
351 
352  len = VARSIZE_ANY(tupdata + off);
353  }
354  else
355  {
356  off = att_align_nominal(off, tupdesc->attrs[i]->attalign);
357  len = attr->attlen;
358  }
359 
360  if (tupdata_len < off + len)
361  ereport(ERROR,
362  (errcode(ERRCODE_DATA_CORRUPTED),
363  errmsg("unexpected end of tuple data")));
364 
365  if (attr->attlen == -1 && do_detoast)
366  attr_data = DatumGetByteaPCopy(tupdata + off);
367  else
368  {
369  attr_data = (bytea *) palloc(len + VARHDRSZ);
370  SET_VARSIZE(attr_data, len + VARHDRSZ);
371  memcpy(VARDATA(attr_data), tupdata + off, len);
372  }
373 
374  off = att_addlength_pointer(off, tupdesc->attrs[i]->attlen,
375  tupdata + off);
376  }
377 
378  raw_attrs = accumArrayResult(raw_attrs, PointerGetDatum(attr_data),
379  is_null, BYTEAOID, CurrentMemoryContext);
380  if (attr_data)
381  pfree(attr_data);
382  }
383 
384  if (tupdata_len != off)
385  ereport(ERROR,
386  (errcode(ERRCODE_DATA_CORRUPTED),
387  errmsg("end of tuple reached without looking at all its data")));
388 
389  return makeArrayResult(raw_attrs, CurrentMemoryContext);
390 }
#define DatumGetByteaPCopy(X)
Definition: fmgr.h:261
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:315
#define VARDATA(PTR)
Definition: postgres.h:303
#define att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:4951
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#define PointerGetDatum(X)
Definition: postgres.h:562
#define VARHDRSZ
Definition: c.h:445
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
int natts
Definition: tupdesc.h:73
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:314
void pfree(void *pointer)
Definition: mcxt.c:950
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:317
#define HEAP_HASNULL
Definition: htup_details.h:175
#define ERROR
Definition: elog.h:43
#define NoLock
Definition: lockdefs.h:34
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:168
#define att_addlength_pointer(cur_offset, attlen, attptr)
Definition: tupmacs.h:172
#define ereport(elevel, rest)
Definition: elog.h:122
Datum makeArrayResult(ArrayBuildState *astate, MemoryContext rcontext)
Definition: arrayfuncs.c:5055
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:122
TupleDesc rd_att
Definition: rel.h:115
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
#define NULL
Definition: c.h:229
#define HEAP_NATTS_MASK
Definition: htup_details.h:262
#define BYTEAOID
Definition: pg_type.h:292
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:4991
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
int i
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1114
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328