SET citus.next_shard_id TO 100500;
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

CREATE TABLE ref_table (key int, value int);
SELECT create_reference_table('ref_table');
 create_reference_table
---------------------------------------------------------------------

(1 row)

\copy ref_table FROM stdin delimiter ',';
SELECT citus.clear_network_traffic();
 clear_network_traffic
---------------------------------------------------------------------

(1 row)

SELECT COUNT(*) FROM ref_table;
 count
---------------------------------------------------------------------
     4
(1 row)

-- verify behavior of single INSERT; should fail to execute
SELECT citus.mitmproxy('conn.onQuery(query="^INSERT").kill()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

INSERT INTO ref_table VALUES (5, 6);
ERROR:  connection error: localhost:xxxxx
DETAIL:  server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
SELECT COUNT(*) FROM ref_table WHERE key=5;
 count
---------------------------------------------------------------------
     0
(1 row)

-- verify behavior of UPDATE ... RETURNING; should not execute
SELECT citus.mitmproxy('conn.onQuery(query="^UPDATE").kill()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

UPDATE ref_table SET key=7 RETURNING value;
ERROR:  connection error: localhost:xxxxx
DETAIL:  server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
SELECT COUNT(*) FROM ref_table WHERE key=7;
 count
---------------------------------------------------------------------
     0
(1 row)

-- verify fix to #2214; should raise error and fail to execute
SELECT citus.mitmproxy('conn.onQuery(query="^UPDATE").kill()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

BEGIN;
DELETE FROM ref_table WHERE key=5;
UPDATE ref_table SET key=value;
ERROR:  connection error: localhost:xxxxx
DETAIL:  server closed the connection unexpectedly
	This probably means the server terminated abnormally
	before or while processing the request.
COMMIT;
SELECT COUNT(*) FROM ref_table WHERE key=value;
 count
---------------------------------------------------------------------
     0
(1 row)

-- all shards should still be healthy
SELECT COUNT(*) FROM pg_dist_shard_placement WHERE shardstate = 3;
 count
---------------------------------------------------------------------
     0
(1 row)

-- ==== Clean up, we're done here ====
SELECT citus.mitmproxy('conn.allow()');
 mitmproxy
---------------------------------------------------------------------

(1 row)

DROP TABLE ref_table;
