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

Commit 07ff8f9

Browse files
committed
Merge branch 'broadcast' of https://git.postgrespro.ru/a.sher/pg_shardman into broadcast
2 parents e7cfaa5 + c350eaf commit 07ff8f9

13 files changed

+284
-178
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
*.o
22
*.so
33
.dir-locals.el
4-
pg_shardman*.sql
54
bin/setup.sh
65

76
*.pyc

Makefile

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,44 @@
11
# the extension name
22
EXTENSION = pg_shardman
3-
EXTVERSION = 1.0
3+
EXTVERSION = 0.0.2
44
# This file will be executed by CREATE EXTENSION, so let pgxs install it.
55
DATA = $(EXTENSION)--$(EXTVERSION).sql
66

7+
REGRESS = shardman_installation
8+
79
MODULE_big = pg_shardman
810
OBJS = pg_shardman.o
911
PGFILEDESC = "pg_shardman - sharding for Postgres"
1012

11-
ifdef USE_SOURCETREE # assume the extension is in contrib/ dir of pg distribution
12-
PG_CPPFLAGS = -I$(libpq_srcdir)
13-
SHLIB_LINK = $(libpq)
14-
SHLIB_PREREQS = submake-libpq
15-
subdir = contrib/pg_shardman
16-
top_builddir = ../..
17-
include $(top_builddir)/src/Makefile.global
18-
include $(top_srcdir)/contrib/contrib-global.mk
19-
else # use pgxs
13+
mkfile_path := $(abspath $(lastword $(MAKEFILE_LIST))) # abs path to this makefile
14+
mkfile_dir := $(shell basename $(dir $(mkfile_path))) # parent dir of the project
15+
ifndef USE_PGXS # hmm, user didn't requested to use pgxs
16+
ifneq ($(mkfile_dir),contrib) # a-ha, but we are not inside 'contrib' dir
17+
USE_PGXS := 1 # so use it anyway, most probably that's use user wants
18+
endif
19+
endif
20+
21+
ifdef USE_PGXS # use pgxs
2022
# You can specify path to pg_config in PG_CONFIG var
2123
ifndef PG_CONFIG
2224
PG_CONFIG := pg_config
2325
endif
2426
PG_CONFIG = pg_config
25-
2627
INCLUDEDIR := $(shell $(PG_CONFIG) --includedir)
2728
PG_CPPFLAGS += -I$(INCLUDEDIR) # add server's include directory for libpq-fe.h
2829
SHLIB_LINK += -lpq # add libpq
29-
3030
PGXS := $(shell $(PG_CONFIG) --pgxs)
3131
include $(PGXS)
32+
33+
else # assume the extension is in contrib/ dir of pg distribution
34+
PG_CPPFLAGS = -I$(libpq_srcdir) # include libpq-fe, defined in Makefile.global.in
35+
SHLIB_LINK = $(libpq) # defined in Makefile.global.in
36+
SHLIB_PREREQS = submake-libpq
37+
subdir = contrib/pg_shardman
38+
top_builddir = ../..
39+
include $(top_builddir)/src/Makefile.global
40+
include $(top_srcdir)/contrib/contrib-global.mk
3241
endif
42+
43+
python_tests:
44+
$(MAKE) -C tests/python

expected/shardman_installation.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
\set VERBOSITY terse
2+
CREATE EXTENSION pg_shardman CASCADE;
3+
NOTICE: installing required extension "pg_pathman"
4+
NOTICE: installing required extension "postgres_fdw"
5+
DROP EXTENSION pg_shardman CASCADE;

