-- -- MULTI_CREATE_TABLE -- ALTER SEQUENCE pg_catalog.pg_dist_shardid_seq RESTART 360000; ALTER SEQUENCE pg_catalog.pg_dist_jobid_seq RESTART 360000; -- Create new table definitions for use in testing in distributed planning and -- execution functionality. Also create indexes to boost performance. CREATE TABLE lineitem ( l_orderkey bigint not null, l_partkey integer not null, l_suppkey integer not null, l_linenumber integer not null, l_quantity decimal(15, 2) not null, l_extendedprice decimal(15, 2) not null, l_discount decimal(15, 2) not null, l_tax decimal(15, 2) not null, l_returnflag char(1) not null, l_linestatus char(1) not null, l_shipdate date not null, l_commitdate date not null, l_receiptdate date not null, l_shipinstruct char(25) not null, l_shipmode char(10) not null, l_comment varchar(44) not null, PRIMARY KEY(l_orderkey, l_linenumber) ); SELECT master_create_distributed_table('lineitem', 'l_orderkey', 'append'); WARNING: table "lineitem" has a UNIQUE or EXCLUDE constraint DETAIL: UNIQUE constraints, EXCLUDE constraints, and PRIMARY KEYs on append-partitioned tables cannot be enforced. HINT: Consider using hash partitioning. master_create_distributed_table --------------------------------- (1 row) CREATE INDEX lineitem_time_index ON lineitem (l_shipdate); NOTICE: using one-phase commit for distributed DDL commands HINT: You can enable two-phase commit for extra safety with: SET citus.multi_shard_commit_protocol TO '2pc' CREATE TABLE orders ( o_orderkey bigint not null, o_custkey integer not null, o_orderstatus char(1) not null, o_totalprice decimal(15,2) not null, o_orderdate date not null, o_orderpriority char(15) not null, o_clerk char(15) not null, o_shippriority integer not null, o_comment varchar(79) not null, PRIMARY KEY(o_orderkey) ); SELECT master_create_distributed_table('orders', 'o_orderkey', 'append'); WARNING: table "orders" has a UNIQUE or EXCLUDE constraint DETAIL: UNIQUE constraints, EXCLUDE constraints, and PRIMARY KEYs on append-partitioned tables cannot be enforced. HINT: Consider using hash partitioning. master_create_distributed_table --------------------------------- (1 row) CREATE TABLE customer ( c_custkey integer not null, c_name varchar(25) not null, c_address varchar(40) not null, c_nationkey integer not null, c_phone char(15) not null, c_acctbal decimal(15,2) not null, c_mktsegment char(10) not null, c_comment varchar(117) not null); SELECT master_create_distributed_table('customer', 'c_custkey', 'append'); master_create_distributed_table --------------------------------- (1 row) CREATE TABLE nation ( n_nationkey integer not null, n_name char(25) not null, n_regionkey integer not null, n_comment varchar(152)); SELECT create_reference_table('nation'); create_reference_table ------------------------ (1 row) CREATE TABLE part ( p_partkey integer not null, p_name varchar(55) not null, p_mfgr char(25) not null, p_brand char(10) not null, p_type varchar(25) not null, p_size integer not null, p_container char(10) not null, p_retailprice decimal(15,2) not null, p_comment varchar(23) not null); SELECT master_create_distributed_table('part', 'p_partkey', 'append'); master_create_distributed_table --------------------------------- (1 row) CREATE TABLE supplier ( s_suppkey integer not null, s_name char(25) not null, s_address varchar(40) not null, s_nationkey integer, s_phone char(15) not null, s_acctbal decimal(15,2) not null, s_comment varchar(101) not null ); SELECT create_reference_table('supplier'); create_reference_table ------------------------ (1 row) -- create a single shard supplier table which is not -- a reference table CREATE TABLE supplier_single_shard ( s_suppkey integer not null, s_name char(25) not null, s_address varchar(40) not null, s_nationkey integer, s_phone char(15) not null, s_acctbal decimal(15,2) not null, s_comment varchar(101) not null ); SELECT master_create_distributed_table('supplier_single_shard', 's_suppkey', 'append'); master_create_distributed_table --------------------------------- (1 row) CREATE TABLE mx_table_test (col1 int, col2 text); -- Since we're superuser, we can set the replication model to 'streaming' to -- create a one-off MX table... but if we forget to set the replication factor to one, -- we should see an error reminding us to fix that SET citus.replication_model TO 'streaming'; SELECT create_distributed_table('mx_table_test', 'col1'); ERROR: replication factors above one are incompatible with the streaming replication model HINT: Try again after reducing "citus.shard_replication_factor" to one or setting "citus.replication_model" to "statement". -- ok, so now actually create the one-off MX table SET citus.shard_replication_factor TO 1; SELECT create_distributed_table('mx_table_test', 'col1'); create_distributed_table -------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='mx_table_test'::regclass; repmodel ---------- s (1 row) DROP TABLE mx_table_test; -- Show that master_create_distributed_table ignores citus.replication_model GUC CREATE TABLE s_table(a int); SELECT master_create_distributed_table('s_table', 'a', 'hash'); NOTICE: using statement-based replication DETAIL: The current replication_model setting is 'streaming', which is not supported by master_create_distributed_table. HINT: Use create_distributed_table to use the streaming replication model. master_create_distributed_table --------------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='s_table'::regclass; repmodel ---------- c (1 row) -- Show that master_create_worker_shards complains when RF>1 and replication model is streaming UPDATE pg_dist_partition SET repmodel = 's' WHERE logicalrelid='s_table'::regclass; SELECT master_create_worker_shards('s_table', 4, 2); ERROR: using replication factor 2 with the streaming replication model is not supported DETAIL: The table s_table is marked as streaming replicated and the shard replication factor of streaming replicated tables must be 1. HINT: Use replication factor 1. DROP TABLE s_table; RESET citus.replication_model; -- Show that create_distributed_table with append and range distributions ignore -- citus.replication_model GUC SET citus.shard_replication_factor TO 2; SET citus.replication_model TO streaming; CREATE TABLE repmodel_test (a int); SELECT create_distributed_table('repmodel_test', 'a', 'append'); NOTICE: using statement-based replication DETAIL: Streaming replication is supported only for hash-distributed tables. create_distributed_table -------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; CREATE TABLE repmodel_test (a int); SELECT create_distributed_table('repmodel_test', 'a', 'range'); NOTICE: using statement-based replication DETAIL: Streaming replication is supported only for hash-distributed tables. create_distributed_table -------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; -- Show that master_create_distributed_table created statement replicated tables no matter -- what citus.replication_model set to CREATE TABLE repmodel_test (a int); SELECT master_create_distributed_table('repmodel_test', 'a', 'hash'); NOTICE: using statement-based replication DETAIL: The current replication_model setting is 'streaming', which is not supported by master_create_distributed_table. HINT: Use create_distributed_table to use the streaming replication model. master_create_distributed_table --------------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; CREATE TABLE repmodel_test (a int); SELECT master_create_distributed_table('repmodel_test', 'a', 'append'); NOTICE: using statement-based replication DETAIL: The current replication_model setting is 'streaming', which is not supported by master_create_distributed_table. HINT: Use create_distributed_table to use the streaming replication model. master_create_distributed_table --------------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; CREATE TABLE repmodel_test (a int); SELECT master_create_distributed_table('repmodel_test', 'a', 'range'); NOTICE: using statement-based replication DETAIL: The current replication_model setting is 'streaming', which is not supported by master_create_distributed_table. HINT: Use create_distributed_table to use the streaming replication model. master_create_distributed_table --------------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; -- Check that the replication_model overwrite behavior is the same with RF=1 SET citus.shard_replication_factor TO 1; CREATE TABLE repmodel_test (a int); SELECT create_distributed_table('repmodel_test', 'a', 'append'); NOTICE: using statement-based replication DETAIL: Streaming replication is supported only for hash-distributed tables. create_distributed_table -------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; CREATE TABLE repmodel_test (a int); SELECT create_distributed_table('repmodel_test', 'a', 'range'); NOTICE: using statement-based replication DETAIL: Streaming replication is supported only for hash-distributed tables. create_distributed_table -------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; CREATE TABLE repmodel_test (a int); SELECT master_create_distributed_table('repmodel_test', 'a', 'hash'); NOTICE: using statement-based replication DETAIL: The current replication_model setting is 'streaming', which is not supported by master_create_distributed_table. HINT: Use create_distributed_table to use the streaming replication model. master_create_distributed_table --------------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; CREATE TABLE repmodel_test (a int); SELECT master_create_distributed_table('repmodel_test', 'a', 'append'); NOTICE: using statement-based replication DETAIL: The current replication_model setting is 'streaming', which is not supported by master_create_distributed_table. HINT: Use create_distributed_table to use the streaming replication model. master_create_distributed_table --------------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; CREATE TABLE repmodel_test (a int); SELECT master_create_distributed_table('repmodel_test', 'a', 'range'); NOTICE: using statement-based replication DETAIL: The current replication_model setting is 'streaming', which is not supported by master_create_distributed_table. HINT: Use create_distributed_table to use the streaming replication model. master_create_distributed_table --------------------------------- (1 row) SELECT repmodel FROM pg_dist_partition WHERE logicalrelid='repmodel_test'::regclass; repmodel ---------- c (1 row) DROP TABLE repmodel_test; RESET citus.replication_model; -- Test initial data loading CREATE TABLE data_load_test (col1 int, col2 text, col3 serial); INSERT INTO data_load_test VALUES (132, 'hello'); INSERT INTO data_load_test VALUES (243, 'world'); -- table must be empty when using append- or range-partitioning SELECT create_distributed_table('data_load_test', 'col1', 'append'); ERROR: cannot distribute relation "data_load_test" DETAIL: Relation "data_load_test" contains data. HINT: Empty your table before distributing it. SELECT create_distributed_table('data_load_test', 'col1', 'range'); ERROR: cannot distribute relation "data_load_test" DETAIL: Relation "data_load_test" contains data. HINT: Empty your table before distributing it. -- table must be empty when using master_create_distributed_table (no shards created) SELECT master_create_distributed_table('data_load_test', 'col1', 'hash'); ERROR: cannot distribute relation "data_load_test" DETAIL: Relation "data_load_test" contains data. HINT: Empty your table before distributing it. -- create_distributed_table creates shards and copies data into the distributed table SELECT create_distributed_table('data_load_test', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) SELECT * FROM data_load_test ORDER BY col1; col1 | col2 | col3 ------+-------+------ 132 | hello | 1 243 | world | 2 (2 rows) DROP TABLE data_load_test; -- ensure writes in the same transaction as create_distributed_table are visible BEGIN; CREATE TABLE data_load_test (col1 int, col2 text, col3 serial); INSERT INTO data_load_test VALUES (132, 'hello'); SELECT create_distributed_table('data_load_test', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) INSERT INTO data_load_test VALUES (243, 'world'); END; SELECT * FROM data_load_test ORDER BY col1; col1 | col2 | col3 ------+-------+------ 132 | hello | 1 243 | world | 2 (2 rows) DROP TABLE data_load_test; -- creating co-located distributed tables in the same transaction works BEGIN; CREATE TABLE data_load_test1 (col1 int, col2 text, col3 serial); INSERT INTO data_load_test1 VALUES (132, 'hello'); SELECT create_distributed_table('data_load_test1', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) CREATE TABLE data_load_test2 (col1 int, col2 text, col3 serial); INSERT INTO data_load_test2 VALUES (132, 'world'); SELECT create_distributed_table('data_load_test2', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) SELECT a.col2 ||' '|| b.col2 FROM data_load_test1 a JOIN data_load_test2 b USING (col1) WHERE col1 = 132; ?column? ------------- hello world (1 row) DROP TABLE data_load_test1, data_load_test2; END; -- creating an index after loading data works BEGIN; CREATE TABLE data_load_test (col1 int, col2 text, col3 serial); INSERT INTO data_load_test VALUES (132, 'hello'); SELECT create_distributed_table('data_load_test', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) CREATE INDEX data_load_test_idx ON data_load_test (col2); END; DROP TABLE data_load_test; -- popping in and out of existence in the same transaction works BEGIN; CREATE TABLE data_load_test (col1 int, col2 text, col3 serial); INSERT INTO data_load_test VALUES (132, 'hello'); SELECT create_distributed_table('data_load_test', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) DROP TABLE data_load_test; END; -- but dropping after a write on the distributed table is currently disallowed BEGIN; CREATE TABLE data_load_test (col1 int, col2 text, col3 serial); INSERT INTO data_load_test VALUES (132, 'hello'); SELECT create_distributed_table('data_load_test', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) INSERT INTO data_load_test VALUES (243, 'world'); DROP TABLE data_load_test; ERROR: shard drop operations must not appear in transaction blocks containing other distributed modifications CONTEXT: SQL statement "SELECT master_drop_all_shards(v_obj.objid, v_obj.schema_name, v_obj.object_name)" PL/pgSQL function citus_drop_trigger() line 21 at PERFORM END; -- Test data loading after dropping a column CREATE TABLE data_load_test (col1 int, col2 text, col3 text); INSERT INTO data_load_test VALUES (132, 'hello', 'world'); INSERT INTO data_load_test VALUES (243, 'world', 'hello'); ALTER TABLE data_load_test DROP COLUMN col2; SELECT create_distributed_table('data_load_test', 'col1'); NOTICE: Copying data from local table... create_distributed_table -------------------------- (1 row) SELECT * FROM data_load_test; col1 | col3 ------+------- 132 | world 243 | hello (2 rows) DROP TABLE data_load_test; SET citus.shard_replication_factor TO default; SET citus.shard_count to 4; CREATE TABLE lineitem_hash_part (like lineitem); SELECT create_distributed_table('lineitem_hash_part', 'l_orderkey'); create_distributed_table -------------------------- (1 row) CREATE TABLE orders_hash_part (like orders); SELECT create_distributed_table('orders_hash_part', 'o_orderkey'); create_distributed_table -------------------------- (1 row)