PostgreSQL Source Code  git master
rawpage.c File Reference
#include "postgres.h"
#include "pageinspect.h"
#include "access/htup_details.h"
#include "access/relation.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

◆ get_page_from_raw()

Page get_page_from_raw ( bytea raw_page)

Definition at line 188 of file rawpage.c.

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

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

189 {
190  Page page;
191  int raw_page_size;
192 
193  raw_page_size = VARSIZE_ANY_EXHDR(raw_page);
194 
195  if (raw_page_size != BLCKSZ)
196  ereport(ERROR,
197  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
198  errmsg("invalid page size"),
199  errdetail("Expected %d bytes, got %d.",
200  BLCKSZ, raw_page_size)));
201 
202  page = palloc(raw_page_size);
203 
204  memcpy(page, VARDATA_ANY(raw_page), raw_page_size);
205 
206  return page;
207 }
#define VARDATA_ANY(PTR)
Definition: postgres.h:348
int errcode(int sqlerrcode)
Definition: elog.c:570
#define ERROR
Definition: elog.h:43
int errdetail(const char *fmt,...)
Definition: elog.c:860
#define ereport(elevel, rest)
Definition: elog.h:141
Page page
Definition: btreefuncs.c:246
#define VARSIZE_ANY_EXHDR(PTR)
Definition: postgres.h:341
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
Pointer Page
Definition: bufpage.h:78

◆ get_raw_page()

Datum get_raw_page ( PG_FUNCTION_ARGS  )

Definition at line 47 of file rawpage.c.

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

48 {
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:265
int errhint(const char *fmt,...)
Definition: elog.c:974
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:360
NameData relname
Definition: pg_class.h:35
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:358
#define ereport(elevel, rest)
Definition: elog.h:141
#define PG_NARGS()
Definition: fmgr.h:198
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549

◆ get_raw_page_fork()

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, relname, and text_to_cstring().

Referenced by get_raw_page().

77 {
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:265
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:360
NameData relname
Definition: pg_class.h:35
ForkNumber forkname_to_number(const char *forkName)
Definition: relpath.c:47
#define PG_GETARG_TEXT_PP(n)
Definition: fmgr.h:303
unsigned int uint32
Definition: c.h:358
ForkNumber
Definition: relpath.h:40
char * text_to_cstring(const text *t)
Definition: varlena.c:204
Definition: c.h:549

◆ get_raw_page_internal()

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(), palloc(), RBM_NORMAL, RelationData::rd_rel, ReadBufferExtended(), relation_close(), RELATION_IS_OTHER_TEMP, relation_openrv(), RelationGetNumberOfBlocksInFork(), RelationGetRelationName, ReleaseBuffer(), 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 page 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  if (rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
133  ereport(ERROR,
134  (errcode(ERRCODE_WRONG_OBJECT_TYPE),
135  errmsg("cannot get raw page from partitioned index \"%s\"",
136  RelationGetRelationName(rel))));
137 
138  /*
139  * Reject attempts to read non-local temporary relations; we would be
140  * likely to get wrong data since we have no visibility into the owning
141  * session's local buffers.
142  */
143  if (RELATION_IS_OTHER_TEMP(rel))
144  ereport(ERROR,
145  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
146  errmsg("cannot access temporary tables of other sessions")));
147 
148  if (blkno >= RelationGetNumberOfBlocksInFork(rel, forknum))
149  ereport(ERROR,
150  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
151  errmsg("block number %u is out of range for relation \"%s\"",
152  blkno, RelationGetRelationName(rel))));
153 
154  /* Initialize buffer to copy to */
155  raw_page = (bytea *) palloc(BLCKSZ + VARHDRSZ);
156  SET_VARSIZE(raw_page, BLCKSZ + VARHDRSZ);
157  raw_page_data = VARDATA(raw_page);
158 
159  /* Take a verbatim copy of the page */
160 
161  buf = ReadBufferExtended(rel, forknum, blkno, RBM_NORMAL, NULL);
163 
164  memcpy(raw_page_data, BufferGetPage(buf), BLCKSZ);
165 
167  ReleaseBuffer(buf);
168 
170 
171  return raw_page;
172 }
#define BUFFER_LOCK_UNLOCK
Definition: bufmgr.h:86
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARHDRSZ
Definition: c.h:555
Buffer ReadBufferExtended(Relation reln, ForkNumber forkNum, BlockNumber blockNum, ReadBufferMode mode, BufferAccessStrategy strategy)
Definition: bufmgr.c:642
#define AccessShareLock
Definition: lockdefs.h:36
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
void ReleaseBuffer(Buffer buffer)
Definition: bufmgr.c:3365
RangeVar * makeRangeVarFromNameList(List *names)
Definition: namespace.c:3054
Form_pg_class rd_rel
Definition: rel.h:83
#define ERROR
Definition: elog.h:43
static char * buf
Definition: pg_test_fsync.c:68
#define RelationGetRelationName(relation)
Definition: rel.h:453
#define BufferGetPage(buffer)
Definition: bufmgr.h:159
#define ereport(elevel, rest)
Definition: elog.h:141
List * textToQualifiedNameList(text *textval)
Definition: varlena.c:3586
void LockBuffer(Buffer buffer, int mode)
Definition: bufmgr.c:3602
Relation relation_openrv(const RangeVar *relation, LOCKMODE lockmode)
Definition: relation.c:138
BlockNumber RelationGetNumberOfBlocksInFork(Relation relation, ForkNumber forkNum)
Definition: bufmgr.c:2800
void relation_close(Relation relation, LOCKMODE lockmode)
Definition: relation.c:206
#define RELATION_IS_OTHER_TEMP(relation)
Definition: rel.h:549
void * palloc(Size size)
Definition: mcxt.c:949
int errmsg(const char *fmt,...)
Definition: elog.c:784
#define BUFFER_LOCK_SHARE
Definition: bufmgr.h:87
Definition: c.h:549
#define SET_VARSIZE(PTR, len)
Definition: postgres.h:329
int Buffer
Definition: buf.h:23

