PostgreSQL Source Code  git master
rawpage.c
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2  *
3  * rawpage.c
4  * Functions to extract a raw page as bytea and inspect it
5  *
6  * Access-method specific inspection functions are in separate files.
7  *
8  * Copyright (c) 2007-2018, PostgreSQL Global Development Group
9  *
10  * IDENTIFICATION
11  * contrib/pageinspect/rawpage.c
12  *
13  *-------------------------------------------------------------------------
14  */
15 
16 #include "postgres.h"
17 
18 #include "pageinspect.h"
19 
20 #include "access/htup_details.h"
21 #include "catalog/namespace.h"
22 #include "catalog/pg_type.h"
23 #include "funcapi.h"
24 #include "miscadmin.h"
25 #include "storage/bufmgr.h"
26 #include "storage/checksum.h"
27 #include "utils/builtins.h"
28 #include "utils/pg_lsn.h"
29 #include "utils/rel.h"
30 #include "utils/varlena.h"
31 
33 
34 static bytea *get_raw_page_internal(text *relname, ForkNumber forknum,
35  BlockNumber blkno);
36 
37 
38 /*
39  * get_raw_page
40  *
41  * Returns a copy of a page from shared buffers as a bytea
42  */
44 
45 Datum
47 {
48  text *relname = PG_GETARG_TEXT_PP(0);
49  uint32 blkno = PG_GETARG_UINT32(1);
50  bytea *raw_page;
51 
52  /*
53  * We don't normally bother to check the number of arguments to a C
54  * function, but here it's needed for safety because early 8.4 beta
55  * releases mistakenly redefined get_raw_page() as taking three arguments.
56  */
57  if (PG_NARGS() != 2)
58  ereport(ERROR,
59  (errmsg("wrong number of arguments to get_raw_page()"),
60  errhint("Run the updated pageinspect.sql script.")));
61 
62  raw_page = get_raw_page_internal(relname, MAIN_FORKNUM, blkno);
63 
64  PG_RETURN_BYTEA_P(raw_page);
65 }
66 
67 /*
68  * get_raw_page_fork
69  *
70  * Same, for any fork
71  */
73 
74 Datum
76 {
77  text *relname = PG_GETARG_TEXT_PP(0);
78  text *forkname = PG_GETARG_TEXT_PP(1);
79  uint32 blkno = PG_GETARG_UINT32(2);
80  bytea *raw_page;
81  ForkNumber forknum;
82 
83  forknum = forkname_to_number(text_to_cstring(forkname));
84 
85  raw_page = get_raw_page_internal(relname, forknum, blkno);
86 
87  PG_RETURN_BYTEA_P(raw_page);
88 }
89 
90 /*
91  * workhorse
92  */
93 static bytea *
95 {
96  bytea *raw_page;
97  RangeVar *relrv;
98  Relation rel;
99  char *raw_page_data;
100  Buffer buf;
101 
102  if (!superuser())
103  ereport(ERROR,
104  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
105  (errmsg("must be superuser to use raw functions"))));
106 
108  rel = relation_openrv(relrv, AccessShareLock);
109 
110  /* Check that this relation has storage */
111  if (rel->rd_rel->relkind == RELKIND_VIEW)
112  ereport(ERROR,
113  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
114  errmsg("cannot get raw page from view \"%s\"",
115  RelationGetRelationName(rel))));
116  if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
117  ereport(ERROR,
118  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
119  errmsg("cannot get raw page from composite type \"%s\"",
120  RelationGetRelationName(rel))));
121  if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
122  ereport(ERROR,
123  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
124  errmsg("cannot get raw page from foreign table \"%s\"",
125  RelationGetRelationName(rel))));
126  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
127  ereport(ERROR,
128  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
129  errmsg("cannot get raw page from partitioned table \"%s\"",
130  RelationGetRelationName(rel))));
131 
132  /*
133  * Reject attempts to read non-local temporary relations; we would be
134  * likely to get wrong data since we have no visibility into the owning
135  * session's local buffers.
136  */
137  if (RELATION_IS_OTHER_TEMP(rel))
138  ereport(ERROR,
139  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
140  errmsg("cannot access temporary tables of other sessions")));
141 
142  if (blkno >= RelationGetNumberOfBlocksInFork(rel, forknum))
143  ereport(ERROR,
144  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
145  errmsg("block number %u is out of range for relation \"%s\"",
146  blkno, RelationGetRelationName(rel))));
147 
148  /* Initialize buffer to copy to */
149  raw_page = (bytea *) palloc(BLCKSZ + VARHDRSZ);
150  SET_VARSIZE(raw_page, BLCKSZ + VARHDRSZ);
151  raw_page_data = VARDATA(raw_page);
152 
153  /* Take a verbatim copy of the page */
154 
155  buf = ReadBufferExtended(rel, forknum, blkno, RBM_NORMAL, NULL);
157 
158  memcpy(raw_page_data, BufferGetPage(buf), BLCKSZ);
159 
161  ReleaseBuffer(buf);
162 
164 
165  return raw_page;
166 }
167 
168 
169 /*
170  * get_page_from_raw
171  *
172  * Get a palloc'd, maxalign'ed page image from the result of get_raw_page()
173  *
174  * On machines with MAXALIGN = 8, the payload of a bytea is not maxaligned,
175  * since it will start 4 bytes into a palloc'd value. On alignment-picky
176  * machines, this will cause failures in accesses to 8-byte-wide values
177  * within the page. We don't need to worry if accessing only 4-byte or
178  * smaller fields, but when examining a struct that contains 8-byte fields,
179  * use this function for safety.
180  */
181 Page
183 {
184  Page page;
185  int raw_page_size;
186 
187  raw_page_size = VARSIZE_ANY_EXHDR(raw_page);
188 
189  if (raw_page_size != BLCKSZ)
190  ereport(ERROR,
191  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
192  errmsg("invalid page size"),
193  errdetail("Expected %d bytes, got %d.",
194  BLCKSZ, raw_page_size)));
195 
196  page = palloc(raw_page_size);
197 
198  memcpy(page, VARDATA_ANY(raw_page), raw_page_size);
199 
200  return page;
201 }
202 
203 
204 /*
205  * page_header
206  *
207  * Allows inspection of page header fields of a raw page
208  */
209 
211 
212 Datum
214 {
215  bytea *raw_page = PG_GETARG_BYTEA_P(0);
216  int raw_page_size;
217 
218  TupleDesc tupdesc;
219 
220  Datum result;
221  HeapTuple tuple;
222  Datum values[9];
223  bool nulls[9];
224 
225  PageHeader page;
226  XLogRecPtr lsn;
227 
228  if (!superuser())
229  ereport(ERROR,
230  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
231  (errmsg("must be superuser to use raw page functions"))));
232 
233  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
234 
235  /*
236  * Check that enough data was supplied, so that we don't try to access
237  * fields outside the supplied buffer.
238  */
239  if (raw_page_size < SizeOfPageHeaderData)
240  ereport(ERROR,
241  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
242  errmsg("input page too small (%d bytes)", raw_page_size)));
243 
244  page = (PageHeader) VARDATA(raw_page);
245 
246  /* Build a tuple descriptor for our result type */
247  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
248  elog(ERROR, "return type must be a row type");
249 
250  /* Extract information from the page header */
251 
252  lsn = PageGetLSN(page);
253 
254  /* pageinspect >= 1.2 uses pg_lsn instead of text for the LSN field. */
255  if (TupleDescAttr(tupdesc, 0)->atttypid == TEXTOID)
256  {
257  char lsnchar[64];
258 
259  snprintf(lsnchar, sizeof(lsnchar), "%X/%X",
260  (uint32) (lsn >> 32), (uint32) lsn);
261  values[0] = CStringGetTextDatum(lsnchar);
262  }
263  else
264  values[0] = LSNGetDatum(lsn);
265  values[1] = UInt16GetDatum(page->pd_checksum);
266  values[2] = UInt16GetDatum(page->pd_flags);
267  values[3] = UInt16GetDatum(page->pd_lower);
268  values[4] = UInt16GetDatum(page->pd_upper);
269  values[5] = UInt16GetDatum(page->pd_special);
270  values[6] = UInt16GetDatum(PageGetPageSize(page));
271  values[7] = UInt16GetDatum(PageGetPageLayoutVersion(page));
272  values[8] = TransactionIdGetDatum(page->pd_prune_xid);
273 
274  /* Build and return the tuple. */
275 
276  memset(nulls, 0, sizeof(nulls));
277 
278  tuple = heap_form_tuple(tupdesc, values, nulls);
279  result = HeapTupleGetDatum(tuple);
280 
281  PG_RETURN_DATUM(result);
282 }
283 
284 /*
285  * page_checksum
286  *
287  * Compute checksum of a raw page
288  */
289 
291 
292 Datum
294 {
295  bytea *raw_page = PG_GETARG_BYTEA_P(0);
296  uint32 blkno = PG_GETARG_INT32(1);
297  int raw_page_size;
298  PageHeader page;
299 
300  if (!superuser())
301  ereport(ERROR,
302  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
303  (errmsg("must be superuser to use raw page functions"))));
304 
305  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
306 
307  /*
308  * Check that the supplied page is of the right size.
309  */
310  if (raw_page_size != BLCKSZ)
311  ereport(ERROR,
312  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
313  errmsg("incorrect size of input page (%d bytes)", raw_page_size)));
314 
315  page = (PageHeader) VARDATA(raw_page);
316 
317  PG_RETURN_INT16(pg_checksum_page((char *) page, blkno));
318 }
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:240
Page get_page_from_raw(bytea *raw_page)
Definition: rawpage.c:182
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1202
#define PG_GETARG_INT32(n)
Definition: fmgr.h:239
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
int errhint(const char *fmt,...)
Definition: elog.c:987
#define VARDATA_ANY(PTR)
Definition: postgres.h:346
#define VARDATA(PTR)
Definition: postgres.h:302
uint16 pd_flags
Definition: bufpage.h:153
#define VARSIZE(PTR)
Definition: postgres.h:303
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:93
#define VARHDRSZ
Definition: c.h:522
PG_MODULE_MAGIC
Definition: rawpage.c:32
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define AccessShareLock
Definition: lockdefs.h:36
#define PG_RETURN_INT16(x)
Definition: fmgr.h:321
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
bool superuser(void)
Definition: superuser.c:47
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: heapam.c:1270
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
uint32 BlockNumber
Definition: block.h:31
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
static bytea * get_raw_page_internal(text *relname, ForkNumber forknum, BlockNumber blkno)
Definition: rawpage.c:94
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3042
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1074
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:335
#define SizeOfPageHeaderData
Definition: bufpage.h:212
Form_pg_class rd_rel
Definition: rel.h:84
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:278
uint16 pd_checksum
Definition: bufpage.h:152
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:304
#define ERROR
Definition: elog.h:43
Datum page_header(PG_FUNCTION_ARGS)
Definition: rawpage.c:213
static char * buf
Definition: pg_test_fsync.c:67
#define PageGetPageLayoutVersion(page)
Definition: bufpage.h:271
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define PageGetPageSize(page)
Definition: bufpage.h:264
#define RelationGetRelationName(relation)
Definition: rel.h:441
unsigned int uint32
Definition: c.h:325
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
Oid atttypid
Definition: pg_attribute.h:49
#define ereport(elevel, rest)
Definition: elog.h:122
ForkNumber
Definition: relpath.h:40
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3232
LocationIndex pd_special
Definition: bufpage.h:156
#define TransactionIdGetDatum(X)
Definition: postgres.h:504
Datum get_raw_page_fork(PG_FUNCTION_ARGS)
Definition: rawpage.c:75
uintptr_t Datum
Definition: postgres.h:365
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:318
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
Datum get_raw_page(PG_FUNCTION_ARGS)
Definition: rawpage.c:46
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2788
PageHeaderData * PageHeader
Definition: bufpage.h:162
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TransactionId pd_prune_xid
Definition: bufpage.h:158
PG_FUNCTION_INFO_V1(get_raw_page)
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:538
#define PG_NARGS()
Definition: fmgr.h:173
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:231
#define PageGetLSN(page)
Definition: bufpage.h:362
static Datum values[MAXATTR]
Definition: bootstrap.c:164
char * text_to_cstring(const text *t)
Definition: varlena.c:182
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:339
void * palloc(Size size)
Definition: mcxt.c:924
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
#define CStringGetTextDatum(s)
Definition: builtins.h:95
Definition: c.h:516
#define PG_FUNCTION_ARGS
Definition: fmgr.h:163
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:327
#define elog
Definition: elog.h:219
LocationIndex pd_upper
Definition: bufpage.h:155
int Buffer
Definition: buf.h:23
#define UInt16GetDatum(X)
Definition: postgres.h:448
uint16 pg_checksum_page(char *page, BlockNumber blkno)
Pointer Page
Definition: bufpage.h:74
Datum page_checksum(PG_FUNCTION_ARGS)
Definition: rawpage.c:293
LocationIndex pd_lower
Definition: bufpage.h:154