pg_shardman--1.0.sql renamed to pg_shardman--0.0.2.sql

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ CREATE TABLE replicas (
5959
-- * It allows to set up pgbouncer, as replication can't go through it.
6060
-- If conn_string is null, super_conn_string is used everywhere.
6161
CREATE FUNCTION add_node(super_conn_string text, conn_string text = NULL,
62-
repl_group text = 'default') RETURNS void AS $$
62+
repl_group text = 'default') RETURNS int AS $$
6363
DECLARE
6464
new_node_id int;
6565
system_id bigint;
@@ -90,11 +90,14 @@ DECLARE
9090
master_node_id int;
9191
sys_id bigint;
9292
conn_string_effective text = COALESCE(conn_string, super_conn_string);
93+
redirected bool;
9394
BEGIN
94-
IF shardman.redirect_to_shardlord(
95+
SELECT * FROM shardman.redirect_to_shardlord_with_res(
9596
format('add_node(%L, %L, %L)', super_conn_string, conn_string, repl_group))
97+
INTO new_node_id, redirected;
98+
IF redirected
9699
THEN
97-
RETURN;
100+
RETURN new_node_id;
98101
END IF;
99102

100103
-- Insert new node in nodes table
@@ -237,6 +240,8 @@ BEGIN
237240
PERFORM shardman.broadcast(create_rules);
238241
-- Broadcast create subscriptions for shared tables
239242
PERFORM shardman.broadcast(subs, super_connstr => true);
243+
244+
RETURN new_node_id;
240245
END
241246
$$ LANGUAGE plpgsql;
242247

@@ -353,7 +358,7 @@ BEGIN
353358
PERFORM shardman.broadcast(conf, ignore_errors:=true, super_connstr => true);
354359
END IF;
355360
/* To correctly remove foreign servers we need to update pf_depend table, otherwise
356-
* our hack with direct update pg_foreign_table leaves deteriorated dependencies
361+
* our hack with direct update pg_foreign_table leaves deteriorated dependencies
357362
-- Remove foreign servers at all nodes for the removed node
358363
FOR node IN SELECT * FROM shardman.nodes WHERE id<>rm_node_id
359364
LOOP
@@ -467,6 +472,10 @@ BEGIN
467472
RAISE EXCEPTION 'Table % is already sharded', rel_name;
468473
END IF;
469474

475+
IF (SELECT count(*) FROM shardman.nodes) = 0 THEN
476+
RAISE EXCEPTION 'Please add some nodes first';
477+
END IF;
478+
470479
-- Generate SQL statement creating this table
471480
SELECT shardman.gen_create_table_sql(rel_name) INTO create_table;
472481

@@ -1005,7 +1014,7 @@ $$ LANGUAGE plpgsql;
10051014

10061015

10071016
-- Move replica to other node. This function is able to move replica only within replication group.
1008-
-- It initiates copying data to new replica, disables logical replication to original replica,
1017+
-- It initiates copying data to new replica, disables logical replication to original replica,
10091018
-- waits completion of initial table sync and then removes old replica.
10101019
CREATE FUNCTION mv_replica(mv_part_name text, src_node_id int, dst_node_id int)
10111020
RETURNS void AS $$
@@ -1243,7 +1252,7 @@ BEGIN
12431252
PERFORM shardman.broadcast(format('%s:CREATE FOREIGN TABLE %I %s SERVER %s OPTIONS (table_name %L);',
12441253
src_node.id, fdw_part_name, table_attrs, srv_name, part.part_name));
12451254
ELSIF shardman.not_exists(src_node.id, format('pg_class c,pg_foreign_table f,pg_foreign_server s WHERE c.oid=f.ftrelid AND c.relname=%L AND f.ftserver=s.oid AND s.srvname = %L', fdw_part_name, srv_name))
1246-
THEN
1255+
THEN
12471256
RAISE NOTICE 'Bind foreign table % to server % at node %', fdw_part_name, srv_name, src_node.id;
12481257
PERFORM shardman.broadcast(format('%s:UPDATE pg_foreign_table SET ftserver = (SELECT oid FROM pg_foreign_server WHERE srvname = %L) WHERE ftrelid = (SELECT oid FROM pg_class WHERE relname=%L);',
12491258
src_node.id, srv_name, fdw_part_name));
@@ -1550,17 +1559,26 @@ END
15501559
$$ LANGUAGE plpgsql;
15511560

15521561
-- Execute command at shardlord
1553-
CREATE FUNCTION redirect_to_shardlord(cmd text) RETURNS bool AS $$
1562+
CREATE FUNCTION redirect_to_shardlord_with_res(cmd text, out res int,
1563+
out redirected bool) AS $$
15541564
BEGIN
15551565
IF NOT shardman.is_shardlord() THEN
15561566
RAISE NOTICE 'Redirect command "%" to shardlord',cmd;
1557-
PERFORM shardman.broadcast(format('0:SELECT shardman.%s;', cmd));
1558-
RETURN true;
1567+
res = (shardman.broadcast(format('0:SELECT shardman.%s;', cmd)))::int;
1568+
redirected = true;
15591569
ELSE
1560-
RETURN false;
1570+
redirected = false;
15611571
END IF;
15621572
END
15631573
$$ LANGUAGE plpgsql;
1574+
-- same, but don't care for the result -- to avoid changing all calls to
1575+
-- redirect_to_shardlord to '.redirected'
1576+
CREATE FUNCTION redirect_to_shardlord(cmd text) RETURNS bool AS $$
1577+
DECLARE
1578+
BEGIN
1579+
RETURN redirected FROM shardman.redirect_to_shardlord_with_res(cmd);
1580+
END
1581+
$$ LANGUAGE plpgsql;
15641582

15651583

15661584
-- Generate based on information from catalog SQL statement creating this table

pg_shardman.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ wait_command_completion(PGconn* conn)
104104
int wc = WaitLatchOrSocket(MyLatch,
105105
WL_LATCH_SET | WL_SOCKET_READABLE,
106106
PQsocket(conn),
107+
#if defined (PGPRO_EE)
108+
false,
109+
#endif
107110
-1L, PG_WAIT_EXTENSION);
108111
ResetLatch(MyLatch);
109112

pg_shardman.control

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
comment = 'Postgresql sharding via pg_pathman, postgres_fdw, LR and others'
22
# CREATE EXTENSION will run extname--version.sql file
3-
default_version = '1.0'
3+
default_version = '0.0.2'
44
# TODO: make ext relocatable at least during installation. For that, we need to
55
# * learn the scheme when connecting from another node
66
# * add @extschema@ to search path where needed, e.g. in all trigger funcs

postgresql.conf.common

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
shared_preload_libraries = 'pg_pathman'
22

3+
shardman.shardlord_connstring = 'port=5432' # shardlord's connstring
4+
35
# a bit usual performance-related settings
46
shared_buffers = 512MB
57
effective_cache_size = 512MB

postgresql.conf.worker

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ synchronous_commit = on
2222
# for 2pc
2323
max_prepared_transactions = 1000
2424
postgres_fdw.use_2pc = on
25-
# only for testing performace; setting this to 'on' violates visibilitys
25+
# only for testing performace; setting this to 'on' violates visibility
2626
postgres_fdw.use_repeatable_read = off
2727

28-
shardman.shardlord_connstring = 'port=5432' # shardlord's connstring
2928
# This node is shardlord?
3029
shardman.shardlord = off

readme.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ Below goes full list of commands.
321321
### Membership
322322

323323
```plpgsql
324-
add_node(super_conn_string text, conn_string text = NULL, repl_group text = 'default')
324+
add_node(super_conn_string text, conn_string text = NULL, repl_group text = 'default') returns int
325325
```
326326
Add node with given libpq connstring(s) to the cluster. Node is assigned unique
327327
id. If node previously contained shardman state from old cluster (not one
@@ -343,6 +343,8 @@ We don't move any parts and replicas to newly added node, see `rebalance_*`
343343
commands for that below. However, freshly added node instantly becomes aware
344344
of sharded tables and can accept queries to them.
345345

346+
Returns id of the new node.
347+
346348
```plpgsql
347349
get_my_id()
348350
```

run_shardlord.py

Lines changed: 0 additions & 151 deletions
This file was deleted.

sql/shardman_installation.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\set VERBOSITY terse
2+
3+
CREATE EXTENSION pg_shardman CASCADE;
4+
DROP EXTENSION pg_shardman CASCADE;

tests/python/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
all:
2+
python3 -m unittest tests.py -v -f

0 commit comments

Comments
 (0)