-- add coordinator in idempotent way, this is needed so we get a nodeid
SET client_min_messages TO ERROR;
SELECT 1 FROM master_add_node('localhost', :master_port, groupid => 0);
 ?column?
---------------------------------------------------------------------
        1
(1 row)

RESET client_min_messages;
-- Kill maintenance daemon so it gets restarted and gets a gpid containing our
-- nodeid
SELECT COUNT(pg_terminate_backend(pid)) >= 0
FROM pg_stat_activity
WHERE application_name = 'Citus Maintenance Daemon';
 ?column?
---------------------------------------------------------------------
 t
(1 row)

-- reconnect to make sure we get a session with the gpid containing our nodeid
\c - - - -
CREATE SCHEMA global_cancel;
SET search_path TO global_cancel;
SET citus.next_shard_id TO 56789000;
SET citus.grep_remote_commands TO '%pg_cancel_backend%';
CREATE TABLE dist_table (a INT, b INT);
SELECT create_distributed_table ('dist_table', 'a', shard_count:=4);
 create_distributed_table
---------------------------------------------------------------------

(1 row)

INSERT INTO dist_table VALUES (1, 1);
SELECT global_pid AS coordinator_gpid FROM get_all_active_transactions() WHERE process_id = pg_backend_pid() \gset
SELECT pg_typeof(:coordinator_gpid);
 pg_typeof
---------------------------------------------------------------------
 bigint
(1 row)

SELECT pg_cancel_backend(:coordinator_gpid);
ERROR:  canceling statement due to user request
SET citus.log_remote_commands TO ON;
SELECT pg_cancel_backend(:coordinator_gpid) FROM dist_table WHERE a = 1;
NOTICE:  issuing SELECT pg_cancel_backend('xxxxx'::bigint) AS pg_cancel_backend FROM global_cancel.dist_table_56789000 dist_table WHERE (a OPERATOR(pg_catalog.=) 1)
DETAIL:  on server postgres@localhost:xxxxx connectionId: xxxxxxx
ERROR:  canceling statement due to user request
BEGIN;
SELECT pg_cancel_backend(:coordinator_gpid) FROM dist_table WHERE a = 1;
NOTICE:  issuing SELECT pg_cancel_backend('xxxxx'::bigint) AS pg_cancel_backend FROM global_cancel.dist_table_56789000 dist_table WHERE (a OPERATOR(pg_catalog.=) 1)
DETAIL:  on server postgres@localhost:xxxxx connectionId: xxxxxxx
ERROR:  canceling statement due to user request
END;
SET citus.log_remote_commands TO OFF;
SELECT global_pid AS maintenance_daemon_gpid
FROM pg_stat_activity psa JOIN get_all_active_transactions() gaat ON psa.pid = gaat.process_id
WHERE application_name = 'Citus Maintenance Daemon' \gset
SET client_min_messages TO ERROR;
CREATE USER global_cancel_user;
SELECT 1 FROM run_command_on_workers('CREATE USER global_cancel_user');
 ?column?
---------------------------------------------------------------------
        1
        1
(2 rows)

RESET client_min_messages;
\c - global_cancel_user - :master_port
SELECT pg_typeof(:maintenance_daemon_gpid);
 pg_typeof
---------------------------------------------------------------------
 bigint
(1 row)

\set VERBOSITY terse
SELECT pg_cancel_backend(:maintenance_daemon_gpid);
ERROR:  must be a superuser to cancel superuser query
SELECT pg_terminate_backend(:maintenance_daemon_gpid);
ERROR:  must be a superuser to terminate superuser process
\set VERBOSITY default
-- we can cancel our own backend
SELECT pg_cancel_backend(citus_backend_gpid());
ERROR:  canceling statement due to user request
\c - postgres - :master_port
DROP USER global_cancel_user;
SET client_min_messages TO DEBUG;
-- 10000000000 is the node id multiplier for global pid
SELECT pg_cancel_backend(10000000000 * citus_coordinator_nodeid() + 0);
DEBUG:  PID 0 is not a PostgreSQL backend process
DETAIL:  from localhost:xxxxx
 pg_cancel_backend
---------------------------------------------------------------------
 f
(1 row)

SELECT pg_terminate_backend(10000000000 * citus_coordinator_nodeid() + 0);
DEBUG:  PID 0 is not a PostgreSQL backend process
DETAIL:  from localhost:xxxxx
 pg_terminate_backend
---------------------------------------------------------------------
 f
(1 row)

RESET client_min_messages;
SELECT citus_backend_gpid() = citus_calculate_gpid(citus_coordinator_nodeid(), pg_backend_pid());
 ?column?
---------------------------------------------------------------------
 t
(1 row)

SELECT nodename = citus_nodename_for_nodeid(nodeid) AND nodeport = citus_nodeport_for_nodeid(nodeid)
FROM pg_dist_node
WHERE isactive = true AND noderole = 'primary';
 ?column?
---------------------------------------------------------------------
 t
 t
 t
(3 rows)

SELECT citus_nodeid_for_gpid(10000000000 * 2 + 3);
 citus_nodeid_for_gpid
---------------------------------------------------------------------
                     2
(1 row)

SELECT citus_pid_for_gpid(10000000000 * 2 + 3);
 citus_pid_for_gpid
---------------------------------------------------------------------
                  3
(1 row)

DROP SCHEMA global_cancel CASCADE;
NOTICE:  drop cascades to table global_cancel.dist_table
