简体   繁体   English

在 GCP 上的表上禁用触发器

[英]disable trigger on table on GCP

I'm doing DB migration and I would need to disable the foreign key constraints on a table in order to migrate peacefully.我正在进行数据库迁移,我需要禁用表上的外键约束才能和平迁移。

From this: How do I temporarily disable triggers in PostgreSQL?从这里: 如何暂时禁用 PostgreSQL 中的触发器?

I've gathered that I would need superuser permissions to run:我收集到我需要超级用户权限才能运行:

SET session_replication_role = replica;

and

SET session_replication_role = DEFAULT;

However;然而; on google cloud platform it is impossible to get those privileges as detailed in the docs:在谷歌云平台上,不可能获得文档中详述的那些特权:

The postgres user is part of the cloudsqlsuperuser role and has the following attributes (privileges): CREATEROLE, CREATEDB, and LOGIN. postgres 用户是 cloudsqlsuperuser 角色的一部分,具有以下属性(权限):CREATEROLE、CREATEDB 和 LOGIN。 It does not have the SUPERUSER or REPLICATION attributes它没有 SUPERUSER 或 REPLICATION 属性

and to this StackOverflow question: upgrade user to superuser in postgres google cloud以及这个 StackOverflow 问题: upgrade user to superuser in postgres google cloud

I also tried to disable triggers via:我还尝试通过以下方式禁用触发器:

alter table orders.orders disable trigger all;

However;然而; for this it seems to me, that I also need su rights: ERROR: permission denied: "RI_ConstraintTrigger_a_25017" is a system trigger为此,在我看来,我还需要 su 权限: ERROR: permission denied: "RI_ConstraintTrigger_a_25017" is a system trigger

So then, what is the solution, can I even disable triggers // foreign key constraints on this particular table or not?那么,解决方案是什么,我什至可以禁用触发器 // 这个特定表上的外键约束吗?

Actually you don't need superuser to issue实际上你不需要超级用户来发布

SET session_replication_role = replica;
SET session_replication_role = default;

you just need REPLICATION ( I just tested it )你只需要复制(我刚刚测试过)

ALTER ROLE <your user> REPLICATION;

Like you have read from the documentation , these commands require superuser privileges, which are not available to users in CloudSQL yet.就像您从文档中看到的一样,这些命令需要superuser权限,而 CloudSQL 中的用户尚不具备这些权限。

Because Cloud SQL for PostgreSQL is a managed service, it restricts access to certain system procedures and tables that require advanced privileges.由于 PostgreSQL 的 Cloud SQL 是一项托管服务,因此它限制了对某些需要高级权限的系统过程和表的访问。

There is actually an open feature request focusing on this feature.实际上有一个开放的功能请求专注于此功能。 It does not seem to have an ETA yet as per the available info.根据可用信息,它似乎还没有 ETA。 Regardless, it appears that there are replication services which do not require superuser , as this thread says.无论如何,正如该线程所说,似乎有些复制服务不需要superuser Otherwise, GCP also offers the Data Migration Service , which, according to the documentation, supports migrating foreign key constraints for PostgreSQL instances.否则,GCP 还提供 数据迁移服务,根据文档,该服务支持迁移 PostgreSQL 实例的外键约束。

There is no way to get superuser privilege but you can gain some of the superuser privileges by setting the right database flags.没有办法获得超级用户权限,但您可以通过设置正确的数据库标志来获得一些超级用户权限。

Quoting from the documentation: https://cloud.google.com/sql/docs/postgres/flags#session_replication_role引用文档: https://cloud.google.com/sql/docs/postgres/flags#session_replication_role

session_replication_role PostgreSQL has a session_replication_role flag which is designed to be used for logical replication and which allows disabling constraint triggers in individual sessions. session_replication_role PostgreSQL 有一个 session_replication_role 标志,该标志旨在用于逻辑复制,并允许在单个会话中禁用约束触发器。

Sometimes this flag can also be used for some maintenance operations to circumvent constraint (most often Foreign Key) checks.有时这个标志也可以用于一些维护操作来规避约束(最常见的是外键)检查。

This flag can be set in a session by any user which has the REPLICATION property set.任何设置了 REPLICATION 属性的用户都可以在 session 中设置此标志。 The REPLICATION property for any user can be set by cloudsqlsuperuser when one of flags cloudsql.enable_pglogical or cloudsql.logical_decoding is set for the instance.当为实例设置标志 cloudsql.enable_pglogical 或 cloudsql.logical_decoding 之一时,cloudsqlsuperuser 可以设置任何用户的 REPLICATION 属性。

How you can set a flag in your database is very easy and explained here: https://cloud.google.com/sql/docs/postgres/flags#set_a_database_flag for all cases (console, gcloud, terraform and etc)如何在数据库中设置标志非常简单,并在此处进行了解释: https://cloud.google.com/sql/docs/postgres/flags#set_a_database_flag适用于所有情况(控制台、gcloud、terraform 等)

What worked for me什么对我有用

I have found the above solution while trying to solve the very same problem.我在尝试解决同样的问题时找到了上述解决方案。 However, as @Paolo Castelletti said,然而,正如@Paolo Castelletti 所说,

ALTER ROLE <your user> REPLICATION;

was actually enough for me to apply实际上足以让我申请

SET session_replication_role = replica;

I wanted to share the other approach as well in case it helps someone我也想分享另一种方法,以防它对某人有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM