分区表上的唯一约束名称
Unique constraints name on partitioned tables
我正在为多租户应用程序使用分区 tables,我必须检查唯一约束名称以 return suitable 错误。
如何检查每个分区中的唯一约束名称 table?
如何生成分区 table 中的唯一约束名称?
谢谢!
test@0:test> \d+ issuefield
...
15 Indexes:
16 "issuefield_pk_cp" PRIMARY KEY, btree (id, org_id)
17 "issuefield_key_uq" UNIQUE CONSTRAINT, btree (key, org_id)
18 "issuefield_lower_name_uq" UNIQUE, btree (lower(name::text), org_id) WITH (fillfactor='90')
19 "issuefield_name_uq" UNIQUE CONSTRAINT, btree (name, org_id)
20 Check constraints:
21 "key_check" CHECK (key::text ~ '^[a-zA-Z][a-zA-Z0-9_]*$'::text AND (lower(key::text) <> ALL (ARRAY['organization'::text, 'space'::text, 'status'::text, 'statusCategory'::text, 'fun
ction'::text])))
22 "name_check" CHECK (COALESCE(btrim(name::text), ''::text) <> ''::text)
23 Partition key: LIST (org_id)
24 Partitions: public.issuefield_abc FOR VALUES IN ('eaed29a9-0813-4948-9e47-40da6db51d15')
25
Time: 0.034s
test@0:test> \d+ issuefield_abc
STDIN
...
15 Indexes:
16 "issuefield_abc_pkey" PRIMARY KEY, btree (id, org_id)
17 "issuefield_abc_key_org_id_key" UNIQUE CONSTRAINT, btree (key, org_id)
18 "issuefield_abc_lower_org_id_idx" UNIQUE, btree (lower(name::text), org_id) WITH (fillfactor='90')
19 "issuefield_abc_name_org_id_key" UNIQUE CONSTRAINT, btree (name, org_id)
20 Check constraints:
21 "key_check" CHECK (key::text ~ '^[a-zA-Z][a-zA-Z0-9_]*$'::text AND (lower(key::text) <> ALL (ARRAY['organization'::text, 'space'::text, 'status'::text, 'statusCategory'::text, 'fun
ction'::text])))
22 "name_check" CHECK (COALESCE(btrim(name::text), ''::text) <> ''::text)
....
30 Partition of: public.issuefield FOR VALUES IN ('eaed29a9-0813-4948-9e47-40da6db51d15')
31 Partition constraint: ((org_id IS NOT NULL) AND (org_id = 'eaed29a9-0813-4948-9e47-40da6db51d15'::uuid))
32 Has OIDs: no
如您所见,检查约束的名称对于所有分区都是相同的。这不适用于主键和唯一键,因为它们的名称与索引名称相同,并且每个模式都是唯一的。
PostgreSQL 只是为分区上的索引和约束选择一个名称,就像在不指定名称的情况下创建约束或索引时所做的那样。要对此进行更多控制,请执行以下操作:
先创建没有PARTITION OF
的分区:
CREATE TABLE issuefield_abc (LIKE issuefield);
使用您选择的名称创建约束和索引
将table附加为一个分区——约束和索引将自动成为分区table
上各自约束和索引的分区
我正在为多租户应用程序使用分区 tables,我必须检查唯一约束名称以 return suitable 错误。
如何检查每个分区中的唯一约束名称 table? 如何生成分区 table 中的唯一约束名称?
谢谢!
test@0:test> \d+ issuefield
...
15 Indexes:
16 "issuefield_pk_cp" PRIMARY KEY, btree (id, org_id)
17 "issuefield_key_uq" UNIQUE CONSTRAINT, btree (key, org_id)
18 "issuefield_lower_name_uq" UNIQUE, btree (lower(name::text), org_id) WITH (fillfactor='90')
19 "issuefield_name_uq" UNIQUE CONSTRAINT, btree (name, org_id)
20 Check constraints:
21 "key_check" CHECK (key::text ~ '^[a-zA-Z][a-zA-Z0-9_]*$'::text AND (lower(key::text) <> ALL (ARRAY['organization'::text, 'space'::text, 'status'::text, 'statusCategory'::text, 'fun
ction'::text])))
22 "name_check" CHECK (COALESCE(btrim(name::text), ''::text) <> ''::text)
23 Partition key: LIST (org_id)
24 Partitions: public.issuefield_abc FOR VALUES IN ('eaed29a9-0813-4948-9e47-40da6db51d15')
25
Time: 0.034s
test@0:test> \d+ issuefield_abc
STDIN
...
15 Indexes:
16 "issuefield_abc_pkey" PRIMARY KEY, btree (id, org_id)
17 "issuefield_abc_key_org_id_key" UNIQUE CONSTRAINT, btree (key, org_id)
18 "issuefield_abc_lower_org_id_idx" UNIQUE, btree (lower(name::text), org_id) WITH (fillfactor='90')
19 "issuefield_abc_name_org_id_key" UNIQUE CONSTRAINT, btree (name, org_id)
20 Check constraints:
21 "key_check" CHECK (key::text ~ '^[a-zA-Z][a-zA-Z0-9_]*$'::text AND (lower(key::text) <> ALL (ARRAY['organization'::text, 'space'::text, 'status'::text, 'statusCategory'::text, 'fun
ction'::text])))
22 "name_check" CHECK (COALESCE(btrim(name::text), ''::text) <> ''::text)
....
30 Partition of: public.issuefield FOR VALUES IN ('eaed29a9-0813-4948-9e47-40da6db51d15')
31 Partition constraint: ((org_id IS NOT NULL) AND (org_id = 'eaed29a9-0813-4948-9e47-40da6db51d15'::uuid))
32 Has OIDs: no
如您所见,检查约束的名称对于所有分区都是相同的。这不适用于主键和唯一键,因为它们的名称与索引名称相同,并且每个模式都是唯一的。
PostgreSQL 只是为分区上的索引和约束选择一个名称,就像在不指定名称的情况下创建约束或索引时所做的那样。要对此进行更多控制,请执行以下操作:
先创建没有
PARTITION OF
的分区:CREATE TABLE issuefield_abc (LIKE issuefield);
使用您选择的名称创建约束和索引
将table附加为一个分区——约束和索引将自动成为分区table
上各自约束和索引的分区