我在 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 版中引入的標識標志宣告的。
時間過去了,我們已經對舊表進行了磁區,同時保留了原始序列:
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);
他們的專欄的 DDLid似乎是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
我查看了檔案,它提供了SET IDENTITYor的方法ADD IDENTITY,但無法洗掉它或更改為一次性序列而不嘗試洗掉現有序列。
? 那么,我怎樣才能從列序列對中洗掉一個標識標志,這樣它就不會影響使用該序列的其他表?
UPD:嘗試UPDATE pg_attribute SET attidentity='' WHERE attrelid=16816;在 localhost 上運行,仍然接收[2BP01]和[0A000]. :/
雖然我設法執行了DROP DEFAULT值位,但這似乎是一個死胡同。
uj5u.com熱心網友回復:
我認為沒有一種安全且受支持的方法可以做到這一點(無需修改目錄)。幸運的是,序列并沒有什么特別的地方會導致洗掉它們成為問題。因此,請稍等片刻,然后:
洗掉使用身份序列的默認值
記錄序列的當前值
放下桌子
創建具有適當
START值的新序列使用新序列設定新的默認值
如果你想要一個標識列,你應該在磁區表上定義它,而不是在其中一個磁區上。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/418784.html
標籤:
