mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
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
This commit is contained in:
@ -170,9 +170,11 @@ rollback;
|
||||
-- check that it fails on an unsupported relkind
|
||||
create view vw as select 1;
|
||||
select heap_force_kill('vw'::regclass, ARRAY['(0, 1)']::tid[]);
|
||||
ERROR: "vw" is not a table, materialized view, or TOAST table
|
||||
ERROR: cannot operate on relation "vw"
|
||||
DETAIL: This operation is not supported for views.
|
||||
select heap_force_freeze('vw'::regclass, ARRAY['(0, 1)']::tid[]);
|
||||
ERROR: "vw" is not a table, materialized view, or TOAST table
|
||||
ERROR: cannot operate on relation "vw"
|
||||
DETAIL: This operation is not supported for views.
|
||||
-- cleanup.
|
||||
drop view vw;
|
||||
drop extension pg_surgery;
|
||||
|
@ -37,7 +37,6 @@ static int32 tidcmp(const void *a, const void *b);
|
||||
static Datum heap_force_common(FunctionCallInfo fcinfo,
|
||||
HeapTupleForceOption heap_force_opt);
|
||||
static void sanity_check_tid_array(ArrayType *ta, int *ntids);
|
||||
static void sanity_check_relation(Relation rel);
|
||||
static BlockNumber find_tids_one_page(ItemPointer tids, int ntids,
|
||||
OffsetNumber *next_start_ptr);
|
||||
|
||||
@ -101,8 +100,28 @@ heap_force_common(FunctionCallInfo fcinfo, HeapTupleForceOption heap_force_opt)
|
||||
|
||||
rel = relation_open(relid, RowExclusiveLock);
|
||||
|
||||
/* Check target relation. */
|
||||
sanity_check_relation(rel);
|
||||
/*
|
||||
* Check target relation.
|
||||
*/
|
||||
if (rel->rd_rel->relkind != RELKIND_RELATION &&
|
||||
rel->rd_rel->relkind != RELKIND_MATVIEW &&
|
||||
rel->rd_rel->relkind != RELKIND_TOASTVALUE)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("cannot operate on relation \"%s\"",
|
||||
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),
|
||||
errmsg("only heap AM is supported")));
|
||||
|
||||
/* Must be owner of the table or superuser. */
|
||||
if (!pg_class_ownercheck(RelationGetRelid(rel), GetUserId()))
|
||||
aclcheck_error(ACLCHECK_NOT_OWNER,
|
||||
get_relkind_objtype(rel->rd_rel->relkind),
|
||||
RelationGetRelationName(rel));
|
||||
|
||||
tids = ((ItemPointer) ARR_DATA_PTR(ta));
|
||||
|
||||
@ -363,35 +382,6 @@ sanity_check_tid_array(ArrayType *ta, int *ntids)
|
||||
*ntids = ArrayGetNItems(ARR_NDIM(ta), ARR_DIMS(ta));
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* sanity_check_relation()
|
||||
*
|
||||
* Perform sanity checks on the given relation.
|
||||
* ------------------------------------------------------------------------
|
||||
*/
|
||||
static void
|
||||
sanity_check_relation(Relation rel)
|
||||
{
|
||||
if (rel->rd_rel->relkind != RELKIND_RELATION &&
|
||||
rel->rd_rel->relkind != RELKIND_MATVIEW &&
|
||||
rel->rd_rel->relkind != RELKIND_TOASTVALUE)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_WRONG_OBJECT_TYPE),
|
||||
errmsg("\"%s\" is not a table, materialized view, or TOAST table",
|
||||
RelationGetRelationName(rel))));
|
||||
|
||||
if (rel->rd_rel->relam != HEAP_TABLE_AM_OID)
|
||||
ereport(ERROR,
|
||||
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
|
||||
errmsg("only heap AM is supported")));
|
||||
|
||||
/* Must be owner of the table or superuser. */
|
||||
if (!pg_class_ownercheck(RelationGetRelid(rel), GetUserId()))
|
||||
aclcheck_error(ACLCHECK_NOT_OWNER,
|
||||
get_relkind_objtype(rel->rd_rel->relkind),
|
||||
RelationGetRelationName(rel));
|
||||
}
|
||||
|
||||
/*-------------------------------------------------------------------------
|
||||
* find_tids_one_page()
|
||||
*
|
||||
|
Reference in New Issue
Block a user