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/catalog.h"
21 #include "catalog/namespace.h"
22 #include "catalog/pg_type.h"
25 #include "miscadmin.h"
26 #include "storage/bufmgr.h"
27 #include "utils/builtins.h"
31 Datum
get_raw_page(PG_FUNCTION_ARGS
);
32 Datum
page_header(PG_FUNCTION_ARGS
);
37 * Returns a copy of a page from shared buffers as a bytea
39 PG_FUNCTION_INFO_V1(get_raw_page
);
42 get_raw_page(PG_FUNCTION_ARGS
)
44 text
*relname
= PG_GETARG_TEXT_P(0);
45 text
*forkname
= PG_GETARG_TEXT_P(1);
46 uint32 blkno
= PG_GETARG_UINT32(2);
57 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
58 (errmsg("must be superuser to use raw functions"))));
60 forknum
= forkname_to_number(text_to_cstring(forkname
));
62 relrv
= makeRangeVarFromNameList(textToQualifiedNameList(relname
));
63 rel
= relation_openrv(relrv
, AccessShareLock
);
65 /* Check that this relation has storage */
66 if (rel
->rd_rel
->relkind
== RELKIND_VIEW
)
68 (errcode(ERRCODE_WRONG_OBJECT_TYPE
),
69 errmsg("cannot get raw page from view \"%s\"",
70 RelationGetRelationName(rel
))));
71 if (rel
->rd_rel
->relkind
== RELKIND_COMPOSITE_TYPE
)
73 (errcode(ERRCODE_WRONG_OBJECT_TYPE
),
74 errmsg("cannot get raw page from composite type \"%s\"",
75 RelationGetRelationName(rel
))));
77 if (blkno
>= RelationGetNumberOfBlocks(rel
))
78 elog(ERROR
, "block number %u is out of range for relation \"%s\"",
79 blkno
, RelationGetRelationName(rel
));
81 /* Initialize buffer to copy to */
82 raw_page
= (bytea
*) palloc(BLCKSZ
+ VARHDRSZ
);
83 SET_VARSIZE(raw_page
, BLCKSZ
+ VARHDRSZ
);
84 raw_page_data
= VARDATA(raw_page
);
86 /* Take a verbatim copy of the page */
88 buf
= ReadBufferWithFork(rel
, forknum
, blkno
);
89 LockBuffer(buf
, BUFFER_LOCK_SHARE
);
91 memcpy(raw_page_data
, BufferGetPage(buf
), BLCKSZ
);
93 LockBuffer(buf
, BUFFER_LOCK_UNLOCK
);
96 relation_close(rel
, AccessShareLock
);
98 PG_RETURN_BYTEA_P(raw_page
);
104 * Allows inspection of page header fields of a raw page
107 PG_FUNCTION_INFO_V1(page_header
);
110 page_header(PG_FUNCTION_ARGS
)
112 bytea
*raw_page
= PG_GETARG_BYTEA_P(0);
128 (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE
),
129 (errmsg("must be superuser to use raw page functions"))));
131 raw_page_size
= VARSIZE(raw_page
) - VARHDRSZ
;
134 * Check that enough data was supplied, so that we don't try to access
135 * fields outside the supplied buffer.
137 if (raw_page_size
< sizeof(PageHeaderData
))
139 (errcode(ERRCODE_INVALID_PARAMETER_VALUE
),
140 errmsg("input page too small (%d bytes)", raw_page_size
)));
142 page
= (PageHeader
) VARDATA(raw_page
);
144 /* Build a tuple descriptor for our result type */
145 if (get_call_result_type(fcinfo
, NULL
, &tupdesc
) != TYPEFUNC_COMPOSITE
)
146 elog(ERROR
, "return type must be a row type");
148 /* Extract information from the page header */
150 lsn
= PageGetLSN(page
);
151 snprintf(lsnchar
, sizeof(lsnchar
), "%X/%X", lsn
.xlogid
, lsn
.xrecoff
);
153 values
[0] = CStringGetTextDatum(lsnchar
);
154 values
[1] = UInt16GetDatum(PageGetTLI(page
));
155 values
[2] = UInt16GetDatum(page
->pd_flags
);
156 values
[3] = UInt16GetDatum(page
->pd_lower
);
157 values
[4] = UInt16GetDatum(page
->pd_upper
);
158 values
[5] = UInt16GetDatum(page
->pd_special
);
159 values
[6] = UInt16GetDatum(PageGetPageSize(page
));
160 values
[7] = UInt16GetDatum(PageGetPageLayoutVersion(page
));
161 values
[8] = TransactionIdGetDatum(page
->pd_prune_xid
);
163 /* Build and return the tuple. */
165 memset(nulls
, 0, sizeof(nulls
));
167 tuple
= heap_form_tuple(tupdesc
, values
, nulls
);
168 result
= HeapTupleGetDatum(tuple
);
170 PG_RETURN_DATUM(result
);