Assign error codes where missing for user-facing failures
authorMichael Paquier <michael@paquier.xyz>
Thu, 4 Jul 2024 00:48:40 +0000 (09:48 +0900)
committerMichael Paquier <michael@paquier.xyz>
Thu, 4 Jul 2024 00:48:40 +0000 (09:48 +0900)
All the errors triggered in the code paths patched here would cause the
backend to issue an internal_error errcode, which is a state that should
be used only for "can't happen" situations.  However, these code paths
are reachable by the regression tests, and could be seen by users in
valid cases.  Some regression tests expect internal errcodes as they
manipulate the backend state to cause corruption (like checksums), or
use elog() because it is more convenient (like injection points), these
have no need to change.

This reduces the number of internal failures triggered in a check-world
by more than half, while providing correct errcodes for these valid
cases.

Reviewed-by: Robert Haas
Discussion: https://postgr.es/m/Zic_GNgos5sMxKoa@paquier.xyz

12 files changed:
contrib/pg_walinspect/pg_walinspect.c
src/backend/access/transam/xlogrecovery.c
src/backend/backup/basebackup.c
src/backend/commands/matview.c
src/backend/commands/tablecmds.c
src/backend/commands/trigger.c
src/backend/commands/user.c
src/backend/libpq/be-secure-openssl.c
src/backend/optimizer/util/appendinfo.c
src/backend/replication/slotfuncs.c
src/backend/utils/adt/pg_locale.c
src/backend/utils/adt/tid.c

index ee2918726d66a7dee0d081da78639c0b9ed31602..9e3e05e398ac09fcde441843c70b79b08a8427df 100644 (file)
@@ -101,7 +101,8 @@ InitXLogReaderState(XLogRecPtr lsn)
         */
        if (lsn < XLOG_BLCKSZ)
                ereport(ERROR,
-                               (errmsg("could not read WAL at LSN %X/%X",
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("could not read WAL at LSN %X/%X",
                                                LSN_FORMAT_ARGS(lsn))));
 
        private_data = (ReadLocalXLogPageNoWaitPrivate *)
index b45b83317200536f0809f398f5f2a222cf659545..2ed3ea2b45bcbd45aea8dcc9cb578ba4870b8dee 100644 (file)
@@ -1898,7 +1898,8 @@ PerformWalRecovery(void)
                recoveryTarget != RECOVERY_TARGET_UNSET &&
                !reachedRecoveryTarget)
                ereport(FATAL,
-                               (errmsg("recovery ended before configured recovery target was reached")));
+                               (errcode(ERRCODE_CONFIG_FILE_ERROR),
+                                errmsg("recovery ended before configured recovery target was reached")));
 }
 
 /*
index 9a2bf59e84ee4a14ea5dfe24940aa146851084d9..01b35e26bda08532c55c53665bb356d9938c3492 100644 (file)
@@ -2045,12 +2045,14 @@ _tarWriteHeader(bbsink *sink, const char *filename, const char *linktarget,
                                break;
                        case TAR_NAME_TOO_LONG:
                                ereport(ERROR,
-                                               (errmsg("file name too long for tar format: \"%s\"",
+                                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                                errmsg("file name too long for tar format: \"%s\"",
                                                                filename)));
                                break;
                        case TAR_SYMLINK_TOO_LONG:
                                ereport(ERROR,
-                                               (errmsg("symbolic link target too long for tar format: "
+                                               (errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+                                                errmsg("symbolic link target too long for tar format: "
                                                                "file name \"%s\", target \"%s\"",
                                                                filename, linktarget)));
                                break;
index 6d09b755564e32bbe81bf1c3c3a59e29e4709a17..ea05d4b224fa857692a7547efeb212351179ae02 100644 (file)
@@ -803,7 +803,9 @@ refresh_by_match_merge(Oid matviewOid, Oid tempOid, Oid relowner,
         * That's a pretty silly thing to do.)
         */
        if (!foundUniqueIndex)
