diff options
author | Michael Paquier <michael@paquier.xyz> | 2023-06-30 07:49:01 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2023-06-30 07:49:01 +0900 |
commit | 97d89101045fac8cb36f4ef6c08526ea0841a596 (patch) | |
tree | 66182695a153a728cef7735d7119448762a0eb6a /src/backend/commands/cluster.c | |
parent | 5f87a021257f1ea1cacdced0cf49ff1c58ecf5e9 (diff) | |
download | postgresql-97d89101045fac8cb36f4ef6c08526ea0841a596.tar.gz postgresql-97d89101045fac8cb36f4ef6c08526ea0841a596.zip |
Fix pg_depend entry to AMs after ALTER TABLE .. SET ACCESS METHOD
ALTER TABLE .. SET ACCESS METHOD was not registering a dependency to the
new access method with the relation altered in its rewrite phase, making
possible the drop of an access method even if there are relations that
depend on it. During the rewrite, a temporary relation is created to
build the new relation files before swapping the new and old files, and,
while the temporary relation was registering a correct dependency to the
new AM, the old relation did not do that. A dependency on the access
method is added when the relation files are swapped, which is the point
where pg_class is updated.
Materialized views and tables use the same code path, hence both were
impacted.
Backpatch down to 15, where this command has been introduced.
Reported-by: Alexander Lakhin
Reviewed-by: Nathan Bossart, Andres Freund
Discussion: https://postgr.es/m/18000-9145c25b1af475ca@postgresql.org
Backpatch-through: 15
Diffstat (limited to 'src/backend/commands/cluster.c')
-rw-r--r-- | src/backend/commands/cluster.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/src/backend/commands/cluster.c b/src/backend/commands/cluster.c index 3bfabb6d10b..03b24ab90f1 100644 --- a/src/backend/commands/cluster.c +++ b/src/backend/commands/cluster.c @@ -1070,6 +1070,8 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_pg_class, relfilenumber2; RelFileNumber swaptemp; char swptmpchr; + Oid relam1, + relam2; /* We need writable copies of both pg_class tuples. */ relRelation = table_open(RelationRelationId, RowExclusiveLock); @@ -1086,6 +1088,8 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_pg_class, relfilenumber1 = relform1->relfilenode; relfilenumber2 = relform2->relfilenode; + relam1 = relform1->relam; + relam2 = relform2->relam; if (RelFileNumberIsValid(relfilenumber1) && RelFileNumberIsValid(relfilenumber2)) @@ -1258,6 +1262,31 @@ swap_relation_files(Oid r1, Oid r2, bool target_is_pg_class, } /* + * Now that pg_class has been updated with its relevant information for + * the swap, update the dependency of the relations to point to their new + * table AM, if it has changed. + */ + if (relam1 != relam2) + { + if (changeDependencyFor(RelationRelationId, + r1, + AccessMethodRelationId, + relam1, + relam2) != 1) + elog(ERROR, "failed to change access method dependency for relation \"%s.%s\"", + get_namespace_name(get_rel_namespace(r1)), + get_rel_name(r1)); + if (changeDependencyFor(RelationRelationId, + r2, + AccessMethodRelationId, + relam2, + relam1) != 1) + elog(ERROR, "failed to change access method dependency for relation \"%s.%s\"", + get_namespace_name(get_rel_namespace(r2)), + get_rel_name(r2)); + } + + /* * Post alter hook for modified relations. The change to r2 is always * internal, but r1 depends on the invocation context. */ |