mirror of https://github.com/citusdata/citus.git
232 lines
7.1 KiB
Plaintext
232 lines
7.1 KiB
Plaintext
--
|
|
-- MULTI_APPEND_TABLE_TO_SHARD
|
|
--
|
|
ALTER SEQUENCE pg_catalog.pg_dist_shardid_seq RESTART 230000;
|
|
ALTER SEQUENCE pg_catalog.pg_dist_jobid_seq RESTART 230000;
|
|
-- Initialize tables to join
|
|
CREATE TABLE multi_append_table_to_shard_right
|
|
(
|
|
right_number INTEGER not null,
|
|
right_text TEXT not null
|
|
);
|
|
SELECT master_create_distributed_table('multi_append_table_to_shard_right', 'right_number', 'append');
|
|
master_create_distributed_table
|
|
---------------------------------
|
|
|
|
(1 row)
|
|
|
|
CREATE TABLE multi_append_table_to_shard_left
|
|
(
|
|
left_number INTEGER not null,
|
|
left_text TEXT not null
|
|
);
|
|
SELECT master_create_distributed_table('multi_append_table_to_shard_left', 'left_number', 'append');
|
|
master_create_distributed_table
|
|
---------------------------------
|
|
|
|
(1 row)
|
|
|
|
CREATE TABLE multi_append_table_to_shard_right_hash
|
|
(
|
|
right_number INTEGER not null,
|
|
right_text TEXT not null
|
|
);
|
|
SELECT master_create_distributed_table('multi_append_table_to_shard_right_hash', 'right_number', 'hash');
|
|
master_create_distributed_table
|
|
---------------------------------
|
|
|
|
(1 row)
|
|
|
|
SELECT master_create_worker_shards('multi_append_table_to_shard_right_hash', 1, 1);
|
|
master_create_worker_shards
|
|
-----------------------------
|
|
|
|
(1 row)
|
|
|
|
-- Replicate 'left' table on both workers
|
|
SELECT set_config('citus.shard_replication_factor', '2', false);
|
|
set_config
|
|
------------
|
|
2
|
|
(1 row)
|
|
|
|
\copy multi_append_table_to_shard_left FROM '@abs_srcdir@/data/agg.data'
|
|
\copy multi_append_table_to_shard_left FROM '@abs_srcdir@/data/agg.data'
|
|
-- Place 'right' table only on the primary worker
|
|
SELECT set_config('citus.shard_replication_factor', '1', false);
|
|
set_config
|
|
------------
|
|
1
|
|
(1 row)
|
|
|
|
\copy multi_append_table_to_shard_right FROM '@abs_srcdir@/data/agg.data'
|
|
-- Reset shard replication factor to ensure tasks will be assigned to both workers
|
|
SELECT set_config('citus.shard_replication_factor', '2', false);
|
|
set_config
|
|
------------
|
|
2
|
|
(1 row)
|
|
|
|
-- All 8 rows in left table match a row in right table
|
|
SELECT COUNT(*)
|
|
FROM multi_append_table_to_shard_left,
|
|
multi_append_table_to_shard_right
|
|
WHERE left_number = right_number;
|
|
count
|
|
-------
|
|
8
|
|
(1 row)
|
|
|
|
-- Now append more data to the 'right' table
|
|
CREATE TABLE multi_append_table_to_shard_stage
|
|
(
|
|
number INTEGER not null,
|
|
text TEXT not null
|
|
);
|
|
COPY multi_append_table_to_shard_stage FROM '@abs_srcdir@/data/agg.data';
|
|
SELECT master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
|
|
FROM
|
|
pg_dist_shard
|
|
WHERE 'multi_append_table_to_shard_right'::regclass::oid = logicalrelid;
|
|
master_append_table_to_shard
|
|
------------------------------
|
|
0.0533333
|
|
(1 row)
|
|
|
|
-- Only the primary worker will see the new matches, as the secondary still uses a cached shard
|
|
SELECT COUNT(*)
|
|
FROM multi_append_table_to_shard_left,
|
|
multi_append_table_to_shard_right
|
|
WHERE left_number = right_number;
|
|
count
|
|
-------
|
|
12
|
|
(1 row)
|
|
|
|
-- Now add a lot of data to ensure we increase the size on disk
|
|
DELETE FROM multi_append_table_to_shard_stage;
|
|
COPY multi_append_table_to_shard_stage FROM '@abs_srcdir@/data/large_records.data' with delimiter '|';
|
|
SELECT master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
|
|
FROM
|
|
pg_dist_shard
|
|
WHERE 'multi_append_table_to_shard_right'::regclass::oid = logicalrelid;
|
|
master_append_table_to_shard
|
|
------------------------------
|
|
0.106667
|
|
(1 row)
|
|
|
|
-- This join will refresh the shard on the secondary, all 8 rows in the left table will match twice (16)
|
|
SELECT COUNT(*)
|
|
FROM multi_append_table_to_shard_left,
|
|
multi_append_table_to_shard_right
|
|
WHERE left_number = right_number;
|
|
count
|
|
-------
|
|
16
|
|
(1 row)
|
|
|
|
-- Check that we error out if we try to append data to a hash partitioned table.
|
|
SELECT master_create_empty_shard('multi_append_table_to_shard_right_hash');
|
|
ERROR: relation "multi_append_table_to_shard_right_hash" is a hash partitioned table
|
|
DETAIL: We currently don't support creating shards on hash-partitioned tables
|
|
SELECT master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
|
|
FROM
|
|
pg_dist_shard
|
|
WHERE 'multi_append_table_to_shard_right_hash'::regclass::oid = logicalrelid;
|
|
ERROR: cannot append to shardId 230000
|
|
DETAIL: We currently don't support appending to shards in hash-partitioned or reference tables
|
|
-- Clean up after test
|
|
SELECT master_apply_delete_command('DELETE FROM multi_append_table_to_shard_right');
|
|
master_apply_delete_command
|
|
-----------------------------
|
|
1
|
|
(1 row)
|
|
|
|
SELECT master_apply_delete_command('DELETE FROM multi_append_table_to_shard_left');
|
|
master_apply_delete_command
|
|
-----------------------------
|
|
2
|
|
(1 row)
|
|
|
|
DROP TABLE multi_append_table_to_shard_stage;
|
|
DROP TABLE multi_append_table_to_shard_right;
|
|
DROP TABLE multi_append_table_to_shard_left;
|
|
-- Check partitioning by date
|
|
CREATE TABLE multi_append_table_to_shard_date
|
|
(
|
|
event_date DATE,
|
|
value INT
|
|
);
|
|
SELECT master_create_distributed_table('multi_append_table_to_shard_date', 'event_date', 'append');
|
|
master_create_distributed_table
|
|
---------------------------------
|
|
|
|
(1 row)
|
|
|
|
-- Create an empty shard and check that we can query the table
|
|
SELECT master_create_empty_shard('multi_append_table_to_shard_date');
|
|
master_create_empty_shard
|
|
---------------------------
|
|
230004
|
|
(1 row)
|
|
|
|
SELECT * FROM multi_append_table_to_shard_date;
|
|
event_date | value
|
|
------------+-------
|
|
(0 rows)
|
|
|
|
-- Create an empty distributed table and check that we can query it
|
|
CREATE TABLE multi_append_table_to_shard_stage (LIKE multi_append_table_to_shard_date);
|
|
SELECT master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
|
|
FROM
|
|
pg_dist_shard
|
|
WHERE 'multi_append_table_to_shard_date'::regclass::oid = logicalrelid;
|
|
master_append_table_to_shard
|
|
------------------------------
|
|
0
|
|
(1 row)
|
|
|
|
SELECT * FROM multi_append_table_to_shard_date;
|
|
event_date | value
|
|
------------+-------
|
|
(0 rows)
|
|
|
|
-- INSERT NULL values and check that we can query the table
|
|
INSERT INTO multi_append_table_to_shard_stage VALUES (NULL, NULL);
|
|
SELECT master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
|
|
FROM
|
|
pg_dist_shard
|
|
WHERE 'multi_append_table_to_shard_date'::regclass::oid = logicalrelid;
|
|
master_append_table_to_shard
|
|
------------------------------
|
|
0.0266667
|
|
(1 row)
|
|
|
|
SELECT * FROM multi_append_table_to_shard_date;
|
|
event_date | value
|
|
------------+-------
|
|
|
|
|
(1 row)
|
|
|
|
-- INSERT regular values and check that we can query the table
|
|
INSERT INTO multi_append_table_to_shard_stage VALUES ('2016-01-01', 3);
|
|
SELECT master_append_table_to_shard(shardid, 'multi_append_table_to_shard_stage', 'localhost', 57636)
|
|
FROM
|
|
pg_dist_shard
|
|
WHERE 'multi_append_table_to_shard_date'::regclass::oid = logicalrelid;
|
|
master_append_table_to_shard
|
|
------------------------------
|
|
0.0266667
|
|
(1 row)
|
|
|
|
SELECT * FROM multi_append_table_to_shard_date;
|
|
event_date | value
|
|
------------+-------
|
|
|
|
|
|
|
|
01-01-2016 | 3
|
|
(3 rows)
|
|
|
|
DROP TABLE multi_append_table_to_shard_stage;
|
|
DROP TABLE multi_append_table_to_shard_date;
|