--
-- Hide shard names on MX worker nodes
--
ALTER SEQUENCE pg_catalog.pg_dist_shardid_seq RESTART 1130000;
-- make sure that the signature of the citus_table_is_visible
-- and pg_table_is_visible are the same since the logic
-- relies on that
SELECT
	proname, proisstrict, proretset, provolatile,
	proparallel, pronargs, pronargdefaults ,prorettype,
	proargtypes, proacl
FROM
	pg_proc
WHERE
	proname LIKE '%table_is_visible%'
ORDER BY 1;
        proname         | proisstrict | proretset | provolatile | proparallel | pronargs | pronargdefaults | prorettype | proargtypes | proacl
---------------------------------------------------------------------
 citus_table_is_visible | t           | f         | s           | s           |        1 |               0 |         16 | 26          |
 pg_table_is_visible    | t           | f         | s           | s           |        1 |               0 |         16 | 26          |
(2 rows)

CREATE SCHEMA mx_hide_shard_names;
SET search_path TO 'mx_hide_shard_names';
SET citus.shard_count TO 4;
SET citus.shard_replication_factor TO 1;
SET citus.replication_model TO 'streaming';
SELECT start_metadata_sync_to_node('localhost', :worker_1_port);
 start_metadata_sync_to_node
---------------------------------------------------------------------

(1 row)

SELECT start_metadata_sync_to_node('localhost', :worker_2_port);
 start_metadata_sync_to_node
---------------------------------------------------------------------

(1 row)

