【发布时间】:2022-01-21 16:09:00
【问题描述】:
我在 PostgreSQL 12.9 中有一些表被声明为类似
-- This table is written in old style
create table old_style_table_1 (
id bigserial not null primary key,
...
);
-- This table uses new feature
create table new_style_table_2 (
id bigint generated by default as identity,
...
);
第二个表似乎是使用第 10 版中引入的identity flag 声明的。
时间过去了,我们已经对旧表进行了分区,同时保留了原始序列:
CREATE TABLE partitioned_old_style_table_1 (LIKE old_style_table_1 INCLUDING DEFAULTS) PARTITION BY HASH (user_id);
CREATE TABLE partitioned_new_style_table_2 (LIKE new_style_table_2 INCLUDING DEFAULTS) PARTITION BY HASH (user_id);
id 列的 DDL 似乎是 id bigint default nextval('old_style_table_1_id_seq') not null 和 id bigint default nextval('new_style_table_2_id_seq') not null。
到目前为止一切正常。分区表被证明是一个很大的好处,我们决定通过删除旧表来淘汰它们。
DROP TABLE old_style_table_1, new_style_table_2;
-- [2BP01] ERROR: cannot drop desired object(s) because other objects depend on them
-- Detail: default value for column id of table old_style_table_1 depends on sequence old_style_table_1_id_seq
-- default value for column id of table new_style_table_2 depends on sequence new_style_table_2_id_seq
经过一番思考,我发现序列可能在 postgres 中有所有者,所以我选择更改它们:
ALTER SEQUENCE old_style_table_1_id_seq OWNED BY partitioned_old_style_table_1.id;
DROP TABLE old_style_table_1;
-- Worked out flawlessly
ALTER SEQUENCE new_style_table_2_id_seq OWNED BY partitioned_new_style_table_2.id;
ALTER SEQUENCE new_style_table_2_id_seq OWNED BY NONE;
-- Here's the culprit of the question:
-- [0A000] ERROR: cannot change ownership of identity sequence
因此,很明显,此列将pg_attribute.attidentity 设置为'd' 的事实禁止我:
• 更改列的默认值:
ALTER TABLE new_style_table_2 ALTER COLUMN id SET DEFAULT 0;
-- [42601] ERROR: column "id" of relation "new_style_table_2" is an identity column
• 删除默认值:
ALTER TABLE new_style_table_2 ALTER COLUMN id DROP DEFAULT;
-- [42601] ERROR: column "id" of relation "new_style_table_2" is an identity column
-- Hint: Use ALTER TABLE ... ALTER COLUMN ... DROP IDENTITY instead.
• 完全删除标识、列或表(新表已经依赖于序列):
ALTER TABLE new_style_table_2 ALTER COLUMN id DROP IDENTITY IF EXISTS;
-- or
ALTER TABLE new_style_table_2 DROP COLUMN id;
-- or
DROP TABLE new_style_table_2;
-- result in
-- [2BP01] ERROR: cannot drop desired object(s) because other objects depend on them
-- default value for column id of table partitioned_new_style_table_2 depends on sequence new_style_table_2_id_seq
我查看了documentation,它提供了通往SET IDENTITY 或ADD IDENTITY 的方法,但没有办法将其删除或更改为一次性序列而不尝试删除现有序列。
➥ 那么,我怎样才能从列序列对中删除一个标识标志,这样它就不会影响使用该序列的其他表?
UPD:尝试在本地主机上运行UPDATE pg_attribute SET attidentity='' WHERE attrelid=16816;,仍然收到[2BP01] 和[0A000]。 :/
虽然我设法执行了DROP DEFAULT 值位,但这似乎是一个死胡同。
【问题讨论】: