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

Commit cd6d152

Browse files
committed
Fix mistyping in comments
1 parent e13f7f1 commit cd6d152

File tree

3 files changed

+94
-80
lines changed

3 files changed

+94
-80
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ EXTENSION = pg_shardman
66
DATA = pg_shardman--1.0.sql
77

88
MODULE_big = pg_shardman
9-
OBJS = src/pg_shardman.o
9+
OBJS = pg_shardman.o
1010
PGFILEDESC = "pg_shardman - sharding for Postgres"
1111

1212
PG_CPPFLAGS = -I$(libpq_srcdir)

pg_shardman--1.0.sql

Lines changed: 82 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* ------------------------------------------------------------------------
22
*
3-
* shardman.sql
4-
* Commands infrastructure, interface funcs, common utility funcs.
3+
* pg_shardman.sql
4+
* Sharding and replication using pg_pathman, postres_fdw and logical replication
55
*
66
* Copyright (c) 2017, Postgres Professional
77
*
@@ -16,7 +16,7 @@
1616
-- list of nodes present in the cluster
1717
CREATE TABLE nodes (
1818
id serial PRIMARY KEY,
19-
conn_string text UNIQUE NOT NULL,
19+
connection_string text UNIQUE NOT NULL,
2020
replication_group text NOT NULL -- group of nodes within which shard replicas are allocated
2121
);
2222

@@ -39,11 +39,10 @@ CREATE TABLE replicas (
3939

4040

4141
-- Add a node: adjust logical replication channels in replication group and create foreign servers
42-
CREATE FUNCTION add_node(conn_string text, repl_group text) RETURNS void AS $$
42+
CREATE FUNCTION add_node(conn_string text, repl_group text DEFAULT 'all') RETURNS void AS $$
4343
DECLARE
44-
new_node_id integer;
45-
node nodes;
46-
shardlord_connstr text;
44+
new_node_id int;
45+
node nodes;
4746
pubs text := '';
4847
subs text := '';
4948
sync text := '';
@@ -55,12 +54,13 @@ DECLARE
5554
new_server_opts text;
5655
new_um_opts text;
5756
sync_standbys text;
58-
sync_repl boolean := shardman.synchronous_replication();
57+
shardlord_conn_string text;
58+
sync_repl bool := shardman.synchronous_replication();
5959
BEGIN
6060
-- Insert new node in nodes table
61-
INSERT INTO shardman.nodes (conn_string,replication_group) values (conn_string, repl_group) returning id into new_node_id;
61+
INSERT INTO shardman.nodes (connection_string,replication_group) VALUES (conn_string, repl_group) RETURNING id INTO new_node_id;
6262

63-
-- Construct list of sychronous standbyes (subscriptions have name node_$ID)
63+
-- Construct list of synchronous standbyes (subscriptions have name node_$ID)
6464
SELECT string_agg('node_'||id, ',') INTO sync_standbys from nodes;
6565

6666
-- Construct foreign server options from connection string of new node
@@ -72,54 +72,55 @@ BEGIN
7272
CREATE PUBLICATION shardman_meta_pub FOR TABLE shardman.nodes,shardman.partitions,shardman.replicas;
7373
END IF;
7474

75-
-- Adjust replication channels within repplication group.
76-
-- We need all-to-all replication channels, so add
75+
-- Adjust replication channels within replication group.
76+
-- We need all-to-all replication channels between all group members.
7777
FOR node IN SELECT * FROM shardman.nodes WHERE replication_group=repl_group AND id<>new_node_id
7878
LOOP
7979
-- Add to new node publications for all existed nodes and add publication for new node to all existed nodes
8080
pubs := format('%s%s:CREATE PUBLICATION node_%s;
8181
%s:CREATE PUBLICATION node_%s;',
82-
pubs, node.id, new_node_id,
83-
new_node_id, node.id);
84-
-- Add to new node subscriptions for to existed nodes and add subsciption to new node to all existed nodes
82+
pubs, node.id, new_node_id,
83+
new_node_id, node.id);
84+
-- Add to new node subscriptions to existed nodes and add subscription to new node to all existed nodes
8585
subs := format('%s%s:CREATE SUBSCRIPTION node_%s CONNECTION %L PUBLICATION node_%s with (synchronous_commit = local);
8686
%s:CREATE SUBSCRIPTION node_%s CONNECTION %L PUBLICATION node_%s with (synchronous_commit = local);',
8787
subs, node.id, new_node_id, conn_string, node.id,
88-
new_node_id, node.id, node.conn_string, new_node_id);
88+
new_node_id, node.id, node.connection_string, new_node_id);
8989

9090
-- Adjust synchronous standby list at all nodes
9191
sync := format('%s%s:ALTER SYSTEM SET synchronous_standby_names to %L;',
92-
sync, node.id, sync_standbys);
92+
sync, node.id, sync_standbys);
9393
conf := format('%s%s:SELECT pg_reload_conf();',
94-
conf, node.id);
94+
conf, node.id);
9595
END LOOP;
9696

9797
-- Create subscription to shardlord for metadata
98-
SELECT shardman.shardlord_connection_string() INTO shardlord_connstr;
98+
SELECT shardman.shardlord_connection_string() INTO shardlord_conn_string;
9999
subs := format('%s%s:CREATE SUBSCRIPTION shardman_meta_sub CONNECTION %L PUBLICATION shardman_meta_pub with (synchronous_commit = local);',
100100
subs, new_node_id, shardlord_conn_string);
101101

102-
-- Broadcasr create publication commands
102+
-- Broadcast create publication commands
103103
PERFORM shardman.broadcast(pubs);
104-
-- Broadcasr create subscription commands
104+
-- Broadcast create subscription commands
105105
PERFORM shardman.broadcast(subs);
106106

107-
-- In case of synchronous replication broadcasr commands updating synchronous standby list
107+
-- In case of synchronous replication broadcast update synchronous standby list commands
108108
IF sync_repl THEN
109109
-- Adjust synchronous standby list at new nodes
110110
sync := format('%s%s:ALTER SYSTEM SET synchronous_standby_names to %L;',
111-
sync, new_node_id, sync_stanbys);
111+
sync, new_node_id, sync_stanbys);
112+
-- Reload configuration at new node
112113
conf := format('%s%s:SELECT pg_reload_conf();',
113-
conf, new_node_id);
114-
PERFORM shardman.broadcast(sync);
115-
PERFORM shardman.broadcast(conf);
114+
conf, new_node_id);
115+
PERFORM shardman.broadcast(sync);
116+
PERFORM shardman.broadcast(conf);
116117
END IF;
117118

118-
-- Add foriegn servers for connection with new node and backward
119+
-- Add foreign servers for connection to the new node and backward
119120
FOR node IN SELECT * FROM shardman.nodes WHERE id<>new_node_id
120121
LOOP
121122
-- Construct foreign server options from connection string of this node
122-
SELECT * FROM shardman.conninfo_to_postgres_fdw_opts(node.conn_string) INTO server_opts, um_opts;
123+
SELECT * FROM shardman.conninfo_to_postgres_fdw_opts(node.connection_string) INTO server_opts, um_opts;
123124

124125
-- Create foreign server for new node at all other nodes and servers at new node for all other nodes
125126
fdws := format('%s%s:CREATE SERVER node_%s FOREIGN DATA WRAPPER postgres_fdw %s;
@@ -134,7 +135,7 @@ BEGIN
134135
node.id, new_node_id, new_um_ops);
135136
END LOOP;
136137

137-
-- Broadcast command for creating foriegn servers
138+
-- Broadcast command for creating foreign servers
138139
PERFORM shardman.broadcast(fdws);
139140
-- Broadcast command for creating user mapping for this servers
140141
PERFORM shardman.broadcast(usms);
@@ -144,23 +145,23 @@ $$ LANGUAGE plpgsql;
144145

145146
-- Remove node: try to choose alternative from one of replicas of this nodes, exclude node from replication channels and remove foreign servers.
146147
-- To remove node with existed partitions use force=true parameter
147-
CREATE FUNCTION rm_node(rm_node_id int, force boolean DEFAULT false) RETURNS void AS $$
148+
CREATE FUNCTION rm_node(rm_node_id int, force bool DEFAULT false) RETURNS void AS $$
148149
DECLARE
149150
node nodes;
150151
part partitions;
151152
pubs text := '';
152153
subs text := '';
153154
fdws text := '';
154155
fdw_part_name text;
155-
new_master_id integer;
156+
new_master_id int;
156157
sync_standbys text;
157-
sync_repl boolean := shardman.synchronous_replication();
158+
sync_repl bool := shardman.synchronous_replication();
158159
BEGIN
159-
-- If it is not forced remove, check if there are no parittions at this node
160+
-- If it is not forced remove, check if there are no partitions at this node
160161
IF NOT force THEN
161162
IF EXISTS (SELECT * FROM shardman.partitions WHERE node=rm_node_id)
162163
THEN
163-
RAISE ERROR 'Use forse=true to remove non-empty node';
164+
RAISE ERROR 'Use force=true to remove non-empty node';
164165
END IF;
165166
END IF;
166167

@@ -194,8 +195,10 @@ BEGIN
194195
-- In case of synchronous replication update synchronous standbys list
195196
IF sync_repl
196197
THEN
198+
-- Update synchronous standbys list at the removed node
199+
sync := format('%s%s:SELECT shardman_set_sync_standbys(%L);',
200+
sync, rm_node_id, sync_standbys);
197201
PERFORM shardman.broadcast(sync, ignore_errors:=true);
198-
PERFORM shardman.broadcast(fdws, ignore_errors:=true);
199202
END IF;
200203

201204
-- Remove foreign servers at all nodes for the removed node
@@ -212,10 +215,10 @@ BEGIN
212215
PERFORM shardman.broadcast(fdws, ignore_errors:=true);
213216
fdws := '';
214217

215-
-- Exclude parittions of removed node
218+
-- Exclude partitions of removed node
216219
FOR part in SELECT * from shardman.partitions where node=rm_node_id
217220
LOOP
218-
-- Is there some replica of thi node
221+
-- Is there some replica of this node?
219222
SELECT node INTO new_master_id FROM shardman.replicas WHERE part_name=part.part_name ORDER BY random() LIMIT 1;
220223
IF new_master_id IS NOT NULL
221224
THEN -- exists some replica for this node: redirect foreign table to this replica and refresh LR channels for this replication group
@@ -232,7 +235,7 @@ BEGIN
232235
-- Publish this partition at new master
233236
pubs := format('%s%s:ALTER PUBLICATION node_%s ADD TABLE %I;',
234237
pubs, new_master_id, repl.node, part.part_name);
235-
-- And refresg subscriptions and replicas
238+
-- And refresh subscriptions and replicas
236239
subs := format('%s%s:ALTER SUBSCRIPTION node_%s REFRESH PUBLICATION',
237240
subs, repl.node, new_master_id);
238241
END LOOP;
@@ -251,12 +254,12 @@ BEGIN
251254
LOOP
252255
fdw_part_name := format('%s_fdw', part.part_name);
253256
IF node.id=new_master_id THEN
254-
-- At new master node replace foreign link with local paritition
257+
-- At new master node replace foreign link with local partition
255258
fdws := format('%s%d:SELECT replace_hash_partition(%L,%L);',
256259
fdws, node.id, fdw_part_name, part.part_name);
257260
ELSE
258261
-- At all other nodes adjust foreign server for foreign table to refer to new master node.
259-
-- It is noty possible to alter foreign server for foreign table so we have to do it in such "hackers" way:
262+
-- It is not possible to alter foreign server for foreign table so we have to do it in such "hackers" way:
260263
fdws := format('%s%s:UPDATE pg_foreign_table SET ftserver = (SELECT oid FROM pg_foreign_server WHERE srvname = ''node_%s'') WHERE ftrelid = (SELECT oid FROM pg_class WHERE relname=%L);',
261264
fdws, node.id, new_master_id, fdw_part_name);
262265
END IF;
@@ -281,8 +284,8 @@ RETURNS void AS $$
281284
DECLARE
282285
create_table text;
283286
node nodes;
284-
node_ids integer[];
285-
node_id integer;
287+
node_ids int[];
288+
node_id int;
286289
part_name text;
287290
fdw_part_name text;
288291
table_attrs text;
@@ -291,9 +294,8 @@ DECLARE
291294
create_partitions text := '';
292295
create_fdws text := '';
293296
replace_parts text := '';
294-
i integer;
295-
-- Do not drop partitions replaced with foreign tables, because them can be used for replicas
296-
-- drop_parts text := '';
297+
i int;
298+
n_nodes int;
297299
BEGIN
298300
IF EXISTS(SELECT relation from shardman.tables where relation = rel_name)
299301
THEN
@@ -307,62 +309,60 @@ BEGIN
307309
-- Create parent table at all nodes
308310
create_tables := format('%s%s:%s;',
309311
create_tables, node.id, create_table);
310-
-- Create parittions using pathman at all nodes
311-
create_partitions := format('%s%s:select create_hash_partitions(%L,%L,%s);',
312+
-- Create partitions using pathman at all nodes
313+
create_partitions := format('%s%s:select create_hash_partitions(%L,%L,%L);',
312314
create_partitions, node.id, rel_name, expr, partitions_count);
313315
END LOOP;
314316

315-
-- Broadcase create table commands
317+
-- Broadcast create table commands
316318
PERFORM shardman.broadcast(create_tables);
317-
-- Broadcase create hash partitions command
319+
-- Broadcast create hash partitions command
318320
PERFORM shardman.broadcast(create_partitions);
319321

320322
-- Get list of nodes in random order
321323
SELECT ARRAY(SELECT id from shardman.nodes ORDER BY random()) INTO node_ids;
324+
n_nodes := array_length(node_ids, 1);
322325

323-
-- Reconstruct table attribures from parent table
326+
-- Reconstruct table attributes from parent table
324327
SELECT shardman.reconstruct_table_attrs(rel_nam) INTO table_attr;
325328

326329
FOR i IN 1..partitions_count
327330
LOOP
328331
-- Choose location of new partition
329-
node_id := node_ids[1 + (i % partitions_count)]; -- round robin
332+
node_id := node_ids[1 + (i % n_nodes)]; -- round robin
330333
part_name := format('%s_%s', rel_name, i);
331334
fdw_part_name := format('%s_fdw', part_name);
332335
-- Insert information about new partition in partitions table
333336
INSERT INTO shardman.partitions (part_name,node,relation) VALUES (part_name, node_id, rel_name);
334-
-- Construct name of the servers where paritition will be located
337+
-- Construct name of the servers where partition will be located
335338
srv_name := format('node_%s', node_id);
336339

337-
-- Replace local partition with foreign table at all nodes excepot owner
340+
-- Replace local partition with foreign table at all nodes except owner
338341
FOR node IN SELECT * from shardman.nodes WHERE id<>node_id
339342
LOOP
340-
-- Create foriegn table for this partition
343+
-- Create foreign table for this partition
341344
create_fdw := format('%s%s:CREATE FOREIGN TABLE %I %s SERVER %s OPTIONS (table_name %L)',
342345
create_fdws, node.id, fdw_part_name, table_attrs, srv_name, part_name);
343346
replace_parts := format('%s%s:SELECT replace_hash_partition(%L, %L);',
344-
replace_parts, node.id, part_name, fdw_part_name);
345-
--drop_parts := format('%s%s:DROP TABKE %I;',
346-
-- drop_parts, node.id, part_name);
347+
replace_parts, node.id, part_name, fdw_part_name);
347348
END LOOP;
348349
END LOOP;
349350

350351
-- Broadcast create foreign table commands
351352
PERFORM shardman.broadcast(create_fdws);
352-
-- Broadcasr replace hash parition commands
353+
-- Broadcast replace hash partition commands
353354
PERFORM shardman.broadcast(replace_parts);
354-
-- PERFORM shardman.broadcast(drop_parts);
355355
END
356356
$$ LANGUAGE plpgsql;
357357

358358
-- Provide requested level of redundancy. 0 means no redundancy.
359-
-- If existed level of redundancy os greater than specified, then right now this function does nothing.
360-
CREATE FUNCTION set_redundancy(rel_name regclass, redundancy integer)
359+
-- If existed level of redundancy is greater than specified, then right now this function does nothing.
360+
CREATE FUNCTION set_redundancy(rel_name regclass, redundancy int)
361361
RETURNS void AS $$
362362
DECLARE
363363
part partitions;
364-
n_replicas integer;
365-
repl_node integer;
364+
n_replicas int;
365+
repl_node int;
366366
repl_group text;
367367
pubs text := '';
368368
subs text := '';
@@ -375,7 +375,7 @@ BEGIN
375375
IF n_replicas < redundancy
376376
THEN -- If it is smaller than requested...
377377
SELECT replication_group INTO repl_group FROM shardman.nodes where id=part.node;
378-
-- ...then add requested number of replicas in correspodent replication group
378+
-- ...then add requested number of replicas in corresponding replication group
379379
FOR repl_node IN SELECT id FROM shardman.nodes WHERE replication_group=repl_group AND NOT EXISTS(SELECT * FROM shardman.replicas WHERE node=id AND part_name=part.part_name ORDER by random() LIMIT redundancy-n_replicas
380380
LOOP
381381
-- Insert information about new replica in replicas table
@@ -393,20 +393,31 @@ BEGIN
393393
PERFORM shardman.broadcast(pubs);
394394
-- Broadcast alter subscription commands
395395
PERFORM shardman.broadcast(fdws);
396+
396397
-- This function doesn't wait completion of replication sync
397398
END
398399
$$ LANGUAGE plpgsql;
399400

400401

401402
-- Utility functions
402403

404+
-- Generate based on information from catalog SQL statement creating this table
403405
CREATE FUNCTION gen_create_table_sql(relation text)
404406
RETURNS text AS 'pg_shardman' LANGUAGE C STRICT;
405407

408+
-- Reconstruct table attributes for foreign table
406409
CREATE FUNCTION reconstruct_table_attrs(relation regclass)
407410
RETURNS text AS 'pg_shardman' LANGUAGE C STRICT;
408411

409-
CREATE FUNCTION broadcast(cmds text) RETURNS text
412+
-- Broadcast SQL commands to nodes and wait their completion.
413+
-- cmds is list of SQL commands separated by by semi-columns with node prefix: node-id:sql-statement;
414+
-- By default functions throws error is execution is failed at some of the nodes, with ignore_errors=true errors are ignored
415+
-- and function returns string with "Error:" prefix containing list of errors separated by semi-columns with nodes prefixes.
416+
-- In case o normal completion this function return list with node prefixes separated by semi-columns with single result of select queries
417+
-- or number of affected rows for other commands.
418+
-- If two_phase parameter is true, then each statement is wrapped in blocked and prepared with subsequent commit or rollback of prepared transaction
419+
-- at second phase of two phase commit.
420+
CREATE FUNCTION broadcast(cmds text, ignore_errors bool DEFAULT false, two_phase bool DEFAULT false) RETURNS text
410421
RETURNS text AS 'pg_shardman' LANGUAGE C STRICT;
411422

412423
-- Options to postgres_fdw are specified in two places: user & password in user
@@ -415,12 +426,12 @@ RETURNS text AS 'pg_shardman' LANGUAGE C STRICT;
415426
-- format, i.e. we can't say create server ... options (dbname 'port=4848
416427
-- host=blabla.org'). So we have to parse the opts and pass them manually. libpq
417428
-- knows how to do it, but doesn't expose that. On the other hand, quote_literal
418-
-- (which is neccessary here) doesn't seem to have handy C API. I resorted to
429+
-- (which is necessary here) doesn't seem to have handy C API. I resorted to
419430
-- have C function which parses the opts and returns them in two parallel
420431
-- arrays, and this sql function joins them with quoting. TODO: of course,
421432
-- quote_literal_cstr exists.
422433
-- Returns two strings: one with opts ready to pass to CREATE FOREIGN SERVER
423-
-- stmt, and one wih opts ready to pass to CREATE USER MAPPING.
434+
-- stmt, and one with opts ready to pass to CREATE USER MAPPING.
424435
CREATE FUNCTION conninfo_to_postgres_fdw_opts(IN conn_string text,
425436
OUT server_opts text, OUT um_opts text) RETURNS record AS $$
426437
DECLARE
@@ -464,12 +475,15 @@ BEGIN
464475
END IF;
465476
END $$ LANGUAGE plpgsql STRICT;
466477

478+
-- Parse connection string. This function is used by conninfo_to_postgres_fdw_opts to construct postgres_fdw options list.
467479
CREATE FUNCTION pq_conninfo_parse(IN conninfo text, OUT keys text[], OUT vals text[])
468480
RETURNS record AS 'pg_shardman' LANGUAGE C STRICT;
469481

482+
-- Get shardlord connection string from configuration parameters
470483
CREATE FUNCTION shardload_connection_string()
471484
RETURNS text AS 'pg_shardman' LANGUAGE C STRICT;
472485

486+
-- Check from configuration parameters is sycnhronous replixation mode was enabled
473487
CREATE FUNCTION synchronous_replication()
474-
RETURNS boolean AS 'pg_shardman' LANGUAGE C STRICT;
488+
RETURNS bool AS 'pg_shardman' LANGUAGE C STRICT;
475489

0 commit comments

Comments
 (0)