@@ -105,6 +105,7 @@ DECLARE
105
105
master_node_id int ;
106
106
sys_id bigint ;
107
107
conn_string_effective text = COALESCE(conn_string, super_conn_string);
108
+ conn_string_effective_user text ;
108
109
BEGIN
109
110
IF NOT shardman .is_shardlord ()
110
111
THEN
@@ -113,18 +114,19 @@ BEGIN
113
114
super_conn_string, conn_string, repl_group))::int ;
114
115
END IF;
115
116
116
- -- Insert new node in nodes table
117
+ -- Insert new node in nodes table.
118
+ -- We have to update system_id (and repl_group, for which we probably need
119
+ -- system_id) only after insert, because broadcast fetches connstring from
120
+ -- shardman.nodes itself.
117
121
INSERT INTO shardman .nodes (system_id, super_connection_string,
118
122
connection_string, replication_group)
119
123
VALUES (0 , super_conn_string, conn_string_effective, ' ' )
120
124
RETURNING id INTO new_node_id;
121
125
122
- -- We have to update system_id along with dependant repl_group after insert,
123
- -- because otherwise broadcast will not work.
124
126
sys_id := shardman .broadcast (
125
127
format(' %s:SELECT shardman.get_system_identifier();' ,
126
- new_node_id))::bigint ;
127
- IF EXISTS(SELECT 1 FROM shardman .nodes where system_id = sys_id) THEN
128
+ new_node_id), super_connstr => true )::bigint ;
129
+ IF EXISTS(SELECT 1 FROM shardman .nodes WHERE system_id = sys_id) THEN
128
130
RAISE EXCEPTION ' Node with system id % is already in the cluster' , sys_id;
129
131
END IF;
130
132
UPDATE shardman .nodes SET system_id = sys_id WHERE id = new_node_id;
@@ -133,6 +135,19 @@ BEGIN
133
135
(CASE WHEN repl_group IS NULL THEN sys_id::text ELSE repl_group END)
134
136
WHERE id = new_node_id;
135
137
138
+ -- If conn_string is provided, make sure effective user has permissions on
139
+ -- shardman schema and postgres_fdw.
140
+ IF conn_string IS NOT NULL THEN
141
+ conn_string_effective_user := shardman .broadcast (
142
+ format(' %s:SELECT current_user;' , new_node_id),
143
+ super_connstr => false);
144
+ PERFORM shardman .broadcast (
145
+ format(' {%s:GRANT USAGE ON FOREIGN DATA WRAPPER postgres_fdw TO %s;
146
+ GRANT USAGE ON SCHEMA shardman TO %s;}' ,
147
+ new_node_id, conn_string_effective_user, conn_string_effective_user),
148
+ super_connstr => true);
149
+ END IF;
150
+
136
151
-- Adjust replication channels within replication group.
137
152
-- We need all-to-all replication channels between all group members.
138
153
FOR node IN SELECT * FROM shardman .nodes WHERE replication_group = repl_group
0 commit comments