Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d46fe6e

Browse files
committed
Added SQL for handling moved partition on non-src and non-dst nodes.
Now moving partition done.
1 parent bb6e282 commit d46fe6e

File tree

2 files changed

+35
-14
lines changed

2 files changed

+35
-14
lines changed

bin/common.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ master_port=5432
1313

1414
# declare -a worker_datadirs=()
1515
# declare -a worker_ports=()
16+
1617
# declare -a worker_datadirs=("${HOME}/postgres/data2")
1718
# declare -a worker_ports=("5433")
19+
1820
declare -a worker_datadirs=("${HOME}/postgres/data2" "${HOME}/postgres/data3")
1921
declare -a worker_ports=("5433" "5434")
2022

23+
# declare -a worker_datadirs=("${HOME}/postgres/data2" "${HOME}/postgres/data3" "${HOME}/postgres/data4")
24+
# declare -a worker_ports=("5433" "5434" "5435")
25+
2126
#------------------------------------------------------------
2227
PATH="$PATH:${pgpath}bin/"
2328
function start_nodes()
@@ -57,6 +62,5 @@ function run_demo()
5762
psql -p 5433 -c "INSERT INTO partitioned_table SELECT generate_series(1, 1000), random();"
5863
psql -c "select shardman.add_node('port=5433');"
5964
psql -c "select shardman.add_node('port=5434');"
60-
sleep 5
6165
psql -c "select shardman.create_hash_partitions(2, 'partitioned_table', 'id', 2);"
6266
}

pg_shardman--0.0.1.sql

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,15 @@ BEGIN
112112
RETURN format('%s_fdw', part_name);
113113
END $$ LANGUAGE plpgsql STRICT;
114114

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.
116116
CREATE FUNCTION reset_foreign_server_opts(srvname name) RETURNS void AS $$
117117
DECLARE
118118
opts text[];
119119
opt text;
120120
opt_key text;
121121
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$,
124124
srvname) INTO opts;
125125
FOREACH opt IN ARRAY opts LOOP
126126
opt_key := regexp_replace(substring(opt from '^.*?='), '=$', '');
@@ -135,9 +135,9 @@ DECLARE
135135
opt text;
136136
opt_key text;
137137
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
141141
ums.umuser = umuser$q$, srvname)
142142
INTO opts;
143143

@@ -157,12 +157,27 @@ END $$ LANGUAGE plpgsql STRICT;
157157
CREATE FUNCTION update_fdw_server(part partitions) RETURNS void AS $$
158158
DECLARE
159159
connstring text;
160+
server_opts text;
161+
um_opts text;
160162
BEGIN
161163
-- ALTER FOREIGN TABLE doesn't support changing server, ALTER SERVER doesn't
162164
-- support dropping all params, and I don't want to recreate foreign table
163165
-- each time server params change, so resorting to these hacks.
164166
PERFORM shardman.reset_foreign_server_opts(part.part_name);
165167
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;
166181
END $$ LANGUAGE plpgsql STRICT;
167182

168183
-- Replace existing hash partition with foreign, assuming 'partition' shows
@@ -179,13 +194,14 @@ BEGIN
179194

180195
SELECT nodes.connstring FROM shardman.nodes WHERE id = part.owner
181196
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;
184199

185200
EXECUTE format('CREATE SERVER %I FOREIGN DATA WRAPPER
186201
postgres_fdw %s;', part.part_name, server_opts);
187202
EXECUTE format('DROP USER MAPPING IF EXISTS FOR CURRENT_USER SERVER %I;',
188203
part.part_name);
204+
-- TODO: support not only CURRENT_USER
189205
EXECUTE format('CREATE USER MAPPING FOR CURRENT_USER SERVER %I
190206
%s;', part.part_name, um_opts);
191207
SELECT shardman.get_fdw_part_name(part.part_name) INTO fdw_part_name;
@@ -270,7 +286,7 @@ BEGIN
270286
PERFORM shardman.replace_foreign_part_with_usual(NEW);
271287
ELSE -- other nodes
272288
-- just update foreign server
273-
PERFORM shardman.update_fdw_server(part);
289+
PERFORM shardman.update_fdw_server(NEW);
274290
END IF;
275291
RETURN NULL;
276292
END
@@ -564,11 +580,12 @@ CREATE FUNCTION reconstruct_table_attrs(relation regclass)
564580
-- (which is neccessary here) doesn't seem to have handy C API. I resorted to
565581
-- have C function which parses the opts and returns them in two parallel
566582
-- 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.
567584
-- Returns two strings: one with opts ready to pass to CREATE FOREIGN SERVER
568585
-- stmt, and one wih opts ready to pass to CREATE USER MAPPING.
569586
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 $$
572589
DECLARE
573590
connstring_keywords text[];
574591
connstring_vals text[];
@@ -589,14 +606,14 @@ BEGIN
589606
um_opts := um_opts || ', ';
590607
END IF;
591608
um_opts_first_time_through := false;
592-
um_opts := um_opts ||
609+
um_opts := prfx || um_opts ||
593610
format('%s %L', connstring_keywords[i], connstring_vals[i]);
594611
ELSE -- server option
595612
IF NOT server_opts_first_time_through THEN
596613
server_opts := server_opts || ', ';
597614
END IF;
598615
server_opts_first_time_through := false;
599-
server_opts := server_opts ||
616+
server_opts := prfx || server_opts ||
600617
format('%s %L', connstring_keywords[i], connstring_vals[i]);
601618
END IF;
602619
END LOOP;

0 commit comments

Comments
 (0)