CREATE SCHEMA worker_copy_table_to_node;
SET search_path TO worker_copy_table_to_node;
SET citus.shard_count TO 1; -- single shard table for ease of testing
SET citus.shard_replication_factor TO 1;
SET citus.next_shard_id TO 62629600;
SELECT nodeid AS worker_1_node FROM pg_dist_node WHERE nodeport=:worker_1_port \gset
SELECT nodeid AS worker_2_node FROM pg_dist_node WHERE nodeport=:worker_2_port \gset
CREATE TABLE t(a int);
INSERT INTO t SELECT generate_series(1, 100);
CREATE TABLE ref(a int);
INSERT INTO ref SELECT generate_series(1, 100);
select create_distributed_table('t', 'a');
NOTICE:  Copying data from local table...
NOTICE:  copying the data has completed
DETAIL:  The local data in the table is no longer visible, but is still on disk.
HINT:  To remove the local data, run: SELECT truncate_local_data_after_distributing_table($$worker_copy_table_to_node.t$$)
 create_distributed_table
---------------------------------------------------------------------

(1 row)

select create_reference_table('ref');
NOTICE:  Copying data from local table...
NOTICE:  copying the data has completed
DETAIL:  The local data in the table is no longer visible, but is still on disk.
HINT:  To remove the local data, run: SELECT truncate_local_data_after_distributing_table($$worker_copy_table_to_node.ref$$)
 create_reference_table
---------------------------------------------------------------------

(1 row)

\c - - - :worker_2_port
SET search_path TO worker_copy_table_to_node;
-- Create empty shard on worker 2 too
CREATE TABLE t_62629600(a int);
\c - - - :worker_1_port
SET search_path TO worker_copy_table_to_node;
-- Make sure that the UDF doesn't work on Citus tables
SELECT worker_copy_table_to_node('t', :worker_1_node);
ERROR:  table worker_copy_table_to_node.t is a Citus table, only copies of shards or regular postgres tables are supported
SELECT worker_copy_table_to_node('ref', :worker_1_node);
ERROR:  table worker_copy_table_to_node.ref is a Citus table, only copies of shards or regular postgres tables are supported
-- It should work on shards
SELECT worker_copy_table_to_node('t_62629600', :worker_1_node);
 worker_copy_table_to_node
---------------------------------------------------------------------

(1 row)

SELECT count(*) FROM t;
 count
---------------------------------------------------------------------
   200
(1 row)

SELECT count(*) FROM t_62629600;
 count
---------------------------------------------------------------------
   200
(1 row)

SELECT worker_copy_table_to_node('t_62629600', :worker_2_node);
 worker_copy_table_to_node
---------------------------------------------------------------------

(1 row)

\c - - - :worker_2_port
SET search_path TO worker_copy_table_to_node;
SELECT count(*) FROM t_62629600;
 count
---------------------------------------------------------------------
   200
(1 row)

\c - - - :master_port
SET search_path TO worker_copy_table_to_node;
SET client_min_messages TO WARNING;
DROP SCHEMA worker_copy_table_to_node CASCADE;
