2007-05-17 23:11:25 +04:00
|
|
|
/*
|
2010-09-21 00:08:53 +04:00
|
|
|
* contrib/pageinspect/btreefuncs.c
|
2008-05-17 05:28:26 +04:00
|
|
|
*
|
|
|
|
*
|
2007-05-17 23:11:25 +04:00
|
|
|
* btreefuncs.c
|
|
|
|
*
|
|
|
|
* Copyright (c) 2006 Satoshi Nagayasu <nagayasus@nttdata.co.jp>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and
|
|
|
|
* its documentation for any purpose, without fee, and without a
|
|
|
|
* written agreement is hereby granted, provided that the above
|
|
|
|
* copyright notice and this paragraph and the following two
|
|
|
|
* paragraphs appear in all copies.
|
|
|
|
*
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE TO ANY PARTY FOR DIRECT,
|
|
|
|
* INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING
|
|
|
|
* LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
|
|
|
|
* DOCUMENTATION, EVEN IF THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* THE AUTHOR SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS
|
|
|
|
* IS" BASIS, AND THE AUTHOR HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE,
|
|
|
|
* SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "postgres.h"
|
|
|
|
|
|
|
|
#include "access/nbtree.h"
|
2019-01-21 21:18:20 +03:00
|
|
|
#include "access/relation.h"
|
2007-05-17 23:11:25 +04:00
|
|
|
#include "catalog/namespace.h"
|
Restructure index access method API to hide most of it at the C level.
This patch reduces pg_am to just two columns, a name and a handler
function. All the data formerly obtained from pg_am is now provided
in a C struct returned by the handler function. This is similar to
the designs we've adopted for FDWs and tablesample methods. There
are multiple advantages. For one, the index AM's support functions
are now simple C functions, making them faster to call and much less
error-prone, since the C compiler can now check function signatures.
For another, this will make it far more practical to define index access
methods in installable extensions.
A disadvantage is that SQL-level code can no longer see attributes
of index AMs; in particular, some of the crosschecks in the opr_sanity
regression test are no longer possible from SQL. We've addressed that
by adding a facility for the index AM to perform such checks instead.
(Much more could be done in that line, but for now we're content if the
amvalidate functions more or less replace what opr_sanity used to do.)
We might also want to expose some sort of reporting functionality, but
this patch doesn't do that.
Alexander Korotkov, reviewed by Petr Jelínek, and rather heavily
editorialized on by me.
2016-01-18 03:36:59 +03:00
|
|
|
#include "catalog/pg_am.h"
|
2020-02-29 23:10:17 +03:00
|
|
|
#include "catalog/pg_type.h"
|
2007-08-27 03:22:49 +04:00
|
|
|
#include "funcapi.h"
|
|
|
|
#include "miscadmin.h"
|
2019-10-23 06:56:22 +03:00
|
|
|
#include "pageinspect.h"
|
2020-02-29 23:10:17 +03:00
|
|
|
#include "utils/array.h"
|
2007-05-17 23:11:25 +04:00
|
|
|
#include "utils/builtins.h"
|
2011-02-23 20:18:09 +03:00
|
|
|
#include "utils/rel.h"
|
2017-01-21 04:29:53 +03:00
|
|
|
#include "utils/varlena.h"
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
PG_FUNCTION_INFO_V1(bt_metap);
|
2021-01-19 12:28:05 +03:00
|
|
|
PG_FUNCTION_INFO_V1(bt_page_items_1_9);
|
2007-08-27 03:22:49 +04:00
|
|
|
PG_FUNCTION_INFO_V1(bt_page_items);
|
2017-04-05 06:48:49 +03:00
|
|
|
PG_FUNCTION_INFO_V1(bt_page_items_bytea);
|
2021-01-19 12:28:05 +03:00
|
|
|
PG_FUNCTION_INFO_V1(bt_page_stats_1_9);
|
2007-08-27 03:22:49 +04:00
|
|
|
PG_FUNCTION_INFO_V1(bt_page_stats);
|
2023-01-02 21:02:29 +03:00
|
|
|
PG_FUNCTION_INFO_V1(bt_multi_page_stats);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
|
2007-05-17 23:11:25 +04:00
|
|
|
#define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
|
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* structure for single btree page statistics
|
|
|
|
* ------------------------------------------------
|
|
|
|
*/
|
|
|
|
typedef struct BTPageStat
|
|
|
|
{
|
|
|
|
uint32 blkno;
|
|
|
|
uint32 live_items;
|
|
|
|
uint32 dead_items;
|
|
|
|
uint32 page_size;
|
|
|
|
uint32 max_avail;
|
|
|
|
uint32 free_size;
|
|
|
|
uint32 avg_item_size;
|
|
|
|
char type;
|
|
|
|
|
|
|
|
/* opaque data */
|
|
|
|
BlockNumber btpo_prev;
|
|
|
|
BlockNumber btpo_next;
|
2021-02-25 05:41:34 +03:00
|
|
|
uint32 btpo_level;
|
2007-05-17 23:11:25 +04:00
|
|
|
uint16 btpo_flags;
|
|
|
|
BTCycleId btpo_cycleid;
|
|
|
|
} BTPageStat;
|
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
/*
|
|
|
|
* cross-call data structure for SRF for page stats
|
|
|
|
*/
|
|
|
|
typedef struct ua_page_stats
|
|
|
|
{
|
|
|
|
Oid relid;
|
|
|
|
int64 blkno;
|
|
|
|
int64 blk_count;
|
|
|
|
bool allpages;
|
|
|
|
} ua_page_stats;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* cross-call data structure for SRF for page items
|
|
|
|
*/
|
|
|
|
typedef struct ua_page_items
|
|
|
|
{
|
|
|
|
Page page;
|
|
|
|
OffsetNumber offset;
|
|
|
|
bool leafpage;
|
|
|
|
bool rightmost;
|
|
|
|
TupleDesc tupd;
|
|
|
|
} ua_page_items;
|
|
|
|
|
2007-05-17 23:11:25 +04:00
|
|
|
|
|
|
|
/* -------------------------------------------------
|
|
|
|
* GetBTPageStatistics()
|
|
|
|
*
|
2007-08-27 03:22:49 +04:00
|
|
|
* Collect statistics of single b-tree page
|
2007-05-17 23:11:25 +04:00
|
|
|
* -------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
|
|
|
|
{
|
2016-04-20 16:31:19 +03:00
|
|
|
Page page = BufferGetPage(buffer);
|
2007-05-17 23:11:25 +04:00
|
|
|
PageHeader phdr = (PageHeader) page;
|
|
|
|
OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
|
2022-04-01 07:24:50 +03:00
|
|
|
BTPageOpaque opaque = BTPageGetOpaque(page);
|
2007-05-17 23:11:25 +04:00
|
|
|
int item_size = 0;
|
|
|
|
int off;
|
|
|
|
|
|
|
|
stat->blkno = blkno;
|
|
|
|
|
|
|
|
stat->max_avail = BLCKSZ - (BLCKSZ - phdr->pd_special + SizeOfPageHeaderData);
|
|
|
|
|
|
|
|
stat->dead_items = stat->live_items = 0;
|
|
|
|
|
|
|
|
stat->page_size = PageGetPageSize(page);
|
|
|
|
|
|
|
|
/* page type (flags) */
|
|
|
|
if (P_ISDELETED(opaque))
|
|
|
|
{
|
2021-02-25 05:41:34 +03:00
|
|
|
/* We divide deleted pages into leaf ('d') or internal ('D') */
|
|
|
|
if (P_ISLEAF(opaque) || !P_HAS_FULLXID(opaque))
|
|
|
|
stat->type = 'd';
|
|
|
|
else
|
|
|
|
stat->type = 'D';
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Report safexid in a deleted page.
|
|
|
|
*
|
|
|
|
* Handle pg_upgrade'd deleted pages that used the previous safexid
|
|
|
|
* representation in btpo_level field (this used to be a union type
|
|
|
|
* called "bpto").
|
|
|
|
*/
|
|
|
|
if (P_HAS_FULLXID(opaque))
|
|
|
|
{
|
|
|
|
FullTransactionId safexid = BTPageGetDeleteXid(page);
|
|
|
|
|
2021-09-18 00:19:51 +03:00
|
|
|
elog(DEBUG2, "deleted page from block %u has safexid %u:%u",
|
2021-02-25 05:41:34 +03:00
|
|
|
blkno, EpochFromFullTransactionId(safexid),
|
|
|
|
XidFromFullTransactionId(safexid));
|
|
|
|
}
|
|
|
|
else
|
2021-09-18 00:19:51 +03:00
|
|
|
elog(DEBUG2, "deleted page from block %u has safexid %u",
|
2021-02-25 05:41:34 +03:00
|
|
|
blkno, opaque->btpo_level);
|
|
|
|
|
|
|
|
/* Don't interpret BTDeletedPageData as index tuples */
|
|
|
|
maxoff = InvalidOffsetNumber;
|
2007-05-17 23:11:25 +04:00
|
|
|
}
|
|
|
|
else if (P_IGNORE(opaque))
|
|
|
|
stat->type = 'e';
|
|
|
|
else if (P_ISLEAF(opaque))
|
|
|
|
stat->type = 'l';
|
|
|
|
else if (P_ISROOT(opaque))
|
|
|
|
stat->type = 'r';
|
|
|
|
else
|
|
|
|
stat->type = 'i';
|
|
|
|
|
|
|
|
/* btpage opaque data */
|
|
|
|
stat->btpo_prev = opaque->btpo_prev;
|
|
|
|
stat->btpo_next = opaque->btpo_next;
|
2021-02-25 05:41:34 +03:00
|
|
|
stat->btpo_level = opaque->btpo_level;
|
2007-05-17 23:11:25 +04:00
|
|
|
stat->btpo_flags = opaque->btpo_flags;
|
|
|
|
stat->btpo_cycleid = opaque->btpo_cycleid;
|
|
|
|
|
|
|
|
/* count live and dead tuples, and free space */
|
|
|
|
for (off = FirstOffsetNumber; off <= maxoff; off++)
|
|
|
|
{
|
|
|
|
IndexTuple itup;
|
|
|
|
|
|
|
|
ItemId id = PageGetItemId(page, off);
|
|
|
|
|
|
|
|
itup = (IndexTuple) PageGetItem(page, id);
|
|
|
|
|
|
|
|
item_size += IndexTupleSize(itup);
|
|
|
|
|
2007-09-13 02:10:26 +04:00
|
|
|
if (!ItemIdIsDead(id))
|
2007-05-17 23:11:25 +04:00
|
|
|
stat->live_items++;
|
|
|
|
else
|
|
|
|
stat->dead_items++;
|
|
|
|
}
|
|
|
|
stat->free_size = PageGetFreeSpace(page);
|
|
|
|
|
|
|
|
if ((stat->live_items + stat->dead_items) > 0)
|
|
|
|
stat->avg_item_size = item_size / (stat->live_items + stat->dead_items);
|
|
|
|
else
|
|
|
|
stat->avg_item_size = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------
|
2023-01-02 21:02:29 +03:00
|
|
|
* check_relation_block_range()
|
2007-05-17 23:11:25 +04:00
|
|
|
*
|
2023-01-02 21:02:29 +03:00
|
|
|
* Verify that a block number (given as int64) is valid for the relation.
|
2007-05-17 23:11:25 +04:00
|
|
|
* -----------------------------------------------
|
|
|
|
*/
|
2023-01-02 21:02:29 +03:00
|
|
|
static void
|
|
|
|
check_relation_block_range(Relation rel, int64 blkno)
|
2007-05-17 23:11:25 +04:00
|
|
|
{
|
2023-01-02 21:02:29 +03:00
|
|
|
/* Ensure we can cast to BlockNumber */
|
|
|
|
if (blkno < 0 || blkno > MaxBlockNumber)
|
2007-08-27 03:22:49 +04:00
|
|
|
ereport(ERROR,
|
2023-01-02 21:02:29 +03:00
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("invalid block number %lld",
|
|
|
|
(long long) blkno)));
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
if ((BlockNumber) (blkno) >= RelationGetNumberOfBlocks(rel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("block number %lld is out of range",
|
|
|
|
(long long) blkno)));
|
|
|
|
}
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
/* -----------------------------------------------
|
|
|
|
* bt_index_block_validate()
|
|
|
|
*
|
|
|
|
* Validate index type is btree and block number
|
|
|
|
* is valid (and not the metapage).
|
|
|
|
* -----------------------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
bt_index_block_validate(Relation rel, int64 blkno)
|
|
|
|
{
|
2007-05-17 23:11:25 +04:00
|
|
|
if (!IS_INDEX(rel) || !IS_BTREE(rel))
|
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 05:19:39 +03:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a %s index",
|
|
|
|
RelationGetRelationName(rel), "btree")));
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2009-04-01 02:54:31 +04: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-17 23:11:25 +04:00
|
|
|
if (blkno == 0)
|
2021-01-19 12:28:05 +03:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("block 0 is a meta page")));
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
check_relation_block_range(rel, blkno);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------
|
|
|
|
* bt_page_stats()
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM bt_page_stats('t1_pkey', 1);
|
|
|
|
* Arguments are index relation name and block number
|
|
|
|
* -----------------------------------------------
|
|
|
|
*/
|
|
|
|
static Datum
|
|
|
|
bt_page_stats_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
|
|
|
|
{
|
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
|
|
|
int64 blkno = (ext_version == PAGEINSPECT_V1_8 ? PG_GETARG_UINT32(1) : PG_GETARG_INT64(1));
|
|
|
|
Buffer buffer;
|
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
|
|
|
Datum result;
|
|
|
|
HeapTuple tuple;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
int j;
|
|
|
|
char *values[11];
|
|
|
|
BTPageStat stat;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("must be superuser to use pageinspect functions")));
|
|
|
|
|
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
|
|
|
bt_index_block_validate(rel, blkno);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
buffer = ReadBuffer(rel, blkno);
|
2012-12-01 02:02:29 +04:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
/* keep compiler quiet */
|
|
|
|
stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
|
|
|
|
stat.btpo_flags = stat.free_size = stat.avg_item_size = 0;
|
|
|
|
|
|
|
|
GetBTPageStatistics(blkno, buffer, &stat);
|
|
|
|
|
2012-12-01 02:02:29 +04:00
|
|
|
UnlockReleaseBuffer(buffer);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
|
|
|
j = 0;
|
2021-01-19 12:28:05 +03:00
|
|
|
values[j++] = psprintf("%u", stat.blkno);
|
2014-01-07 06:30:26 +04:00
|
|
|
values[j++] = psprintf("%c", stat.type);
|
2021-01-19 12:28:05 +03:00
|
|
|
values[j++] = psprintf("%u", stat.live_items);
|
|
|
|
values[j++] = psprintf("%u", stat.dead_items);
|
|
|
|
values[j++] = psprintf("%u", stat.avg_item_size);
|
|
|
|
values[j++] = psprintf("%u", stat.page_size);
|
|
|
|
values[j++] = psprintf("%u", stat.free_size);
|
|
|
|
values[j++] = psprintf("%u", stat.btpo_prev);
|
|
|
|
values[j++] = psprintf("%u", stat.btpo_next);
|
2021-02-25 05:41:34 +03:00
|
|
|
values[j++] = psprintf("%u", stat.btpo_level);
|
2014-01-07 06:30:26 +04:00
|
|
|
values[j++] = psprintf("%d", stat.btpo_flags);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
|
|
|
|
values);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
result = HeapTupleGetDatum(tuple);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|
|
|
|
|
2021-01-19 12:28:05 +03:00
|
|
|
Datum
|
|
|
|
bt_page_stats_1_9(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return bt_page_stats_internal(fcinfo, PAGEINSPECT_V1_9);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* entry point for old extension version */
|
|
|
|
Datum
|
|
|
|
bt_page_stats(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return bt_page_stats_internal(fcinfo, PAGEINSPECT_V1_8);
|
|
|
|
}
|
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
/* -----------------------------------------------
|
|
|
|
* bt_multi_page_stats()
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM bt_page_stats('t1_pkey', 1, 2);
|
|
|
|
* Arguments are index relation name, first block number, number of blocks
|
|
|
|
* (but number of blocks can be negative to mean "read all the rest")
|
|
|
|
* -----------------------------------------------
|
2007-05-17 23:11:25 +04:00
|
|
|
*/
|
2023-01-02 21:02:29 +03:00
|
|
|
Datum
|
|
|
|
bt_multi_page_stats(PG_FUNCTION_ARGS)
|
2007-05-17 23:11:25 +04:00
|
|
|
{
|
2023-01-02 21:02:29 +03:00
|
|
|
Relation rel;
|
|
|
|
ua_page_stats *uargs;
|
|
|
|
FuncCallContext *fctx;
|
|
|
|
MemoryContext mctx;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
errmsg("must be superuser to use pageinspect functions")));
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
|
|
|
int64 blkno = PG_GETARG_INT64(1);
|
|
|
|
int64 blk_count = PG_GETARG_INT64(2);
|
|
|
|
RangeVar *relrv;
|
|
|
|
|
|
|
|
fctx = SRF_FIRSTCALL_INIT();
|
|
|
|
|
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
|
|
|
/* Check that rel is a valid btree index and 1st block number is OK */
|
|
|
|
bt_index_block_validate(rel, blkno);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if upper bound of the specified range is valid. If only one
|
|
|
|
* page is requested, skip as we've already validated the page. (Also,
|
|
|
|
* it's important to skip this if blk_count is negative.)
|
|
|
|
*/
|
|
|
|
if (blk_count > 1)
|
|
|
|
check_relation_block_range(rel, blkno + blk_count - 1);
|
|
|
|
|
|
|
|
/* Save arguments for reuse */
|
|
|
|
mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
uargs = palloc(sizeof(ua_page_stats));
|
|
|
|
|
|
|
|
uargs->relid = RelationGetRelid(rel);
|
|
|
|
uargs->blkno = blkno;
|
|
|
|
uargs->blk_count = blk_count;
|
|
|
|
uargs->allpages = (blk_count < 0);
|
|
|
|
|
|
|
|
fctx->user_fctx = uargs;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(mctx);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid possibly leaking a relcache reference if the SRF isn't run
|
|
|
|
* to completion, we close and re-open the index rel each time
|
|
|
|
* through, using the index's OID for re-opens to ensure we get the
|
|
|
|
* same rel. Keep the AccessShareLock though, to ensure it doesn't go
|
|
|
|
* away underneath us.
|
|
|
|
*/
|
|
|
|
relation_close(rel, NoLock);
|
|
|
|
}
|
|
|
|
|
|
|
|
fctx = SRF_PERCALL_SETUP();
|
|
|
|
uargs = fctx->user_fctx;
|
|
|
|
|
|
|
|
/* We should have lock already */
|
|
|
|
rel = relation_open(uargs->relid, NoLock);
|
|
|
|
|
|
|
|
/* In all-pages mode, recheck the index length each time */
|
|
|
|
if (uargs->allpages)
|
|
|
|
uargs->blk_count = RelationGetNumberOfBlocks(rel) - uargs->blkno;
|
|
|
|
|
|
|
|
if (uargs->blk_count > 0)
|
|
|
|
{
|
|
|
|
/* We need to fetch next block statistics */
|
|
|
|
Buffer buffer;
|
|
|
|
Datum result;
|
|
|
|
HeapTuple tuple;
|
|
|
|
int j;
|
|
|
|
char *values[11];
|
|
|
|
BTPageStat stat;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
|
|
|
|
buffer = ReadBuffer(rel, uargs->blkno);
|
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
|
|
|
|
|
|
|
/* keep compiler quiet */
|
|
|
|
stat.btpo_prev = stat.btpo_next = InvalidBlockNumber;
|
|
|
|
stat.btpo_flags = stat.free_size = stat.avg_item_size = 0;
|
|
|
|
|
|
|
|
GetBTPageStatistics(uargs->blkno, buffer, &stat);
|
|
|
|
|
|
|
|
UnlockReleaseBuffer(buffer);
|
|
|
|
relation_close(rel, NoLock);
|
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
values[j++] = psprintf("%u", stat.blkno);
|
|
|
|
values[j++] = psprintf("%c", stat.type);
|
|
|
|
values[j++] = psprintf("%u", stat.live_items);
|
|
|
|
values[j++] = psprintf("%u", stat.dead_items);
|
|
|
|
values[j++] = psprintf("%u", stat.avg_item_size);
|
|
|
|
values[j++] = psprintf("%u", stat.page_size);
|
|
|
|
values[j++] = psprintf("%u", stat.free_size);
|
|
|
|
values[j++] = psprintf("%u", stat.btpo_prev);
|
|
|
|
values[j++] = psprintf("%u", stat.btpo_next);
|
|
|
|
values[j++] = psprintf("%u", stat.btpo_level);
|
|
|
|
values[j++] = psprintf("%d", stat.btpo_flags);
|
|
|
|
|
|
|
|
/* Construct tuple to be returned */
|
|
|
|
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
|
|
|
|
values);
|
|
|
|
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Move to the next block number and decrement the number of blocks
|
|
|
|
* still to be fetched
|
|
|
|
*/
|
|
|
|
uargs->blkno++;
|
|
|
|
uargs->blk_count--;
|
|
|
|
|
|
|
|
SRF_RETURN_NEXT(fctx, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Done, so finally we can release the index lock */
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
SRF_RETURN_DONE(fctx);
|
|
|
|
}
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2017-04-05 06:48:49 +03:00
|
|
|
/*-------------------------------------------------------
|
|
|
|
* bt_page_print_tuples()
|
|
|
|
*
|
|
|
|
* Form a tuple describing index tuple at a given offset
|
|
|
|
* ------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static Datum
|
2023-01-02 21:02:29 +03:00
|
|
|
bt_page_print_tuples(ua_page_items *uargs)
|
2017-04-05 06:48:49 +03:00
|
|
|
{
|
2020-02-29 23:10:17 +03:00
|
|
|
Page page = uargs->page;
|
|
|
|
OffsetNumber offset = uargs->offset;
|
|
|
|
bool leafpage = uargs->leafpage;
|
|
|
|
bool rightmost = uargs->rightmost;
|
|
|
|
bool ispivottuple;
|
|
|
|
Datum values[9];
|
|
|
|
bool nulls[9];
|
2017-04-05 06:48:49 +03:00
|
|
|
HeapTuple tuple;
|
|
|
|
ItemId id;
|
|
|
|
IndexTuple itup;
|
|
|
|
int j;
|
|
|
|
int off;
|
|
|
|
int dlen;
|
2020-02-29 23:10:17 +03:00
|
|
|
char *dump,
|
|
|
|
*datacstring;
|
2017-04-05 06:48:49 +03:00
|
|
|
char *ptr;
|
2020-02-29 23:10:17 +03:00
|
|
|
ItemPointer htid;
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
id = PageGetItemId(page, offset);
|
|
|
|
|
|
|
|
if (!ItemIdIsValid(id))
|
|
|
|
elog(ERROR, "invalid ItemId");
|
|
|
|
|
|
|
|
itup = (IndexTuple) PageGetItem(page, id);
|
|
|
|
|
|
|
|
j = 0;
|
2020-02-29 23:10:17 +03:00
|
|
|
memset(nulls, 0, sizeof(nulls));
|
|
|
|
values[j++] = DatumGetInt16(offset);
|
|
|
|
values[j++] = ItemPointerGetDatum(&itup->t_tid);
|
|
|
|
values[j++] = Int32GetDatum((int) IndexTupleSize(itup));
|
|
|
|
values[j++] = BoolGetDatum(IndexTupleHasNulls(itup));
|
|
|
|
values[j++] = BoolGetDatum(IndexTupleHasVarwidths(itup));
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
|
|
|
|
dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
|
2020-02-29 23:10:17 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Make sure that "data" column does not include posting list or pivot
|
|
|
|
* tuple representation of heap TID(s).
|
|
|
|
*
|
|
|
|
* Note: BTreeTupleIsPivot() won't work reliably on !heapkeyspace indexes
|
|
|
|
* (those built before BTREE_VERSION 4), but we have no way of determining
|
|
|
|
* if this page came from a !heapkeyspace index. We may only have a bytea
|
|
|
|
* nbtree page image to go on, so in general there is no metapage that we
|
|
|
|
* can check.
|
|
|
|
*
|
|
|
|
* That's okay here because BTreeTupleIsPivot() can only return false for
|
|
|
|
* a !heapkeyspace pivot, never true for a !heapkeyspace non-pivot. Since
|
|
|
|
* heap TID isn't part of the keyspace in a !heapkeyspace index anyway,
|
|
|
|
* there cannot possibly be a pivot tuple heap TID representation that we
|
|
|
|
* fail to make an adjustment for. A !heapkeyspace index can have
|
|
|
|
* BTreeTupleIsPivot() return true (due to things like suffix truncation
|
|
|
|
* for INCLUDE indexes in Postgres v11), but when that happens
|
|
|
|
* BTreeTupleGetHeapTID() can be trusted to work reliably (i.e. return
|
|
|
|
* NULL).
|
|
|
|
*
|
|
|
|
* Note: BTreeTupleIsPosting() always works reliably, even with
|
|
|
|
* !heapkeyspace indexes.
|
|
|
|
*/
|
|
|
|
if (BTreeTupleIsPosting(itup))
|
|
|
|
dlen -= IndexTupleSize(itup) - BTreeTupleGetPostingOffset(itup);
|
|
|
|
else if (BTreeTupleIsPivot(itup) && BTreeTupleGetHeapTID(itup) != NULL)
|
|
|
|
dlen -= MAXALIGN(sizeof(ItemPointerData));
|
|
|
|
|
|
|
|
if (dlen < 0 || dlen > INDEX_SIZE_MASK)
|
|
|
|
elog(ERROR, "invalid tuple length %d for tuple at offset number %u",
|
|
|
|
dlen, offset);
|
2017-04-05 06:48:49 +03:00
|
|
|
dump = palloc0(dlen * 3 + 1);
|
2020-02-29 23:10:17 +03:00
|
|
|
datacstring = dump;
|
2017-04-05 06:48:49 +03:00
|
|
|
for (off = 0; off < dlen; off++)
|
|
|
|
{
|
|
|
|
if (off > 0)
|
|
|
|
*dump++ = ' ';
|
|
|
|
sprintf(dump, "%02x", *(ptr + off) & 0xff);
|
|
|
|
dump += 2;
|
|
|
|
}
|
2020-02-29 23:10:17 +03:00
|
|
|
values[j++] = CStringGetTextDatum(datacstring);
|
|
|
|
pfree(datacstring);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We need to work around the BTreeTupleIsPivot() !heapkeyspace limitation
|
|
|
|
* again. Deduce whether or not tuple must be a pivot tuple based on
|
|
|
|
* whether or not the page is a leaf page, as well as the page offset
|
|
|
|
* number of the tuple.
|
|
|
|
*/
|
|
|
|
ispivottuple = (!leafpage || (!rightmost && offset == P_HIKEY));
|
|
|
|
|
|
|
|
/* LP_DEAD bit can never be set for pivot tuples, so show a NULL there */
|
|
|
|
if (!ispivottuple)
|
|
|
|
values[j++] = BoolGetDatum(ItemIdIsDead(id));
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Assert(!ItemIdIsDead(id));
|
|
|
|
nulls[j++] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
htid = BTreeTupleGetHeapTID(itup);
|
|
|
|
if (ispivottuple && !BTreeTupleIsPivot(itup))
|
|
|
|
{
|
|
|
|
/* Don't show bogus heap TID in !heapkeyspace pivot tuple */
|
|
|
|
htid = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (htid)
|
|
|
|
values[j++] = ItemPointerGetDatum(htid);
|
|
|
|
else
|
|
|
|
nulls[j++] = true;
|
|
|
|
|
|
|
|
if (BTreeTupleIsPosting(itup))
|
|
|
|
{
|
|
|
|
/* Build an array of item pointers */
|
|
|
|
ItemPointer tids;
|
|
|
|
Datum *tids_datum;
|
|
|
|
int nposting;
|
|
|
|
|
|
|
|
tids = BTreeTupleGetPosting(itup);
|
|
|
|
nposting = BTreeTupleGetNPosting(itup);
|
|
|
|
tids_datum = (Datum *) palloc(nposting * sizeof(Datum));
|
|
|
|
for (int i = 0; i < nposting; i++)
|
|
|
|
tids_datum[i] = ItemPointerGetDatum(&tids[i]);
|
2022-07-01 11:51:45 +03:00
|
|
|
values[j++] = PointerGetDatum(construct_array_builtin(tids_datum, nposting, TIDOID));
|
2020-02-29 23:10:17 +03:00
|
|
|
pfree(tids_datum);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nulls[j++] = true;
|
2017-04-05 06:48:49 +03:00
|
|
|
|
2020-02-29 23:10:17 +03:00
|
|
|
/* Build and return the result tuple */
|
|
|
|
tuple = heap_form_tuple(uargs->tupd, values, nulls);
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
return HeapTupleGetDatum(tuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------
|
|
|
|
* bt_page_items()
|
|
|
|
*
|
|
|
|
* Get IndexTupleData set in a btree page
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM bt_page_items('t1_pkey', 1);
|
|
|
|
*-------------------------------------------------------
|
|
|
|
*/
|
2021-01-19 12:28:05 +03:00
|
|
|
static Datum
|
|
|
|
bt_page_items_internal(PG_FUNCTION_ARGS, enum pageinspect_version ext_version)
|
2007-05-17 23:11:25 +04:00
|
|
|
{
|
2017-03-13 02:35:34 +03:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2021-01-19 12:28:05 +03:00
|
|
|
int64 blkno = (ext_version == PAGEINSPECT_V1_8 ? PG_GETARG_UINT32(1) : PG_GETARG_INT64(1));
|
2007-05-17 23:11:25 +04:00
|
|
|
Datum result;
|
|
|
|
FuncCallContext *fctx;
|
|
|
|
MemoryContext mctx;
|
2023-01-02 21:02:29 +03:00
|
|
|
ua_page_items *uargs;
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2020-01-30 19:32:04 +03:00
|
|
|
errmsg("must be superuser to use pageinspect functions")));
|
2007-05-17 23:11:25 +04:00
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
2007-08-27 03:22:49 +04:00
|
|
|
RangeVar *relrv;
|
|
|
|
Relation rel;
|
|
|
|
Buffer buffer;
|
|
|
|
BTPageOpaque opaque;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
|
2007-05-17 23:11:25 +04:00
|
|
|
fctx = SRF_FIRSTCALL_INIT();
|
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
bt_index_block_validate(rel, blkno);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
buffer = ReadBuffer(rel, blkno);
|
2012-12-01 02:02:29 +04:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
/*
|
|
|
|
* We copy the page into local storage to avoid holding pin on the
|
|
|
|
* buffer longer than we must, and possibly failing to release it at
|
|
|
|
* all if the calling query doesn't fetch all rows.
|
|
|
|
*/
|
|
|
|
mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
|
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
uargs = palloc(sizeof(ua_page_items));
|
2007-08-27 03:22:49 +04:00
|
|
|
|
|
|
|
uargs->page = palloc(BLCKSZ);
|
2016-04-20 16:31:19 +03:00
|
|
|
memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2012-12-01 02:02:29 +04:00
|
|
|
UnlockReleaseBuffer(buffer);
|
2007-08-27 03:22:49 +04:00
|
|
|
relation_close(rel, AccessShareLock);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
uargs->offset = FirstOffsetNumber;
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2022-04-01 07:24:50 +03:00
|
|
|
opaque = BTPageGetOpaque(uargs->page);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2021-02-25 05:41:34 +03:00
|
|
|
if (!P_ISDELETED(opaque))
|
|
|
|
fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Don't interpret BTDeletedPageData as index tuples */
|
|
|
|
elog(NOTICE, "page from block " INT64_FORMAT " is deleted", blkno);
|
|
|
|
fctx->max_calls = 0;
|
|
|
|
}
|
2020-02-29 23:10:17 +03:00
|
|
|
uargs->leafpage = P_ISLEAF(opaque);
|
|
|
|
uargs->rightmost = P_RIGHTMOST(opaque);
|
2007-08-27 03:22:49 +04:00
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
2020-02-29 23:10:17 +03:00
|
|
|
tupleDesc = BlessTupleDesc(tupleDesc);
|
2007-08-27 03:22:49 +04:00
|
|
|
|
2020-02-29 23:10:17 +03:00
|
|
|
uargs->tupd = tupleDesc;
|
2007-08-27 03:22:49 +04:00
|
|
|
|
2007-05-17 23:11:25 +04:00
|
|
|
fctx->user_fctx = uargs;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(mctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fctx = SRF_PERCALL_SETUP();
|
|
|
|
uargs = fctx->user_fctx;
|
|
|
|
|
|
|
|
if (fctx->call_cntr < fctx->max_calls)
|
|
|
|
{
|
2020-09-04 09:02:58 +03:00
|
|
|
result = bt_page_print_tuples(uargs);
|
2017-04-05 06:48:49 +03:00
|
|
|
uargs->offset++;
|
2007-05-17 23:11:25 +04:00
|
|
|
SRF_RETURN_NEXT(fctx, result);
|
|
|
|
}
|
2020-03-17 04:36:53 +03:00
|
|
|
|
|
|
|
SRF_RETURN_DONE(fctx);
|
2007-05-17 23:11:25 +04:00
|
|
|
}
|
|
|
|
|
2021-01-19 12:28:05 +03:00
|
|
|
Datum
|
|
|
|
bt_page_items_1_9(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return bt_page_items_internal(fcinfo, PAGEINSPECT_V1_9);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* entry point for old extension version */
|
|
|
|
Datum
|
|
|
|
bt_page_items(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
return bt_page_items_internal(fcinfo, PAGEINSPECT_V1_8);
|
|
|
|
}
|
|
|
|
|
2017-04-05 06:48:49 +03:00
|
|
|
/*-------------------------------------------------------
|
|
|
|
* bt_page_items_bytea()
|
|
|
|
*
|
|
|
|
* Get IndexTupleData set in a btree page
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM bt_page_items(get_raw_page('t1_pkey', 1));
|
|
|
|
*-------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
Datum
|
|
|
|
bt_page_items_bytea(PG_FUNCTION_ARGS)
|
|
|
|
{
|
|
|
|
bytea *raw_page = PG_GETARG_BYTEA_P(0);
|
|
|
|
Datum result;
|
|
|
|
FuncCallContext *fctx;
|
2023-01-02 21:02:29 +03:00
|
|
|
ua_page_items *uargs;
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2020-01-30 19:32:04 +03:00
|
|
|
errmsg("must be superuser to use raw page functions")));
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
BTPageOpaque opaque;
|
|
|
|
MemoryContext mctx;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
|
|
|
|
fctx = SRF_FIRSTCALL_INIT();
|
|
|
|
mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
|
|
|
|
|
2023-01-02 21:02:29 +03:00
|
|
|
uargs = palloc(sizeof(ua_page_items));
|
2017-04-05 06:48:49 +03:00
|
|
|
|
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 05:19:39 +03:00
|
|
|
uargs->page = get_page_from_raw(raw_page);
|
2017-04-05 06:48:49 +03: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 09:08:03 +03:00
|
|
|
if (PageIsNew(uargs->page))
|
|
|
|
{
|
|
|
|
MemoryContextSwitchTo(mctx);
|
|
|
|
PG_RETURN_NULL();
|
|
|
|
}
|
|
|
|
|
2017-04-05 06:48:49 +03:00
|
|
|
uargs->offset = FirstOffsetNumber;
|
|
|
|
|
pageinspect: Add more sanity checks to prevent out-of-bound reads
A couple of code paths use the special area on the page passed by the
function caller, expecting to find some data in it. However, feeding
an incorrect page can lead to out-of-bound reads when trying to access
the page special area (like a heap page that has no special area,
leading PageGetSpecialPointer() to grab a pointer outside the allocated
page).
The functions used for hash and btree indexes have some protection
already against that, while some other functions using a relation OID
as argument would make sure that the access method involved is correct,
but functions taking in input a raw page without knowing the relation
the page is attached to would run into problems.
This commit improves the set of checks used in the code paths of BRIN,
btree (including one check if a leaf page is found with a non-zero
level), GIN and GiST to verify that the page given in input has a
special area size that fits with each access method, which is done
though PageGetSpecialSize(), becore calling PageGetSpecialPointer().
The scope of the checks done is limited to work with pages that one
would pass after getting a block with get_raw_page(), as it is possible
to craft byteas that could bypass existing code paths. Having too many
checks would also impact the usability of pageinspect, as the existing
code is very useful to look at the content details in a corrupted page,
so the focus is really to avoid out-of-bound reads as this is never a
good thing even with functions whose execution is limited to
superusers.
The safest approach could be to rework the functions so as these fetch a
block using a relation OID and a block number, but there are also cases
where using a raw page is useful.
Tests are added to cover all the code paths that needed such checks, and
an error message for hash indexes is reworded to fit better with what
this commit adds.
Reported-By: Alexander Lakhin
Author: Julien Rouhaud, Michael Paquier
Discussion: https://postgr.es/m/16527-ef7606186f0610a1@postgresql.org
Discussion: https://postgr.es/m/561e187b-3549-c8d5-03f5-525c14e65bd0@postgrespro.ru
Backpatch-through: 10
2022-03-27 11:53:40 +03:00
|
|
|
/* verify the special space has the expected size */
|
|
|
|
if (PageGetSpecialSize(uargs->page) != MAXALIGN(sizeof(BTPageOpaqueData)))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("input page is not a valid %s page", "btree"),
|
|
|
|
errdetail("Expected special size %d, got %d.",
|
|
|
|
(int) MAXALIGN(sizeof(BTPageOpaqueData)),
|
|
|
|
(int) PageGetSpecialSize(uargs->page))));
|
|
|
|
|
2022-04-01 07:24:50 +03:00
|
|
|
opaque = BTPageGetOpaque(uargs->page);
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
if (P_ISMETA(opaque))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("block is a meta page")));
|
|
|
|
|
pageinspect: Add more sanity checks to prevent out-of-bound reads
A couple of code paths use the special area on the page passed by the
function caller, expecting to find some data in it. However, feeding
an incorrect page can lead to out-of-bound reads when trying to access
the page special area (like a heap page that has no special area,
leading PageGetSpecialPointer() to grab a pointer outside the allocated
page).
The functions used for hash and btree indexes have some protection
already against that, while some other functions using a relation OID
as argument would make sure that the access method involved is correct,
but functions taking in input a raw page without knowing the relation
the page is attached to would run into problems.
This commit improves the set of checks used in the code paths of BRIN,
btree (including one check if a leaf page is found with a non-zero
level), GIN and GiST to verify that the page given in input has a
special area size that fits with each access method, which is done
though PageGetSpecialSize(), becore calling PageGetSpecialPointer().
The scope of the checks done is limited to work with pages that one
would pass after getting a block with get_raw_page(), as it is possible
to craft byteas that could bypass existing code paths. Having too many
checks would also impact the usability of pageinspect, as the existing
code is very useful to look at the content details in a corrupted page,
so the focus is really to avoid out-of-bound reads as this is never a
good thing even with functions whose execution is limited to
superusers.
The safest approach could be to rework the functions so as these fetch a
block using a relation OID and a block number, but there are also cases
where using a raw page is useful.
Tests are added to cover all the code paths that needed such checks, and
an error message for hash indexes is reworded to fit better with what
this commit adds.
Reported-By: Alexander Lakhin
Author: Julien Rouhaud, Michael Paquier
Discussion: https://postgr.es/m/16527-ef7606186f0610a1@postgresql.org
Discussion: https://postgr.es/m/561e187b-3549-c8d5-03f5-525c14e65bd0@postgrespro.ru
Backpatch-through: 10
2022-03-27 11:53:40 +03:00
|
|
|
if (P_ISLEAF(opaque) && opaque->btpo_level != 0)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
|
|
|
|
errmsg("block is not a valid btree leaf page")));
|
|
|
|
|
2017-04-05 06:48:49 +03:00
|
|
|
if (P_ISDELETED(opaque))
|
|
|
|
elog(NOTICE, "page is deleted");
|
|
|
|
|
2021-02-25 05:41:34 +03:00
|
|
|
if (!P_ISDELETED(opaque))
|
|
|
|
fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Don't interpret BTDeletedPageData as index tuples */
|
|
|
|
elog(NOTICE, "page from block is deleted");
|
|
|
|
fctx->max_calls = 0;
|
|
|
|
}
|
2020-02-29 23:10:17 +03:00
|
|
|
uargs->leafpage = P_ISLEAF(opaque);
|
|
|
|
uargs->rightmost = P_RIGHTMOST(opaque);
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
2020-02-29 23:10:17 +03:00
|
|
|
tupleDesc = BlessTupleDesc(tupleDesc);
|
2017-04-05 06:48:49 +03:00
|
|
|
|
2020-02-29 23:10:17 +03:00
|
|
|
uargs->tupd = tupleDesc;
|
2017-04-05 06:48:49 +03:00
|
|
|
|
|
|
|
fctx->user_fctx = uargs;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(mctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fctx = SRF_PERCALL_SETUP();
|
|
|
|
uargs = fctx->user_fctx;
|
|
|
|
|
|
|
|
if (fctx->call_cntr < fctx->max_calls)
|
|
|
|
{
|
2020-09-04 09:02:58 +03:00
|
|
|
result = bt_page_print_tuples(uargs);
|
2017-04-05 06:48:49 +03:00
|
|
|
uargs->offset++;
|
|
|
|
SRF_RETURN_NEXT(fctx, result);
|
|
|
|
}
|
2020-03-17 04:36:53 +03:00
|
|
|
|
|
|
|
SRF_RETURN_DONE(fctx);
|
2017-04-05 06:48:49 +03:00
|
|
|
}
|
|
|
|
|
2020-03-08 03:44:53 +03:00
|
|
|
/* Number of output arguments (columns) for bt_metap() */
|
|
|
|
#define BT_METAP_COLS_V1_8 9
|
2007-05-17 23:11:25 +04:00
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* bt_metap()
|
|
|
|
*
|
2007-08-27 03:22:49 +04:00
|
|
|
* Get a btree's meta-page information
|
2007-05-17 23:11:25 +04:00
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM bt_metap('t1_pkey')
|
|
|
|
* ------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
bt_metap(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 02:35:34 +03:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2007-08-27 03:22:49 +04:00
|
|
|
Datum result;
|
2007-05-17 23:11:25 +04:00
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
2007-08-27 03:22:49 +04:00
|
|
|
BTMetaPageData *metad;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
int j;
|
2020-02-29 23:10:17 +03:00
|
|
|
char *values[9];
|
2007-08-27 03:22:49 +04:00
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
2020-01-30 19:32:04 +03:00
|
|
|
errmsg("must be superuser to use pageinspect functions")));
|
2007-05-17 23:11:25 +04:00
|
|
|
|
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
|
|
|
|
|
|
|
if (!IS_INDEX(rel) || !IS_BTREE(rel))
|
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 05:19:39 +03:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a %s index",
|
|
|
|
RelationGetRelationName(rel), "btree")));
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2009-04-01 02:54:31 +04: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-17 23:11:25 +04:00
|
|
|
buffer = ReadBuffer(rel, 0);
|
2012-12-01 02:02:29 +04:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
|
|
|
|
2016-04-20 16:31:19 +03:00
|
|
|
page = BufferGetPage(buffer);
|
2007-08-27 03:22:49 +04:00
|
|
|
metad = BTPageGetMeta(page);
|
|
|
|
|
|
|
|
/* Build a tuple descriptor for our result type */
|
|
|
|
if (get_call_result_type(fcinfo, NULL, &tupleDesc) != TYPEFUNC_COMPOSITE)
|
|
|
|
elog(ERROR, "return type must be a row type");
|
|
|
|
|
2020-03-08 03:44:53 +03:00
|
|
|
/*
|
|
|
|
* We need a kluge here to detect API versions prior to 1.8. Earlier
|
2021-02-25 05:41:34 +03:00
|
|
|
* versions incorrectly used int4 for certain columns.
|
2020-03-08 03:44:53 +03:00
|
|
|
*
|
|
|
|
* There is no way to reliably avoid the problems created by the old
|
|
|
|
* function definition at this point, so insist that the user update the
|
|
|
|
* extension.
|
|
|
|
*/
|
|
|
|
if (tupleDesc->natts < BT_METAP_COLS_V1_8)
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INVALID_FUNCTION_DEFINITION),
|
|
|
|
errmsg("function has wrong number of declared columns"),
|
|
|
|
errhint("To resolve the problem, update the \"pageinspect\" extension to the latest version.")));
|
|
|
|
|
2007-08-27 03:22:49 +04:00
|
|
|
j = 0;
|
2014-01-07 06:30:26 +04:00
|
|
|
values[j++] = psprintf("%d", metad->btm_magic);
|
|
|
|
values[j++] = psprintf("%d", metad->btm_version);
|
2020-03-08 03:44:53 +03:00
|
|
|
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_root);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_level);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastroot);
|
|
|
|
values[j++] = psprintf(INT64_FORMAT, (int64) metad->btm_fastlevel);
|
2018-04-05 17:56:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get values of extended metadata if available, use default values
|
2020-02-29 23:10:17 +03:00
|
|
|
* otherwise. Note that we rely on the assumption that btm_allequalimage
|
|
|
|
* is initialized to zero with indexes that were built on versions prior
|
|
|
|
* to Postgres 13 (just like _bt_metaversion()).
|
2018-04-05 17:56:00 +03:00
|
|
|
*/
|
Make heap TID a tiebreaker nbtree index column.
Make nbtree treat all index tuples as having a heap TID attribute.
Index searches can distinguish duplicates by heap TID, since heap TID is
always guaranteed to be unique. This general approach has numerous
benefits for performance, and is prerequisite to teaching VACUUM to
perform "retail index tuple deletion".
Naively adding a new attribute to every pivot tuple has unacceptable
overhead (it bloats internal pages), so suffix truncation of pivot
tuples is added. This will usually truncate away the "extra" heap TID
attribute from pivot tuples during a leaf page split, and may also
truncate away additional user attributes. This can increase fan-out,
especially in a multi-column index. Truncation can only occur at the
attribute granularity, which isn't particularly effective, but works
well enough for now. A future patch may add support for truncating
"within" text attributes by generating truncated key values using new
opclass infrastructure.
Only new indexes (BTREE_VERSION 4 indexes) will have insertions that
treat heap TID as a tiebreaker attribute, or will have pivot tuples
undergo suffix truncation during a leaf page split (on-disk
compatibility with versions 2 and 3 is preserved). Upgrades to version
4 cannot be performed on-the-fly, unlike upgrades from version 2 to
version 3. contrib/amcheck continues to work with version 2 and 3
indexes, while also enforcing stricter invariants when verifying version
4 indexes. These stricter invariants are the same invariants described
by "3.1.12 Sequencing" from the Lehman and Yao paper.
A later patch will enhance the logic used by nbtree to pick a split
point. This patch is likely to negatively impact performance without
smarter choices around the precise point to split leaf pages at. Making
these two mostly-distinct sets of enhancements into distinct commits
seems like it might clarify their design, even though neither commit is
particularly useful on its own.
The maximum allowed size of new tuples is reduced by an amount equal to
the space required to store an extra MAXALIGN()'d TID in a new high key
during leaf page splits. The user-facing definition of the "1/3 of a
page" restriction is already imprecise, and so does not need to be
revised. However, there should be a compatibility note in the v12
release notes.
Author: Peter Geoghegan
Reviewed-By: Heikki Linnakangas, Alexander Korotkov
Discussion: https://postgr.es/m/CAH2-WzkVb0Kom=R+88fDFb=JSxZMFvbHVC6Mn9LJ2n=X=kS-Uw@mail.gmail.com
2019-03-20 20:04:01 +03:00
|
|
|
if (metad->btm_version >= BTREE_NOVAC_VERSION)
|
2018-04-05 17:56:00 +03:00
|
|
|
{
|
2021-02-25 05:41:34 +03:00
|
|
|
values[j++] = psprintf(INT64_FORMAT,
|
|
|
|
(int64) metad->btm_last_cleanup_num_delpages);
|
2018-05-20 18:40:54 +03:00
|
|
|
values[j++] = psprintf("%f", metad->btm_last_cleanup_num_heap_tuples);
|
2020-02-29 23:10:17 +03:00
|
|
|
values[j++] = metad->btm_allequalimage ? "t" : "f";
|
2018-04-05 17:56:00 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values[j++] = "0";
|
|
|
|
values[j++] = "-1";
|
2020-02-29 23:10:17 +03:00
|
|
|
values[j++] = "f";
|
2018-04-05 17:56:00 +03:00
|
|
|
}
|
2007-08-27 03:22:49 +04:00
|
|
|
|
|
|
|
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
|
|
|
|
values);
|
|
|
|
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
2007-05-17 23:11:25 +04:00
|
|
|
|
2012-12-01 02:02:29 +04:00
|
|
|
UnlockReleaseBuffer(buffer);
|
2007-05-17 23:11:25 +04:00
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|