◆ page_checksum()

Datum page_checksum ( PG_FUNCTION_ARGS  )

Definition at line 299 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.

Referenced by page_header().

300 {
301  bytea *raw_page = PG_GETARG_BYTEA_P(0);
302  uint32 blkno = PG_GETARG_INT32(1);
303  int raw_page_size;
305 
306  if (!superuser())
307  ereport(ERROR,
308  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
309  (errmsg("must be superuser to use raw page functions"))));
310 
311  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
312 
313  /*
314  * Check that the supplied page is of the right size.
315  */
316  if (raw_page_size != BLCKSZ)
317  ereport(ERROR,
318  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
319  errmsg("incorrect size of input page (%d bytes)", raw_page_size)));
320 
321  page = (PageHeader) VARDATA(raw_page);
322 
323  PG_RETURN_INT16(pg_checksum_page((char *) page, blkno));
324 }
#define PG_GETARG_INT32(n)
Definition: fmgr.h:264
#define VARDATA(PTR)
Definition: postgres.h:302
#define VARSIZE(PTR)
Definition: postgres.h:303
#define VARHDRSZ
Definition: c.h:555
#define PG_RETURN_INT16(x)
Definition: fmgr.h:346
int errcode(int sqlerrcode)
Definition: elog.c:570
bool superuser(void)
Definition: superuser.c:47
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:329
#define ERROR
Definition: elog.h:43
unsigned int uint32
Definition: c.h:358
#define ereport(elevel, rest)
Definition: elog.h:141
Page page
Definition: btreefuncs.c:246
PageHeaderData * PageHeader
Definition: bufpage.h:166
int errmsg(const char *fmt,...)
Definition: elog.c:784
Definition: c.h:549
uint16 pg_checksum_page(char *page, BlockNumber blkno)

◆ page_header()

Datum page_header ( PG_FUNCTION_ARGS  )

Definition at line 219 of file rawpage.c.

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

Referenced by get_page_from_raw().

