--
-- failure_insert_select_pushdown
--
-- performs failure/cancellation test for insert/select pushed down to shards.
--
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

CREATE SCHEMA insert_select_pushdown;
SET SEARCH_PATH=insert_select_pushdown;
SET citus.shard_count to 2;
SET citus.shard_replication_factor to 1;
SELECT pg_backend_pid() as pid \gset
CREATE TABLE events_table(user_id int, event_id int, event_type int);
CREATE TABLE events_summary(user_id int, event_id int, event_count int);
SELECT create_distributed_table('events_table', 'user_id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

SELECT create_distributed_table('events_summary', 'user_id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

INSERT INTO events_table VALUES (1, 1, 3 ), (1, 2, 1), (1, 3, 2), (2, 4, 3), (3, 5, 1), (4, 7, 1), (4, 1, 9), (4, 3, 2);
SELECT count(*) FROM events_summary;
 count
---------------------------------------------------------------------
     0
(1 row)

-- insert/select from one distributed table to another
-- kill worker query
SELECT citus.mitmproxy('conn.onQuery(query="^INSERT INTO insert_select_pushdown").kill()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

INSERT INTO events_summary SELECT user_id, event_id, count(*) FROM events_table GROUP BY 1,2;
ERROR:  connection to the remote node postgres@localhost:xxxxx failed with the following error: connection not open
--verify nothing is modified
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

SELECT count(*) FROM events_summary;
 count
---------------------------------------------------------------------
     0
(1 row)

-- cancel worker query
SELECT citus.mitmproxy('conn.onQuery(query="^INSERT INTO insert_select_pushdown").cancel(' || :pid || ')');
 mitmproxy
---------------------------------------------------------------------

(1 row)

INSERT INTO events_summary SELECT user_id, event_id, count(*) FROM events_table GROUP BY 1,2;
ERROR:  canceling statement due to user request
--verify nothing is modified
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

SELECT count(*) FROM events_summary;
 count
---------------------------------------------------------------------
     0
(1 row)

-- test self insert/select
SELECT count(*) FROM events_table;
 count
---------------------------------------------------------------------
     8
(1 row)

-- kill worker query
SELECT citus.mitmproxy('conn.onQuery(query="^INSERT INTO insert_select_pushdown").kill()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

INSERT INTO events_table SELECT * FROM events_table;
ERROR:  connection to the remote node postgres@localhost:xxxxx failed with the following error: connection not open
--verify nothing is modified
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

SELECT count(*) FROM events_table;
 count
---------------------------------------------------------------------
     8
(1 row)

-- cancel worker query
SELECT citus.mitmproxy('conn.onQuery(query="^INSERT INTO insert_select_pushdown").cancel(' || :pid || ')');
 mitmproxy
---------------------------------------------------------------------

(1 row)

INSERT INTO events_table SELECT * FROM events_table;
ERROR:  canceling statement due to user request
--verify nothing is modified
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

SELECT count(*) FROM events_table;
 count
---------------------------------------------------------------------
     8
(1 row)

RESET SEARCH_PATH;
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

DROP SCHEMA insert_select_pushdown CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to table insert_select_pushdown.events_table
drop cascades to table insert_select_pushdown.events_summary
