PostgreSQL Source Code  git master
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros
rawpage.c File Reference
#include "postgres.h"
#include "pageinspect.h"
#include "access/htup_details.h"
#include "catalog/catalog.h"
#include "catalog/namespace.h"
#include "catalog/pg_type.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "storage/bufmgr.h"
#include "storage/checksum.h"
#include "utils/builtins.h"
#include "utils/pg_lsn.h"
#include "utils/rel.h"
#include "utils/varlena.h"
Include dependency graph for rawpage.c:

Go to the source code of this file.

Functions

static byteaget_raw_page_internal (text *relname, ForkNumber forknum, BlockNumber blkno)
 
 PG_FUNCTION_INFO_V1 (get_raw_page)
 
Datum get_raw_page (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (get_raw_page_fork)
 
Datum get_raw_page_fork (PG_FUNCTION_ARGS)
 
Page get_page_from_raw (bytea *raw_page)
 
 PG_FUNCTION_INFO_V1 (page_header)
 
Datum page_header (PG_FUNCTION_ARGS)
 
 PG_FUNCTION_INFO_V1 (page_checksum)
 
Datum page_checksum (PG_FUNCTION_ARGS)
 

Variables

 PG_MODULE_MAGIC
 

Function Documentation

Page get_page_from_raw ( bytea raw_page)

Definition at line 183 of file rawpage.c.

References ereport, errcode(), errdetail(), errmsg(), ERROR, palloc(), VARDATA_ANY, and VARSIZE_ANY_EXHDR.

Referenced by gin_leafpage_items(), gin_metapage_info(), gin_page_opaque_info(), and verify_hash_page().

184 {
185  Page page;
186  int raw_page_size;
187 
188  raw_page_size = VARSIZE_ANY_EXHDR(raw_page);
189 
190  if (raw_page_size != BLCKSZ)
191  ereport(ERROR,
192  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
193  errmsg("invalid page size"),
194  errdetail("Expected %d bytes, got %d.",
195  BLCKSZ, raw_page_size)));
196 
197  page = palloc(raw_page_size);
198 
199  memcpy(page, VARDATA_ANY(raw_page), raw_page_size);
200 
201  return page;
202 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:347
int errcode(int sqlerrcode)
Definition: elog.c:575
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
Page page
Definition: btreefuncs.c:245
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:340
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
Pointer Page
Definition: bufpage.h:74
Datum get_raw_page ( PG_FUNCTION_ARGS  )

Definition at line 47 of file rawpage.c.

References ereport, errhint(), errmsg(), ERROR, get_raw_page_internal(), MAIN_FORKNUM, PG_GETARG_TEXT_PP, PG_GETARG_UINT32, PG_NARGS, and PG_RETURN_BYTEA_P.

48 {
49  text *relname = PG_GETARG_TEXT_PP(0);
50  uint32 blkno = PG_GETARG_UINT32(1);
51  bytea *raw_page;
52 
53  /*
54  * We don't normally bother to check the number of arguments to a C
55  * function, but here it's needed for safety because early 8.4 beta
56  * releases mistakenly redefined get_raw_page() as taking three arguments.
57  */
58  if (PG_NARGS() != 2)
59  ereport(ERROR,
60  (errmsg("wrong number of arguments to get_raw_page()"),
61  errhint("Run the updated pageinspect.sql script.")));
62 
63  raw_page = get_raw_page_internal(relname, MAIN_FORKNUM, blkno);
64 
65  PG_RETURN_BYTEA_P(raw_page);
66 }
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:235
int errhint(const char *fmt,...)
Definition: elog.c:987
static bytea * get_raw_page_internal(text *relname, ForkNumber forknum, BlockNumber blkno)
Definition: rawpage.c:95
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:268
#define ereport(elevel, rest)
Definition: elog.h:122
#define PG_NARGS()
Definition: fmgr.h:168
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
Datum get_raw_page_fork ( PG_FUNCTION_ARGS  )

Definition at line 76 of file rawpage.c.

References forkname_to_number(), get_raw_page_internal(), PG_GETARG_TEXT_PP, PG_GETARG_UINT32, PG_RETURN_BYTEA_P, and text_to_cstring().

77 {
78  text *relname = PG_GETARG_TEXT_PP(0);
79  text *forkname = PG_GETARG_TEXT_PP(1);
80  uint32 blkno = PG_GETARG_UINT32(2);
81  bytea *raw_page;
82  ForkNumber forknum;
83 
84  forknum = forkname_to_number(text_to_cstring(forkname));
85 
86  raw_page = get_raw_page_internal(relname, forknum, blkno);
87 
88  PG_RETURN_BYTEA_P(raw_page);
89 }
#define PG_GETARG_UINT32(n)
Definition: fmgr.h:235
static bytea * get_raw_page_internal(text *relname, ForkNumber forknum, BlockNumber blkno)
Definition: rawpage.c:95
#define PG_RETURN_BYTEA_P(x)
Definition: fmgr.h:329
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:48
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:273
unsigned int uint32
Definition: c.h:268
ForkNumber
Definition: relpath.h:24
char * text_to_cstring(const text *t)
Definition: varlena.c:182
Definition: c.h:439
static bytea * get_raw_page_internal ( text relname,
ForkNumber  forknum,
BlockNumber  blkno 
)
static

Definition at line 95 of file rawpage.c.

References AccessShareLock, buf, BUFFER_LOCK_SHARE, BUFFER_LOCK_UNLOCK, BufferGetPage, ereport, errcode(), errmsg(), ERROR, LockBuffer(), makeRangeVarFromNameList(), NULL, palloc(), RBM_NORMAL, RelationData::rd_rel, ReadBufferExtended(), relation_close(), RELATION_IS_OTHER_TEMP, relation_openrv(), RelationGetNumberOfBlocksInFork(), RelationGetRelationName, ReleaseBuffer(), RELKIND_COMPOSITE_TYPE, RELKIND_FOREIGN_TABLE, RELKIND_PARTITIONED_TABLE, RELKIND_VIEW, SET_VARSIZE, superuser(), textToQualifiedNameList(), VARDATA, and VARHDRSZ.

Referenced by get_raw_page(), and get_raw_page_fork().

96 {
97  bytea *raw_page;
98  RangeVar *relrv;
99  Relation rel;
100  char *raw_page_data;
101  Buffer buf;
102 
103  if (!superuser())
104  ereport(ERROR,
105  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
106  (errmsg("must be superuser to use raw functions"))));
107 
109  rel = relation_openrv(relrv, AccessShareLock);
110 
111  /* Check that this relation has storage */
112  if (rel->rd_rel->relkind == RELKIND_VIEW)
113  ereport(ERROR,
114  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
115  errmsg("cannot get raw page from view \"%s\"",
116  RelationGetRelationName(rel))));
117  if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
118  ereport(ERROR,
119  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
120  errmsg("cannot get raw page from composite type \"%s\"",
121  RelationGetRelationName(rel))));
122  if (rel->rd_rel->relkind == RELKIND_FOREIGN_TABLE)
123  ereport(ERROR,
124  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
125  errmsg("cannot get raw page from foreign table \"%s\"",
126  RelationGetRelationName(rel))));
127  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
128  ereport(ERROR,
129  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
130  errmsg("cannot get raw page from partitioned table \"%s\"",
131  RelationGetRelationName(rel))));
132 
133  /*
134  * Reject attempts to read non-local temporary relations; we would be
135  * likely to get wrong data since we have no visibility into the owning
136  * session's local buffers.
137  */
138  if (RELATION_IS_OTHER_TEMP(rel))
139  ereport(ERROR,
140  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
141  errmsg("cannot access temporary tables of other sessions")));
142 
143  if (blkno >= RelationGetNumberOfBlocksInFork(rel, forknum))
144  ereport(ERROR,
145  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
146  errmsg("block number %u is out of range for relation \"%s\"",
147  blkno, RelationGetRelationName(rel))));
148 
149  /* Initialize buffer to copy to */
150  raw_page = (bytea *) palloc(BLCKSZ + VARHDRSZ);
151  SET_VARSIZE(raw_page, BLCKSZ + VARHDRSZ);
152  raw_page_data = VARDATA(raw_page);
153 
154  /* Take a verbatim copy of the page */
155 
156  buf = ReadBufferExtended(rel, forknum, blkno, RBM_NORMAL, NULL);
158 
159  memcpy(raw_page_data, BufferGetPage(buf), BLCKSZ);
160 
162  ReleaseBuffer(buf);
163 
165 
166  return raw_page;
167 }
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: heapam.c:1192
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:87
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:445
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:640
#define AccessShareLock
Definition: lockdefs.h:36
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
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3309
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:2980
Form_pg_class rd_rel
Definition: rel.h:114
#define RELKIND_COMPOSITE_TYPE
Definition: pg_class.h:166
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:66
#define RelationGetRelationName(relation)
Definition: rel.h:437
#define RELKIND_FOREIGN_TABLE
Definition: pg_class.h:167
#define BufferGetPage(buffer)
Definition: bufmgr.h:160
#define ereport(elevel, rest)
Definition: elog.h:122
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3189
#define RELKIND_PARTITIONED_TABLE
Definition: pg_class.h:168
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3546
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2788
#define NULL
Definition: c.h:229
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:534
void * palloc(Size size)
Definition: mcxt.c:849
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define RELKIND_VIEW
Definition: pg_class.h:164
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:88
Definition: c.h:439
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:328
int Buffer
Definition: buf.h:23
Datum page_checksum ( PG_FUNCTION_ARGS  )

