@@ -112,15 +112,15 @@ BEGIN
112
112
RETURN format(' %s_fdw' , part_name);
113
113
END $$ LANGUAGE plpgsql STRICT;
114
114
115
- -- Drop all foreign server's option . Yes, I don't know simpler ways.
115
+ -- Drop all foreign server's options . Yes, I don't know simpler ways.
116
116
CREATE FUNCTION reset_foreign_server_opts (srvname name) RETURNS void AS $$
117
117
DECLARE
118
118
opts text [];
119
119
opt text ;
120
120
opt_key text ;
121
121
BEGIN
122
- EXECUTE format($q$select coalesce(srvoptions, ' {}' ::text [] from
123
- pg_foreign_server where srvname = %L$q$,
122
+ EXECUTE format($q$select coalesce(srvoptions, ' {}' ::text []) FROM
123
+ pg_foreign_server WHERE srvname = %L$q$,
124
124
srvname) INTO opts;
125
125
FOREACH opt IN ARRAY opts LOOP
126
126
opt_key := regexp_replace(substring (opt from ' ^.*?=' ), ' =$' , ' ' );
@@ -135,9 +135,9 @@ DECLARE
135
135
opt text ;
136
136
opt_key text ;
137
137
BEGIN
138
- EXECUTE format($q$select coalesce(umoptions, ' {}' ::text []) from
139
- pg_user_mapping ums join pg_foreign_server fs
140
- on fs .oid = ums .umserver where fs .srvname = %L and
138
+ EXECUTE format($q$select coalesce(umoptions, ' {}' ::text []) FROM
139
+ pg_user_mapping ums JOIN pg_foreign_server fs
140
+ ON fs .oid = ums .umserver WHERE fs .srvname = %L AND
141
141
ums .umuser = umuser$q$, srvname)
142
142
INTO opts;
143
143
@@ -157,12 +157,27 @@ END $$ LANGUAGE plpgsql STRICT;
157
157
CREATE FUNCTION update_fdw_server (part partitions) RETURNS void AS $$
158
158
DECLARE
159
159
connstring text ;
160
+ server_opts text ;
161
+ um_opts text ;
160
162
BEGIN
161
163
-- ALTER FOREIGN TABLE doesn't support changing server, ALTER SERVER doesn't
162
164
-- support dropping all params, and I don't want to recreate foreign table
163
165
-- each time server params change, so resorting to these hacks.
164
166
PERFORM shardman .reset_foreign_server_opts (part .part_name );
165
167
PERFORM shardman .reset_um_opts (part .part_name , current_user ::regrole);
168
+
169
+ SELECT nodes .connstring FROM shardman .nodes WHERE id = part .owner
170
+ INTO connstring;
171
+ SELECT * FROM shardman .conninfo_to_postgres_fdw_opts (connstring, ' ADD ' )
172
+ INTO server_opts, um_opts;
173
+
174
+ IF server_opts != ' ' THEN
175
+ EXECUTE format(' ALTER SERVER %I %s' , part .part_name , server_opts);
176
+ END IF;
177
+ IF um_opts != ' ' THEN
178
+ EXECUTE format(' ALTER USER MAPPING FOR CURRENT_USER SERVER %I %s' ,
179
+ part .part_name , um_opts);
180
+ END IF;
166
181
END $$ LANGUAGE plpgsql STRICT;
167
182
168
183
-- Replace existing hash partition with foreign, assuming 'partition' shows
@@ -179,13 +194,14 @@ BEGIN
179
194
180
195
SELECT nodes .connstring FROM shardman .nodes WHERE id = part .owner
181
196
INTO connstring;
182
- SELECT * INTO server_opts, um_opts FROM
183
- ( SELECT * FROM shardman . conninfo_to_postgres_fdw_opts (connstring)) opts ;
197
+ SELECT * FROM shardman . conninfo_to_postgres_fdw_opts (connstring)
198
+ INTO server_opts, um_opts ;
184
199
185
200
EXECUTE format(' CREATE SERVER %I FOREIGN DATA WRAPPER
186
201
postgres_fdw %s;' , part .part_name , server_opts);
187
202
EXECUTE format(' DROP USER MAPPING IF EXISTS FOR CURRENT_USER SERVER %I;' ,
188
203
part .part_name );
204
+ -- TODO: support not only CURRENT_USER
189
205
EXECUTE format(' CREATE USER MAPPING FOR CURRENT_USER SERVER %I
190
206
%s;' , part .part_name , um_opts);
191
207
SELECT shardman .get_fdw_part_name (part .part_name ) INTO fdw_part_name;
@@ -270,7 +286,7 @@ BEGIN
270
286
PERFORM shardman .replace_foreign_part_with_usual (NEW);
271
287
ELSE -- other nodes
272
288
-- just update foreign server
273
- PERFORM shardman .update_fdw_server (part );
289
+ PERFORM shardman .update_fdw_server (NEW );
274
290
END IF;
275
291
RETURN NULL ;
276
292
END
@@ -564,11 +580,12 @@ CREATE FUNCTION reconstruct_table_attrs(relation regclass)
564
580
-- (which is neccessary here) doesn't seem to have handy C API. I resorted to
565
581
-- have C function which parses the opts and returns them in two parallel
566
582
-- arrays, and this sql function joins them with quoting.
583
+ -- prfx is prefix added before opt name, e.g. 'ADD ' for use in ALTER SERVER.
567
584
-- Returns two strings: one with opts ready to pass to CREATE FOREIGN SERVER
568
585
-- stmt, and one wih opts ready to pass to CREATE USER MAPPING.
569
586
CREATE FUNCTION conninfo_to_postgres_fdw_opts (
570
- IN connstring text , OUT server_opts text , OUT um_opts text )
571
- RETURNS record AS $$
587
+ IN connstring text , IN prfx text DEFAULT ' ' ,
588
+ OUT server_opts text , OUT um_opts text ) RETURNS record AS $$
572
589
DECLARE
573
590
connstring_keywords text [];
574
591
connstring_vals text [];
@@ -589,14 +606,14 @@ BEGIN
589
606
um_opts := um_opts || ' , ' ;
590
607
END IF;
591
608
um_opts_first_time_through := false;
592
- um_opts := um_opts ||
609
+ um_opts := prfx || um_opts ||
593
610
format(' %s %L' , connstring_keywords[i], connstring_vals[i]);
594
611
ELSE -- server option
595
612
IF NOT server_opts_first_time_through THEN
596
613
server_opts := server_opts || ' , ' ;
597
614
END IF;
598
615
server_opts_first_time_through := false;
599
- server_opts := server_opts ||
616
+ server_opts := prfx || server_opts ||
600
617
format(' %s %L' , connstring_keywords[i], connstring_vals[i]);
601
618
END IF;
602
619
END LOOP;
0 commit comments