2007-05-18 03:11:25 +08:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
*
|
|
|
|
* rawpage.c
|
|
|
|
* Functions to extract a raw page as bytea and inspect it
|
|
|
|
*
|
|
|
|
* Access-method specific inspection functions are in separate files.
|
|
|
|
*
|
2009-01-02 01:24:05 +08:00
|
|
|
* Copyright (c) 2007-2009, PostgreSQL Global Development Group
|
2007-05-18 03:11:25 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2009-04-01 06:54:31 +08:00
|
|
|
* $PostgreSQL: pgsql/contrib/pageinspect/rawpage.c,v 1.11 2009/03/31 22:54:31 tgl Exp $
|
2007-05-18 03:11:25 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/heapam.h"
|
|
|
|
#include "access/transam.h"
|
2008-10-06 22:13:17 +08:00
|
|
|
#include "catalog/catalog.h"
|
2007-05-18 03:11:25 +08:00
|
|
|
#include "catalog/namespace.h"
|
|
|
|
#include "catalog/pg_type.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "fmgr.h"
|
|
|
|
#include "funcapi.h"
|
2007-05-18 03:11:25 +08:00
|
|
|
#include "miscadmin.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "storage/bufmgr.h"
|
|
|
|
#include "utils/builtins.h"
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
Datum get_raw_page(PG_FUNCTION_ARGS);
|
|
|
|
Datum page_header(PG_FUNCTION_ARGS);
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get_raw_page
|
|
|
|
*
|
|
|
|
* Returns a copy of a page from shared buffers as a bytea
|
|
|
|
*/
|
|
|
|
PG_FUNCTION_INFO_V1(get_raw_page);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
get_raw_page(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *relname = PG_GETARG_TEXT_P(0);
|
2008-10-06 22:13:17 +08:00
|
|
|
text *forkname = PG_GETARG_TEXT_P(1);
|
2008-09-30 18:52:14 +08:00
|
|
|
uint32 blkno = PG_GETARG_UINT32(2);
|
2008-10-06 22:13:17 +08:00
|
|
|
ForkNumber forknum;
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
Relation rel;
|
2007-11-16 05:14:46 +08:00
|
|
|
RangeVar *relrv;
|
|
|
|
bytea *raw_page;
|
|
|
|
char *raw_page_data;
|
2007-05-18 03:11:25 +08:00
|
|
|
Buffer buf;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use raw functions"))));
|
|
|
|
|
2008-10-06 22:13:17 +08:00
|
|
|
forknum = forkname_to_number(text_to_cstring(forkname));
|
2008-09-30 18:52:14 +08:00
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
|
|
|
/* Check that this relation has storage */
|
|
|
|
if (rel->rd_rel->relkind == RELKIND_VIEW)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("cannot get raw page from view \"%s\"",
|
2007-11-16 05:14:46 +08:00
|
|
|
RelationGetRelationName(rel))));
|
2007-05-18 03:11:25 +08:00
|
|
|
if (rel->rd_rel->relkind == RELKIND_COMPOSITE_TYPE)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("cannot get raw page from composite type \"%s\"",
|
2007-11-16 05:14:46 +08:00
|
|
|
RelationGetRelationName(rel))));
|
2007-05-18 03:11:25 +08:00
|
|
|
|
2009-04-01 06:54:31 +08:00
|
|
|
/*
|
|
|
|
* Reject attempts to read non-local temporary relations; we would
|
|
|
|
* be likely to get wrong data since we have no visibility into the
|
|
|
|
* owning session's local buffers.
|
|
|
|
*/
|
|
|
|
if (RELATION_IS_OTHER_TEMP(rel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot access temporary tables of other sessions")));
|
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
if (blkno >= RelationGetNumberOfBlocks(rel))
|
|
|
|
elog(ERROR, "block number %u is out of range for relation \"%s\"",
|
|
|
|
blkno, RelationGetRelationName(rel));
|
|
|
|
|
|
|
|
/* Initialize buffer to copy to */
|
|
|
|
raw_page = (bytea *) palloc(BLCKSZ + VARHDRSZ);
|
|
|
|
SET_VARSIZE(raw_page, BLCKSZ + VARHDRSZ);
|
|
|
|
raw_page_data = VARDATA(raw_page);
|
|
|
|
|
|
|
|
/* Take a verbatim copy of the page */
|
|
|
|
|
Unite ReadBufferWithFork, ReadBufferWithStrategy, and ZeroOrReadBuffer
functions into one ReadBufferExtended function, that takes the strategy
and mode as argument. There's three modes, RBM_NORMAL which is the default
used by plain ReadBuffer(), RBM_ZERO, which replaces ZeroOrReadBuffer, and
a new mode RBM_ZERO_ON_ERROR, which allows callers to read corrupt pages
without throwing an error. The FSM needs the new mode to recover from
corrupt pages, which could happend if we crash after extending an FSM file,
and the new page is "torn".
Add fork number to some error messages in bufmgr.c, that still lacked it.
2008-10-31 23:05:00 +08:00
|
|
|
buf = ReadBufferExtended(rel, forknum, blkno, RBM_NORMAL, NULL);
|
2007-05-18 03:11:25 +08:00
|
|
|
LockBuffer(buf, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
memcpy(raw_page_data, BufferGetPage(buf), BLCKSZ);
|
|
|
|
|
|
|
|
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buf);
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(raw_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* page_header
|
|
|
|
*
|
|
|
|
* Allows inspection of page header fields of a raw page
|
|
|
|
*/
|
|
|
|
|
|
|
|
PG_FUNCTION_INFO_V1(page_header);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
page_header(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bytea *raw_page = PG_GETARG_BYTEA_P(0);
|
|
|
|
int raw_page_size;
|
|
|
|
|
|
|
|
TupleDesc tupdesc;
|
|
|
|
|
|
|
|
Datum result;
|
|
|
|
HeapTuple tuple;
|
2007-09-22 05:25:42 +08:00
|
|
|
Datum values[9];
|
|
|
|
bool nulls[9];
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
PageHeader page;
|
|
|
|
XLogRecPtr lsn;
|
|
|
|
char lsnchar[64];
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use raw page functions"))));
|
|
|
|
|
|
|
|
raw_page_size = VARSIZE(raw_page) - VARHDRSZ;
|
|
|
|
|
|
|
|
/*
|
2007-11-16 05:14:46 +08:00
|
|
|
* Check that enough data was supplied, so that we don't try to access
|
|
|
|
* fields outside the supplied buffer.
|
2007-05-18 03:11:25 +08:00
|
|
|
*/
|
2007-11-16 05:14:46 +08:00
|
|
|
if (raw_page_size < sizeof(PageHeaderData))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input page too small (%d bytes)", raw_page_size)));
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
page = (PageHeader) VARDATA(raw_page);
|
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
|
|
|
/* Extract information from the page header */
|
|
|
|
|
|
|
|
lsn = PageGetLSN(page);
|
|
|
|
snprintf(lsnchar, sizeof(lsnchar), "%X/%X", lsn.xlogid, lsn.xrecoff);
|
|
|
|
|
2008-03-26 06:42:46 +08:00
|
|
|
values[0] = CStringGetTextDatum(lsnchar);
|
2007-05-18 03:11:25 +08:00
|
|
|
values[1] = UInt16GetDatum(PageGetTLI(page));
|
|
|
|
values[2] = UInt16GetDatum(page->pd_flags);
|
|
|
|
values[3] = UInt16GetDatum(page->pd_lower);
|
|
|
|
values[4] = UInt16GetDatum(page->pd_upper);
|
|
|
|
values[5] = UInt16GetDatum(page->pd_special);
|
|
|
|
values[6] = UInt16GetDatum(PageGetPageSize(page));
|
|
|
|
values[7] = UInt16GetDatum(PageGetPageLayoutVersion(page));
|
2007-09-22 05:25:42 +08:00
|
|
|
values[8] = TransactionIdGetDatum(page->pd_prune_xid);
|
2007-05-18 03:11:25 +08:00
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
/* Build and return the tuple. */
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
memset(nulls, 0, sizeof(nulls));
|
|
|
|
|
2007-11-16 05:14:46 +08:00
|
|
|
tuple = heap_form_tuple(tupdesc, values, nulls);
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|