2013-11-08 18:30:43 +01:00
|
|
|
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)
|
Remove WITH OIDS support, change oid catalog column visibility.
Previously tables declared WITH OIDS, including a significant fraction
of the catalog tables, stored the oid column not as a normal column,
but as part of the tuple header.
This special column was not shown by default, which was somewhat odd,
as it's often (consider e.g. pg_class.oid) one of the more important
parts of a row. Neither pg_dump nor COPY included the contents of the
oid column by default.
The fact that the oid column was not an ordinary column necessitated a
significant amount of special case code to support oid columns. That
already was painful for the existing, but upcoming work aiming to make
table storage pluggable, would have required expanding and duplicating
that "specialness" significantly.
WITH OIDS has been deprecated since 2005 (commit ff02d0a05280e0).
Remove it.
Removing includes:
- CREATE TABLE and ALTER TABLE syntax for declaring the table to be
WITH OIDS has been removed (WITH (oids[ = true]) will error out)
- pg_dump does not support dumping tables declared WITH OIDS and will
issue a warning when dumping one (and ignore the oid column).
- restoring an pg_dump archive with pg_restore will warn when
restoring a table with oid contents (and ignore the oid column)
- COPY will refuse to load binary dump that includes oids.
- pg_upgrade will error out when encountering tables declared WITH
OIDS, they have to be altered to remove the oid column first.
- Functionality to access the oid of the last inserted row (like
plpgsql's RESULT_OID, spi's SPI_lastoid, ...) has been removed.
The syntax for declaring a table WITHOUT OIDS (or WITH (oids = false)
for CREATE TABLE) is still supported. While that requires a bit of
support code, it seems unnecessary to break applications / dumps that
do not use oids, and are explicit about not using them.
The biggest user of WITH OID columns was postgres' catalog. This
commit changes all 'magic' oid columns to be columns that are normally
declared and stored. To reduce unnecessary query breakage all the
newly added columns are still named 'oid', even if a table's column
naming scheme would indicate 'reloid' or such. This obviously
requires adapting a lot code, mostly replacing oid access via
HeapTupleGetOid() with access to the underlying Form_pg_*->oid column.
The bootstrap process now assigns oids for all oid columns in
genbki.pl that do not have an explicit value (starting at the largest
oid previously used), only oids assigned later by oids will be above
FirstBootstrapObjectId. As the oid column now is a normal column the
special bootstrap syntax for oids has been removed.
Oids are not automatically assigned during insertion anymore, all
backend code explicitly assigns oids with GetNewOidWithIndex(). For
the rare case that insertions into the catalog via SQL are called for
the new pg_nextoid() function can be used (which only works on catalog
tables).
The fact that oid columns on system tables are now normal columns
means that they will be included in the set of columns expanded
by * (i.e. SELECT * FROM pg_class will now include the table's oid,
previously it did not). It'd not technically be hard to hide oid
column by default, but that'd mean confusing behavior would either
have to be carried forward forever, or it'd cause breakage down the
line.
While it's not unlikely that further adjustments are needed, the
scope/invasiveness of the patch makes it worthwhile to get merge this
now. It's painful to maintain externally, too complicated to commit
after the code code freeze, and a dependency of a number of other
patches.
Catversion bump, for obvious reasons.
Author: Andres Freund, with contributions by John Naylor
Discussion: https://postgr.es/m/20180930034810.ywp2c7awz7opzcfr@alap3.anarazel.de
2018-11-21 00:36:57 +01:00
|
|
|
) ;
|
2013-11-08 18:30:43 +01:00
|
|
|
CREATE TABLE test_replica_identity_othertable (id serial primary key);
|
|
|
|
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;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
d
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
-- but 'none' for system tables
|
|
|
|
SELECT relreplident FROM pg_class WHERE oid = 'pg_class'::regclass;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
n
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT relreplident FROM pg_class WHERE oid = 'pg_constraint'::regclass;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
n
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
----
|
2014-03-18 20:03:17 +01:00
|
|
|
-- Make sure we detect ineligible indexes
|
2013-11-08 18:30:43 +01:00
|
|
|
----
|
|
|
|
-- fail, not unique
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_keyab;
|
|
|
|
ERROR: cannot use non-unique index "test_replica_identity_keyab" as replica identity
|
|
|
|
-- fail, not a candidate key, nullable column
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_nonkey;
|
|
|
|
ERROR: index "test_replica_identity_nonkey" cannot be used as replica identity because column "nonkey" is nullable
|
|
|
|
-- fail, hash indexes cannot do uniqueness
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_hash;
|
|
|
|
ERROR: cannot use non-unique index "test_replica_identity_hash" as replica identity
|
|
|
|
-- fail, expression index
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_expr;
|
|
|
|
ERROR: cannot use expression index "test_replica_identity_expr" as replica identity
|
|
|
|
-- fail, partial index
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_partial;
|
|
|
|
ERROR: cannot use partial index "test_replica_identity_partial" as replica identity
|
|
|
|
-- fail, not our index
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_othertable_pkey;
|
|
|
|
ERROR: "test_replica_identity_othertable_pkey" is not an index for table "test_replica_identity"
|
|
|
|
-- fail, deferrable
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY USING INDEX test_replica_identity_unique_defer;
|
|
|
|
ERROR: cannot use non-immediate index "test_replica_identity_unique_defer" as replica identity
|
|
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
d
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
----
|
2016-03-15 23:06:11 +01:00
|
|
|
-- Make sure index cases succeed
|
2013-11-08 18:30:43 +01:00
|
|
|
----
|
|
|
|
-- 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;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
i
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
\d test_replica_identity
|
2016-11-03 17:00:00 +01:00
|
|
|
Table "public.test_replica_identity"
|
|
|
|
Column | Type | Collation | Nullable | Default
|
|
|
|
--------+---------+-----------+----------+---------------------------------------------------
|
|
|
|
id | integer | | not null | nextval('test_replica_identity_id_seq'::regclass)
|
|
|
|
keya | text | | not null |
|
|
|
|
keyb | text | | not null |
|
|
|
|
nonkey | text | | |
|
2013-11-08 18:30:43 +01:00
|
|
|
Indexes:
|
|
|
|
"test_replica_identity_pkey" PRIMARY KEY, btree (id) REPLICA IDENTITY
|
|
|
|
"test_replica_identity_expr" UNIQUE, btree (keya, keyb, (3))
|
2019-07-02 18:32:49 +02:00
|
|
|
"test_replica_identity_hash" hash (nonkey)
|
|
|
|
"test_replica_identity_keyab" btree (keya, keyb)
|
2013-11-08 18:30:43 +01:00
|
|
|
"test_replica_identity_keyab_key" UNIQUE, btree (keya, keyb)
|
|
|
|
"test_replica_identity_nonkey" UNIQUE, btree (keya, nonkey)
|
|
|
|
"test_replica_identity_partial" UNIQUE, btree (keya, keyb) WHERE keyb <> '3'::text
|
|
|
|
"test_replica_identity_unique_defer" UNIQUE CONSTRAINT, btree (keya, keyb) DEFERRABLE
|
|
|
|
"test_replica_identity_unique_nondefer" UNIQUE CONSTRAINT, btree (keya, keyb)
|
|
|
|
|
2017-02-06 10:33:58 +01:00
|
|
|
-- succeed, nondeferrable unique constraint over nonnullable cols
|
2013-11-08 18:30:43 +01:00
|
|
|
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;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
i
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
\d test_replica_identity
|
2016-11-03 17:00:00 +01:00
|
|
|
Table "public.test_replica_identity"
|
|
|
|
Column | Type | Collation | Nullable | Default
|
|
|
|
--------+---------+-----------+----------+---------------------------------------------------
|
|
|
|
id | integer | | not null | nextval('test_replica_identity_id_seq'::regclass)
|
|
|
|
keya | text | | not null |
|
|
|
|
keyb | text | | not null |
|
|
|
|
nonkey | text | | |
|
2013-11-08 18:30:43 +01:00
|
|
|
Indexes:
|
|
|
|
"test_replica_identity_pkey" PRIMARY KEY, btree (id)
|
|
|
|
"test_replica_identity_expr" UNIQUE, btree (keya, keyb, (3))
|
2019-07-02 18:32:49 +02:00
|
|
|
"test_replica_identity_hash" hash (nonkey)
|
|
|
|
"test_replica_identity_keyab" btree (keya, keyb)
|
2013-11-08 18:30:43 +01:00
|
|
|
"test_replica_identity_keyab_key" UNIQUE, btree (keya, keyb) REPLICA IDENTITY
|
|
|
|
"test_replica_identity_nonkey" UNIQUE, btree (keya, nonkey)
|
|
|
|
"test_replica_identity_partial" UNIQUE, btree (keya, keyb) WHERE keyb <> '3'::text
|
|
|
|
"test_replica_identity_unique_defer" UNIQUE CONSTRAINT, btree (keya, keyb) DEFERRABLE
|
|
|
|
"test_replica_identity_unique_nondefer" UNIQUE CONSTRAINT, btree (keya, keyb)
|
|
|
|
|
|
|
|
SELECT count(*) FROM pg_index WHERE indrelid = 'test_replica_identity'::regclass AND indisreplident;
|
|
|
|
count
|
|
|
|
-------
|
|
|
|
1
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
----
|
|
|
|
-- 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;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
d
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
SELECT count(*) FROM pg_index WHERE indrelid = 'test_replica_identity'::regclass AND indisreplident;
|
|
|
|
count
|
|
|
|
-------
|
|
|
|
0
|
|
|
|
(1 row)
|
|
|
|
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY FULL;
|
|
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
f
|
|
|
|
(1 row)
|
|
|
|
|
2014-04-15 19:28:54 +02:00
|
|
|
\d+ test_replica_identity
|
2016-11-03 17:00:00 +01:00
|
|
|
Table "public.test_replica_identity"
|
|
|
|
Column | Type | Collation | Nullable | Default | Storage | Stats target | Description
|
|
|
|
--------+---------+-----------+----------+---------------------------------------------------+----------+--------------+-------------
|
|
|
|
id | integer | | not null | nextval('test_replica_identity_id_seq'::regclass) | plain | |
|
|
|
|
keya | text | | not null | | extended | |
|
|
|
|
keyb | text | | not null | | extended | |
|
|
|
|
nonkey | text | | | | extended | |
|
2013-11-08 18:30:43 +01:00
|
|
|
Indexes:
|
|
|
|
"test_replica_identity_pkey" PRIMARY KEY, btree (id)
|
|
|
|
"test_replica_identity_expr" UNIQUE, btree (keya, keyb, (3))
|
2019-07-02 18:32:49 +02:00
|
|
|
"test_replica_identity_hash" hash (nonkey)
|
|
|
|
"test_replica_identity_keyab" btree (keya, keyb)
|
2013-11-08 18:30:43 +01:00
|
|
|
"test_replica_identity_keyab_key" UNIQUE, btree (keya, keyb)
|
|
|
|
"test_replica_identity_nonkey" UNIQUE, btree (keya, nonkey)
|
|
|
|
"test_replica_identity_partial" UNIQUE, btree (keya, keyb) WHERE keyb <> '3'::text
|
|
|
|
"test_replica_identity_unique_defer" UNIQUE CONSTRAINT, btree (keya, keyb) DEFERRABLE
|
|
|
|
"test_replica_identity_unique_nondefer" UNIQUE CONSTRAINT, btree (keya, keyb)
|
2014-03-30 00:00:11 +01:00
|
|
|
Replica Identity: FULL
|
2013-11-08 18:30:43 +01:00
|
|
|
|
|
|
|
ALTER TABLE test_replica_identity REPLICA IDENTITY NOTHING;
|
|
|
|
SELECT relreplident FROM pg_class WHERE oid = 'test_replica_identity'::regclass;
|
|
|
|
relreplident
|
|
|
|
--------------
|
|
|
|
n
|
|
|
|
(1 row)
|
|
|
|
|
2020-03-13 11:28:11 +01:00
|
|
|
---
|
|
|
|
-- 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
|
|
|
|
Table "public.test_replica_identity2"
|
|
|
|
Column | Type | Collation | Nullable | Default
|
|
|
|
--------+---------+-----------+----------+---------
|
|
|
|
id | integer | | not null |
|
|
|
|
Indexes:
|
|
|
|
"test_replica_identity2_id_key" UNIQUE CONSTRAINT, btree (id) REPLICA IDENTITY
|
|
|
|
|
|
|
|
ALTER TABLE test_replica_identity2 ALTER COLUMN id TYPE bigint;
|
|
|
|
\d test_replica_identity2
|
|
|
|
Table "public.test_replica_identity2"
|
|
|
|
Column | Type | Collation | Nullable | Default
|
|
|
|
--------+--------+-----------+----------+---------
|
|
|
|
id | bigint | | not null |
|
|
|
|
Indexes:
|
|
|
|
"test_replica_identity2_id_key" UNIQUE CONSTRAINT, btree (id) REPLICA IDENTITY
|
|
|
|
|
|
|
|
-- 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
|
|
|
|
Table "public.test_replica_identity3"
|
|
|
|
Column | Type | Collation | Nullable | Default
|
|
|
|
--------+---------+-----------+----------+---------
|
|
|
|
id | integer | | not null |
|
|
|
|
Indexes:
|
|
|
|
"test_replica_identity3_id_key" UNIQUE, btree (id) REPLICA IDENTITY
|
|
|
|
|
|
|
|
ALTER TABLE test_replica_identity3 ALTER COLUMN id TYPE bigint;
|
|
|
|
\d test_replica_identity3
|
|
|
|
Table "public.test_replica_identity3"
|
|
|
|
Column | Type | Collation | Nullable | Default
|
|
|
|
--------+--------+-----------+----------+---------
|
|
|
|
id | bigint | | not null |
|
|
|
|
Indexes:
|
|
|
|
"test_replica_identity3_id_key" UNIQUE, btree (id) REPLICA IDENTITY
|
|
|
|
|
2013-11-08 18:30:43 +01:00
|
|
|
DROP TABLE test_replica_identity;
|
2020-03-13 11:28:11 +01:00
|
|
|
DROP TABLE test_replica_identity2;
|
|
|
|
DROP TABLE test_replica_identity3;
|
2013-11-08 18:30:43 +01:00
|
|
|
DROP TABLE test_replica_identity_othertable;
|