CREATE SCHEMA citus_aggregated_stats;
SET search_path TO citus_aggregated_stats, public;
SET citus.shard_count = 2;
SET citus.next_shard_id TO 1870000;
CREATE USER user1;
GRANT ALL ON SCHEMA citus_aggregated_stats TO user1;
SET SESSION AUTHORIZATION user1;
CREATE TABLE current_check (currentid int, payload text, rlsuser text);
GRANT ALL ON current_check TO PUBLIC;
INSERT INTO current_check VALUES
    (1, 'abc', 'user1'),
    (3, 'cde', 'user1'),
    (4, 'def', 'user1'),
    (4, 'def', 'user1'),
    (3, 'cde', 'user2'),
    (5, NULL, NULL),
    (4, 'def', 'user1');
ALTER TABLE current_check ENABLE ROW LEVEL SECURITY;
SET row_security TO ON;
ANALYZE current_check;
SELECT schemaname, tablename, attname, null_frac, most_common_vals, most_common_freqs FROM pg_stats
  WHERE tablename IN ('current_check', 'dist_current_check', 'ref_current_check', 'citus_local_current_check')
  ORDER BY 1;
       schemaname       |   tablename   |  attname  | null_frac | most_common_vals |  most_common_freqs
---------------------------------------------------------------------
 citus_aggregated_stats | current_check | currentid |         0 | {4,3}            | {0.428571,0.285714}
 citus_aggregated_stats | current_check | payload   |  0.142857 | {def,cde}        | {0.428571,0.285714}
 citus_aggregated_stats | current_check | rlsuser   |  0.142857 | {user1}          | {0.714286}
(3 rows)

SELECT * FROM citus_stats
  WHERE tablename IN ('current_check', 'dist_current_check', 'ref_current_check', 'citus_local_current_check')
  ORDER BY 1;
 schemaname | tablename | attname | null_frac | most_common_vals | most_common_freqs
---------------------------------------------------------------------
(0 rows)

-- test various Citus table types in citus_stats
CREATE TABLE dist_current_check (currentid int, payload text, rlsuser text);
CREATE TABLE ref_current_check (currentid int, payload text, rlsuser text);
CREATE TABLE citus_local_current_check (currentid int, payload text, rlsuser text);
SELECT create_distributed_table('dist_current_check', 'currentid');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

SELECT create_reference_table('ref_current_check');
 create_reference_table
---------------------------------------------------------------------

(1 row)

SELECT citus_add_local_table_to_metadata('citus_local_current_check');
 citus_add_local_table_to_metadata
---------------------------------------------------------------------

(1 row)

INSERT INTO dist_current_check VALUES
    (1, 'abc', 'user1'),
    (3, 'cde', 'user1'),
    (4, 'def', 'user1'),
    (4, 'def', 'user1'),
    (3, 'cde', 'user2'),
    (5, NULL, NULL),
    (4, 'def', 'user1');
INSERT INTO ref_current_check VALUES
    (1, 'abc', 'user1'),
    (3, 'cde', 'user1'),
    (4, 'def', 'user1'),
    (4, 'def', 'user1'),
    (3, 'cde', 'user2'),
    (5, NULL, NULL),
    (4, 'def', 'user1');
INSERT INTO citus_local_current_check VALUES
    (1, 'abc', 'user1'),
    (3, 'cde', 'user1'),
    (4, 'def', 'user1'),
    (4, 'def', 'user1'),
    (3, 'cde', 'user2'),
    (5, NULL, NULL),
    (4, 'def', 'user1');
ANALYZE dist_current_check;
ANALYZE ref_current_check;
ANALYZE citus_local_current_check;
SELECT schemaname, tablename, attname, null_frac, most_common_vals, most_common_freqs FROM pg_stats
  WHERE tablename IN ('current_check', 'dist_current_check', 'ref_current_check', 'citus_local_current_check')
  ORDER BY 1;
       schemaname       |   tablename   |  attname  | null_frac | most_common_vals |  most_common_freqs
---------------------------------------------------------------------
 citus_aggregated_stats | current_check | currentid |         0 | {4,3}            | {0.428571,0.285714}
 citus_aggregated_stats | current_check | payload   |  0.142857 | {def,cde}        | {0.428571,0.285714}
 citus_aggregated_stats | current_check | rlsuser   |  0.142857 | {user1}          | {0.714286}
(3 rows)

SELECT * FROM citus_stats
  WHERE tablename IN ('current_check', 'dist_current_check', 'ref_current_check', 'citus_local_current_check')
  ORDER BY 1;
       schemaname       |         tablename         |  attname  | null_frac | most_common_vals |  most_common_freqs
---------------------------------------------------------------------
 citus_aggregated_stats | dist_current_check        | currentid |         0 | {4,3}            | {0.428571,0.285714}
 citus_aggregated_stats | dist_current_check        | payload   |  0.142857 | {def,cde}        | {0.428571,0.285714}
 citus_aggregated_stats | dist_current_check        | rlsuser   |  0.142857 | {user1}          | {0.714286}
 citus_aggregated_stats | ref_current_check         | currentid |         0 | {4,3}            | {0.428571,0.285714}
 citus_aggregated_stats | ref_current_check         | payload   |  0.142857 | {def,cde}        | {0.428571,0.285714}
 citus_aggregated_stats | ref_current_check         | rlsuser   |  0.142857 | {user1}          | {0.714286}
 citus_aggregated_stats | citus_local_current_check | currentid |         0 | {4,3}            | {0.428571,0.285714}
 citus_aggregated_stats | citus_local_current_check | payload   |  0.142857 | {def,cde}        | {0.428571,0.285714}
 citus_aggregated_stats | citus_local_current_check | rlsuser   |  0.142857 | {user1}          | {0.714286}