Definition at line 294 of file rawpage.c.

References ereport, errcode(), errmsg(), ERROR, pg_checksum_page(), PG_GETARG_BYTEA_P, PG_GETARG_INT32, PG_RETURN_INT16, superuser(), VARDATA, VARHDRSZ, and VARSIZE.

295 {
296  bytea *raw_page = PG_GETARG_BYTEA_P(0);
297  uint32 blkno = PG_GETARG_INT32(1);
298  int raw_page_size;
300 
301  if (!superuser())
302  ereport(ERROR,
303  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
304  (errmsg("must be superuser to use raw page functions"))));
305 
306  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
307 
308  /*
309  * Check that the supplied page is of the right size.
310  */
311  if (raw_page_size != BLCKSZ)
312  ereport(ERROR,
313  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
314  errmsg("incorrect size of input page (%d bytes)", raw_page_size)));
315 
316  page = (PageHeader) VARDATA(raw_page);
317 
318  PG_RETURN_INT16(pg_checksum_page((char *) page, blkno));
319 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:234
#define VARDATA(PTR)
Definition: postgres.h:303
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
#define PG_RETURN_INT16(x)
Definition: fmgr.h:316
int errcode(int sqlerrcode)
Definition: elog.c:575
bool superuser(void)
Definition: superuser.c:47
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:268
#define ereport(elevel, rest)
Definition: elog.h:122
Page page
Definition: btreefuncs.c:245
PageHeaderData * PageHeader
Definition: bufpage.h:162
int errmsg(const char *fmt,...)
Definition: elog.c:797
Definition: c.h:439
uint16 pg_checksum_page(char *page, BlockNumber blkno)
Datum page_header ( PG_FUNCTION_ARGS  )

