Postgres2015全国用户大会将于11月20至21日在北京丽亭华苑酒店召开。本次大会嘉宾阵容强大,国内顶级PostgreSQL数据库专家将悉数到场,并特邀欧洲、俄罗斯、日本、美国等国家和地区的数据库方面专家助阵:
# yum install -y gmp mpfr libmpc libmpc-devel
# git clone https://github.com/citusdata/pg_shard.git# cd pg_shard/
# git checkout mastercommit 7e6103f79e3651eac0b32429f5fb103eb2a8ebddMerge: 2b221d9 ac35076Author: Jason Petersen Date: Fri Aug 28 19:12:16 2015 -0600
Merge branch 'release-1.2.2'......
# . /home/postgres/.bash_profile# make clean; make; make install
su - postgrescd $PGDATA
postgres@digoal-> vi pg_worker_list.conflocalhost 1922localhost 1923localhost 1924localhost 1925
postgres@digoal-> cat $PGDATA1/pg_hba.conf |grep ^locallocal all all trust
postgres@digoal-> cat $PGDATA2/pg_hba.conf |grep ^locallocal all all trustpostgres@digoal-> cat $PGDATA3/pg_hba.conf |grep ^locallocal all all trustpostgres@digoal-> cat $PGDATA4/pg_hba.conf |grep ^locallocal all all trust
vi $PGDATA/postgresql.confshared_preload_libraries = 'pg_shard'pg_ctl restart -m fastpsql
roledatabaseschema
postgres=# create extension pg_shard;
postgres=# CREATE TABLE customer_reviews(customer_id TEXT NOT NULL,review_date DATE,review_rating INTEGER,review_votes INTEGER,review_helpful_votes INTEGER,product_id CHAR(10),product_title TEXT,product_sales_rank BIGINT,product_group TEXT,product_category TEXT,product_subcategory TEXT,similar_product_ids CHAR(10)[]);
postgres=# SELECT master_create_distributed_table('customer_reviews', 'customer_id');
postgres=# SELECT master_create_worker_shards('customer_reviews', 16, 2);
postgres=# \dt pgs_distribution_metadata.*List of relationsSchema | Name | Type | Owner---------------------------+-----------------+-------+----------pgs_distribution_metadata | partition | table | postgrespgs_distribution_metadata | shard | table | postgrespgs_distribution_metadata | shard_placement | table | postgres(3 rows)
postgres=# select * from pgs_distribution_metadata.partition ;relation_id | partition_method | key-------------+------------------+-------------42067 | h | customer_id(1 row)postgres=# select 42067::regclass;regclass------------------customer_reviews(1 row)
postgres=# select * from pgs_distribution_metadata.shard;id | relation_id | storage | min_value | max_value-------+-------------+---------+-------------+-------------10000 | 42067 | t | -2147483648 | -187904819410001 | 42067 | t | -1879048193 | -161061273910002 | 42067 | t | -1610612738 | -134217728410003 | 42067 | t | -1342177283 | -107374182910004 | 42067 | t | -1073741828 | -80530637410005 | 42067 | t | -805306373 | -53687091910006 | 42067 | t | -536870918 | -26843546410007 | 42067 | t | -268435463 | -910008 | 42067 | t | -8 | 26843544610009 | 42067 | t | 268435447 | 53687090110010 | 42067 | t | 536870902 | 80530635610011 | 42067 | t | 805306357 | 107374181110012 | 42067 | t | 1073741812 | 134217726610013 | 42067 | t | 1342177267 | 161061272110014 | 42067 | t | 1610612722 | 187904817610015 | 42067 | t | 1879048177 | 2147483647(16 rows)
postgres=# select * from pgs_distribution_metadata.shard_placement;id | shard_id | shard_state | node_name | node_port----+----------+-------------+-----------+-----------1 | 10000 | 1 | localhost | 19222 | 10000 | 1 | localhost | 19233 | 10001 | 1 | localhost | 19234 | 10001 | 1 | localhost | 19245 | 10002 | 1 | localhost | 19246 | 10002 | 1 | localhost | 19257 | 10003 | 1 | localhost | 19258 | 10003 | 1 | localhost | 19229 | 10004 | 1 | localhost | 192210 | 10004 | 1 | localhost | 192311 | 10005 | 1 | localhost | 192312 | 10005 | 1 | localhost | 192413 | 10006 | 1 | localhost | 192414 | 10006 | 1 | localhost | 192515 | 10007 | 1 | localhost | 192516 | 10007 | 1 | localhost | 192217 | 10008 | 1 | localhost | 192218 | 10008 | 1 | localhost | 192319 | 10009 | 1 | localhost | 192320 | 10009 | 1 | localhost | 192421 | 10010 | 1 | localhost | 192422 | 10010 | 1 | localhost | 192523 | 10011 | 1 | localhost | 192524 | 10011 | 1 | localhost | 192225 | 10012 | 1 | localhost | 192226 | 10012 | 1 | localhost | 192327 | 10013 | 1 | localhost | 192328 | 10013 | 1 | localhost | 192429 | 10014 | 1 | localhost | 192430 | 10014 | 1 | localhost | 192531 | 10015 | 1 | localhost | 192532 | 10015 | 1 | localhost | 1922(32 rows)
postgres=# insert into customer_reviews select generate_series(1,100);ERROR: 0A000: cannot perform distributed planning for the given queryDETAIL: Subqueries are not supported in distributed queries.LOCATION: ErrorIfQueryNotSupported, pg_shard.c:567
postgres=# insert into customer_reviews values ('a',now());ERROR: 0A000: cannot plan sharded modification containing values which are not constants or constant expressionsLOCATION: ErrorIfQueryNotSupported, pg_shard.c:638
postgres=# prepare a (text) as insert into customer_reviews values ($1);ERROR: 0A000: PREPARE commands on distributed tables are unsupportedLOCATION: PgShardProcessUtility, pg_shard.c:2098
postgres@digoal-> vi test.sql\setrandom id 1 1000000insert into customer_reviews values (:id);
postgres@digoal-> pgbench -M extended -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 1000Client 2 aborted in state 1: ERROR: unrecognized node type: 2100Client 4 aborted in state 1: ERROR: unrecognized node type: 2100Client 1 aborted in state 1: ERROR: unrecognized node type: 2100Client 7 aborted in state 1: ERROR: unrecognized node type: 2100Client 3 aborted in state 1: ERROR: unrecognized node type: 2100Client 0 aborted in state 1: ERROR: unrecognized node type: 2100Client 5 aborted in state 1: ERROR: unrecognized node type: 2100Client 6 aborted in state 1: ERROR: unrecognized node type: 2100transaction type: Custom queryscaling factor: 1query mode: extendednumber of clients: 8number of threads: 8duration: 1000 snumber of transactions actually processed: 0postgres@digoal-> pgbench -M simple -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 1000progress: 2.8 s, 0.7 tps, lat 2568.361 ms stddev 16.254progress: 3.2 s, 68.3 tps, lat 578.633 ms stddev 1043.300progress: 3.2 s, 264.5 tps, lat 8.263 ms stddev 3.007progress: 4.0 s, 1193.6 tps, lat 8.561 ms stddev 36.589progress: 5.0 s, 1255.6 tps, lat 6.376 ms stddev 5.437progress: 6.0 s, 1277.5 tps, lat 6.263 ms stddev 2.644
postgres@digoal-> pg_ctl stop -m fast -D /data01/pg_root_1922waiting for server to shut down.... doneserver stoppedpostgres@digoal-> psqlpsql (9.4.4)Type "help" for help.postgres=# select count(*) from customer_reviews ;WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?count-------6296(1 row)
postgres=# select * from pgs_distribution_metadata.shard_placement;id | shard_id | shard_state | node_name | node_port----+----------+-------------+-----------+-----------2 | 10000 | 1 | localhost | 19233 | 10001 | 1 | localhost | 19234 | 10001 | 1 | localhost | 19245 | 10002 | 1 | localhost | 19246 | 10002 | 1 | localhost | 19257 | 10003 | 1 | localhost | 192510 | 10004 | 1 | localhost | 192311 | 10005 | 1 | localhost | 192312 | 10005 | 1 | localhost | 192413 | 10006 | 1 | localhost | 192414 | 10006 | 1 | localhost | 192515 | 10007 | 1 | localhost | 192518 | 10008 | 1 | localhost | 192319 | 10009 | 1 | localhost | 192320 | 10009 | 1 | localhost | 192421 | 10010 | 1 | localhost | 192422 | 10010 | 1 | localhost | 192523 | 10011 | 1 | localhost | 192526 | 10012 | 1 | localhost | 192327 | 10013 | 1 | localhost | 192328 | 10013 | 1 | localhost | 192429 | 10014 | 1 | localhost | 192430 | 10014 | 1 | localhost | 192531 | 10015 | 1 | localhost | 19258 | 10003 | 3 | localhost | 192232 | 10015 | 3 | localhost | 19229 | 10004 | 3 | localhost | 192225 | 10012 | 3 | localhost | 192224 | 10011 | 3 | localhost | 192217 | 10008 | 3 | localhost | 19221 | 10000 | 3 | localhost | 192216 | 10007 | 3 | localhost | 1922(32 rows)
postgres@digoal-> pgbench -M simple -n -r -P 1 -f ./test.sql -c 8 -j 8 -T 1000WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?WARNING: Connection failed to localhost:1922DETAIL: Remote message: could not connect to server: Connection refusedIs the server running on host "localhost" (::1) and acceptingTCP/IP connections on port 1922?could not connect to server: Connection refusedIs the server running on host "localhost" (127.0.0.1) and acceptingTCP/IP connections on port 1922?progress: 1.0 s, 167.9 tps, lat 24.011 ms stddev 84.151progress: 2.0 s, 1306.0 tps, lat 8.279 ms stddev 40.771progress: 3.0 s, 1451.7 tps, lat 5.505 ms stddev 1.778progress: 4.0 s, 1469.8 tps, lat 5.445 ms stddev 1.529progress: 5.0 s, 1447.0 tps, lat 5.519 ms stddev 2.082progress: 6.0 s, 1439.4 tps, lat 5.554 ms stddev 2.656
postgres@digoal-> pg_ctl start -D /data01/pg_root_1922server startingpostgres@digoal-> 0LOG: 00000: redirecting log output to logging collector process0HINT: Future log output will appear in directory "pg_log".0LOCATION: SysLogger_Start, syslogger.c:645
postgres@digoal-> psqlpsql (9.4.4)Type "help" for help.postgres=# select count(*) from customer_reviews ;count-------17729(1 row)
psql -h 127.0.0.1 -p 1922 -c "create extension pg_shard;"psql -h 127.0.0.1 -p 1923 -c "create extension pg_shard;"psql -h 127.0.0.1 -p 1924 -c "create extension pg_shard;"psql -h 127.0.0.1 -p 1925 -c "create extension pg_shard;"
postgres=# select t1.shard_id,t1.node_name,t1.node_port,t2.node_name,t2.node_port from pgs_distribution_metadata.shard_placement t1 , pgs_distribution_metadata.shard_placement t2 where t1.shard_id=t2.shard_id and (t1.node_name||t1.node_port) <> (t2.node_name||t2.node_port) and t1.shard_state=3;shard_id | node_name | node_port | node_name | node_port----------+-----------+-----------+-----------+-----------10003 | localhost | 1922 | localhost | 192510004 | localhost | 1922 | localhost | 192310008 | localhost | 1922 | localhost | 192310011 | localhost | 1922 | localhost | 192510012 | localhost | 1922 | localhost | 192310015 | localhost | 1922 | localhost | 1925(6 rows)
public | master_copy_shard_placement | void | shard_id bigint, source_node_name text, source_node_port integer, target_node_name text, target_node_port integer
postgres=# select master_copy_shard_placement(t1.shard_id,t1.node_name,t1.node_port,t2.node_name,t2.node_port) from pgs_distribution_metadata.shard_placement t1 , pgs_distribution_metadata.shard_placement t2 where t1.shard_id=t2.shard_id and (t1.node_name||t1.node_port) <> (t2.node_name||t2.node_port) and t1.shard_state=3;ERROR: 22023: source placement must be in finalized stateLOCATION: master_copy_shard_placement, repair_shards.c:109
postgres=# select master_copy_shard_placement(t1.shard_id,t2.node_name,t2.node_port,t1.node_name,t1.node_port) from pgs_distribution_metadata.shard_placement t1 , pgs_distribution_metadata.shard_placement t2 where t1.shard_id=t2.shard_id and (t1.node_name||t1.node_port) <> (t2.node_name||t2.node_port) and t1.shard_state=3;master_copy_shard_placement-----------------------------(8 rows)
postgres=# select * from pgs_distribution_metadata.shard_placement;id | shard_id | shard_state | node_name | node_port----+----------+-------------+-----------+-----------2 | 10000 | 1 | localhost | 19233 | 10001 | 1 | localhost | 19234 | 10001 | 1 | localhost | 19245 | 10002 | 1 | localhost | 19246 | 10002 | 1 | localhost | 19257 | 10003 | 1 | localhost | 192510 | 10004 | 1 | localhost | 192311 | 10005 | 1 | localhost | 192312 | 10005 | 1 | localhost | 192413 | 10006 | 1 | localhost | 192414 | 10006 | 1 | localhost | 192515 | 10007 | 1 | localhost | 192518 | 10008 | 1 | localhost | 192319 | 10009 | 1 | localhost | 192320 | 10009 | 1 | localhost | 192421 | 10010 | 1 | localhost | 192422 | 10010 | 1 | localhost | 192523 | 10011 | 1 | localhost | 192526 | 10012 | 1 | localhost | 192327 | 10013 | 1 | localhost | 192328 | 10013 | 1 | localhost | 192429 | 10014 | 1 | localhost | 192430 | 10014 | 1 | localhost | 192531 | 10015 | 1 | localhost | 19251 | 10000 | 1 | localhost | 192216 | 10007 | 1 | localhost | 19228 | 10003 | 1 | localhost | 19229 | 10004 | 1 | localhost | 192217 | 10008 | 1 | localhost | 192224 | 10011 | 1 | localhost | 192225 | 10012 | 1 | localhost | 192232 | 10015 | 1 | localhost | 1922(32 rows)
postgres=# select count(*) from customer_reviews;count-------17729(1 row)
pg_shard is intentionally limited in scope during its first release, but is fully functional within that scope. We classify pg_shard's current limitations into two groups. In one group, we have features that we don't intend to support in the medium term due to architectural decisions we made:
Another group of limitations are shorter-term but we're calling them out here to be clear about unsupported features:
Besides these limitations, we have a list of features that we're looking to add. Instead of prioritizing this list ourselves, we decided to keep an open discussion on GitHub issues and hear what you have to say. So, if you have a favorite feature missing from pg_shard, please do get in touch!