mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Add relkind checks to certain contrib modules
The contrib extensions pageinspect, pg_visibility and pgstattuple only work against regular relations which have storage. They don't work against foreign tables, partitioned (parent) tables, views, et al. Add checks to the user-callable functions to return a useful error message to the user if they mistakenly pass an invalid relation to a function which doesn't accept that kind of relation. In passing, improve some of the existing checks to use ereport() instead of elog(), add a function to consolidate common checks where appropriate, and add some regression tests. Author: Amit Langote, with various changes by me Reviewed by: Michael Paquier and Corey Huinker Discussion: https://postgr.es/m/ab91fd9d-4751-ee77-c87b-4dd704c1e59c@lab.ntt.co.jp
This commit is contained in:
@ -128,7 +128,7 @@ typedef struct HashIndexStat
|
||||
|
||||
static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo);
|
||||
static void GetHashPageStats(Page page, HashIndexStat *stats);
|
||||
|
||||
static void check_relation_relkind(Relation rel);
|
||||
|
||||
/* ------------------------------------------------------
|
||||
* pgstatindex()
|
||||
@ -221,8 +221,10 @@ pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo)
|
||||
BufferAccessStrategy bstrategy = GetAccessStrategy(BAS_BULKREAD);
|
||||
|
||||
if (!IS_INDEX(rel) || !IS_BTREE(rel))
|
||||
elog(ERROR, "relation \"%s\" is not a btree index",
|
||||
RelationGetRelationName(rel));
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("relation \"%s\" is not a btree index",
|
||||
RelationGetRelationName(rel))));
|
||||
|
||||
/*
|
||||
* Reject attempts to read non-local temporary relations; we would be
|
||||
@ -388,6 +390,9 @@ pg_relpages(PG_FUNCTION_ARGS)
|
||||
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
||||
rel = relation_openrv(relrv, AccessShareLock);
|
||||
|
||||
/* only some relkinds have storage */
|
||||
check_relation_relkind(rel);
|
||||
|
||||
/* note: this will work OK on non-local temp tables */
|
||||
|
||||
relpages = RelationGetNumberOfBlocks(rel);
|
||||
@ -409,6 +414,9 @@ pg_relpages_v1_5(PG_FUNCTION_ARGS)
|
||||
relrv = makeRangeVarFromNameList(textToQualifiedNameList(relname));
|
||||
rel = relation_openrv(relrv, AccessShareLock);
|
||||
|
||||
/* only some relkinds have storage */
|
||||
check_relation_relkind(rel);
|
||||
|
||||
/* note: this will work OK on non-local temp tables */
|
||||
|
||||
relpages = RelationGetNumberOfBlocks(rel);
|
||||
@ -433,6 +441,9 @@ pg_relpagesbyid(PG_FUNCTION_ARGS)
|
||||
|
||||
rel = relation_open(relid, AccessShareLock);
|
||||
|
||||
/* only some relkinds have storage */
|
||||
check_relation_relkind(rel);
|
||||
|
||||
/* note: this will work OK on non-local temp tables */
|
||||
|
||||
relpages = RelationGetNumberOfBlocks(rel);
|
||||
@ -452,6 +463,9 @@ pg_relpagesbyid_v1_5(PG_FUNCTION_ARGS)
|
||||
|
||||
rel = relation_open(relid, AccessShareLock);
|
||||
|
||||
/* only some relkinds have storage */
|
||||
check_relation_relkind(rel);
|
||||
|
||||
/* note: this will work OK on non-local temp tables */
|
||||
|
||||
relpages = RelationGetNumberOfBlocks(rel);
|
||||
@ -508,8 +522,10 @@ pgstatginindex_internal(Oid relid, FunctionCallInfo fcinfo)
|
||||
rel = relation_open(relid, AccessShareLock);
|
||||
|
||||
if (!IS_INDEX(rel) || !IS_GIN(rel))
|
||||
elog(ERROR, "relation \"%s\" is not a GIN index",
|
||||
RelationGetRelationName(rel));
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("relation \"%s\" is not a GIN index",
|
||||
RelationGetRelationName(rel))));
|
||||
|
||||
/*
|
||||
* Reject attempts to read non-local temporary relations; we would be
|
||||
@ -581,9 +597,13 @@ pgstathashindex(PG_FUNCTION_ARGS)
|
||||
|
||||
rel = index_open(relid, AccessShareLock);
|
||||
|
||||
/* index_open() checks that it's an index */
|
||||
if (!IS_HASH(rel))
|
||||
elog(ERROR, "relation \"%s\" is not a HASH index",
|
||||
RelationGetRelationName(rel));
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("relation \"%s\" is not a HASH index",
|
||||
RelationGetRelationName(rel))));
|
||||
|
||||
|
||||
/*
|
||||
* Reject attempts to read non-local temporary relations; we would be
|
||||
@ -723,3 +743,21 @@ GetHashPageStats(Page page, HashIndexStat *stats)
|
||||
}
|
||||
stats->free_space += PageGetExactFreeSpace(page);
|
||||
}
|
||||
|
||||
/*
|
||||
* check_relation_relkind - convenience routine to check that relation
|
||||
* is of the relkind supported by the callers
|
||||
*/
|
||||
static void
|
||||
check_relation_relkind(Relation rel)
|
||||
{
|
||||
if (rel->rd_rel->relkind != RELKIND_RELATION &&
|
||||
rel->rd_rel->relkind != RELKIND_INDEX &&
|
||||
rel->rd_rel->relkind != RELKIND_MATVIEW &&
|
||||
rel->rd_rel->relkind != RELKIND_SEQUENCE &&
|
||||
rel->rd_rel->relkind != RELKIND_TOASTVALUE)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is not a table, index, materialized view, sequence, or TOAST table",
|
||||
RelationGetRelationName(rel))));
|
||||
}
|
||||
|
Reference in New Issue
Block a user