2007-05-17 21:11:25 +02:00
|
|
|
/*
|
2010-09-20 22:08:53 +02:00
|
|
|
* contrib/pageinspect/btreefuncs.c
|
2008-05-17 03:28:26 +02:00
|
|
|
*
|
|
|
|
*
|
2007-05-17 21:11:25 +02: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"
|
|
|
|
|
2017-02-03 17:34:41 +01:00
|
|
|
#include "pageinspect.h"
|
|
|
|
|
2007-05-17 21:11:25 +02:00
|
|
|
#include "access/nbtree.h"
|
|
|
|
#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 01:36:59 +01:00
|
|
|
#include "catalog/pg_am.h"
|
2007-08-27 01:22:49 +02:00
|
|
|
#include "funcapi.h"
|
|
|
|
#include "miscadmin.h"
|
2007-05-17 21:11:25 +02:00
|
|
|
#include "utils/builtins.h"
|
2011-02-23 18:18:09 +01:00
|
|
|
#include "utils/rel.h"
|
2017-01-21 02:29:53 +01:00
|
|
|
#include "utils/varlena.h"
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
PG_FUNCTION_INFO_V1(bt_metap);
|
|
|
|
PG_FUNCTION_INFO_V1(bt_page_items);
|
2017-04-05 05:48:49 +02:00
|
|
|
PG_FUNCTION_INFO_V1(bt_page_items_bytea);
|
2007-08-27 01:22:49 +02:00
|
|
|
PG_FUNCTION_INFO_V1(bt_page_stats);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
#define IS_INDEX(r) ((r)->rd_rel->relkind == RELKIND_INDEX)
|
2007-05-17 21:11:25 +02:00
|
|
|
#define IS_BTREE(r) ((r)->rd_rel->relam == BTREE_AM_OID)
|
|
|
|
|
2007-07-16 01:46:20 +02:00
|
|
|
/* note: BlockNumber is unsigned, hence can't be negative */
|
2007-05-17 21:11:25 +02:00
|
|
|
#define CHECK_RELATION_BLOCK_RANGE(rel, blkno) { \
|
2007-07-16 01:46:20 +02:00
|
|
|
if ( RelationGetNumberOfBlocks(rel) <= (BlockNumber) (blkno) ) \
|
2007-07-16 01:09:26 +02:00
|
|
|
elog(ERROR, "block number out of range"); }
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* 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;
|
|
|
|
union
|
|
|
|
{
|
|
|
|
uint32 level;
|
|
|
|
TransactionId xact;
|
|
|
|
} btpo;
|
|
|
|
uint16 btpo_flags;
|
|
|
|
BTCycleId btpo_cycleid;
|
2009-06-11 16:49:15 +02:00
|
|
|
} BTPageStat;
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* -------------------------------------------------
|
|
|
|
* GetBTPageStatistics()
|
|
|
|
*
|
2007-08-27 01:22:49 +02:00
|
|
|
* Collect statistics of single b-tree page
|
2007-05-17 21:11:25 +02:00
|
|
|
* -------------------------------------------------
|
|
|
|
*/
|
|
|
|
static void
|
2009-06-11 16:49:15 +02:00
|
|
|
GetBTPageStatistics(BlockNumber blkno, Buffer buffer, BTPageStat *stat)
|
2007-05-17 21:11:25 +02:00
|
|
|
{
|
2016-04-20 15:31:19 +02:00
|
|
|
Page page = BufferGetPage(buffer);
|
2007-05-17 21:11:25 +02:00
|
|
|
PageHeader phdr = (PageHeader) page;
|
|
|
|
OffsetNumber maxoff = PageGetMaxOffsetNumber(page);
|
|
|
|
BTPageOpaque opaque = (BTPageOpaque) PageGetSpecialPointer(page);
|
|
|
|
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))
|
|
|
|
{
|
|
|
|
stat->type = 'd';
|
|
|
|
stat->btpo.xact = opaque->btpo.xact;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
stat->btpo.level = opaque->btpo.level;
|
|
|
|
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 00:10:26 +02:00
|
|
|
if (!ItemIdIsDead(id))
|
2007-05-17 21:11:25 +02: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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* -----------------------------------------------
|
2012-11-30 23:02:29 +01:00
|
|
|
* bt_page_stats()
|
2007-05-17 21:11:25 +02:00
|
|
|
*
|
2012-11-30 23:02:29 +01:00
|
|
|
* Usage: SELECT * FROM bt_page_stats('t1_pkey', 1);
|
2007-05-17 21:11:25 +02:00
|
|
|
* -----------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
bt_page_stats(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 00:35:34 +01:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2007-05-17 21:11:25 +02:00
|
|
|
uint32 blkno = PG_GETARG_UINT32(1);
|
|
|
|
Buffer buffer;
|
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
|
|
|
Datum result;
|
2007-08-27 01:22:49 +02:00
|
|
|
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"))));
|
2007-05-17 21:11:25 +02: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 03:20:57 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a %s index",
|
|
|
|
RelationGetRelationName(rel), "btree")));
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2009-04-01 00:54:31 +02:00
|
|
|
/*
|
2009-06-11 16:49:15 +02: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.
|
2009-04-01 00:54:31 +02:00
|
|
|
*/
|
|
|
|
if (RELATION_IS_OTHER_TEMP(rel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot access temporary tables of other sessions")));
|
|
|
|
|
2007-05-17 21:11:25 +02:00
|
|
|
if (blkno == 0)
|
2007-07-16 01:09:26 +02:00
|
|
|
elog(ERROR, "block 0 is a meta page");
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
CHECK_RELATION_BLOCK_RANGE(rel, blkno);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
buffer = ReadBuffer(rel, blkno);
|
2012-11-30 23:02:29 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02: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-11-30 23:02:29 +01:00
|
|
|
UnlockReleaseBuffer(buffer);
|
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
2007-08-27 01:22:49 +02: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;
|
2014-01-07 03:30:26 +01:00
|
|
|
values[j++] = psprintf("%d", stat.blkno);
|
|
|
|
values[j++] = psprintf("%c", stat.type);
|
|
|
|
values[j++] = psprintf("%d", stat.live_items);
|
|
|
|
values[j++] = psprintf("%d", stat.dead_items);
|
|
|
|
values[j++] = psprintf("%d", stat.avg_item_size);
|
|
|
|
values[j++] = psprintf("%d", stat.page_size);
|
|
|
|
values[j++] = psprintf("%d", stat.free_size);
|
|
|
|
values[j++] = psprintf("%d", stat.btpo_prev);
|
|
|
|
values[j++] = psprintf("%d", stat.btpo_next);
|
|
|
|
values[j++] = psprintf("%d", (stat.type == 'd') ? stat.btpo.xact : stat.btpo.level);
|
|
|
|
values[j++] = psprintf("%d", stat.btpo_flags);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
|
|
|
|
values);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
result = HeapTupleGetDatum(tuple);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* cross-call data structure for SRF
|
2007-05-17 21:11:25 +02:00
|
|
|
*/
|
|
|
|
struct user_args
|
|
|
|
{
|
|
|
|
Page page;
|
2007-08-27 01:22:49 +02:00
|
|
|
OffsetNumber offset;
|
2007-05-17 21:11:25 +02:00
|
|
|
};
|
|
|
|
|
2017-04-05 05:48:49 +02:00
|
|
|
/*-------------------------------------------------------
|
|
|
|
* bt_page_print_tuples()
|
|
|
|
*
|
|
|
|
* Form a tuple describing index tuple at a given offset
|
|
|
|
* ------------------------------------------------------
|
|
|
|
*/
|
|
|
|
static Datum
|
|
|
|
bt_page_print_tuples(FuncCallContext *fctx, Page page, OffsetNumber offset)
|
|
|
|
{
|
|
|
|
char *values[6];
|
|
|
|
HeapTuple tuple;
|
|
|
|
ItemId id;
|
|
|
|
IndexTuple itup;
|
|
|
|
int j;
|
|
|
|
int off;
|
|
|
|
int dlen;
|
|
|
|
char *dump;
|
|
|
|
char *ptr;
|
|
|
|
|
|
|
|
id = PageGetItemId(page, offset);
|
|
|
|
|
|
|
|
if (!ItemIdIsValid(id))
|
|
|
|
elog(ERROR, "invalid ItemId");
|
|
|
|
|
|
|
|
itup = (IndexTuple) PageGetItem(page, id);
|
|
|
|
|
|
|
|
j = 0;
|
|
|
|
values[j++] = psprintf("%d", offset);
|
|
|
|
values[j++] = psprintf("(%u,%u)",
|
|
|
|
ItemPointerGetBlockNumberNoCheck(&itup->t_tid),
|
|
|
|
ItemPointerGetOffsetNumberNoCheck(&itup->t_tid));
|
|
|
|
values[j++] = psprintf("%d", (int) IndexTupleSize(itup));
|
|
|
|
values[j++] = psprintf("%c", IndexTupleHasNulls(itup) ? 't' : 'f');
|
|
|
|
values[j++] = psprintf("%c", IndexTupleHasVarwidths(itup) ? 't' : 'f');
|
|
|
|
|
|
|
|
ptr = (char *) itup + IndexInfoFindDataOffset(itup->t_info);
|
|
|
|
dlen = IndexTupleSize(itup) - IndexInfoFindDataOffset(itup->t_info);
|
|
|
|
dump = palloc0(dlen * 3 + 1);
|
|
|
|
values[j] = dump;
|
|
|
|
for (off = 0; off < dlen; off++)
|
|
|
|
{
|
|
|
|
if (off > 0)
|
|
|
|
*dump++ = ' ';
|
|
|
|
sprintf(dump, "%02x", *(ptr + off) & 0xff);
|
|
|
|
dump += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
tuple = BuildTupleFromCStrings(fctx->attinmeta, values);
|
|
|
|
|
|
|
|
return HeapTupleGetDatum(tuple);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------
|
|
|
|
* bt_page_items()
|
|
|
|
*
|
|
|
|
* Get IndexTupleData set in a btree page
|
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM bt_page_items('t1_pkey', 1);
|
|
|
|
*-------------------------------------------------------
|
|
|
|
*/
|
2007-05-17 21:11:25 +02:00
|
|
|
Datum
|
|
|
|
bt_page_items(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 00:35:34 +01:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2007-05-17 21:11:25 +02:00
|
|
|
uint32 blkno = PG_GETARG_UINT32(1);
|
|
|
|
Datum result;
|
|
|
|
FuncCallContext *fctx;
|
|
|
|
MemoryContext mctx;
|
2007-08-27 01:22:49 +02:00
|
|
|
struct user_args *uargs;
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pageinspect functions"))));
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
2007-08-27 01:22:49 +02:00
|
|
|
RangeVar *relrv;
|
|
|
|
Relation rel;
|
|
|
|
Buffer buffer;
|
|
|
|
BTPageOpaque opaque;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
|
2007-05-17 21:11:25 +02:00
|
|
|
fctx = SRF_FIRSTCALL_INIT();
|
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
|
|
|
rel = relation_openrv(relrv, AccessShareLock);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02: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 03:20:57 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a %s index",
|
|
|
|
RelationGetRelationName(rel), "btree")));
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2009-04-01 00:54:31 +02:00
|
|
|
/*
|
2009-06-11 16:49:15 +02:00
|
|
|
* Reject attempts to read non-local temporary relations; we would be
|
|
|
|
* likely to get wrong data since we have no visibility into the
|
2009-04-01 00:54:31 +02:00
|
|
|
* owning session's local buffers.
|
|
|
|
*/
|
|
|
|
if (RELATION_IS_OTHER_TEMP(rel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
Phase 3 of pgindent updates.
Don't move parenthesized lines to the left, even if that means they
flow past the right margin.
By default, BSD indent lines up statement continuation lines that are
within parentheses so that they start just to the right of the preceding
left parenthesis. However, traditionally, if that resulted in the
continuation line extending to the right of the desired right margin,
then indent would push it left just far enough to not overrun the margin,
if it could do so without making the continuation line start to the left of
the current statement indent. That makes for a weird mix of indentations
unless one has been completely rigid about never violating the 80-column
limit.
This behavior has been pretty universally panned by Postgres developers.
Hence, disable it with indent's new -lpl switch, so that parenthesized
lines are always lined up with the preceding left paren.
This patch is much less interesting than the first round of indent
changes, but also bulkier, so I thought it best to separate the effects.
Discussion: https://postgr.es/m/E1dAmxK-0006EE-1r@gemulon.postgresql.org
Discussion: https://postgr.es/m/30527.1495162840@sss.pgh.pa.us
2017-06-21 21:35:54 +02:00
|
|
|
errmsg("cannot access temporary tables of other sessions")));
|
2009-04-01 00:54:31 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
if (blkno == 0)
|
|
|
|
elog(ERROR, "block 0 is a meta page");
|
|
|
|
|
|
|
|
CHECK_RELATION_BLOCK_RANGE(rel, blkno);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
buffer = ReadBuffer(rel, blkno);
|
2012-11-30 23:02:29 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
/*
|
2007-11-15 22:14:46 +01: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.
|
2007-08-27 01:22:49 +02:00
|
|
|
*/
|
|
|
|
mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
uargs = palloc(sizeof(struct user_args));
|
|
|
|
|
|
|
|
uargs->page = palloc(BLCKSZ);
|
2016-04-20 15:31:19 +02:00
|
|
|
memcpy(uargs->page, BufferGetPage(buffer), BLCKSZ);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2012-11-30 23:02:29 +01:00
|
|
|
UnlockReleaseBuffer(buffer);
|
2007-08-27 01:22:49 +02:00
|
|
|
relation_close(rel, AccessShareLock);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2007-08-27 01:22:49 +02:00
|
|
|
uargs->offset = FirstOffsetNumber;
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
|
|
|
|
|
|
|
|
if (P_ISDELETED(opaque))
|
2007-07-16 01:09:26 +02:00
|
|
|
elog(NOTICE, "page is deleted");
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
fctx->max_calls = PageGetMaxOffsetNumber(uargs->page);
|
2007-08-27 01:22:49 +02: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");
|
|
|
|
|
|
|
|
fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
|
|
|
|
|
2007-05-17 21:11:25 +02:00
|
|
|
fctx->user_fctx = uargs;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(mctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fctx = SRF_PERCALL_SETUP();
|
|
|
|
uargs = fctx->user_fctx;
|
|
|
|
|
|
|
|
if (fctx->call_cntr < fctx->max_calls)
|
|
|
|
{
|
2017-04-05 05:48:49 +02:00
|
|
|
result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
|
|
|
|
uargs->offset++;
|
2007-05-17 21:11:25 +02:00
|
|
|
SRF_RETURN_NEXT(fctx, result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-08-27 01:22:49 +02:00
|
|
|
pfree(uargs->page);
|
|
|
|
pfree(uargs);
|
2007-05-17 21:11:25 +02:00
|
|
|
SRF_RETURN_DONE(fctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-05 05:48:49 +02: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;
|
|
|
|
struct user_args *uargs;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pageinspect functions"))));
|
|
|
|
|
|
|
|
if (SRF_IS_FIRSTCALL())
|
|
|
|
{
|
|
|
|
BTPageOpaque opaque;
|
|
|
|
MemoryContext mctx;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
|
|
|
|
fctx = SRF_FIRSTCALL_INIT();
|
|
|
|
mctx = MemoryContextSwitchTo(fctx->multi_call_memory_ctx);
|
|
|
|
|
|
|
|
uargs = palloc(sizeof(struct user_args));
|
|
|
|
|
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 03:20:57 +01:00
|
|
|
uargs->page = get_page_from_raw(raw_page);
|
2017-04-05 05:48:49 +02: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 10:54:03 +02: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))));
|
|
|
|
|
2017-04-05 05:48:49 +02:00
|
|
|
opaque = (BTPageOpaque) PageGetSpecialPointer(uargs->page);
|
|
|
|
|
|
|
|
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 10:54:03 +02: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 05:48:49 +02:00
|
|
|
if (P_ISDELETED(opaque))
|
|
|
|
elog(NOTICE, "page is deleted");
|
|
|
|
|
|
|
|
fctx->max_calls = PageGetMaxOffsetNumber(uargs->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");
|
|
|
|
|
|
|
|
fctx->attinmeta = TupleDescGetAttInMetadata(tupleDesc);
|
|
|
|
|
|
|
|
fctx->user_fctx = uargs;
|
|
|
|
|
|
|
|
MemoryContextSwitchTo(mctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
fctx = SRF_PERCALL_SETUP();
|
|
|
|
uargs = fctx->user_fctx;
|
|
|
|
|
|
|
|
if (fctx->call_cntr < fctx->max_calls)
|
|
|
|
{
|
|
|
|
result = bt_page_print_tuples(fctx, uargs->page, uargs->offset);
|
|
|
|
uargs->offset++;
|
|
|
|
SRF_RETURN_NEXT(fctx, result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pfree(uargs);
|
|
|
|
SRF_RETURN_DONE(fctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-17 21:11:25 +02:00
|
|
|
|
|
|
|
/* ------------------------------------------------
|
|
|
|
* bt_metap()
|
|
|
|
*
|
2007-08-27 01:22:49 +02:00
|
|
|
* Get a btree's meta-page information
|
2007-05-17 21:11:25 +02:00
|
|
|
*
|
|
|
|
* Usage: SELECT * FROM bt_metap('t1_pkey')
|
|
|
|
* ------------------------------------------------
|
|
|
|
*/
|
|
|
|
Datum
|
|
|
|
bt_metap(PG_FUNCTION_ARGS)
|
|
|
|
{
|
2017-03-13 00:35:34 +01:00
|
|
|
text *relname = PG_GETARG_TEXT_PP(0);
|
2007-08-27 01:22:49 +02:00
|
|
|
Datum result;
|
2007-05-17 21:11:25 +02:00
|
|
|
Relation rel;
|
|
|
|
RangeVar *relrv;
|
2007-08-27 01:22:49 +02:00
|
|
|
BTMetaPageData *metad;
|
|
|
|
TupleDesc tupleDesc;
|
|
|
|
int j;
|
Skip full index scan during cleanup of B-tree indexes when possible
Vacuum of index consists from two stages: multiple (zero of more) ambulkdelete
calls and one amvacuumcleanup call. When workload on particular table
is append-only, then autovacuum isn't intended to touch this table. However,
user may run vacuum manually in order to fill visibility map and get benefits
of index-only scans. Then ambulkdelete wouldn't be called for indexes
of such table (because no heap tuples were deleted), only amvacuumcleanup would
be called In this case, amvacuumcleanup would perform full index scan for
two objectives: put recyclable pages into free space map and update index
statistics.
This patch allows btvacuumclanup to skip full index scan when two conditions
are satisfied: no pages are going to be put into free space map and index
statistics isn't stalled. In order to check first condition, we store
oldest btpo_xact in the meta-page. When it's precedes RecentGlobalXmin, then
there are some recyclable pages. In order to check second condition we store
number of heap tuples observed during previous full index scan by cleanup.
If fraction of newly inserted tuples is less than
vacuum_cleanup_index_scale_factor, then statistics isn't considered to be
stalled. vacuum_cleanup_index_scale_factor can be defined as both reloption and GUC (default).
This patch bumps B-tree meta-page version. Upgrade of meta-page is performed
"on the fly": during VACUUM meta-page is rewritten with new version. No special
handling in pg_upgrade is required.
Author: Masahiko Sawada, Alexander Korotkov
Review by: Peter Geoghegan, Kyotaro Horiguchi, Alexander Korotkov, Yura Sokolov
Discussion: https://www.postgresql.org/message-id/flat/CAD21AoAX+d2oD_nrd9O2YkpzHaFr=uQeGr9s1rKC3O4ENc568g@mail.gmail.com
2018-04-04 18:29:00 +02:00
|
|
|
char *values[8];
|
2007-08-27 01:22:49 +02:00
|
|
|
Buffer buffer;
|
|
|
|
Page page;
|
|
|
|
HeapTuple tuple;
|
|
|
|
|
|
|
|
if (!superuser())
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
|
|
|
|
(errmsg("must be superuser to use pageinspect functions"))));
|
2007-05-17 21:11:25 +02: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 03:20:57 +01:00
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
|
|
|
errmsg("\"%s\" is not a %s index",
|
|
|
|
RelationGetRelationName(rel), "btree")));
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2009-04-01 00:54:31 +02:00
|
|
|
/*
|
2009-06-11 16:49:15 +02: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.
|
2009-04-01 00:54:31 +02:00
|
|
|
*/
|
|
|
|
if (RELATION_IS_OTHER_TEMP(rel))
|
|
|
|
ereport(ERROR,
|
|
|
|
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
|
|
|
errmsg("cannot access temporary tables of other sessions")));
|
|
|
|
|
2007-05-17 21:11:25 +02:00
|
|
|
buffer = ReadBuffer(rel, 0);
|
2012-11-30 23:02:29 +01:00
|
|
|
LockBuffer(buffer, BUFFER_LOCK_SHARE);
|
|
|
|
|
2016-04-20 15:31:19 +02:00
|
|
|
page = BufferGetPage(buffer);
|
2007-08-27 01:22:49 +02: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");
|
|
|
|
|
|
|
|
j = 0;
|
2014-01-07 03:30:26 +01:00
|
|
|
values[j++] = psprintf("%d", metad->btm_magic);
|
|
|
|
values[j++] = psprintf("%d", metad->btm_version);
|
|
|
|
values[j++] = psprintf("%d", metad->btm_root);
|
|
|
|
values[j++] = psprintf("%d", metad->btm_level);
|
|
|
|
values[j++] = psprintf("%d", metad->btm_fastroot);
|
|
|
|
values[j++] = psprintf("%d", metad->btm_fastlevel);
|
2018-04-05 16:56:00 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get values of extended metadata if available, use default values
|
|
|
|
* otherwise.
|
|
|
|
*/
|
|
|
|
if (metad->btm_version == BTREE_VERSION)
|
|
|
|
{
|
2020-03-11 22:15:00 +01:00
|
|
|
/*
|
|
|
|
* kludge: btm_oldest_btpo_xact is declared as int4, which is wrong.
|
|
|
|
* We should at least avoid raising an error when its value happens to
|
|
|
|
* exceed PG_INT32_MAX, though.
|
|
|
|
*/
|
|
|
|
values[j++] = psprintf("%d", (int) metad->btm_oldest_btpo_xact);
|
2018-05-20 17:40:54 +02:00
|
|
|
values[j++] = psprintf("%f", metad->btm_last_cleanup_num_heap_tuples);
|
2018-04-05 16:56:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
values[j++] = "0";
|
|
|
|
values[j++] = "-1";
|
|
|
|
}
|
2007-08-27 01:22:49 +02:00
|
|
|
|
|
|
|
tuple = BuildTupleFromCStrings(TupleDescGetAttInMetadata(tupleDesc),
|
|
|
|
values);
|
|
|
|
|
|
|
|
result = HeapTupleGetDatum(tuple);
|
2007-05-17 21:11:25 +02:00
|
|
|
|
2012-11-30 23:02:29 +01:00
|
|
|
UnlockReleaseBuffer(buffer);
|
2007-05-17 21:11:25 +02:00
|
|
|
relation_close(rel, AccessShareLock);
|
|
|
|
|
|
|
|
PG_RETURN_DATUM(result);
|
|
|
|
}
|