Add more sanity checks with callers of changeDependencyFor()
authorMichael Paquier <michael@paquier.xyz>
Mon, 10 Jul 2023 04:08:10 +0000 (13:08 +0900)
committerMichael Paquier <michael@paquier.xyz>
Mon, 10 Jul 2023 04:08:10 +0000 (13:08 +0900)
changeDependencyFor() returns the number of pg_depend entries changed,
or 0 if there is a problem.  The callers of this routine expect only one
dependency to change, but they did not check for the result returned.
The following code paths gain checks:
- Namespace for extensions.
- Namespace for various object types (see AlterObjectNamespace).
- Planner support function for a function.

Some existing error messages related to all that are reworded to be more
consistent with the project style, and the new error messages added
follow the same style.  This change has exposed one bug fixed a bit
earlier with bd5ddbe.

Reviewed-by: Heikki Linnakangas, Akshat Jaimini
Discussion: https://postgr.es/m/ZJzD/rn+UbloKjB7@paquier.xyz

src/backend/commands/alter.c
src/backend/commands/cluster.c
src/backend/commands/extension.c
src/backend/commands/functioncmds.c
src/backend/commands/tablecmds.c
src/backend/commands/typecmds.c

index e95dc31bde3582c10f20ffdadcf162ecb224e2bf..d64929df558e4312b236ff2981039f3d962ea2b3 100644 (file)
@@ -847,9 +847,11 @@ AlterObjectNamespace_internal(Relation rel, Oid objid, Oid nspOid)
    pfree(nulls);
    pfree(replaces);
 
-   /* update dependencies to point to the new schema */
-   changeDependencyFor(classId, objid,
-                       NamespaceRelationId, oldNspOid, nspOid);
+   /* update dependency to point to the new schema */
+   if (changeDependencyFor(classId, objid,
+                           NamespaceRelationId, oldNspOid, nspOid) != 1)
+       elog(ERROR, "could not change schema dependency for object %u",
+            objid);
 
    InvokeObjectPostAlterHook(classId, objid, 0);
 
index 92c465c05b4775e64f7960b44c6b394387627f92..a3bef6ac34f61436fbf6beb8fb28fd5ba7655c69 100644 (file)
@@ -1271,7 +1271,7 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_pg_class,
                                AccessMethodRelationId,
                                relam1,
                                relam2) != 1)
-           elog(ERROR, "failed to change access method dependency for relation \"%s.%s\"",
+           elog(ERROR, "could not change access method dependency for relation \"%s.%s\"",
                 get_namespace_name(get_rel_namespace(r1)),
                 get_rel_name(r1));
        if (changeDependencyFor(RelationRelationId,
@@ -1279,7 +1279,7 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_pg_class,
                                AccessMethodRelationId,
                                relam2,
                                relam1) != 1)
-           elog(ERROR, "failed to change access method dependency for relation \"%s.%s\"",
+           elog(ERROR, "could not change access method dependency for relation \"%s.%s\"",
                 get_namespace_name(get_rel_namespace(r2)),
                 get_rel_name(r2));
    }
index 39db7584f3302538609b7246e83f382137ef7af9..9a2ee1c600825f2f187f80a95a3c226364c124fc 100644 (file)
@@ -2944,9 +2944,11 @@ AlterExtensionNamespace(const char *extensionName, const char *newschema, Oid *o
 
    table_close(extRel, RowExclusiveLock);
 
-   /* update dependencies to point to the new schema */
-   changeDependencyFor(ExtensionRelationId, extensionOid,
-                       NamespaceRelationId, oldNspOid, nspOid);
+   /* update dependency to point to the new schema */
+   if (changeDependencyFor(ExtensionRelationId, extensionOid,
+                           NamespaceRelationId, oldNspOid, nspOid) != 1)
+       elog(ERROR, "could not change schema dependency for extension %s",
+            NameStr(extForm->extname));
 
    InvokeObjectPostAlterHook(ExtensionRelationId, extensionOid, 0);
 
index 49c7864c7cfaa1e734f609404d1f78b84c4757e4..7ba6a86ebe6249ce3cf974403cf3871e4cdb3b6e 100644 (file)
@@ -1450,9 +1450,13 @@ AlterFunction(ParseState *pstate, AlterFunctionStmt *stmt)
 
        /* Add or replace dependency on support function */
        if (OidIsValid(procForm->prosupport))
-           changeDependencyFor(ProcedureRelationId, funcOid,
-                               ProcedureRelationId, procForm->prosupport,
-                               newsupport);
+       {
+           if (changeDependencyFor(ProcedureRelationId, funcOid,
+                                   ProcedureRelationId, procForm->prosupport,
+                                   newsupport) != 1)
+               elog(ERROR, "could not change support dependency for function %s",
+                    get_func_name(funcOid));
+       }
        else
        {
            ObjectAddress referenced;
index 5316f5830810043c795dde19b15675e8d683c8aa..53ad3650935c93ca912c38013ec462fafa295388 100644 (file)
@@ -16608,7 +16608,7 @@ AlterRelationNamespaceInternal(Relation classRel, Oid relOid,
                                NamespaceRelationId,
                                oldNspOid,
                                newNspOid) != 1)
-           elog(ERROR, "failed to change schema dependency for relation \"%s\"",
+           elog(ERROR, "could not change schema dependency for relation \"%s\"",
                 NameStr(classForm->relname));
    }
    if (!already_done)
index 216482095d2b58c5c4769cf896e7381151cf207d..5e97606793d2004df5948cab441383a62ed80d8d 100644 (file)
@@ -4059,7 +4059,7 @@ AlterTypeNamespaceInternal(Oid typeOid, Oid nspOid,
        !isImplicitArray)
        if (changeDependencyFor(TypeRelationId, typeOid,
                                NamespaceRelationId, oldNspOid, nspOid) != 1)
-           elog(ERROR, "failed to change schema dependency for type %s",
+           elog(ERROR, "could not change schema dependency for type \"%s\"",
                 format_type_be(typeOid));
 
    InvokeObjectPostAlterHook(TypeRelationId, typeOid, 0);