From acf9c75ea9eddb8cb023a3c06b988e74820f4b74 Mon Sep 17 00:00:00 2001 From: Ayush Tiwari Date: Fri, 20 Nov 2026 21:49:44 +0530 Subject: [PATCH v3] Fix internal errors reachable from SQL currtid_for_view() ended with an elog() when it could not resolve the view's ctid column to a base relation, which happens for example when the view groups by ctid. Ordinary SQL can reach that, so it should not be reported as XX000 (internal_error). Use ERRCODE_FEATURE_NOT_SUPPORTED to match the other unsupported-view checks in the same function. While looking for other instances of the same problem, a few more user-reachable errors turned out to lack an error code or use an inappropriate one: * varlena.c, formatting.c: unicode_assigned() rejects non-UTF8 server encodings without an error code, while unicode_norm_form_from_string() and str_casefold() use ERRCODE_SYNTAX_ERROR for the same check. Use ERRCODE_FEATURE_NOT_SUPPORTED in all three places. * pg_controldata.c, basebackup.c: report a control file CRC mismatch in pg_control_*() and perform_base_backup() as ERRCODE_DATA_CORRUPTED. * gist.c: an inner tuple left invalid by a pre-9.1 crash indicates a corrupt index, so use ERRCODE_INDEX_CORRUPTED, matching gistutil.c. * collationcmds.c: refusing ALTER COLLATION ... REFRESH VERSION on the default collation is ERRCODE_WRONG_OBJECT_TYPE. Do not backpatch, since this changes the SQLSTATE of existing errors. Bug: #19631 Reported-by: Zheng Wang Reported-by: Yanjie Zhao Reported-by: Yiyang Liu Author: Ayush Tiwari Reviewed-by: Pierre Forstmann Reviewed-by: Fujii Masao Discussion: https://postgr.es/m/19631-b443dd6cd8d4e40b@postgresql.org --- Changes in v3: * Use ERRCODE_FEATURE_NOT_SUPPORTED for the encoding checks in unicode_norm_form_from_string() and str_casefold() too. * Add ERRCODE_DATA_CORRUPTED to the control file CRC check in perform_base_backup(). * Drop the currtid_for_view() regression test added in v2. * Rebase on current master. src/backend/access/gist/gist.c | 3 ++- src/backend/backup/basebackup.c | 3 ++- src/backend/commands/collationcmds.c | 3 ++- src/backend/utils/adt/formatting.c | 2 +- src/backend/utils/adt/tid.c | 4 +++- src/backend/utils/adt/varlena.c | 5 +++-- src/backend/utils/misc/pg_controldata.c | 12 ++++++++---- 7 files changed, 21 insertions(+), 11 deletions(-) diff --git a/src/backend/access/gist/gist.c b/src/backend/access/gist/gist.c index 8565e225be7..44597793433 100644 --- a/src/backend/access/gist/gist.c +++ b/src/backend/access/gist/gist.c @@ -762,7 +762,8 @@ gistdoinsert(Relation r, IndexTuple itup, Size freespace, */ if (GistTupleIsInvalid(idxtuple)) ereport(ERROR, - (errmsg("index \"%s\" contains an inner tuple marked as invalid", + (errcode(ERRCODE_INDEX_CORRUPTED), + errmsg("index \"%s\" contains an inner tuple marked as invalid", RelationGetRelationName(r)), errdetail("This is caused by an incomplete page split at crash recovery before upgrading to PostgreSQL 9.1."), errhint("Please REINDEX it."))); diff --git a/src/backend/backup/basebackup.c b/src/backend/backup/basebackup.c index f8c3520e5b8..66e28ab3f1a 100644 --- a/src/backend/backup/basebackup.c +++ b/src/backend/backup/basebackup.c @@ -376,7 +376,8 @@ perform_base_backup(basebackup_options *opt, bbsink *sink, if (!crc_ok) ereport(ERROR, - (errmsg("calculated CRC checksum does not match value stored in file"))); + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("calculated CRC checksum does not match value stored in file"))); /* Send the full file, zero-padded as it is on disk */ controlFile = palloc0(PG_CONTROL_FILE_SIZE); diff --git a/src/backend/commands/collationcmds.c b/src/backend/commands/collationcmds.c index cfa0e4610d9..92faa60a750 100644 --- a/src/backend/commands/collationcmds.c +++ b/src/backend/commands/collationcmds.c @@ -443,7 +443,8 @@ AlterCollation(AlterCollationStmt *stmt) if (collOid == DEFAULT_COLLATION_OID) ereport(ERROR, - (errmsg("cannot refresh version of default collation"), + (errcode(ERRCODE_WRONG_OBJECT_TYPE), + errmsg("cannot refresh version of default collation"), /* translator: %s is an SQL command */ errhint("Use %s instead.", "ALTER DATABASE ... REFRESH COLLATION VERSION"))); diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 6d1d7664f1e..fdd97ec3992 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -1836,7 +1836,7 @@ str_casefold(const char *buff, size_t nbytes, Oid collid) if (GetDatabaseEncoding() != PG_UTF8) ereport(ERROR, - (errcode(ERRCODE_SYNTAX_ERROR), + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Unicode case folding can only be performed if server encoding is UTF8"))); mylocale = pg_newlocale_from_collation(collid); diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c index f730b2f07c2..1fc2f75cc98 100644 --- a/src/backend/utils/adt/tid.c +++ b/src/backend/utils/adt/tid.c @@ -439,7 +439,9 @@ currtid_for_view(Relation viewrel, const ItemPointerData *tid) break; } } - elog(ERROR, "currtid cannot handle this view"); + ereport(ERROR, + errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("currtid cannot handle this view")); return NULL; } diff --git a/src/backend/utils/adt/varlena.c b/src/backend/utils/adt/varlena.c index 6c95fcb24b7..74d863e3ff2 100644 --- a/src/backend/utils/adt/varlena.c +++ b/src/backend/utils/adt/varlena.c @@ -5428,7 +5428,7 @@ unicode_norm_form_from_string(const char *formstr) */ if (GetDatabaseEncoding() != PG_UTF8) ereport(ERROR, - (errcode(ERRCODE_SYNTAX_ERROR), + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("Unicode normalization can only be performed if server encoding is UTF8"))); if (pg_strcasecmp(formstr, "NFC") == 0) @@ -5488,7 +5488,8 @@ unicode_assigned(PG_FUNCTION_ARGS) if (GetDatabaseEncoding() != PG_UTF8) ereport(ERROR, - (errmsg("Unicode categorization can only be performed if server encoding is UTF8"))); + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("Unicode categorization can only be performed if server encoding is UTF8"))); /* convert to char32_t */ size = pg_mbstrlen_with_len(VARDATA_ANY(input), VARSIZE_ANY_EXHDR(input)); diff --git a/src/backend/utils/misc/pg_controldata.c b/src/backend/utils/misc/pg_controldata.c index 1f47302fe2a..4eac0b33cd1 100644 --- a/src/backend/utils/misc/pg_controldata.c +++ b/src/backend/utils/misc/pg_controldata.c @@ -47,7 +47,8 @@ pg_control_system(PG_FUNCTION_ARGS) LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, - (errmsg("calculated CRC checksum does not match value stored in file"))); + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("calculated CRC checksum does not match value stored in file"))); values[0] = Int32GetDatum(ControlFile->pg_control_version); nulls[0] = false; @@ -87,7 +88,8 @@ pg_control_checkpoint(PG_FUNCTION_ARGS) LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, - (errmsg("calculated CRC checksum does not match value stored in file"))); + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("calculated CRC checksum does not match value stored in file"))); /* * Calculate name of the WAL file containing the latest checkpoint's REDO @@ -184,7 +186,8 @@ pg_control_recovery(PG_FUNCTION_ARGS) LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, - (errmsg("calculated CRC checksum does not match value stored in file"))); + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("calculated CRC checksum does not match value stored in file"))); values[0] = LSNGetDatum(ControlFile->minRecoveryPoint); nulls[0] = false; @@ -225,7 +228,8 @@ pg_control_init(PG_FUNCTION_ARGS) LWLockRelease(ControlFileLock); if (!crc_ok) ereport(ERROR, - (errmsg("calculated CRC checksum does not match value stored in file"))); + (errcode(ERRCODE_DATA_CORRUPTED), + errmsg("calculated CRC checksum does not match value stored in file"))); values[0] = Int32GetDatum(ControlFile->maxAlign); nulls[0] = false; base-commit: 67c10344ddbd1073f1b7aafb09d1b196a48735a1 -- 2.34.1