(9 rows)

-- create a dist table with million rows to simulate 3.729223e+06 in reltuples
-- this tests casting numbers like 3.729223e+06 to bigint
CREATE TABLE organizations (
    org_id bigint,
    id int
);
SELECT create_distributed_table('organizations', 'org_id');
 create_distributed_table
---------------------------------------------------------------------

(1 row)

INSERT INTO organizations(org_id, id)
  SELECT i, 1
  FROM generate_series(1,2000000) i;
ANALYZE organizations;
SELECT attname, null_frac, most_common_vals, most_common_freqs FROM citus_stats
  WHERE tablename IN ('organizations')
  ORDER BY 1;
 attname | null_frac | most_common_vals | most_common_freqs
---------------------------------------------------------------------
 id      |         0 | {1}              | {1}
(1 row)

-- more real-world scenario:
-- outputs of pg_stats and citus_stats are NOT the same
-- but citus_stats does a fair estimation job
SELECT setseed(0.42);
 setseed
---------------------------------------------------------------------

(1 row)

CREATE TABLE orders (id bigint , custid int, product text, quantity int);
INSERT INTO orders(id, custid, product, quantity)
SELECT i, (random() * 100)::int, 'product' || (random() * 10)::int, NULL
FROM generate_series(1,11) d(i);
-- frequent customer
INSERT INTO orders(id, custid, product, quantity)
SELECT 1200, 17, 'product' || (random() * 10)::int, NULL
FROM generate_series(1, 57) sk(i);
-- popular product
INSERT INTO orders(id, custid, product, quantity)
SELECT i+100 % 17, NULL, 'product3', (random() * 40)::int
FROM generate_series(1, 37) sk(i);
-- frequent customer
INSERT INTO orders(id, custid, product, quantity)
SELECT 1390, 76, 'product' || ((random() * 20)::int % 3), (random() * 30)::int
FROM generate_series(1, 33) sk(i);
ANALYZE orders;
-- pg_stats
SELECT schemaname, tablename, attname, null_frac, most_common_vals, most_common_freqs FROM pg_stats
  WHERE tablename IN ('orders')
ORDER BY 3;
       schemaname       | tablename | attname  | null_frac |                                       most_common_vals                                       |                                                                                                       most_common_freqs
---------------------------------------------------------------------
 citus_aggregated_stats | orders    | custid   |  0.268116 | {17,76}                                                                                      | {0.413043,0.23913}
 citus_aggregated_stats | orders    | id       |         0 | {1200,1390}                                                                                  | {0.413043,0.23913}
 citus_aggregated_stats | orders    | product  |         0 | {product3,product2,product0,product1,product9,product4,product8,product5,product10,product6} | {0.347826,0.15942,0.115942,0.108696,0.0652174,0.057971,0.0507246,0.0362319,0.0289855,0.0289855}
 citus_aggregated_stats | orders    | quantity |  0.492754 | {26,23,6,8,11,12,13,17,20,25,30,4,14,15,16,19,24,27,35,36,38,40}                             | {0.0362319,0.0289855,0.0217391,0.0217391,0.0217391,0.0217391,0.0217391,0.0217391,0.0217391,0.0217391,0.0217391,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928}
(4 rows)

SELECT create_distributed_table('orders', 'id');
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($$citus_aggregated_stats.orders$$)
 create_distributed_table
---------------------------------------------------------------------

(1 row)

ANALYZE orders;
-- citus_stats
SELECT schemaname, tablename, attname, null_frac, most_common_vals, most_common_freqs FROM citus_stats
  WHERE tablename IN ('orders')
ORDER BY 3;
       schemaname       | tablename | attname  | null_frac |                                       most_common_vals                                       |                                                                                        most_common_freqs
---------------------------------------------------------------------
 citus_aggregated_stats | orders    | custid   |  0.268116 | {17,76}                                                                                      | {0.413043,0.23913}
 citus_aggregated_stats | orders    | id       |         0 | {1200,1390}                                                                                  | {0.413043,0.23913}
 citus_aggregated_stats | orders    | product  |         0 | {product3,product2,product0,product1,product9,product4,product8,product5,product10,product6} | {0.347826,0.15942,0.115942,0.108696,0.0652174,0.057971,0.0507246,0.0362319,0.0289855,0.0289855}
 citus_aggregated_stats | orders    | quantity |  0.492754 | {26,13,17,20,23,8,11,12,14,16,19,24,25,27,30,35,38,40,6}                                     | {0.0362319,0.0217391,0.0217391,0.0217391,0.0217391,0.0217391,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928,0.0144928}
(4 rows)

RESET SESSION AUTHORIZATION;
DROP SCHEMA citus_aggregated_stats CASCADE;
NOTICE:  drop cascades to 8 other objects
DETAIL:  drop cascades to table current_check
drop cascades to table dist_current_check
drop cascades to table ref_current_check
drop cascades to table citus_local_current_check_1870003
drop cascades to table ref_current_check_1870002
drop cascades to table citus_local_current_check
drop cascades to table organizations
drop cascades to table orders
DROP USER user1;
