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.
|
|
|
|
*
|
2022-01-08 08:04:57 +08:00
|
|
|
* Copyright (c) 2007-2022, PostgreSQL Global Development Group
|
2007-05-18 03:11:25 +08:00
|
|
|
*
|
|
|
|
* IDENTIFICATION
|
2010-09-21 04:08:53 +08:00
|
|
|
* contrib/pageinspect/rawpage.c
|
2007-05-18 03:11:25 +08:00
|
|
|
*
|
|
|
|
*-------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
2012-08-31 04:15:44 +08:00
|
|
|
#include "access/htup_details.h"
|
2019-01-22 02:18:20 +08:00
|
|
|
#include "access/relation.h"
|
2007-05-18 03:11:25 +08:00
|
|
|
#include "catalog/namespace.h"
|
2014-03-03 20:14:31 +08:00
|
|
|
#include "catalog/pg_type.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "funcapi.h"
|
2007-05-18 03:11:25 +08:00
|
|
|
#include "miscadmin.h"
|
2019-10-23 11:56:22 +08:00
|
|
|
#include "pageinspect.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "storage/bufmgr.h"
|
2017-03-17 21:49:10 +08:00
|
|
|
#include "storage/checksum.h"
|
2008-05-12 08:00:54 +08:00
|
|
|
#include "utils/builtins.h"
|
2014-03-03 20:14:31 +08:00
|
|
|
#include "utils/pg_lsn.h"
|
2011-02-24 01:18:09 +08:00
|
|
|
#include "utils/rel.h"
|
2017-01-21 09:29:53 +08:00
|
|
|
#include "utils/varlena.h"
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
PG_MODULE_MAGIC;
|
|
|
|
|
2009-06-09 00:22:44 +08:00
|
|
|
static bytea *get_raw_page_internal(text *relname, ForkNumber forknum,
|
|
|
|
BlockNumber blkno);
|
|
|
|
|
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
/*
|
|
|
|
* get_raw_page
|
|
|
|
*
|
|
|
|
* Returns a copy of a page from shared buffers as a bytea
|
|
|
|
*/
|
2021-01-19 17:28:05 +08:00
|
|
|
PG_FUNCTION_INFO_V1(get_raw_page_1_9);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
get_raw_page_1_9(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
|
|
|
int64 blkno = PG_GETARG_INT64(1);
|
|
|
|
bytea *raw_page;
|
|
|
|
|
|
|
|
if (blkno < 0 || blkno > MaxBlockNumber)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid block number")));
|
|
|
|
|
|
|
|
raw_page = get_raw_page_internal(relname, MAIN_FORKNUM, blkno);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(raw_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* entry point for old extension version
|
|
|
|
*/
|
2007-05-18 03:11:25 +08:00
|
|
|
PG_FUNCTION_INFO_V1(get_raw_page);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
get_raw_page(PG_FUNCTION_ARGS)
|
2009-06-09 00:22:44 +08:00
|
|
|
{
|
2017-03-13 07:35:34 +08:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2009-06-09 00:22:44 +08:00
|
|
|
uint32 blkno = PG_GETARG_UINT32(1);
|
|
|
|
bytea *raw_page;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We don't normally bother to check the number of arguments to a C
|
|
|
|
* function, but here it's needed for safety because early 8.4 beta
|
|
|
|
* releases mistakenly redefined get_raw_page() as taking three arguments.
|
|
|
|
*/
|
|
|
|
if (PG_NARGS() != 2)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errmsg("wrong number of arguments to get_raw_page()"),
|
|
|
|
errhint("Run the updated pageinspect.sql script.")));
|
|
|
|
|
|
|
|
raw_page = get_raw_page_internal(relname, MAIN_FORKNUM, blkno);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(raw_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* get_raw_page_fork
|
|
|
|
*
|
|
|
|
* Same, for any fork
|
|
|
|
*/
|
2021-01-19 17:28:05 +08:00
|
|
|
PG_FUNCTION_INFO_V1(get_raw_page_fork_1_9);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
get_raw_page_fork_1_9(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *forkname = PG_GETARG_TEXT_PP(1);
|
|
|
|
int64 blkno = PG_GETARG_INT64(2);
|
|
|
|
bytea *raw_page;
|
|
|
|
ForkNumber forknum;
|
|
|
|
|
|
|
|
forknum = forkname_to_number(text_to_cstring(forkname));
|
|
|
|
|
|
|
|
if (blkno < 0 || blkno > MaxBlockNumber)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid block number")));
|
|
|
|
|
|
|
|
raw_page = get_raw_page_internal(relname, forknum, blkno);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(raw_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry point for old extension version
|
|
|
|
*/
|
2009-06-09 00:22:44 +08:00
|
|
|
PG_FUNCTION_INFO_V1(get_raw_page_fork);
|
|
|
|
|
|
|
|
Datum
|
|
|
|
get_raw_page_fork(PG_FUNCTION_ARGS)
|
2007-05-18 03:11:25 +08:00
|
|
|
{
|
2017-03-13 07:35:34 +08:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
|
|
|
text *forkname = PG_GETARG_TEXT_PP(1);
|
2008-09-30 18:52:14 +08:00
|
|
|
uint32 blkno = PG_GETARG_UINT32(2);
|
2009-06-09 00:22:44 +08:00
|
|
|
bytea *raw_page;
|
2008-10-06 22:13:17 +08:00
|
|
|
ForkNumber forknum;
|
2007-05-18 03:11:25 +08:00
|
|
|
|
2009-06-09 00:22:44 +08:00
|
|
|
forknum = forkname_to_number(text_to_cstring(forkname));
|
|
|
|
|
|
|
|
raw_page = get_raw_page_internal(relname, forknum, blkno);
|
|
|
|
|
|
|
|
PG_RETURN_BYTEA_P(raw_page);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* workhorse
|
|
|
|
*/
|
|
|
|
static bytea *
|
|
|
|
get_raw_page_internal(text *relname, ForkNumber forknum, BlockNumber blkno)
|
|
|
|
{
|
2007-05-18 03:11:25 +08:00
|
|
|
bytea *raw_page;
|
2009-06-09 00:22:44 +08:00
|
|
|
RangeVar *relrv;
|
|
|
|
Relation rel;
|
2007-05-18 03:11:25 +08:00
|
|
|
char *raw_page_data;
|
|
|
|
Buffer buf;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2020-01-31 00:32:04 +08:00
|
|
|
errmsg("must be superuser to use raw page functions")));
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
Improve error messages about mismatching relkind
Most error messages about a relkind that was not supported or
appropriate for the command was of the pattern
"relation \"%s\" is not a table, foreign table, or materialized view"
This style can become verbose and tedious to maintain. Moreover, it's
not very helpful: If I'm trying to create a comment on a TOAST table,
which is not supported, then the information that I could have created
a comment on a materialized view is pointless.
Instead, write the primary error message shorter and saying more
directly that what was attempted is not possible. Then, in the detail
message, explain that the operation is not supported for the relkind
the object was. To simplify that, add a new function
errdetail_relkind_not_supported() that does this.
In passing, make use of RELKIND_HAS_STORAGE() where appropriate,
instead of listing out the relkinds individually.
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://www.postgresql.org/message-id/flat/dc35a398-37d0-75ce-07ea-1dd71d98f8ec@2ndquadrant.com
2021-07-08 15:38:52 +08:00
|
|
|
if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
|
2007-05-18 03:11:25 +08:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
Improve error messages about mismatching relkind
Most error messages about a relkind that was not supported or
appropriate for the command was of the pattern
"relation \"%s\" is not a table, foreign table, or materialized view"
This style can become verbose and tedious to maintain. Moreover, it's
not very helpful: If I'm trying to create a comment on a TOAST table,
which is not supported, then the information that I could have created
a comment on a materialized view is pointless.
Instead, write the primary error message shorter and saying more
directly that what was attempted is not possible. Then, in the detail
message, explain that the operation is not supported for the relkind
the object was. To simplify that, add a new function
errdetail_relkind_not_supported() that does this.
In passing, make use of RELKIND_HAS_STORAGE() where appropriate,
instead of listing out the relkinds individually.
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://www.postgresql.org/message-id/flat/dc35a398-37d0-75ce-07ea-1dd71d98f8ec@2ndquadrant.com
2021-07-08 15:38:52 +08:00
|
|
|
errmsg("cannot get raw page from relation \"%s\"",
|
|
|
|
RelationGetRelationName(rel)),
|
|
|
|
errdetail_relkind_not_supported(rel->rd_rel->relkind)));
|
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")));
|
|
|
|
|
2014-07-22 23:45:46 +08:00
|
|
|
if (blkno >= RelationGetNumberOfBlocksInFork(rel, forknum))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("block number %u is out of range for relation \"%s\"",
|
|
|
|
blkno, RelationGetRelationName(rel))));
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2016-04-20 21:31:19 +08:00
|
|
|
memcpy(raw_page_data, BufferGetPage(buf), BLCKSZ);
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
LockBuffer(buf, BUFFER_LOCK_UNLOCK);
|
|
|
|
ReleaseBuffer(buf);
|
|
|
|
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
2009-06-09 00:22:44 +08:00
|
|
|
return raw_page;
|
2007-05-18 03:11:25 +08:00
|
|
|
}
|
|
|
|
|
2017-02-04 00:34:41 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* get_page_from_raw
|
|
|
|
*
|
|
|
|
* Get a palloc'd, maxalign'ed page image from the result of get_raw_page()
|
|
|
|
*
|
|
|
|
* On machines with MAXALIGN = 8, the payload of a bytea is not maxaligned,
|
|
|
|
* since it will start 4 bytes into a palloc'd value. On alignment-picky
|
|
|
|
* machines, this will cause failures in accesses to 8-byte-wide values
|
|
|
|
* within the page. We don't need to worry if accessing only 4-byte or
|
|
|
|
* smaller fields, but when examining a struct that contains 8-byte fields,
|
|
|
|
* use this function for safety.
|
|
|
|
*/
|
|
|
|
Page
|
|
|
|
get_page_from_raw(bytea *raw_page)
|
|
|
|
{
|
|
|
|
Page page;
|
|
|
|
int raw_page_size;
|
|
|
|
|
2017-03-13 07:35:34 +08:00
|
|
|
raw_page_size = VARSIZE_ANY_EXHDR(raw_page);
|
2017-02-04 00:34:41 +08:00
|
|
|
|
|
|
|
if (raw_page_size != BLCKSZ)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid page size"),
|
|
|
|
errdetail("Expected %d bytes, got %d.",
|
|
|
|
BLCKSZ, raw_page_size)));
|
|
|
|
|
|
|
|
page = palloc(raw_page_size);
|
|
|
|
|
2017-03-13 07:35:34 +08:00
|
|
|
memcpy(page, VARDATA_ANY(raw_page), raw_page_size);
|
2017-02-04 00:34:41 +08:00
|
|
|
|
|
|
|
return page;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-07-11 13:20:35 +08:00
|
|
|
Page page;
|
|
|
|
PageHeader pageheader;
|
2007-05-18 03:11:25 +08:00
|
|
|
XLogRecPtr lsn;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2020-01-31 00:32:04 +08:00
|
|
|
errmsg("must be superuser to use raw page functions")));
|
2007-05-18 03:11:25 +08:00
|
|
|
|
2022-07-11 13:20:35 +08:00
|
|
|
page = get_page_from_raw(raw_page);
|
|
|
|
pageheader = (PageHeader) page;
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
2014-03-03 20:14:31 +08:00
|
|
|
/* pageinspect >= 1.2 uses pg_lsn instead of text for the LSN field. */
|
2017-08-21 02:19:07 +08:00
|
|
|
if (TupleDescAttr(tupdesc, 0)->atttypid == TEXTOID)
|
2014-03-03 20:14:31 +08:00
|
|
|
{
|
|
|
|
char lsnchar[64];
|
2014-05-07 00:12:18 +08:00
|
|
|
|
2021-02-23 17:14:38 +08:00
|
|
|
snprintf(lsnchar, sizeof(lsnchar), "%X/%X", LSN_FORMAT_ARGS(lsn));
|
2014-03-03 20:14:31 +08:00
|
|
|
values[0] = CStringGetTextDatum(lsnchar);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
values[0] = LSNGetDatum(lsn);
|
2022-07-11 13:20:35 +08:00
|
|
|
values[1] = UInt16GetDatum(pageheader->pd_checksum);
|
|
|
|
values[2] = UInt16GetDatum(pageheader->pd_flags);
|
2021-07-12 10:05:27 +08:00
|
|
|
|
|
|
|
/* pageinspect >= 1.10 uses int4 instead of int2 for those fields */
|
|
|
|
switch (TupleDescAttr(tupdesc, 3)->atttypid)
|
|
|
|
{
|
|
|
|
case INT2OID:
|
|
|
|
Assert(TupleDescAttr(tupdesc, 4)->atttypid == INT2OID &&
|
|
|
|
TupleDescAttr(tupdesc, 5)->atttypid == INT2OID &&
|
|
|
|
TupleDescAttr(tupdesc, 6)->atttypid == INT2OID);
|
2022-07-11 13:20:35 +08:00
|
|
|
values[3] = UInt16GetDatum(pageheader->pd_lower);
|
|
|
|
values[4] = UInt16GetDatum(pageheader->pd_upper);
|
|
|
|
values[5] = UInt16GetDatum(pageheader->pd_special);
|
2021-07-12 10:05:27 +08:00
|
|
|
values[6] = UInt16GetDatum(PageGetPageSize(page));
|
|
|
|
break;
|
|
|
|
case INT4OID:
|
|
|
|
Assert(TupleDescAttr(tupdesc, 4)->atttypid == INT4OID &&
|
|
|
|
TupleDescAttr(tupdesc, 5)->atttypid == INT4OID &&
|
|
|
|
TupleDescAttr(tupdesc, 6)->atttypid == INT4OID);
|
2022-07-11 13:20:35 +08:00
|
|
|
values[3] = Int32GetDatum(pageheader->pd_lower);
|
|
|
|
values[4] = Int32GetDatum(pageheader->pd_upper);
|
|
|
|
values[5] = Int32GetDatum(pageheader->pd_special);
|
2021-07-12 10:05:27 +08:00
|
|
|
values[6] = Int32GetDatum(PageGetPageSize(page));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
elog(ERROR, "incorrect output types");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-05-18 03:11:25 +08:00
|
|
|
values[7] = UInt16GetDatum(PageGetPageLayoutVersion(page));
|
2022-07-11 13:20:35 +08:00
|
|
|
values[8] = TransactionIdGetDatum(pageheader->pd_prune_xid);
|
2007-05-18 03:11:25 +08:00
|
|
|
|
|
|
|
/* Build and return the tuple. */
|
|
|
|
|
|
|
|
memset(nulls, 0, sizeof(nulls));
|
|
|
|
|
|
|
|
tuple = heap_form_tuple(tupdesc, values, nulls);
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|
2017-03-17 21:49:10 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* page_checksum
|
|
|
|
*
|
|
|
|
* Compute checksum of a raw page
|
|
|
|
*/
|
|
|
|
|
2021-01-19 17:28:05 +08:00
|
|
|
PG_FUNCTION_INFO_V1(page_checksum_1_9);
|
2017-03-17 21:49:10 +08:00
|
|
|
PG_FUNCTION_INFO_V1(page_checksum);
|
|
|
|
|
2021-01-19 17:28:05 +08:00
|
|
|
static Datum
|
|
|
|
page_checksum_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
|
2017-03-17 21:49:10 +08:00
|
|
|
{
|
|
|
|
bytea *raw_page = PG_GETARG_BYTEA_P(0);
|
2021-01-19 17:28:05 +08:00
|
|
|
int64 blkno = (ext_version == PAGEINSPECT_V1_8 ? PG_GETARG_UINT32(1) : PG_GETARG_INT64(1));
|
pageinspect: Fix handling of page sizes and AM types
This commit fixes a set of issues related to the use of the SQL
functions in this module when the caller is able to pass down raw page
data as input argument:
- The page size check was fuzzy in a couple of places, sometimes
looking after only a sub-range, but what we are looking for is an exact
match on BLCKSZ. After considering a few options here, I have settled
down to do a generalization of get_page_from_raw(). Most of the SQL
functions already used that, and this is not strictly required if not
accessing an 8-byte-wide value from a raw page, but this feels safer in
the long run for alignment-picky environment, particularly if a code
path begins to access such values. This also reduces the number of
strings that need to be translated.
- The BRIN function brin_page_items() uses a Relation but it did not
check the access method of the opened index, potentially leading to
crashes. All the other functions in need of a Relation already did
that.
- Some code paths could fail on elog(), but we should to use ereport()
for failures that can be triggered by the user.
Tests are added to stress all the cases that are fixed as of this
commit, with some junk raw pages (\set VERBOSITY ensures that this works
across all page sizes) and unexpected index types when functions open
relations.
Author: Michael Paquier, Justin Prysby
Discussion: https://postgr.es/m/20220218030020.GA1137@telsasoft.com
Backpatch-through: 10
2022-03-16 10:19:39 +08:00
|
|
|
Page page;
|
2017-03-17 21:49:10 +08:00
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2020-01-31 00:32:04 +08:00
|
|
|
errmsg("must be superuser to use raw page functions")));
|
2017-03-17 21:49:10 +08:00
|
|
|
|
2021-01-19 17:28:05 +08:00
|
|
|
if (blkno < 0 || blkno > MaxBlockNumber)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid block number")));
|
|
|
|
|
pageinspect: Fix handling of page sizes and AM types
This commit fixes a set of issues related to the use of the SQL
functions in this module when the caller is able to pass down raw page
data as input argument:
- The page size check was fuzzy in a couple of places, sometimes
looking after only a sub-range, but what we are looking for is an exact
match on BLCKSZ. After considering a few options here, I have settled
down to do a generalization of get_page_from_raw(). Most of the SQL
functions already used that, and this is not strictly required if not
accessing an 8-byte-wide value from a raw page, but this feels safer in
the long run for alignment-picky environment, particularly if a code
path begins to access such values. This also reduces the number of
strings that need to be translated.
- The BRIN function brin_page_items() uses a Relation but it did not
check the access method of the opened index, potentially leading to
crashes. All the other functions in need of a Relation already did
that.
- Some code paths could fail on elog(), but we should to use ereport()
for failures that can be triggered by the user.
Tests are added to stress all the cases that are fixed as of this
commit, with some junk raw pages (\set VERBOSITY ensures that this works
across all page sizes) and unexpected index types when functions open
relations.
Author: Michael Paquier, Justin Prysby
Discussion: https://postgr.es/m/20220218030020.GA1137@telsasoft.com
Backpatch-through: 10
2022-03-16 10:19:39 +08:00
|
|
|
page = get_page_from_raw(raw_page);
|
2017-03-17 21:49:10 +08:00
|
|
|
|
pageinspect: Fix handling of all-zero pages
Getting from get_raw_page() an all-zero page is considered as a valid
case by the buffer manager and it can happen for example when finding a
corrupted page with zero_damaged_pages enabled (using zero_damaged_pages
to look at corrupted pages happens), or after a crash when a relation
file is extended before any WAL for its new data is generated (before a
vacuum or autovacuum job comes in to do some cleanup).
However, all the functions of pageinspect, as of the index AMs (except
hash that has its own idea of new pages), heap, the FSM or the page
header have never worked with all-zero pages, causing various crashes
when going through the page internals.
This commit changes all the pageinspect functions to be compliant with
all-zero pages, where the choice is made to return NULL or no rows for
SRFs when finding a new page. get_raw_page() still works the same way,
returning a batch of zeros in the bytea of the page retrieved. A hard
error could be used but NULL, while more invasive, is useful when
scanning relation files in full to get a batch of results for a single
relation in one query. Tests are added for all the code paths
impacted.
Reported-by: Daria Lepikhova
Author: Michael Paquier
Discussion: https://postgr.es/m/561e187b-3549-c8d5-03f5-525c14e65bd0@postgrespro.ru
Backpatch-through: 10
2022-04-14 14:08:03 +08:00
|
|
|
if (PageIsNew(page))
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
|
2017-03-17 21:49:10 +08:00
|
|
|
PG_RETURN_INT16(pg_checksum_page((char *) page, blkno));
|
|
|
|
}
|
2021-01-19 17:28:05 +08:00
|
|
|
|
|
|
|
Datum
|
|
|
|
page_checksum_1_9(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return page_checksum_internal(fcinfo, PAGEINSPECT_V1_9);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Entry point for old extension version
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
page_checksum(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return page_checksum_internal(fcinfo, PAGEINSPECT_V1_8);
|
|
|
|
}
|