-               elog(ERROR, "could not find suitable unique index on materialized view");
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("could not find suitable unique index on materialized view"));
 
        appendStringInfoString(&querybuf,
                                                   " AND newdata.* OPERATOR(pg_catalog.*=) mv.*) "
index 9e9dc5c2c13ad37c5c4c40b4abe64e1a62af011f..dbfe0d6b1c1a4f818fc7c064dc9b62080604d929 100644 (file)
@@ -11363,7 +11363,8 @@ ATExecAlterConstraint(Relation rel, AlterTableCmd *cmd, bool recurse,
                }
 
                ereport(ERROR,
-                               (errmsg("cannot alter constraint \"%s\" on relation \"%s\"",
+                               (errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+                                errmsg("cannot alter constraint \"%s\" on relation \"%s\"",
                                                cmdcon->conname, RelationGetRelationName(rel)),
                                 ancestorname && ancestortable ?
                                 errdetail("Constraint \"%s\" is derived from constraint \"%s\" of relation \"%s\".",
index 58b7fc5bbd511408f06b3d81dd2a77fdfc5636ac..170360edda8cc06a685060b448f9e278ecdd4546 100644 (file)
@@ -1519,6 +1519,7 @@ renametrig(RenameStmt *stmt)
                 */
                if (OidIsValid(trigform->tgparentid))
                        ereport(ERROR,
+                                       errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                        errmsg("cannot rename trigger \"%s\" on table \"%s\"",
                                                   stmt->subname, RelationGetRelationName(targetrel)),
                                        errhint("Rename the trigger on the partitioned table \"%s\" instead.",
index 104b66e4b430f3c48a535277ea042f4f0814810c..e7ade898a4782983016efe51b894d22e84d6ecb8 100644 (file)
@@ -1730,6 +1730,7 @@ AddRoleMems(Oid currentUserId, const char *rolename, Oid roleid,
                 */
                if (memberid == ROLE_PG_DATABASE_OWNER)
                        ereport(ERROR,
+                                       errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                        errmsg("role \"%s\" cannot be a member of any role",
                                                   get_rolespec_name(memberRole)));
 
@@ -2121,6 +2122,7 @@ check_role_membership_authorization(Oid currentUserId, Oid roleid,
         */
        if (is_grant && roleid == ROLE_PG_DATABASE_OWNER)
                ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
                                errmsg("role \"%s\" cannot have explicit members",
                                           GetUserNameFromId(roleid, false)));
 
index 39b1a66236b63c021a72c338d1b6a7c4a1a3a00a..387f30cdaa89836e5718ade3b4df1447338d37fd 100644 (file)
@@ -250,7 +250,8 @@ be_tls_init(bool isServerStart)
                if (ssl_ver_min > ssl_ver_max)
                {
                        ereport(isServerStart ? FATAL : LOG,
-                                       (errmsg("could not set SSL protocol version range"),
+                                       (errcode(ERRCODE_CONFIG_FILE_ERROR),
+                                        errmsg("could not set SSL protocol version range"),
                                         errdetail("\"%s\" cannot be higher than \"%s\"",
                                                           "ssl_min_protocol_version",
                                                           "ssl_max_protocol_version")));
index 6ba4eba224a5834ccfb87b601fc5d7f6711dd890..49897226371dcad29d715f6049e66784563f3c27 100644 (file)
@@ -160,11 +160,15 @@ make_inh_translation_list(Relation oldrelation, Relation newrelation,
 
                /* Found it, check type and collation match */
                if (atttypid != att->atttypid || atttypmod != att->atttypmod)
-                       elog(ERROR, "attribute \"%s\" of relation \"%s\" does not match parent's type",
-                                attname, RelationGetRelationName(newrelation));
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
+                                        errmsg("attribute \"%s\" of relation \"%s\" does not match parent's type",
+                                                       attname, RelationGetRelationName(newrelation))));
                if (attcollation != att->attcollation)
-                       elog(ERROR, "attribute \"%s\" of relation \"%s\" does not match parent's collation",
-                                attname, RelationGetRelationName(newrelation));
+                       ereport(ERROR,
+                                       (errcode(ERRCODE_INVALID_COLUMN_DEFINITION),
+                                        errmsg("attribute \"%s\" of relation \"%s\" does not match parent's collation",
+                                                       attname, RelationGetRelationName(newrelation))));
 
                vars = lappend(vars, makeVar(newvarno,
                                                                         (AttrNumber) (new_attno + 1),
index dd6c1d5a7e3537d987e59647cdb4d23fc8271b18..38595b3a47230554e9ca08f70c30cdafc58a9fcd 100644 (file)
@@ -523,7 +523,8 @@ pg_replication_slot_advance(PG_FUNCTION_ARGS)
 
        if (XLogRecPtrIsInvalid(moveto))
                ereport(ERROR,
-                               (errmsg("invalid target WAL LSN")));
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("invalid target WAL LSN")));
 
        /* Build a tuple descriptor for our result type */
        if (get_call_result_type(fcinfo, NULL, &tupdesc) != TYPEFUNC_COMPOSITE)
index 7e5bb2b703a72aafeee68b56bdb47e29cac4a03b..2673bafe60af24eee34c44ccb8f8d4b30cd0c7f1 100644 (file)
@@ -1481,7 +1481,8 @@ make_icu_collator(const char *iculocstr,
                                                                  UCOL_DEFAULT, UCOL_DEFAULT_STRENGTH, NULL, &status);
                if (U_FAILURE(status))
                        ereport(ERROR,
-                                       (errmsg("could not open collator for locale \"%s\" with rules \"%s\": %s",
+                                       (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                        errmsg("could not open collator for locale \"%s\" with rules \"%s\": %s",
                                                        iculocstr, icurules, u_errorName(status))));
        }
 
@@ -2609,7 +2610,8 @@ pg_ucol_open(const char *loc_str)
                if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
                {
                        ereport(ERROR,
-                                       (errmsg("could not get language from locale \"%s\": %s",
+                                       (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                        errmsg("could not get language from locale \"%s\": %s",
                                                        loc_str, u_errorName(status))));
                }
 
@@ -2630,7 +2632,8 @@ pg_ucol_open(const char *loc_str)
        if (U_FAILURE(status))
                ereport(ERROR,
                /* use original string for error report */
-                               (errmsg("could not open collator for locale \"%s\": %s",
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("could not open collator for locale \"%s\": %s",
                                                orig_str, u_errorName(status))));
 
        if (U_ICU_VERSION_MAJOR_NUM < 54)
@@ -2646,7 +2649,8 @@ pg_ucol_open(const char *loc_str)
                {
                        ucol_close(collator);
                        ereport(ERROR,
-                                       (errmsg("could not open collator for locale \"%s\": %s",
+                                       (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                        errmsg("could not open collator for locale \"%s\": %s",
                                                        orig_str, u_errorName(status))));
                }
        }
@@ -2957,7 +2961,8 @@ icu_language_tag(const char *loc_str, int elevel)
 
                if (elevel > 0)
                        ereport(elevel,
-                                       (errmsg("could not convert locale name \"%s\" to language tag: %s",
+                                       (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                        errmsg("could not convert locale name \"%s\" to language tag: %s",
                                                        loc_str, u_errorName(status))));
                return NULL;
        }
@@ -2998,7 +3003,8 @@ icu_validate_locale(const char *loc_str)
        if (U_FAILURE(status) || status == U_STRING_NOT_TERMINATED_WARNING)
        {
                ereport(elevel,
-                               (errmsg("could not get language from ICU locale \"%s\": %s",
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("could not get language from ICU locale \"%s\": %s",
                                                loc_str, u_errorName(status)),
                                 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
                                                 "icu_validation_level", "disabled")));
@@ -3027,7 +3033,8 @@ icu_validate_locale(const char *loc_str)
 
        if (!found)
                ereport(elevel,
-                               (errmsg("ICU locale \"%s\" has unknown language \"%s\"",
+                               (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+                                errmsg("ICU locale \"%s\" has unknown language \"%s\"",
                                                loc_str, lang),
                                 errhint("To disable ICU locale validation, set the parameter \"%s\" to \"%s\".",
                                                 "icu_validation_level", "disabled")));
index 8cff1e7a12e8013fd1a0214abc71ab1abb11e7c6..dd46001a4eba55984e6956771f6b90d5a6702514 100644 (file)
@@ -312,9 +312,11 @@ currtid_internal(Relation rel, ItemPointer tid)
                return currtid_for_view(rel, tid);
 
        if (!RELKIND_HAS_STORAGE(rel->rd_rel->relkind))
-               elog(ERROR, "cannot look at latest visible tid for relation \"%s.%s\"",
-                        get_namespace_name(RelationGetNamespace(rel)),
-                        RelationGetRelationName(rel));
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("cannot look at latest visible tid for relation \"%s.%s\"",
+                                          get_namespace_name(RelationGetNamespace(rel)),
+                                          RelationGetRelationName(rel)));
 
        ItemPointerCopy(tid, result);
 
@@ -349,16 +351,22 @@ currtid_for_view(Relation viewrel, ItemPointer tid)
                if (strcmp(NameStr(attr->attname), "ctid") == 0)
                {
                        if (attr->atttypid != TIDOID)
-                               elog(ERROR, "ctid isn't of type TID");
+                               ereport(ERROR,
+                                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                               errmsg("ctid isn't of type TID"));
                        tididx = i;
                        break;
                }
        }
        if (tididx < 0)
-               elog(ERROR, "currtid cannot handle views with no CTID");
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("currtid cannot handle views with no CTID"));
        rulelock = viewrel->rd_rules;
        if (!rulelock)
-               elog(ERROR, "the view has no rules");
+               ereport(ERROR,
+                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                               errmsg("the view has no rules"));
        for (i = 0; i < rulelock->numLocks; i++)
        {
                rewrite = rulelock->rules[i];
@@ -368,7 +376,9 @@ currtid_for_view(Relation viewrel, ItemPointer tid)
                        TargetEntry *tle;
 
                        if (list_length(rewrite->actions) != 1)
-                               elog(ERROR, "only one select rule is allowed in views");
+                               ereport(ERROR,
+                                               errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
+                                               errmsg("only one select rule is allowed in views"));
                        query = (Query *) linitial(rewrite->actions);
                        tle = get_tle_by_resno(query->targetList, tididx + 1);
                        if (tle && tle->expr && IsA(tle->expr, Var))