1 /*-------------------------------------------------------------------------
4 * Functions to extract a raw page as bytea and inspect it
6 * Access-method specific inspection functions are in separate files.
8 * Copyright (c) 2007-2008, PostgreSQL Global Development Group
13 *-------------------------------------------------------------------------
18 #include "access/heapam.h"
19 #include "access/transam.h"
20 #include "catalog/namespace.h"
21 #include "catalog/pg_type.h"
24 #include "miscadmin.h"
25 #include "storage/bufmgr.h"
26 #include "utils/builtins.h"
30 Datum
get_raw_page(PG_FUNCTION_ARGS
);
31 Datum
page_header(PG_FUNCTION_ARGS
);
36 * Returns a copy of a page from shared buffers as a bytea
38 PG_FUNCTION_INFO_V1(get_raw_page
);
41 get_raw_page(PG_FUNCTION_ARGS
)
43 text
*relname
= PG_GETARG_TEXT_P(0);
44 uint32 blkno
= PG_GETARG_UINT32(1);
54 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
55 (errmsg("must be superuser to use raw functions"))));
57 relrv
= makeRangeVarFromNameList(textToQualifiedNameList(relname
));
58 rel
= relation_openrv(relrv
, AccessShareLock
);
60 /* Check that this relation has storage */
61 if (rel
->rd_rel
->relkind
== RELKIND_VIEW
)
63 (errcode(ERRCODE_WRONG_OBJECT_TYPE
),
64 errmsg("cannot get raw page from view \"%s\"",
65 RelationGetRelationName(rel
))));
66 if (rel
->rd_rel
->relkind
== RELKIND_COMPOSITE_TYPE
)
68 (errcode(ERRCODE_WRONG_OBJECT_TYPE
),
69 errmsg("cannot get raw page from composite type \"%s\"",
70 RelationGetRelationName(rel
))));
72 if (blkno
>= RelationGetNumberOfBlocks(rel
))
73 elog(ERROR
, "block number %u is out of range for relation \"%s\"",
74 blkno
, RelationGetRelationName(rel
));
76 /* Initialize buffer to copy to */
77 raw_page
= (bytea
*) palloc(BLCKSZ
+ VARHDRSZ
);
78 SET_VARSIZE(raw_page
, BLCKSZ
+ VARHDRSZ
);
79 raw_page_data
= VARDATA(raw_page
);
81 /* Take a verbatim copy of the page */
83 buf
= ReadBuffer(rel
, blkno
);
84 LockBuffer(buf
, BUFFER_LOCK_SHARE
);
86 memcpy(raw_page_data
, BufferGetPage(buf
), BLCKSZ
);
88 LockBuffer(buf
, BUFFER_LOCK_UNLOCK
);
91 relation_close(rel
, AccessShareLock
);
93 PG_RETURN_BYTEA_P(raw_page
);
99 * Allows inspection of page header fields of a raw page
102 PG_FUNCTION_INFO_V1(page_header
);
105 page_header(PG_FUNCTION_ARGS
)
107 bytea
*raw_page
= PG_GETARG_BYTEA_P(0);
123 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
124 (errmsg("must be superuser to use raw page functions"))));
126 raw_page_size
= VARSIZE(raw_page
) - VARHDRSZ
;
129 * Check that enough data was supplied, so that we don't try to access
130 * fields outside the supplied buffer.
132 if (raw_page_size
< sizeof(PageHeaderData
))
134 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
135 errmsg("input page too small (%d bytes)", raw_page_size
)));
137 page
= (PageHeader
) VARDATA(raw_page
);
139 /* Build a tuple descriptor for our result type */
140 if (get_call_result_type(fcinfo
, NULL
, &tupdesc
) != TYPEFUNC_COMPOSITE
)
141 elog(ERROR
, "return type must be a row type");
143 /* Extract information from the page header */
145 lsn
= PageGetLSN(page
);
146 snprintf(lsnchar
, sizeof(lsnchar
), "%X/%X", lsn
.xlogid
, lsn
.xrecoff
);
148 values
[0] = CStringGetTextDatum(lsnchar
);
149 values
[1] = UInt16GetDatum(PageGetTLI(page
));
150 values
[2] = UInt16GetDatum(page
->pd_flags
);
151 values
[3] = UInt16GetDatum(page
->pd_lower
);
152 values
[4] = UInt16GetDatum(page
->pd_upper
);
153 values
[5] = UInt16GetDatum(page
->pd_special
);
154 values
[6] = UInt16GetDatum(PageGetPageSize(page
));
155 values
[7] = UInt16GetDatum(PageGetPageLayoutVersion(page
));
156 values
[8] = TransactionIdGetDatum(page
->pd_prune_xid
);
158 /* Build and return the tuple. */
160 memset(nulls
, 0, sizeof(nulls
));
162 tuple
= heap_form_tuple(tupdesc
, values
, nulls
);
163 result
= HeapTupleGetDatum(tuple
);
165 PG_RETURN_DATUM(result
);