220 {
221  bytea *raw_page = PG_GETARG_BYTEA_P(0);
222  int raw_page_size;
223 
224  TupleDesc tupdesc;
225 
226  Datum result;
227  HeapTuple tuple;
228  Datum values[9];
229  bool nulls[9];
230 
232  XLogRecPtr lsn;
233 
234  if (!superuser())
235  ereport(ERROR,
236  (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
237  (errmsg("must be superuser to use raw page functions"))));
238 
239  raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
240 
241  /*
242  * Check that enough data was supplied, so that we don't try to access
243  * fields outside the supplied buffer.
244  */
245  if (raw_page_size < SizeOfPageHeaderData)
246  ereport(ERROR,
247  (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
248  errmsg("input page too small (%d bytes)", raw_page_size)));
249 
250  page = (PageHeader) VARDATA(raw_page);
251 
252  /* Build a tuple descriptor for our result type */
253  if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
254  elog(ERROR, "return type must be a row type");
255 
256  /* Extract information from the page header */
257 
258  lsn = PageGetLSN(page);
259 
260  /* pageinspect >= 1.2 uses pg_lsn instead of text for the LSN field. */
261  if (TupleDescAttr(tupdesc, 0)->atttypid == TEXTOID)
262  {
263  char lsnchar[64];
264 
265  snprintf(lsnchar, sizeof(lsnchar), "%X/%X",
266  (uint32) (lsn >> 32), (uint32) lsn);
267  values[0] = CStringGetTextDatum(lsnchar);
268  }
269  else
270  values[0] = LSNGetDatum(lsn);
271  values[1] = UInt16GetDatum(page->pd_checksum);
272  values[2] = UInt16GetDatum(page->pd_flags);
273  values[3] = UInt16GetDatum(page->pd_lower);
274  values[4] = UInt16GetDatum(page->pd_upper);
275  values[5] = UInt16GetDatum(page->pd_special);
276  values[6] = UInt16GetDatum(PageGetPageSize(page));
277  values[7] = UInt16GetDatum(PageGetPageLayoutVersion(page));
278  values[8] = TransactionIdGetDatum(page->pd_prune_xid);
279 
280  /* Build and return the tuple. */
281 
282  memset(nulls, 0, sizeof(nulls));
283 
284  tuple = heap_form_tuple(tupdesc, values, nulls);
285  result = HeapTupleGetDatum(tuple);
286 
287  PG_RETURN_DATUM(result);
288 }
TypeFuncClass get_call_result_type(FunctionCallInfo fcinfo, Oid *resultTypeId, TupleDesc *resultTupleDesc)
Definition: funcapi.c:196
#define VARDATA(PTR)
Definition: postgres.h:302
uint16 pd_flags
Definition: bufpage.h:157
#define VARSIZE(PTR)
Definition: postgres.h:303
#define TupleDescAttr(tupdesc, i)
Definition: tupdesc.h:92
#define VARHDRSZ
Definition: c.h:555
int errcode(int sqlerrcode)
Definition: elog.c:570
#define LSNGetDatum(X)
Definition: pg_lsn.h:22
bool superuser(void)
Definition: superuser.c:47
HeapTuple heap_form_tuple(TupleDesc tupleDescriptor, Datum *values, bool *isnull)
Definition: heaptuple.c:1020
#define SizeOfPageHeaderData
Definition: bufpage.h:216
uint16 pd_checksum
Definition: bufpage.h:156
#define PG_GETARG_BYTEA_P(n)
Definition: fmgr.h:329
#define ERROR
Definition: elog.h:43
#define PageGetPageLayoutVersion(page)
Definition: bufpage.h:275
#define PageGetPageSize(page)
Definition: bufpage.h:268
unsigned int uint32
Definition: c.h:358
Oid atttypid
Definition: pg_attribute.h:49
#define ereport(elevel, rest)
Definition: elog.h:141
LocationIndex pd_special
Definition: bufpage.h:160
#define TransactionIdGetDatum(X)
Definition: postgres.h:521
uintptr_t Datum
Definition: postgres.h:367
#define PG_RETURN_DATUM(x)
Definition: fmgr.h:343
Page page
Definition: btreefuncs.c:246
PageHeaderData * PageHeader
Definition: bufpage.h:166
uint64 XLogRecPtr
Definition: xlogdefs.h:21
TransactionId pd_prune_xid
Definition: bufpage.h:162
#define HeapTupleGetDatum(tuple)
Definition: funcapi.h:221
#define PageGetLSN(page)
Definition: bufpage.h:366
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 CStringGetTextDatum(s)
Definition: builtins.h:83
Definition: c.h:549
LocationIndex pd_upper
Definition: bufpage.h:159
#define snprintf
Definition: port.h:192
#define UInt16GetDatum(X)
Definition: postgres.h:465
LocationIndex pd_lower
Definition: bufpage.h:158

◆ PG_FUNCTION_INFO_V1() [1/4]

PG_FUNCTION_INFO_V1 ( get_raw_page  )

◆ PG_FUNCTION_INFO_V1() [2/4]

PG_FUNCTION_INFO_V1 ( get_raw_page_fork  )

◆ PG_FUNCTION_INFO_V1() [3/4]

PG_FUNCTION_INFO_V1 ( page_header  )

◆ PG_FUNCTION_INFO_V1() [4/4]

PG_FUNCTION_INFO_V1 ( page_checksum  )

Variable Documentation

◆ PG_MODULE_MAGIC

PG_MODULE_MAGIC

Definition at line 33 of file rawpage.c.