mirror of
https://github.com/postgres/postgres.git
synced 2026-03-07 07:40:31 -05:00
We now create contype='n' pg_constraint rows for not-null constraints on user tables. Only one such constraint is allowed for a column. We propagate these constraints to other tables during operations such as adding inheritance relationships, creating and attaching partitions and creating tables LIKE other tables. These related constraints mostly follow the well-known rules of conislocal and coninhcount that we have for CHECK constraints, with some adaptations: for example, as opposed to CHECK constraints, we don't match not-null ones by name when descending a hierarchy to alter or remove it, instead matching by the name of the column that they apply to. This means we don't require the constraint names to be identical across a hierarchy. The inheritance status of these constraints can be controlled: now we can be sure that if a parent table has one, then all children will have it as well. They can optionally be marked NO INHERIT, and then children are free not to have one. (There's currently no support for altering a NO INHERIT constraint into inheriting down the hierarchy, but that's a desirable future feature.) This also opens the door for having these constraints be marked NOT VALID, as well as allowing UNIQUE+NOT NULL to be used for functional dependency determination, as envisioned by commite49ae8d3bc. It's likely possible to allow DEFERRABLE constraints as followup work, as well. psql shows these constraints in \d+, though we may want to reconsider if this turns out to be too noisy. Earlier versions of this patch hid constraints that were on the same columns of the primary key, but I'm not sure that that's very useful. If clutter is a problem, we might be better off inventing a new \d++ command and not showing the constraints in \d+. For now, we omit these constraints on system catalog columns, because they're unlikely to achieve anything. The main difference to the previous attempt at this (b0e96f3119) is that we now require that such a constraint always exists when a primary key is in the column; we didn't require this previously which had a number of unpalatable consequences. With this requirement, the code is easier to reason about. For example: - We no longer have "throwaway constraints" during pg_dump. We needed those for the case where a table had a PK without a not-null underneath, to prevent a slow scan of the data during restore of the PK creation, which was particularly problematic for pg_upgrade. - We no longer have to cope with attnotnull being set spuriously in case a primary key is dropped indirectly (e.g., via DROP COLUMN). Some bits of code in this patch were authored by Jian He. Author: Álvaro Herrera <alvherre@alvh.no-ip.org> Author: Bernd Helmle <mailings@oopsware.de> Reviewed-by: 何建 (jian he) <jian.universality@gmail.com> Reviewed-by: 王刚 (Tender Wang) <tndrwang@gmail.com> Reviewed-by: Justin Pryzby <pryzby@telsasoft.com> Reviewed-by: Peter Eisentraut <peter.eisentraut@enterprisedb.com> Reviewed-by: Dean Rasheed <dean.a.rasheed@gmail.com> Discussion: https://postgr.es/m/202408310358.sdhumtyuy2ht@alvherre.pgsql
143 lines
6.6 KiB
SQL
143 lines
6.6 KiB
SQL
CREATE TABLE test_replica_identity (
|
|
id serial primary key,
|
|
keya text not null,
|
|
keyb text not null,
|
|
nonkey text,
|
|
CONSTRAINT test_replica_identity_unique_defer UNIQUE (keya, keyb) DEFERRABLE,
|
|
CONSTRAINT test_replica_identity_unique_nondefer UNIQUE (keya, keyb)
|
|
) ;
|
|
|
|
CREATE TABLE test_replica_identity_othertable (id serial primary key);
|
|
CREATE TABLE test_replica_identity_t3 (id serial constraint pk primary key deferrable);
|
|
|
|
CREATE INDEX test_replica_identity_keyab ON test_replica_identity (keya, keyb);
|
|
CREATE UNIQUE INDEX test_replica_identity_keyab_key ON test_replica_identity (keya, keyb);
|
|
CREATE UNIQUE INDEX test_replica_identity_nonkey ON test_replica_identity (keya, nonkey);
|
|
CREATE INDEX test_replica_identity_hash ON test_replica_identity USING hash (nonkey);
|
|
CREATE UNIQUE INDEX test_replica_identity_expr ON test_replica_identity (keya, keyb, (3));
|
|
CREATE UNIQUE INDEX test_replica_identity_partial ON test_replica_identity (keya, keyb) WHERE keyb != '3';
|
|
|
|
-- default is 'd'/DEFAULT for user created tables
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
-- but 'none' for system tables
|
|
SELECT relreplident FROM pg_class WHERE oid = 'pg_class'::regclass;
|
|
SELECT relreplident FROM pg_class WHERE oid = 'pg_constraint'::regclass;
|
|
|
|
----
|
|
-- Make sure we detect ineligible indexes
|
|
----
|
|
|
|
-- fail, not unique
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_keyab;
|
|
-- fail, not a candidate key, nullable column
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_nonkey;
|
|
-- fail, hash indexes cannot do uniqueness
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_hash;
|
|
-- fail, expression index
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_expr;
|
|
-- fail, partial index
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_partial;
|
|
-- fail, not our index
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_othertable_pkey;
|
|
-- fail, deferrable
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_unique_defer;
|
|
-- fail, deferrable
|
|
ALTER TABLE test_replica_identity_t3 REPLICA IDENTITY USING INDEX pk;
|
|
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
|
|
----
|
|
-- Make sure index cases succeed
|
|
----
|
|
|
|
-- succeed, primary key
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_pkey;
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
\d test_replica_identity
|
|
|
|
-- succeed, nondeferrable unique constraint over nonnullable cols
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_unique_nondefer;
|
|
|
|
-- succeed unique index over nonnullable cols
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_keyab_key;
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_keyab_key;
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
\d test_replica_identity
|
|
SELECT count(*) FROM pg_index WHERE indrelid = 'test_replica_identity'::regclass AND indisreplident;
|
|
|
|
----
|
|
-- Make sure non index cases work
|
|
----
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY DEFAULT;
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
SELECT count(*) FROM pg_index WHERE indrelid = 'test_replica_identity'::regclass AND indisreplident;
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY FULL;
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
\d+ test_replica_identity
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY NOTHING;
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
|
|
---
|
|
-- Test that ALTER TABLE rewrite preserves nondefault replica identity
|
|
---
|
|
|
|
-- constraint variant
|
|
CREATE TABLE test_replica_identity2 (id int UNIQUE NOT NULL);
|
|
ALTER TABLE test_replica_identity2 REPLICA IDENTITY USING INDEX test_replica_identity2_id_key;
|
|
\d test_replica_identity2
|
|
ALTER TABLE test_replica_identity2 ALTER COLUMN id TYPE bigint;
|
|
\d test_replica_identity2
|
|
|
|
-- straight index variant
|
|
CREATE TABLE test_replica_identity3 (id int NOT NULL);
|
|
CREATE UNIQUE INDEX test_replica_identity3_id_key ON test_replica_identity3 (id);
|
|
ALTER TABLE test_replica_identity3 REPLICA IDENTITY USING INDEX test_replica_identity3_id_key;
|
|
\d test_replica_identity3
|
|
ALTER TABLE test_replica_identity3 ALTER COLUMN id TYPE bigint;
|
|
\d test_replica_identity3
|
|
|
|
-- ALTER TABLE DROP NOT NULL is not allowed for columns part of an index
|
|
-- used as replica identity.
|
|
ALTER TABLE test_replica_identity3 ALTER COLUMN id DROP NOT NULL;
|
|
-- but it's OK when the identity is FULL
|
|
ALTER TABLE test_replica_identity3 REPLICA IDENTITY FULL;
|
|
ALTER TABLE test_replica_identity3 ALTER COLUMN id DROP NOT NULL;
|
|
|
|
--
|
|
-- Test that replica identity can be set on an index that's not yet valid.
|
|
-- (This matches the way pg_dump will try to dump a partitioned table.)
|
|
--
|
|
CREATE TABLE test_replica_identity4(id integer NOT NULL) PARTITION BY LIST (id);
|
|
CREATE TABLE test_replica_identity4_1(id integer NOT NULL);
|
|
ALTER TABLE ONLY test_replica_identity4
|
|
ATTACH PARTITION test_replica_identity4_1 FOR VALUES IN (1);
|
|
ALTER TABLE ONLY test_replica_identity4
|
|
ADD CONSTRAINT test_replica_identity4_pkey PRIMARY KEY (id);
|
|
ALTER TABLE ONLY test_replica_identity4
|
|
REPLICA IDENTITY USING INDEX test_replica_identity4_pkey;
|
|
ALTER TABLE ONLY test_replica_identity4_1
|
|
ADD CONSTRAINT test_replica_identity4_1_pkey PRIMARY KEY (id);
|
|
\d+ test_replica_identity4
|
|
ALTER INDEX test_replica_identity4_pkey
|
|
ATTACH PARTITION test_replica_identity4_1_pkey;
|
|
\d+ test_replica_identity4
|
|
|
|
-- Dropping the primary key is not allowed if that would leave the replica
|
|
-- identity as nullable
|
|
CREATE TABLE test_replica_identity5 (a int not null, b int, c int,
|
|
PRIMARY KEY (b, c));
|
|
CREATE UNIQUE INDEX test_replica_identity5_a_b_key ON test_replica_identity5 (a, b);
|
|
ALTER TABLE test_replica_identity5 REPLICA IDENTITY USING INDEX test_replica_identity5_a_b_key;
|
|
ALTER TABLE test_replica_identity5 DROP CONSTRAINT test_replica_identity5_pkey;
|
|
ALTER TABLE test_replica_identity5 ALTER b SET NOT NULL;
|
|
ALTER TABLE test_replica_identity5 DROP CONSTRAINT test_replica_identity5_pkey;
|
|
ALTER TABLE test_replica_identity5 ALTER b DROP NOT NULL;
|
|
|
|
DROP TABLE test_replica_identity;
|
|
DROP TABLE test_replica_identity2;
|
|
DROP TABLE test_replica_identity3;
|
|
DROP TABLE test_replica_identity4;
|
|
DROP TABLE test_replica_identity5;
|
|
DROP TABLE test_replica_identity_othertable;
|
|
DROP TABLE test_replica_identity_t3;
|