Skip to content

Commit 99883e9

Browse files
committed
Fixes a bug that alter node and create node does not work with
primary=on even when primary=off update or drop node against the primary node has been done in the same session.
1 parent 4143782 commit 99883e9

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

src/backend/pgxc/nodemgr/nodemgr.c

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ PgxcNodeCreate(CreateNodeStmt *stmt)
492492
bool is_primary = false;
493493
bool is_preferred = false;
494494
Datum node_id;
495+
Oid nodeOid;
495496

496497
/* Only a DB administrator can add nodes */
497498
if (!superuser())
@@ -577,11 +578,14 @@ PgxcNodeCreate(CreateNodeStmt *stmt)
577578
htup = heap_form_tuple(pgxcnodesrel->rd_att, values, nulls);
578579

579580
/* Insert tuple in catalog */
580-
simple_heap_insert(pgxcnodesrel, htup);
581+
nodeOid = simple_heap_insert(pgxcnodesrel, htup);
581582

582583
CatalogUpdateIndexes(pgxcnodesrel, htup);
583584

584585
heap_close(pgxcnodesrel, RowExclusiveLock);
586+
587+
if (is_primary)
588+
primary_data_node = nodeOid;
585589
}
586590

587591
/*
@@ -598,6 +602,9 @@ PgxcNodeAlter(AlterNodeStmt *stmt)
598602
int node_port;
599603
bool is_preferred;
600604
bool is_primary;
605+
bool was_primary;
606+
bool primary_off = false;
607+
Oid new_primary = InvalidOid;
601608
HeapTuple oldtup, newtup;
602609
Oid nodeOid = get_pgxc_nodeoid(node_name);
603610
Relation rel;
@@ -634,7 +641,7 @@ PgxcNodeAlter(AlterNodeStmt *stmt)
634641
node_host = get_pgxc_nodehost(nodeOid);
635642
node_port = get_pgxc_nodeport(nodeOid);
636643
is_preferred = is_pgxc_nodepreferred(nodeOid);
637-
is_primary = is_pgxc_nodeprimary(nodeOid);
644+
is_primary = was_primary = is_pgxc_nodeprimary(nodeOid);
638645
node_type = get_pgxc_nodetype(nodeOid);
639646
node_type_old = node_type;
640647
node_id = get_pgxc_node_id(nodeOid);
@@ -651,11 +658,22 @@ PgxcNodeAlter(AlterNodeStmt *stmt)
651658
*/
652659
if (is_primary &&
653660
OidIsValid(primary_data_node) &&
654-
node_id != primary_data_node)
661+
nodeOid != primary_data_node)
655662
ereport(ERROR,
656663
(errcode(ERRCODE_SYNTAX_ERROR),
657664
errmsg("PGXC node %s: two nodes cannot be primary",
658665
node_name)));
666+
/*
667+
* If this node is a primary and the statement says primary = false,
668+
* we need to invalidate primary_data_node when the whole operation
669+
* is successful.
670+
*/
671+
if (was_primary && !is_primary &&
672+
OidIsValid(primary_data_node) &&
673+
nodeOid == primary_data_node)
674+
primary_off = true;
675+
else if (is_primary)
676+
new_primary = nodeOid;
659677

660678
/* Check type dependency */
661679
if (node_type_old == PGXC_NODE_COORDINATOR &&
@@ -698,6 +716,12 @@ PgxcNodeAlter(AlterNodeStmt *stmt)
698716
/* Update indexes */
699717
CatalogUpdateIndexes(rel, newtup);
700718

719+
/* Invalidate primary_data_node if needed */
720+
if (primary_off)
721+
primary_data_node = InvalidOid;
722+
/* Update primary datanode if needed */
723+
if (OidIsValid(new_primary))
724+
primary_data_node = new_primary;
701725
/* Release lock at Commit */
702726
heap_close(rel, NoLock);
703727
}
@@ -715,6 +739,7 @@ PgxcNodeRemove(DropNodeStmt *stmt)
715739
HeapTuple tup;
716740
const char *node_name = stmt->node_name;
717741
Oid noid = get_pgxc_nodeoid(node_name);
742+
bool is_primary;
718743

719744
/* Only a DB administrator can remove cluster nodes */
720745
if (!superuser())
@@ -747,6 +772,7 @@ PgxcNodeRemove(DropNodeStmt *stmt)
747772
/* Delete the pgxc_node tuple */
748773
relation = heap_open(PgxcNodeRelationId, RowExclusiveLock);
749774
tup = SearchSysCache1(PGXCNODEOID, ObjectIdGetDatum(noid));
775+
is_primary = is_pgxc_nodeprimary(noid);
750776
if (!HeapTupleIsValid(tup)) /* should not happen */
751777
ereport(ERROR,
752778
(errcode(ERRCODE_UNDEFINED_OBJECT),
@@ -757,5 +783,7 @@ PgxcNodeRemove(DropNodeStmt *stmt)
757783

758784
ReleaseSysCache(tup);
759785

786+
if (is_primary)
787+
primary_data_node = InvalidOid;
760788
heap_close(relation, RowExclusiveLock);
761789
}

0 commit comments

Comments
 (0)