diff options
author | Stephen Frost | 2017-03-09 21:34:25 +0000 |
---|---|---|
committer | Stephen Frost | 2017-03-09 21:34:25 +0000 |
commit | c08d82f38ebf763b79bd43ae34b7310ee47aaacd (patch) | |
tree | 47963b660339cf68e64c2f836b23c52d9a252be1 /contrib/pgstattuple/pgstatindex.c | |
parent | b54aad8e34bd6299093e965c50f4a23da96d7cc3 (diff) |
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
Diffstat (limited to 'contrib/pgstattuple/pgstatindex.c')
-rw-r--r-- | contrib/pgstattuple/pgstatindex.c | 52 |
1 files changed, 45 insertions, 7 deletions
diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c index 17a53e3bb7d..c69f9ec0937 100644 --- a/contrib/pgstattuple/pgstatindex.c +++ b/contrib/pgstattuple/pgstatindex.c @@ -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)))); +} |