mirror of
https://git.postgresql.org/git/postgresql.git
synced 2024-10-02 23:26:53 +02:00
29abde637b
We don't allow to create replication slot_name as an empty string ('') via SQL API pg_create_logical_replication_slot() but it is allowed to be set via Alter Subscription command. This will lead to apply worker repeatedly keep trying to stream data via slot_name '' and the user is not allowed to create the slot with that name. Author: Japin Li Reviewed-By: Ranier Vilela, Amit Kapila Backpatch-through: 10, where it was introduced Discussion: https://postgr.es/m/MEYP282MB1669CBD98E721C77CA696499B61A9@MEYP282MB1669.AUSP282.PROD.OUTLOOK.COM
310 lines
20 KiB
Plaintext
310 lines
20 KiB
Plaintext
--
|
|
-- SUBSCRIPTION
|
|
--
|
|
CREATE ROLE regress_subscription_user LOGIN SUPERUSER;
|
|
CREATE ROLE regress_subscription_user2;
|
|
CREATE ROLE regress_subscription_user_dummy LOGIN NOSUPERUSER;
|
|
SET SESSION AUTHORIZATION 'regress_subscription_user';
|
|
-- fail - no publications
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'foo';
|
|
ERROR: syntax error at or near ";"
|
|
LINE 1: CREATE SUBSCRIPTION regress_testsub CONNECTION 'foo';
|
|
^
|
|
-- fail - no connection
|
|
CREATE SUBSCRIPTION regress_testsub PUBLICATION foo;
|
|
ERROR: syntax error at or near "PUBLICATION"
|
|
LINE 1: CREATE SUBSCRIPTION regress_testsub PUBLICATION foo;
|
|
^
|
|
-- fail - cannot do CREATE SUBSCRIPTION CREATE SLOT inside transaction block
|
|
BEGIN;
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'testconn' PUBLICATION testpub WITH (create_slot);
|
|
ERROR: CREATE SUBSCRIPTION ... WITH (create_slot = true) cannot run inside a transaction block
|
|
COMMIT;
|
|
-- fail - invalid connection string
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'testconn' PUBLICATION testpub;
|
|
ERROR: invalid connection string syntax: missing "=" after "testconn" in connection info string
|
|
|
|
-- fail - duplicate publications
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION foo, testpub, foo WITH (connect = false);
|
|
ERROR: publication name "foo" used more than once
|
|
-- ok
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false);
|
|
WARNING: tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
|
|
COMMENT ON SUBSCRIPTION regress_testsub IS 'test subscription';
|
|
SELECT obj_description(s.oid, 'pg_subscription') FROM pg_subscription s;
|
|
obj_description
|
|
-------------------
|
|
test subscription
|
|
(1 row)
|
|
|
|
-- fail - name already exists
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false);
|
|
ERROR: subscription "regress_testsub" already exists
|
|
-- fail - must be superuser
|
|
SET SESSION AUTHORIZATION 'regress_subscription_user2';
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION foo WITH (connect = false);
|
|
ERROR: must be superuser to create subscriptions
|
|
SET SESSION AUTHORIZATION 'regress_subscription_user';
|
|
-- fail - invalid option combinations
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, copy_data = true);
|
|
ERROR: connect = false and copy_data = true are mutually exclusive options
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, enabled = true);
|
|
ERROR: connect = false and enabled = true are mutually exclusive options
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, create_slot = true);
|
|
ERROR: connect = false and create_slot = true are mutually exclusive options
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE, enabled = true);
|
|
ERROR: slot_name = NONE and enabled = true are mutually exclusive options
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE, create_slot = true);
|
|
ERROR: slot_name = NONE and create_slot = true are mutually exclusive options
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE);
|
|
ERROR: subscription with slot_name = NONE must also set enabled = false
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE, enabled = false);
|
|
ERROR: subscription with slot_name = NONE must also set create_slot = false
|
|
CREATE SUBSCRIPTION regress_testsub2 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE, create_slot = false);
|
|
ERROR: subscription with slot_name = NONE must also set enabled = false
|
|
-- ok - with slot_name = NONE
|
|
CREATE SUBSCRIPTION regress_testsub3 CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (slot_name = NONE, connect = false);
|
|
WARNING: tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
|
|
-- fail
|
|
ALTER SUBSCRIPTION regress_testsub3 ENABLE;
|
|
ERROR: cannot enable subscription that does not have a slot name
|
|
ALTER SUBSCRIPTION regress_testsub3 REFRESH PUBLICATION;
|
|
ERROR: ALTER SUBSCRIPTION ... REFRESH is not allowed for disabled subscriptions
|
|
DROP SUBSCRIPTION regress_testsub3;
|
|
-- fail - invalid connection string
|
|
ALTER SUBSCRIPTION regress_testsub CONNECTION 'foobar';
|
|
ERROR: invalid connection string syntax: missing "=" after "foobar" in connection info string
|
|
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | f | f | d | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
ALTER SUBSCRIPTION regress_testsub SET PUBLICATION testpub2, testpub3 WITH (refresh = false);
|
|
ALTER SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist2';
|
|
ALTER SUBSCRIPTION regress_testsub SET (slot_name = 'newname');
|
|
-- fail
|
|
ALTER SUBSCRIPTION regress_testsub SET (slot_name = '');
|
|
ERROR: replication slot name "" is too short
|
|
-- fail
|
|
ALTER SUBSCRIPTION regress_doesnotexist CONNECTION 'dbname=regress_doesnotexist2';
|
|
ERROR: subscription "regress_doesnotexist" does not exist
|
|
ALTER SUBSCRIPTION regress_testsub SET (create_slot = false);
|
|
ERROR: unrecognized subscription parameter: "create_slot"
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+---------------------+--------+-----------+------------------+--------------------+------------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub2,testpub3} | f | f | d | off | dbname=regress_doesnotexist2
|
|
(1 row)
|
|
|
|
BEGIN;
|
|
ALTER SUBSCRIPTION regress_testsub ENABLE;
|
|
\dRs
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication
|
|
-----------------+---------------------------+---------+---------------------
|
|
regress_testsub | regress_subscription_user | t | {testpub2,testpub3}
|
|
(1 row)
|
|
|
|
ALTER SUBSCRIPTION regress_testsub DISABLE;
|
|
\dRs
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication
|
|
-----------------+---------------------------+---------+---------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub2,testpub3}
|
|
(1 row)
|
|
|
|
COMMIT;
|
|
-- fail - must be owner of subscription
|
|
SET ROLE regress_subscription_user_dummy;
|
|
ALTER SUBSCRIPTION regress_testsub RENAME TO regress_testsub_dummy;
|
|
ERROR: must be owner of subscription regress_testsub
|
|
RESET ROLE;
|
|
ALTER SUBSCRIPTION regress_testsub RENAME TO regress_testsub_foo;
|
|
ALTER SUBSCRIPTION regress_testsub_foo SET (synchronous_commit = local);
|
|
ALTER SUBSCRIPTION regress_testsub_foo SET (synchronous_commit = foobar);
|
|
ERROR: invalid value for parameter "synchronous_commit": "foobar"
|
|
HINT: Available values: local, remote_write, remote_apply, on, off.
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
---------------------+---------------------------+---------+---------------------+--------+-----------+------------------+--------------------+------------------------------
|
|
regress_testsub_foo | regress_subscription_user | f | {testpub2,testpub3} | f | f | d | local | dbname=regress_doesnotexist2
|
|
(1 row)
|
|
|
|
-- rename back to keep the rest simple
|
|
ALTER SUBSCRIPTION regress_testsub_foo RENAME TO regress_testsub;
|
|
-- fail - new owner must be superuser
|
|
ALTER SUBSCRIPTION regress_testsub OWNER TO regress_subscription_user2;
|
|
ERROR: permission denied to change owner of subscription "regress_testsub"
|
|
HINT: The owner of a subscription must be a superuser.
|
|
ALTER ROLE regress_subscription_user2 SUPERUSER;
|
|
-- now it works
|
|
ALTER SUBSCRIPTION regress_testsub OWNER TO regress_subscription_user2;
|
|
-- fail - cannot do DROP SUBSCRIPTION inside transaction block with slot name
|
|
BEGIN;
|
|
DROP SUBSCRIPTION regress_testsub;
|
|
ERROR: DROP SUBSCRIPTION cannot run inside a transaction block
|
|
COMMIT;
|
|
ALTER SUBSCRIPTION regress_testsub SET (slot_name = NONE);
|
|
-- now it works
|
|
BEGIN;
|
|
DROP SUBSCRIPTION regress_testsub;
|
|
COMMIT;
|
|
DROP SUBSCRIPTION IF EXISTS regress_testsub;
|
|
NOTICE: subscription "regress_testsub" does not exist, skipping
|
|
DROP SUBSCRIPTION regress_testsub; -- fail
|
|
ERROR: subscription "regress_testsub" does not exist
|
|
-- fail - binary must be boolean
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, binary = foo);
|
|
ERROR: binary requires a Boolean value
|
|
-- now it works
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, binary = true);
|
|
WARNING: tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | t | f | d | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
ALTER SUBSCRIPTION regress_testsub SET (binary = false);
|
|
ALTER SUBSCRIPTION regress_testsub SET (slot_name = NONE);
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | f | f | d | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
DROP SUBSCRIPTION regress_testsub;
|
|
-- fail - streaming must be boolean
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, streaming = foo);
|
|
ERROR: streaming requires a Boolean value
|
|
-- now it works
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, streaming = true);
|
|
WARNING: tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | f | t | d | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
ALTER SUBSCRIPTION regress_testsub SET (streaming = false);
|
|
ALTER SUBSCRIPTION regress_testsub SET (slot_name = NONE);
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | f | f | d | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
-- fail - publication already exists
|
|
ALTER SUBSCRIPTION regress_testsub ADD PUBLICATION testpub WITH (refresh = false);
|
|
ERROR: publication "testpub" is already in subscription "regress_testsub"
|
|
-- fail - publication used more than once
|
|
ALTER SUBSCRIPTION regress_testsub ADD PUBLICATION testpub1, testpub1 WITH (refresh = false);
|
|
ERROR: publication name "testpub1" used more than once
|
|
-- ok - add two publications into subscription
|
|
ALTER SUBSCRIPTION regress_testsub ADD PUBLICATION testpub1, testpub2 WITH (refresh = false);
|
|
-- fail - publications already exist
|
|
ALTER SUBSCRIPTION regress_testsub ADD PUBLICATION testpub1, testpub2 WITH (refresh = false);
|
|
ERROR: publication "testpub1" is already in subscription "regress_testsub"
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-----------------------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub,testpub1,testpub2} | f | f | d | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
-- fail - publication used more then once
|
|
ALTER SUBSCRIPTION regress_testsub DROP PUBLICATION testpub1, testpub1 WITH (refresh = false);
|
|
ERROR: publication name "testpub1" used more than once
|
|
-- fail - all publications are deleted
|
|
ALTER SUBSCRIPTION regress_testsub DROP PUBLICATION testpub, testpub1, testpub2 WITH (refresh = false);
|
|
ERROR: cannot drop all the publications from a subscription
|
|
-- fail - publication does not exist in subscription
|
|
ALTER SUBSCRIPTION regress_testsub DROP PUBLICATION testpub3 WITH (refresh = false);
|
|
ERROR: publication "testpub3" is not in subscription "regress_testsub"
|
|
-- fail - do not support copy_data option
|
|
ALTER SUBSCRIPTION regress_testsub DROP PUBLICATION testpub1 WITH (refresh = false, copy_data = true);
|
|
ERROR: unrecognized subscription parameter: "copy_data"
|
|
-- ok - delete publications
|
|
ALTER SUBSCRIPTION regress_testsub DROP PUBLICATION testpub1, testpub2 WITH (refresh = false);
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | f | f | d | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
DROP SUBSCRIPTION regress_testsub;
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION mypub
|
|
WITH (connect = false, create_slot = false, copy_data = false);
|
|
WARNING: tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
|
|
ALTER SUBSCRIPTION regress_testsub ENABLE;
|
|
-- fail - ALTER SUBSCRIPTION with refresh is not allowed in a transaction
|
|
-- block or function
|
|
BEGIN;
|
|
ALTER SUBSCRIPTION regress_testsub SET PUBLICATION mypub WITH (refresh = true);
|
|
ERROR: ALTER SUBSCRIPTION with refresh cannot run inside a transaction block
|
|
END;
|
|
BEGIN;
|
|
ALTER SUBSCRIPTION regress_testsub REFRESH PUBLICATION;
|
|
ERROR: ALTER SUBSCRIPTION ... REFRESH cannot run inside a transaction block
|
|
END;
|
|
CREATE FUNCTION func() RETURNS VOID AS
|
|
$$ ALTER SUBSCRIPTION regress_testsub SET PUBLICATION mypub WITH (refresh = true) $$ LANGUAGE SQL;
|
|
SELECT func();
|
|
ERROR: ALTER SUBSCRIPTION with refresh cannot be executed from a function
|
|
CONTEXT: SQL function "func" statement 1
|
|
ALTER SUBSCRIPTION regress_testsub DISABLE;
|
|
ALTER SUBSCRIPTION regress_testsub SET (slot_name = NONE);
|
|
DROP SUBSCRIPTION regress_testsub;
|
|
DROP FUNCTION func;
|
|
-- fail - two_phase must be boolean
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, two_phase = foo);
|
|
ERROR: two_phase requires a Boolean value
|
|
-- now it works
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (connect = false, two_phase = true);
|
|
WARNING: tables were not subscribed, you will have to run ALTER SUBSCRIPTION ... REFRESH PUBLICATION to subscribe the tables
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | f | f | p | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
--fail - alter of two_phase option not supported.
|
|
ALTER SUBSCRIPTION regress_testsub SET (two_phase = false);
|
|
ERROR: unrecognized subscription parameter: "two_phase"
|
|
--fail - cannot set streaming when two_phase enabled
|
|
ALTER SUBSCRIPTION regress_testsub SET (streaming = true);
|
|
ERROR: cannot set streaming = true for two-phase enabled subscription
|
|
ALTER SUBSCRIPTION regress_testsub SET (slot_name = NONE);
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
-----------------+---------------------------+---------+-------------+--------+-----------+------------------+--------------------+-----------------------------
|
|
regress_testsub | regress_subscription_user | f | {testpub} | f | f | p | off | dbname=regress_doesnotexist
|
|
(1 row)
|
|
|
|
DROP SUBSCRIPTION regress_testsub;
|
|
-- fail - two_phase and streaming are mutually exclusive.
|
|
CREATE SUBSCRIPTION regress_testsub CONNECTION 'dbname=regress_doesnotexist' PUBLICATION testpub WITH (streaming = true, two_phase = true);
|
|
ERROR: two_phase = true and streaming = true are mutually exclusive options
|
|
\dRs+
|
|
List of subscriptions
|
|
Name | Owner | Enabled | Publication | Binary | Streaming | Two phase commit | Synchronous commit | Conninfo
|
|
------+-------+---------+-------------+--------+-----------+------------------+--------------------+----------
|
|
(0 rows)
|
|
|
|
RESET SESSION AUTHORIZATION;
|
|
DROP ROLE regress_subscription_user;
|
|
DROP ROLE regress_subscription_user2;
|
|
DROP ROLE regress_subscription_user_dummy;
|