Definition at line 214 of file rawpage.c.

References tupleDesc::attrs, CStringGetTextDatum, elog, ereport, errcode(), errmsg(), ERROR, get_call_result_type(), heap_form_tuple(), HeapTupleGetDatum, LSNGetDatum, NULL, PageGetLSN, PageGetPageLayoutVersion, PageGetPageSize, PageHeaderData::pd_checksum, PageHeaderData::pd_flags, PageHeaderData::pd_lower, PageHeaderData::pd_prune_xid, PageHeaderData::pd_special, PageHeaderData::pd_upper, PG_GETARG_BYTEA_P, PG_RETURN_DATUM, result, SizeOfPageHeaderData, snprintf(), superuser(), TEXTOID, TransactionIdGetDatum, TYPEFUNC_COMPOSITE, UInt16GetDatum, values, VARDATA, VARHDRSZ, and VARSIZE.

215 {
216  bytea *raw_page = PG_GETARG_BYTEA_P(0);
217  int raw_page_size;
218 
219  TupleDesc tupdesc;
220 
221  Datum result;
222  HeapTuple tuple;
223  Datum values[9];
224  bool nulls[9];
225 
227  XLogRecPtr lsn;
228 
229  if (!superuser())
230  ereport(ERROR,
231  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
232  (errmsg("must be superuser to use raw page functions"))));
233 
234  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
235 
236  /*
237  * Check that enough data was supplied, so that we don't try to access
238  * fields outside the supplied buffer.
239  */
240  if (raw_page_size < SizeOfPageHeaderData)
241  ereport(ERROR,
242  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
243  errmsg("input page too small (%d bytes)", raw_page_size)));
244 
245  page = (PageHeader) VARDATA(raw_page);
246 
247  /* Build a tuple descriptor for our result type */
248  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
249  elog(ERROR, "return type must be a row type");
250 
251  /* Extract information from the page header */
252 
253  lsn = PageGetLSN(page);
254 
255  /* pageinspect >= 1.2 uses pg_lsn instead of text for the LSN field. */
256  if (tupdesc->attrs[0]->atttypid == TEXTOID)
257  {
258  char lsnchar[64];
259 
260  snprintf(lsnchar, sizeof(lsnchar), "%X/%X",
261  (uint32) (lsn >> 32), (uint32) lsn);
262  values[0] = CStringGetTextDatum(lsnchar);
263  }
264  else
265  values[0] = LSNGetDatum(lsn);
266  values[1] = UInt16GetDatum(page->pd_checksum);
267  values[2] = UInt16GetDatum(page->pd_flags);
268  values[3] = UInt16GetDatum(page->pd_lower);
269  values[4] = UInt16GetDatum(page->pd_upper);
270  values[5] = UInt16GetDatum(page->pd_special);
271  values[6] = UInt16GetDatum(PageGetPageSize(page));
272  values[7] = UInt16GetDatum(PageGetPageLayoutVersion(page));
273  values[8] = TransactionIdGetDatum(page->pd_prune_xid);
274 
275  /* Build and return the tuple. */
276 
277  memset(nulls, 0, sizeof(nulls));
278 
279  tuple = heap_form_tuple(tupdesc, values, nulls);
280  result = HeapTupleGetDatum(tuple);
281 
282  PG_RETURN_DATUM(result);
283 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:211
#define VARDATA(PTR)
Definition: postgres.h:303
uint16 pd_flags
Definition: bufpage.h:153
#define TEXTOID
Definition: pg_type.h:324
#define VARSIZE(PTR)
Definition: postgres.h:304
#define VARHDRSZ
Definition: c.h:445
Form_pg_attribute * attrs
Definition: tupdesc.h:74
int errcode(int sqlerrcode)
Definition: elog.c:575
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
bool superuser(void)
Definition: superuser.c:47
return result
Definition: formatting.c:1632
int snprintf(char *str, size_t count, const char *fmt,...) pg_attribute_printf(3
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:692
#define SizeOfPageHeaderData
Definition: bufpage.h:213
uint16 pd_checksum
Definition: bufpage.h:152
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:299
#define ERROR
Definition: elog.h:43
#define PageGetPageLayoutVersion(page)
Definition: bufpage.h:272
#define PageGetPageSize(page)
Definition: bufpage.h:265
unsigned int uint32
Definition: c.h:268
#define ereport(elevel, rest)
Definition: elog.h:122
LocationIndex pd_special
Definition: bufpage.h:156
#define TransactionIdGetDatum(X)
Definition: postgres.h:527
uintptr_t Datum
Definition: postgres.h:372
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:313
Page page
Definition: btreefuncs.c:245
PageHeaderData * PageHeader
Definition: bufpage.h:162
#define NULL
Definition: c.h:229
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TransactionId pd_prune_xid
Definition: bufpage.h:158
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:222
#define PageGetLSN(page)
Definition: bufpage.h:363
static Datum values[MAXATTR]
Definition: bootstrap.c:163
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define CStringGetTextDatum(s)
Definition: builtins.h:91
Definition: c.h:439
#define elog
Definition: elog.h:219
LocationIndex pd_upper
Definition: bufpage.h:155
#define UInt16GetDatum(X)
Definition: postgres.h:471
LocationIndex pd_lower
Definition: bufpage.h:154
PG_FUNCTION_INFO_V1 ( get_raw_page  )
PG_FUNCTION_INFO_V1 ( get_raw_page_fork  )
PG_FUNCTION_INFO_V1 ( page_header  )
PG_FUNCTION_INFO_V1 ( page_checksum  )

Variable Documentation

PG_MODULE_MAGIC

Definition at line 33 of file rawpage.c.