CREATE TABLE test_table(id int, time date);
SELECT create_distributed_table('test_table', 'id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

-- first show that the views does not show
-- any shards on the coordinator as expected
SELECT * FROM citus_shards_on_worker;
 Schema | Name | Type | Owner
---------------------------------------------------------------------
(0 rows)

SELECT * FROM citus_shard_indexes_on_worker;
 Schema | Name | Type | Owner | Table
---------------------------------------------------------------------
(0 rows)

-- now show that we see the shards, but not the
-- indexes as there are no indexes
\c - - - :worker_1_port
SET search_path TO 'mx_hide_shard_names';
SELECT * FROM citus_shards_on_worker ORDER BY 2;
       Schema        |        Name        | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names | test_table_1130000 | table | postgres
 mx_hide_shard_names | test_table_1130002 | table | postgres
(2 rows)

SELECT * FROM citus_shard_indexes_on_worker ORDER BY 2;
 Schema | Name | Type | Owner | Table
---------------------------------------------------------------------
(0 rows)

-- also show that nested calls to pg_table_is_visible works fine
-- if both of the calls to the pg_table_is_visible haven't been
-- replaced, we would get 0 rows in the output
SELECT
	pg_table_is_visible((SELECT
								"t1"."Name"::regclass
						 FROM
						 	citus_shards_on_worker as t1
						 WHERE
						 	NOT pg_table_is_visible("t1"."Name"::regclass)
						 LIMIT
						 	1));
 pg_table_is_visible
---------------------------------------------------------------------
 f
(1 row)

-- now create an index
\c - - - :master_port
SET search_path TO 'mx_hide_shard_names';
CREATE INDEX test_index ON mx_hide_shard_names.test_table(id);
-- now show that we see the shards, and the
-- indexes as well
\c - - - :worker_1_port
SET search_path TO 'mx_hide_shard_names';
SELECT * FROM citus_shards_on_worker ORDER BY 2;
       Schema        |        Name        | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names | test_table_1130000 | table | postgres
 mx_hide_shard_names | test_table_1130002 | table | postgres
(2 rows)

SELECT * FROM citus_shard_indexes_on_worker ORDER BY 2;
       Schema        |        Name        | Type  |  Owner   |       Table
---------------------------------------------------------------------
 mx_hide_shard_names | test_index_1130000 | index | postgres | test_table_1130000
 mx_hide_shard_names | test_index_1130002 | index | postgres | test_table_1130002
(2 rows)

-- we should be able to select from the shards directly if we
-- know the name of the tables
SELECT count(*) FROM test_table_1130000;
 count
---------------------------------------------------------------------
     0
(1 row)

-- disable the config so that table becomes visible
SELECT pg_table_is_visible('test_table_1130000'::regclass);
 pg_table_is_visible
---------------------------------------------------------------------
 f
(1 row)

SET citus.override_table_visibility TO FALSE;
SELECT pg_table_is_visible('test_table_1130000'::regclass);
 pg_table_is_visible
---------------------------------------------------------------------
 t
(1 row)

\c - - - :master_port
-- make sure that we're resilient to the edge cases
-- such that the table name includes the shard number
SET search_path TO 'mx_hide_shard_names';
SET citus.shard_count TO 4;
SET citus.shard_replication_factor TO 1;
SET citus.replication_model TO 'streaming';
-- not existing shard ids appended to the distributed table name
CREATE TABLE test_table_102008(id int, time date);
SELECT create_distributed_table('test_table_102008', 'id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

\c - - - :worker_1_port
SET search_path TO 'mx_hide_shard_names';
-- existing shard ids appended to a local table name
-- note that we cannot create a distributed or local table
-- with the same name since a table with the same
-- name already exists :)
CREATE TABLE test_table_2_1130000(id int, time date);
SELECT * FROM citus_shards_on_worker ORDER BY 2;
       Schema        |           Name            | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names | test_table_102008_1130004 | table | postgres
 mx_hide_shard_names | test_table_102008_1130006 | table | postgres
 mx_hide_shard_names | test_table_1130000        | table | postgres
 mx_hide_shard_names | test_table_1130002        | table | postgres
(4 rows)

\d
                       List of relations
       Schema        |         Name         | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names | test_table           | table | postgres
 mx_hide_shard_names | test_table_102008    | table | postgres
 mx_hide_shard_names | test_table_2_1130000 | table | postgres
(3 rows)

\c - - - :master_port
-- make sure that don't mess up with schemas
CREATE SCHEMA mx_hide_shard_names_2;
SET search_path TO 'mx_hide_shard_names_2';
SET citus.shard_count TO 4;
SET citus.shard_replication_factor TO 1;
SET citus.replication_model TO 'streaming';
CREATE TABLE test_table(id int, time date);
SELECT create_distributed_table('test_table', 'id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

CREATE INDEX test_index ON mx_hide_shard_names_2.test_table(id);
\c - - - :worker_1_port
SET search_path TO 'mx_hide_shard_names';
SELECT * FROM citus_shards_on_worker ORDER BY 2;
       Schema        |           Name            | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names | test_table_102008_1130004 | table | postgres
 mx_hide_shard_names | test_table_102008_1130006 | table | postgres
 mx_hide_shard_names | test_table_1130000        | table | postgres
 mx_hide_shard_names | test_table_1130002        | table | postgres
(4 rows)

SELECT * FROM citus_shard_indexes_on_worker ORDER BY 2;
       Schema        |        Name        | Type  |  Owner   |       Table
---------------------------------------------------------------------
 mx_hide_shard_names | test_index_1130000 | index | postgres | test_table_1130000
 mx_hide_shard_names | test_index_1130002 | index | postgres | test_table_1130002
(2 rows)

SET search_path TO 'mx_hide_shard_names_2';
SELECT * FROM citus_shards_on_worker ORDER BY 2;
        Schema         |        Name        | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names_2 | test_table_1130008 | table | postgres
 mx_hide_shard_names_2 | test_table_1130010 | table | postgres
(2 rows)

SELECT * FROM citus_shard_indexes_on_worker ORDER BY 2;
        Schema         |        Name        | Type  |  Owner   |       Table
---------------------------------------------------------------------
 mx_hide_shard_names_2 | test_index_1130008 | index | postgres | test_table_1130008
 mx_hide_shard_names_2 | test_index_1130010 | index | postgres | test_table_1130010
(2 rows)

SET search_path TO 'mx_hide_shard_names_2, mx_hide_shard_names';
SELECT * FROM citus_shards_on_worker ORDER BY 2;
 Schema | Name | Type | Owner
---------------------------------------------------------------------
(0 rows)

SELECT * FROM citus_shard_indexes_on_worker ORDER BY 2;
 Schema | Name | Type | Owner | Table
---------------------------------------------------------------------
(0 rows)

-- now try very long table names
\c - - - :master_port
SET citus.shard_count TO 4;
SET citus.shard_replication_factor TO 1;
SET citus.replication_model TO 'streaming';
CREATE SCHEMA mx_hide_shard_names_3;
SET search_path TO 'mx_hide_shard_names_3';
-- Verify that a table name > 56 characters handled properly.
CREATE TABLE too_long_12345678901234567890123456789012345678901234567890 (
        col1 integer not null,
        col2 integer not null);
SELECT create_distributed_table('too_long_12345678901234567890123456789012345678901234567890', 'col1');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

\c - - - :worker_1_port
SET search_path TO 'mx_hide_shard_names_3';
SELECT * FROM citus_shards_on_worker ORDER BY 2;
        Schema         |                              Name                               | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names_3 | too_long_1234567890123456789012345678901234567_e0119164_1130012 | table | postgres
 mx_hide_shard_names_3 | too_long_1234567890123456789012345678901234567_e0119164_1130014 | table | postgres
(2 rows)

\d
                                           List of relations
        Schema         |                            Name                             | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names_3 | too_long_12345678901234567890123456789012345678901234567890 | table | postgres
(1 row)

-- now try weird schema names
\c - - - :master_port
SET citus.shard_count TO 4;
SET citus.shard_replication_factor TO 1;
SET citus.replication_model TO 'streaming';
CREATE SCHEMA "CiTuS.TeeN";
SET search_path TO "CiTuS.TeeN";
CREATE TABLE "TeeNTabLE.1!?!"(id int, "TeNANt_Id" int);
CREATE INDEX "MyTenantIndex" ON  "CiTuS.TeeN"."TeeNTabLE.1!?!"("TeNANt_Id");
-- create distributed table with weird names
SELECT create_distributed_table('"CiTuS.TeeN"."TeeNTabLE.1!?!"', 'TeNANt_Id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

\c - - - :worker_1_port
SET search_path TO "CiTuS.TeeN";
SELECT * FROM citus_shards_on_worker ORDER BY 2;
   Schema   |          Name          | Type  |  Owner
---------------------------------------------------------------------
 CiTuS.TeeN | TeeNTabLE.1!?!_1130016 | table | postgres
 CiTuS.TeeN | TeeNTabLE.1!?!_1130018 | table | postgres
(2 rows)

SELECT * FROM citus_shard_indexes_on_worker ORDER BY 2;
   Schema   |         Name          | Type  |  Owner   |         Table
---------------------------------------------------------------------
 CiTuS.TeeN | MyTenantIndex_1130016 | index | postgres | TeeNTabLE.1!?!_1130016
 CiTuS.TeeN | MyTenantIndex_1130018 | index | postgres | TeeNTabLE.1!?!_1130018
(2 rows)

\d
               List of relations
   Schema   |      Name      | Type  |  Owner
---------------------------------------------------------------------
 CiTuS.TeeN | TeeNTabLE.1!?! | table | postgres
(1 row)

\di
                       List of relations
   Schema   |     Name      | Type  |  Owner   |     Table
---------------------------------------------------------------------
 CiTuS.TeeN | MyTenantIndex | index | postgres | TeeNTabLE.1!?!
(1 row)

-- clean-up
\c - - - :master_port
-- show that common psql functions do not show shards
-- including the ones that are not in the current schema
SET search_path TO 'mx_hide_shard_names';
\d
                     List of relations
       Schema        |       Name        | Type  |  Owner
---------------------------------------------------------------------
 mx_hide_shard_names | test_table        | table | postgres
 mx_hide_shard_names | test_table_102008 | table | postgres
(2 rows)

\di
                        List of relations
       Schema        |    Name    | Type  |  Owner   |   Table
---------------------------------------------------------------------
 mx_hide_shard_names | test_index | index | postgres | test_table
(1 row)

DROP SCHEMA mx_hide_shard_names CASCADE;
NOTICE:  drop cascades to 2 other objects
DETAIL:  drop cascades to table test_table
drop cascades to table test_table_102008
DROP SCHEMA mx_hide_shard_names_2 CASCADE;
NOTICE:  drop cascades to table mx_hide_shard_names_2.test_table
DROP SCHEMA mx_hide_shard_names_3 CASCADE;
NOTICE:  drop cascades to table mx_hide_shard_names_3.too_long_12345678901234567890123456789012345678901234567890
DROP SCHEMA "CiTuS.TeeN" CASCADE;
NOTICE:  drop cascades to table "CiTuS.TeeN"."TeeNTabLE.1!?!"
