Skip to content

Commit 6da9759

Browse files
committed
Add RENAME support for PUBLICATIONs and SUBSCRIPTIONs
From: Petr Jelinek <petr.jelinek@2ndquadrant.com>
1 parent 713f7c4 commit 6da9759

File tree

9 files changed

+90
-8
lines changed

9 files changed

+90
-8
lines changed

src/backend/commands/alter.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "catalog/pg_opclass.h"
3333
#include "catalog/pg_opfamily.h"
3434
#include "catalog/pg_proc.h"
35+
#include "catalog/pg_subscription.h"
3536
#include "catalog/pg_ts_config.h"
3637
#include "catalog/pg_ts_dict.h"
3738
#include "catalog/pg_ts_parser.h"
@@ -90,6 +91,12 @@ report_name_conflict(Oid classId, const char *name)
9091
case LanguageRelationId:
9192
msgfmt = gettext_noop("language \"%s\" already exists");
9293
break;
94+
case PublicationRelationId:
95+
msgfmt = gettext_noop("publication \"%s\" already exists");
96+
break;
97+
case SubscriptionRelationId:
98+
msgfmt = gettext_noop("subscription \"%s\" already exists");
99+
break;
93100
default:
94101
elog(ERROR, "unsupported object class %u", classId);
95102
break;
@@ -256,6 +263,12 @@ AlterObjectRename_internal(Relation rel, Oid objectId, const char *new_name)
256263
IsThereOpFamilyInNamespace(new_name, opf->opfmethod,
257264
opf->opfnamespace);
258265
}
266+
else if (classId == SubscriptionRelationId)
267+
{
268+
if (SearchSysCacheExists2(SUBSCRIPTIONNAME, MyDatabaseId,
269+
CStringGetDatum(new_name)))
270+
report_name_conflict(classId, new_name);
271+
}
259272
else if (nameCacheId >= 0)
260273
{
261274
if (OidIsValid(namespaceId))
@@ -364,6 +377,8 @@ ExecRenameStmt(RenameStmt *stmt)
364377
case OBJECT_TSDICTIONARY:
365378
case OBJECT_TSPARSER:
366379
case OBJECT_TSTEMPLATE:
380+
case OBJECT_PUBLICATION:
381+
case OBJECT_SUBSCRIPTION:
367382
{
368383
ObjectAddress address;
369384
Relation catalog;

src/backend/parser/gram.y

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8151,6 +8151,15 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
81518151
n->missing_ok = true;
81528152
$$ = (Node *)n;
81538153
}
8154+
| ALTER PUBLICATION name RENAME TO name
8155+
{
8156+
RenameStmt *n = makeNode(RenameStmt);
8157+
n->renameType = OBJECT_PUBLICATION;
8158+
n->object = list_make1(makeString($3));
8159+
n->newname = $6;
8160+
n->missing_ok = false;
8161+
$$ = (Node *)n;
8162+
}
81548163
| ALTER SCHEMA name RENAME TO name
81558164
{
81568165
RenameStmt *n = makeNode(RenameStmt);
@@ -8169,6 +8178,15 @@ RenameStmt: ALTER AGGREGATE aggregate_with_argtypes RENAME TO name
81698178
n->missing_ok = false;
81708179
$$ = (Node *)n;
81718180
}
8181+
| ALTER SUBSCRIPTION name RENAME TO name
8182+
{
8183+
RenameStmt *n = makeNode(RenameStmt);
8184+
n->renameType = OBJECT_SUBSCRIPTION;
8185+
n->object = list_make1(makeString($3));
8186+
n->newname = $6;
8187+
n->missing_ok = false;
8188+
$$ = (Node *)n;
8189+
}
81728190
| ALTER TABLE relation_expr RENAME TO name
81738191
{
81748192
RenameStmt *n = makeNode(RenameStmt);

src/backend/replication/logical/worker.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1259,6 +1259,21 @@ reread_subscription(void)
12591259
proc_exit(0);
12601260
}
12611261

1262+
/*
1263+
* Exit if subscription name was changed (it's used for
1264+
* fallback_application_name). The launcher will start new worker.
1265+
*/
1266+
if (strcmp(newsub->name, MySubscription->name) != 0)
1267+
{
1268+
ereport(LOG,
1269+
(errmsg("logical replication worker for subscription \"%s\" will "
1270+
"restart because subscription was renamed",
1271+
MySubscription->name)));
1272+
1273+
walrcv_disconnect(wrconn);
1274+
proc_exit(0);
1275+
}
1276+
12621277
/*
12631278
* Exit if publication list was changed. The launcher will start
12641279
* new worker.
@@ -1292,7 +1307,6 @@ reread_subscription(void)
12921307

12931308
/* Check for other changes that should never happen too. */
12941309
if (newsub->dbid != MySubscription->dbid ||
1295-
strcmp(newsub->name, MySubscription->name) != 0 ||
12961310
strcmp(newsub->slotname, MySubscription->slotname) != 0)
12971311
{
12981312
elog(ERROR, "subscription %u changed unexpectedly",

src/bin/psql/tab-complete.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,7 +1463,8 @@ psql_completion(const char *text, int start, int end)
14631463
/* ALTER PUBLICATION <name> ...*/
14641464
else if (Matches3("ALTER","PUBLICATION",MatchAny))
14651465
{
1466-
COMPLETE_WITH_LIST5("WITH", "ADD TABLE", "SET TABLE", "DROP TABLE", "OWNER TO");
1466+
COMPLETE_WITH_LIST6("WITH", "ADD TABLE", "SET TABLE", "DROP TABLE",
1467+
"OWNER TO", "RENAME TO");
14671468
}
14681469
/* ALTER PUBLICATION <name> .. WITH ( ... */
14691470
else if (HeadMatches3("ALTER", "PUBLICATION",MatchAny) && TailMatches2("WITH", "("))
@@ -1474,7 +1475,8 @@ psql_completion(const char *text, int start, int end)
14741475
/* ALTER SUBSCRIPTION <name> ... */
14751476
else if (Matches3("ALTER","SUBSCRIPTION",MatchAny))
14761477
{
1477-
COMPLETE_WITH_LIST6("WITH", "CONNECTION", "SET PUBLICATION", "ENABLE", "DISABLE", "OWNER TO");
1478+
COMPLETE_WITH_LIST7("WITH", "CONNECTION", "SET PUBLICATION", "ENABLE",
1479+
"DISABLE", "OWNER TO", "RENAME TO");
14781480
}
14791481
else if (HeadMatches3("ALTER", "SUBSCRIPTION", MatchAny) && TailMatches2("WITH", "("))
14801482
{

src/test/regress/expected/publication.out

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,15 @@ DROP TABLE testpub_tbl1;
148148
t | t | t
149149
(1 row)
150150

151-
DROP PUBLICATION testpub_default;
151+
ALTER PUBLICATION testpub_default RENAME TO testpub_foo;
152+
\dRp testpub_foo
153+
List of publications
154+
Name | Owner | Inserts | Updates | Deletes
155+
-------------+--------------------------+---------+---------+---------
156+
testpub_foo | regress_publication_user | t | t | t
157+
(1 row)
158+
159+
DROP PUBLICATION testpub_foo;
152160
DROP PUBLICATION testpib_ins_trunct;
153161
DROP PUBLICATION testpub_fortbl;
154162
DROP SCHEMA pub_test CASCADE;

src/test/regress/expected/subscription.out

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ ALTER SUBSCRIPTION testsub DISABLE;
6161
(1 row)
6262

6363
COMMIT;
64-
DROP SUBSCRIPTION testsub NODROP SLOT;
64+
ALTER SUBSCRIPTION testsub RENAME TO testsub_foo;
65+
\dRs
66+
List of subscriptions
67+
Name | Owner | Enabled | Publication
68+
-------------+---------------------------+---------+--------------------
69+
testsub_foo | regress_subscription_user | f | {testpub,testpub1}
70+
(1 row)
71+
72+
DROP SUBSCRIPTION testsub_foo NODROP SLOT;
6573
RESET SESSION AUTHORIZATION;
6674
DROP ROLE regress_subscription_user;

src/test/regress/sql/publication.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,11 @@ DROP TABLE testpub_tbl1;
7373

7474
\dRp+ testpub_default
7575

76-
DROP PUBLICATION testpub_default;
76+
ALTER PUBLICATION testpub_default RENAME TO testpub_foo;
77+
78+
\dRp testpub_foo
79+
80+
DROP PUBLICATION testpub_foo;
7781
DROP PUBLICATION testpib_ins_trunct;
7882
DROP PUBLICATION testpub_fortbl;
7983

src/test/regress/sql/subscription.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,11 @@ ALTER SUBSCRIPTION testsub DISABLE;
3838

3939
COMMIT;
4040

41-
DROP SUBSCRIPTION testsub NODROP SLOT;
41+
ALTER SUBSCRIPTION testsub RENAME TO testsub_foo;
42+
43+
\dRs
44+
45+
DROP SUBSCRIPTION testsub_foo NODROP SLOT;
4246

4347
RESET SESSION AUTHORIZATION;
4448
DROP ROLE regress_subscription_user;

src/test/subscription/t/001_rep_changes.pl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,17 @@
169169
$node_subscriber->safe_psql('postgres', "SELECT count(*), min(a), max(a) FROM tab_full");
170170
is($result, qq(11|0|100), 'check replicated insert after alter publication');
171171

172+
# check restart on rename
173+
$oldpid = $node_publisher->safe_psql('postgres',
174+
"SELECT pid FROM pg_stat_replication WHERE application_name = '$appname';");
175+
$node_subscriber->safe_psql('postgres',
176+
"ALTER SUBSCRIPTION tap_sub RENAME TO tap_sub_renamed");
177+
$node_publisher->poll_query_until('postgres',
178+
"SELECT pid != $oldpid FROM pg_stat_replication WHERE application_name = '$appname';")
179+
or die "Timed out while waiting for apply to restart";
180+
172181
# check all the cleanup
173-
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub");
182+
$node_subscriber->safe_psql('postgres', "DROP SUBSCRIPTION tap_sub_renamed");
174183

175184
$result =
176185
$node_subscriber->safe_psql('postgres', "SELECT count(*) FROM pg_subscription");

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy