diff options
| author | Peter Eisentraut | 2021-07-08 07:38:52 +0000 |
|---|---|---|
| committer | Peter Eisentraut | 2021-07-08 07:44:51 +0000 |
| commit | 2ed532ee8c474e9767e76e1f3251cc3a0224358c (patch) | |
| tree | 3365ea8086789ae6b1b0ddf3a3edd16459075a7d /contrib/pgstattuple | |
| parent | b9734c13f168ef0d487aa122e486ca9b6dd6aa59 (diff) | |
Improve error messages about mismatching relkind
Most error messages about a relkind that was not supported or
appropriate for the command was of the pattern
"relation \"%s\" is not a table, foreign table, or materialized view"
This style can become verbose and tedious to maintain. Moreover, it's
not very helpful: If I'm trying to create a comment on a TOAST table,
which is not supported, then the information that I could have created
a comment on a materialized view is pointless.
Instead, write the primary error message shorter and saying more
directly that what was attempted is not possible. Then, in the detail
message, explain that the operation is not supported for the relkind
the object was. To simplify that, add a new function
errdetail_relkind_not_supported() that does this.
In passing, make use of RELKIND_HAS_STORAGE() where appropriate,
instead of listing out the relkinds individually.
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Reviewed-by: Alvaro Herrera <alvherre@alvh.no-ip.org>
Discussion: https://www.postgresql.org/message-id/flat/dc35a398-37d0-75ce-07ea-1dd71d98f8ec@2ndquadrant.com
Diffstat (limited to 'contrib/pgstattuple')
| -rw-r--r-- | contrib/pgstattuple/expected/pgstattuple.out | 30 | ||||
| -rw-r--r-- | contrib/pgstattuple/pgstatapprox.c | 5 | ||||
| -rw-r--r-- | contrib/pgstattuple/pgstatindex.c | 75 | ||||
| -rw-r--r-- | contrib/pgstattuple/pgstattuple.c | 31 |
4 files changed, 52 insertions, 89 deletions
diff --git a/contrib/pgstattuple/expected/pgstattuple.out b/contrib/pgstattuple/expected/pgstattuple.out index 40f7825ddb..e4ac86f9e3 100644 --- a/contrib/pgstattuple/expected/pgstattuple.out +++ b/contrib/pgstattuple/expected/pgstattuple.out @@ -155,13 +155,17 @@ create table test_partitioned (a int) partition by range (a); create index test_partitioned_index on test_partitioned(a); -- these should all fail select pgstattuple('test_partitioned'); -ERROR: "test_partitioned" (partitioned table) is not supported +ERROR: cannot get tuple-level statistics for relation "test_partitioned" +DETAIL: This operation is not supported for partitioned tables. select pgstattuple('test_partitioned_index'); -ERROR: "test_partitioned_index" (partitioned index) is not supported +ERROR: cannot get tuple-level statistics for relation "test_partitioned_index" +DETAIL: This operation is not supported for partitioned indexes. select pgstattuple_approx('test_partitioned'); -ERROR: "test_partitioned" is not a table, materialized view, or TOAST table +ERROR: relation "test_partitioned" is of wrong relation kind +DETAIL: This operation is not supported for partitioned tables. select pg_relpages('test_partitioned'); -ERROR: "test_partitioned" is not a table, index, materialized view, sequence, or TOAST table +ERROR: cannot get page count of relation "test_partitioned" +DETAIL: This operation is not supported for partitioned tables. select pgstatindex('test_partitioned'); ERROR: relation "test_partitioned" is not a btree index select pgstatginindex('test_partitioned'); @@ -171,11 +175,14 @@ ERROR: "test_partitioned" is not an index create view test_view as select 1; -- these should all fail select pgstattuple('test_view'); -ERROR: "test_view" (view) is not supported +ERROR: cannot get tuple-level statistics for relation "test_view" +DETAIL: This operation is not supported for views. select pgstattuple_approx('test_view'); -ERROR: "test_view" is not a table, materialized view, or TOAST table +ERROR: relation "test_view" is of wrong relation kind +DETAIL: This operation is not supported for views. select pg_relpages('test_view'); -ERROR: "test_view" is not a table, index, materialized view, sequence, or TOAST table +ERROR: cannot get page count of relation "test_view" +DETAIL: This operation is not supported for views. select pgstatindex('test_view'); ERROR: relation "test_view" is not a btree index select pgstatginindex('test_view'); @@ -187,11 +194,14 @@ create server dummy_server foreign data wrapper dummy; create foreign table test_foreign_table () server dummy_server; -- these should all fail select pgstattuple('test_foreign_table'); -ERROR: "test_foreign_table" (foreign table) is not supported +ERROR: cannot get tuple-level statistics for relation "test_foreign_table" +DETAIL: This operation is not supported for foreign tables. select pgstattuple_approx('test_foreign_table'); -ERROR: "test_foreign_table" is not a table, materialized view, or TOAST table +ERROR: relation "test_foreign_table" is of wrong relation kind +DETAIL: This operation is not supported for foreign tables. select pg_relpages('test_foreign_table'); -ERROR: "test_foreign_table" is not a table, index, materialized view, sequence, or TOAST table +ERROR: cannot get page count of relation "test_foreign_table" +DETAIL: This operation is not supported for foreign tables. select pgstatindex('test_foreign_table'); ERROR: relation "test_foreign_table" is not a btree index select pgstatginindex('test_foreign_table'); diff --git a/contrib/pgstattuple/pgstatapprox.c b/contrib/pgstattuple/pgstatapprox.c index 1fe193bb25..3b836f370e 100644 --- a/contrib/pgstattuple/pgstatapprox.c +++ b/contrib/pgstattuple/pgstatapprox.c @@ -289,8 +289,9 @@ pgstattuple_approx_internal(Oid relid, FunctionCallInfo fcinfo) rel->rd_rel->relkind == RELKIND_TOASTVALUE)) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("\"%s\" is not a table, materialized view, or TOAST table", - RelationGetRelationName(rel)))); + errmsg("relation \"%s\" is of wrong relation kind", + RelationGetRelationName(rel)), + errdetail_relkind_not_supported(rel->rd_rel->relkind))); if (rel->rd_rel->relam != HEAP_TABLE_AM_OID) ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), diff --git a/contrib/pgstattuple/pgstatindex.c b/contrib/pgstattuple/pgstatindex.c index 5368bb30f0..6c4b053dd0 100644 --- a/contrib/pgstattuple/pgstatindex.c +++ b/contrib/pgstattuple/pgstatindex.c @@ -128,8 +128,8 @@ typedef struct HashIndexStat } HashIndexStat; static Datum pgstatindex_impl(Relation rel, FunctionCallInfo fcinfo); +static int64 pg_relpages_impl(Relation rel); static void GetHashPageStats(Page page, HashIndexStat *stats); -static void check_relation_relkind(Relation rel); /* ------------------------------------------------------ * pgstatindex() @@ -383,7 +383,6 @@ Datum pg_relpages(PG_FUNCTION_ARGS) { text *relname = PG_GETARG_TEXT_PP(0); - int64 relpages; Relation rel; RangeVar *relrv; @@ -395,16 +394,7 @@ 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); - - relation_close(rel, AccessShareLock); - - PG_RETURN_INT64(relpages); + PG_RETURN_INT64(pg_relpages_impl(rel)); } /* No need for superuser checks in v1.5, see above */ @@ -412,23 +402,13 @@ Datum pg_relpages_v1_5(PG_FUNCTION_ARGS) { text *relname = PG_GETARG_TEXT_PP(0); - int64 relpages; Relation rel; RangeVar *relrv; 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); - - relation_close(rel, AccessShareLock); - - PG_RETURN_INT64(relpages); + PG_RETURN_INT64(pg_relpages_impl(rel)); } /* Must keep superuser() check, see above. */ @@ -436,7 +416,6 @@ Datum pg_relpagesbyid(PG_FUNCTION_ARGS) { Oid relid = PG_GETARG_OID(0); - int64 relpages; Relation rel; if (!superuser()) @@ -446,16 +425,7 @@ 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); - - relation_close(rel, AccessShareLock); - - PG_RETURN_INT64(relpages); + PG_RETURN_INT64(pg_relpages_impl(rel)); } /* No need for superuser checks in v1.5, see above */ @@ -463,13 +433,24 @@ Datum pg_relpagesbyid_v1_5(PG_FUNCTION_ARGS) { Oid relid = PG_GETARG_OID(0); - int64 relpages; Relation rel; rel = relation_open(relid, AccessShareLock); - /* only some relkinds have storage */ - check_relation_relkind(rel); + PG_RETURN_INT64(pg_relpages_impl(rel)); +} + +static int64 +pg_relpages_impl(Relation rel) +{ + int64 relpages; + + if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind)) + ereport(ERROR, + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("cannot get page count of relation \"%s\"", + RelationGetRelationName(rel)), + errdetail_relkind_not_supported(rel->rd_rel->relkind))); /* note: this will work OK on non-local temp tables */ @@ -477,7 +458,7 @@ pg_relpagesbyid_v1_5(PG_FUNCTION_ARGS) relation_close(rel, AccessShareLock); - PG_RETURN_INT64(relpages); + return relpages; } /* ------------------------------------------------------ @@ -754,21 +735,3 @@ 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)))); -} diff --git a/contrib/pgstattuple/pgstattuple.c b/contrib/pgstattuple/pgstattuple.c index 21fdeff8af..f408e6d84d 100644 --- a/contrib/pgstattuple/pgstattuple.c +++ b/contrib/pgstattuple/pgstattuple.c @@ -284,31 +284,20 @@ pgstat_relation(Relation rel, FunctionCallInfo fcinfo) err = "unknown index"; break; } + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("index \"%s\" (%s) is not supported", + RelationGetRelationName(rel), err))); break; - case RELKIND_VIEW: - err = "view"; - break; - case RELKIND_COMPOSITE_TYPE: - err = "composite type"; - break; - case RELKIND_FOREIGN_TABLE: - err = "foreign table"; - break; - case RELKIND_PARTITIONED_TABLE: - err = "partitioned table"; - break; - case RELKIND_PARTITIONED_INDEX: - err = "partitioned index"; - break; + default: - err = "unknown"; - break; + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("cannot get tuple-level statistics for relation \"%s\"", + RelationGetRelationName(rel)), + errdetail_relkind_not_supported(rel->rd_rel->relkind))); } - ereport(ERROR, - (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), - errmsg("\"%s\" (%s) is not supported", - RelationGetRelationName(rel), err))); return 0; /* should not happen */ } |
