PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
heapfuncs.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * heapfuncs.c
4  * Functions to investigate heap pages
5  *
6  * We check the input to these functions for corrupt pointers etc. that
7  * might cause crashes, but at the same time we try to print out as much
8  * information as possible, even if it's nonsense. That's because if a
9  * page is corrupt, we don't know why and how exactly it is corrupt, so we
10  * let the user judge it.
11  *
12  * These functions are restricted to superusers for the fear of introducing
13  * security holes if the input checking isn't as water-tight as it should be.
14  * You'd need to be superuser to obtain a raw page image anyway, so
15  * there's hardly any use case for using these without superuser-rights
16  * anyway.
17  *
18  * Copyright (c) 2007-2017, PostgreSQL Global Development Group
19  *
20  * IDENTIFICATION
21  * contrib/pageinspect/heapfuncs.c
22  *
23  *-------------------------------------------------------------------------
24  */
25 
26 #include "postgres.h"
27 
28 #include "pageinspect.h"
29 
30 #include "access/htup_details.h"
31 #include "funcapi.h"
32 #include "catalog/pg_type.h"
33 #include "miscadmin.h"
34 #include "utils/array.h"
35 #include "utils/builtins.h"
36 #include "utils/rel.h"
37 
38 
39 /*
40  * bits_to_text
41  *
42  * Converts a bits8-array of 'len' bits to a human-readable
43  * c-string representation.
44  */
45 static char *
46 bits_to_text(bits8 *bits, int len)
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 }
60 
61 
62 /*
63  * text_to_bits
64  *
65  * Converts a c-string representation of bits into a bits8-array. This is
66  * the reverse operation of previous routine.
67  */
68 static bits8 *
69 text_to_bits(char *str, int len)
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 }
97 
98 /*
99  * heap_page_items
100  *
101  * Allows inspection of line pointers and tuple headers of a heap page.
102  */
104 
105 typedef struct heap_page_items_state
106 {
111 
112 Datum
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 }
278 
279 /*
280  * tuple_data_split_internal
281  *
282  * Split raw tuple data taken directly from a page into an array of bytea
283  * elements. This routine does a lookup on NULL values and creates array
284  * elements accordingly. This is a reimplementation of nocachegetattr()
285  * in heaptuple.c simplified for educational purposes.
286  */
287 static Datum
288 tuple_data_split_internal(Oid relid, char *tupdata,
289  uint16 tupdata_len, uint16 t_infomask,
290  uint16 t_infomask2, bits8 *t_bits,
291  bool do_detoast)
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 }
391 
392 /*
393  * tuple_data_split
394  *
395  * Split raw tuple data taken directly from page into distinct elements
396  * taking into account null values.
397  */
399 
400 Datum
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 DatumGetByteaPCopy(X)
Definition: fmgr.h:261
uint64 call_cntr
Definition: funcapi.h:65
#define VARATT_IS_EXTERNAL_ONDISK(PTR)
Definition: postgres.h:315
#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 att_align_nominal(cur_offset, attalign)
Definition: tupmacs.h:144
#define MinHeapTupleSize
Definition: htup_details.h:562
bits8 t_bits[FLEXIBLE_ARRAY_MEMBER]
Definition: htup_details.h:163
ArrayBuildState * initArrayResult(Oid element_type, MemoryContext rcontext, bool subcontext)
Definition: arrayfuncs.c:4951
#define VARSIZE(PTR)
Definition: postgres.h:304
#define att_isnull(ATT, BITS)
Definition: tupmacs.h:21
#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
Form_pg_attribute * attrs
Definition: tupdesc.h:74
static MemoryContext MemoryContextSwitchTo(MemoryContext context)
Definition: palloc.h:109
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1260
return result
Definition: formatting.c:1618
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define PG_GETARG_BOOL(n)
Definition: fmgr.h:239
#define SizeOfPageHeaderData
Definition: bufpage.h:213
unsigned int Oid
Definition: postgres_ext.h:31
#define PageGetMaxOffsetNumber(page)
Definition: bufpage.h:354
#define SRF_PERCALL_SETUP()
Definition: funcapi.h:289
int natts
Definition: tupdesc.h:73
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define SRF_RETURN_NEXT(_funcctx, _result)
Definition: funcapi.h:291
#define VARATT_IS_EXTERNAL(PTR)
Definition: postgres.h:314
#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
void pfree(void *pointer)
Definition: mcxt.c:950
#define ItemIdGetLength(itemId)
Definition: itemid.h:58
#define VARATT_IS_EXTERNAL_INDIRECT(PTR)
Definition: postgres.h:317
#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 NoLock
Definition: lockdefs.h:34
#define PG_GETARG_OID(n)
Definition: fmgr.h:240
#define FirstOffsetNumber
Definition: off.h:27
static bits8 * text_to_bits(char *str, int len)
Definition: heapfuncs.c:69
FormData_pg_attribute * Form_pg_attribute
Definition: pg_attribute.h:187
struct heap_page_items_state heap_page_items_state
#define UInt32GetDatum(X)
Definition: postgres.h:499
#define ItemIdGetOffset(itemId)
Definition: itemid.h:64
MemoryContext CurrentMemoryContext
Definition: mcxt.c:37
TupleDesc CreateTupleDescCopyConstr(TupleDesc tupdesc)
Definition: tupdesc.c:168
#define PG_RETURN_ARRAYTYPE_P(x)
Definition: array.h:246
#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 byte(x, n)
Definition: rijndael.c:68
#define PageGetItemId(page, offsetNumber)
Definition: bufpage.h:232
uint8 bits8
Definition: c.h:275
#define ItemIdGetFlags(itemId)
Definition: itemid.h:70
uintptr_t Datum
Definition: postgres.h:372
#define att_align_pointer(cur_offset, attalign, attlen, attptr)
Definition: tupmacs.h:122
#define PG_GETARG_INT16(n)
Definition: fmgr.h:236
TupleDesc rd_att
Definition: rel.h:115
#define VARSIZE_ANY(PTR)
Definition: postgres.h:334
#define ItemIdHasStorage(itemId)
Definition: itemid.h:119
Datum tuple_data_split(PG_FUNCTION_ARGS)
Definition: heapfuncs.c:401
PG_FUNCTION_INFO_V1(heap_page_items)
#define PG_ARGISNULL(n)
Definition: fmgr.h:174
#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 PG_NARGS()
Definition: fmgr.h:168
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
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
#define BYTEAOID
Definition: pg_type.h:292
static Datum values[MAXATTR]
Definition: bootstrap.c:163
char * text_to_cstring(const text *t)
Definition: varlena.c:182
ArrayBuildState * accumArrayResult(ArrayBuildState *astate, Datum dvalue, bool disnull, Oid element_type, MemoryContext rcontext)
Definition: arrayfuncs.c:4991
#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
Relation relation_open(Oid relationId, LOCKMODE lockmode)
Definition: heapam.c:1114
Definition: c.h:439
#define PG_FUNCTION_ARGS
Definition: fmgr.h:158
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
#define elog
Definition: elog.h:219
Datum heap_page_items(PG_FUNCTION_ARGS)
Definition: heapfuncs.c:113
static char * bits_to_text(bits8 *bits, int len)
Definition: heapfuncs.c:46
#define UInt16GetDatum(X)
Definition: postgres.h:471
#define PG_RETURN_NULL()
Definition: fmgr.h:305
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