Skip to content

Commit 0bc6e4e

Browse files
committed
Add tests for citus_stat_user_tables
1 parent 4773328 commit 0bc6e4e

File tree

6 files changed

+141
-26
lines changed

6 files changed

+141
-26
lines changed

src/backend/distributed/sql/udfs/citus_stat_user_tables/13.1-1.sql

Lines changed: 11 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/backend/distributed/sql/udfs/citus_stat_user_tables/latest.sql

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
CREATE OR REPLACE FUNCTION pg_catalog.citus_stat_user_tables(
1+
CREATE OR REPLACE FUNCTION pg_catalog.citus_stat_user_tables()
22
RETURNS TABLE (
3-
dist_table regclass,
3+
relname regclass,
44
n_tup_ins bigint,
55
n_tup_upd bigint,
66
n_tup_del bigint,
@@ -17,8 +17,8 @@ BEGIN
1717
SELECT ( SELECT json_agg(row_to_json(f)) FROM ( SELECT result FROM
1818
run_command_on_shards(logicalrelid, $$ SELECT json_agg(row_to_json(d))
1919
FROM ( SELECT '$$ || logicalrelid || $$' AS dist_table,
20-
s.relname, n_tup_ins, n_tup_upd, n_tup_del,
21-
n_tup_hot_upd, n_tup_newpage_upd, n_live_tup, n_dead_tup
20+
s.relname, s.n_tup_ins, s.n_tup_upd, s.n_tup_del,
21+
s.n_tup_hot_upd, s.n_tup_newpage_upd, s.n_live_tup, s.n_dead_tup
2222
FROM pg_stat_user_tables s
2323
JOIN pg_class c ON s.relname = c.relname
2424
WHERE c.oid = '%s'::regclass::oid) d $$)) f)
@@ -29,7 +29,7 @@ BEGIN
2929
FROM pg_dist_stats_double_json),
3030

3131
pg_dist_stats_regular AS (
32-
SELECT (json_array_elements(result::json)->>'dist_table')::regclass AS dist_table,
32+
SELECT (json_array_elements(result::json)->>'dist_table')::regclass AS relname,
3333
(json_array_elements(result::json)->>'relname')::name AS shardname,
3434
(json_array_elements(result::json)->>'n_tup_ins')::bigint AS n_tup_ins,
3535
(json_array_elements(result::json)->>'n_tup_upd')::bigint AS n_tup_upd,
@@ -41,16 +41,15 @@ BEGIN
4141
FROM pg_dist_stats_single_json
4242
WHERE result != '')
4343

44-
SELECT dist_table, sum(n_tup_ins)::bigint AS n_tup_ins, sum(n_tup_upd)::bigint AS n_tup_upd,
45-
sum(n_tup_del)::bigint AS n_tup_del, sum(n_tup_hot_upd)::bigint AS n_tup_hot_upd,
46-
sum(n_tup_newpage_upd)::bigint AS n_tup_newpage_upd,
47-
sum(n_live_tup)::bigint AS n_live_tup, sum(n_dead_tup)::bigint AS n_dead_tup
48-
FROM pg_dist_stats_regular
44+
SELECT relname, sum(s.n_tup_ins)::bigint AS n_tup_ins, sum(s.n_tup_upd)::bigint AS n_tup_upd,
45+
sum(s.n_tup_del)::bigint AS n_tup_del, sum(s.n_tup_hot_upd)::bigint AS n_tup_hot_upd,
46+
sum(s.n_tup_newpage_upd)::bigint AS n_tup_newpage_upd,
47+
sum(s.n_live_tup)::bigint AS n_live_tup, sum(s.n_dead_tup)::bigint AS n_dead_tup
48+
FROM pg_dist_stats_regular s
4949
GROUP BY 1 ORDER BY 1;
5050

5151
END;
5252
$func$ LANGUAGE plpgsql;
5353

54-
COMMENT ON FUNCTION pg_catalog.citus_stat_user_tables(
55-
qualified_table_name text)
54+
COMMENT ON FUNCTION pg_catalog.citus_stat_user_tables()
5655
IS 'provides some pg_stat_user_tables entries for Citus tables';

src/test/regress/expected/citus_aggregated_stats.out

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,72 @@ SELECT * FROM citus_column_stats('dist_current_check');
6060
DROP TABLE current_check;
6161
DROP TABLE dist_current_check;
6262
RESET SESSION AUTHORIZATION;
63+
RESET row_security;
64+
-- compare pg_stat_user_tables with citus_stat_user_tables
65+
CREATE TABLE trunc_stats_test(id serial);
66+
CREATE TABLE trunc_stats_dist_test(id serial);
67+
SELECT create_distributed_table('trunc_stats_dist_test', 'id');
68+
create_distributed_table
69+
---------------------------------------------------------------------
70+
71+
(1 row)
72+
73+
-- rollback a savepoint: this should count 4 inserts and have 2
74+
-- live tuples after commit (and 2 dead ones due to aborted subxact)
75+
BEGIN;
76+
INSERT INTO trunc_stats_test DEFAULT VALUES;
77+
INSERT INTO trunc_stats_test DEFAULT VALUES;
78+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
79+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
80+
SAVEPOINT p1;
81+
INSERT INTO trunc_stats_test DEFAULT VALUES;
82+
INSERT INTO trunc_stats_test DEFAULT VALUES;
83+
TRUNCATE trunc_stats_test;
84+
INSERT INTO trunc_stats_test DEFAULT VALUES;
85+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
86+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
87+
TRUNCATE trunc_stats_dist_test;
88+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
89+
ROLLBACK TO SAVEPOINT p1;
90+
COMMIT;
91+
\c - - - :worker_1_port
92+
SELECT pg_stat_force_next_flush();
93+
pg_stat_force_next_flush
94+
---------------------------------------------------------------------
95+
96+
(1 row)
97+
98+
\c - - - :worker_2_port
99+
SELECT pg_stat_force_next_flush();
100+
pg_stat_force_next_flush
101+
---------------------------------------------------------------------
102+
103+
(1 row)
104+
105+
\c - - - :master_port
106+
SELECT pg_stat_force_next_flush();
107+
pg_stat_force_next_flush
108+
---------------------------------------------------------------------
109+
110+
(1 row)
111+
112+
SELECT relname, n_tup_ins, n_live_tup, n_dead_tup
113+
FROM pg_stat_user_tables
114+
WHERE relname like 'trunc_stats%';
115+
relname | n_tup_ins | n_live_tup | n_dead_tup
116+
---------------------------------------------------------------------
117+
trunc_stats_test | 4 | 2 | 2
118+
trunc_stats_dist_test | 0 | 0 | 0
119+
(2 rows)
120+
121+
SELECT relname, n_tup_ins, n_live_tup, n_dead_tup
122+
FROM citus_stat_user_tables();
123+
relname | n_tup_ins | n_live_tup | n_dead_tup
124+
---------------------------------------------------------------------
125+
trunc_stats_dist_test | 4 | 2 | 2
126+
(1 row)
127+
63128
REVOKE ALL ON SCHEMA public FROM user1;
64129
DROP USER user1;
130+
DROP TABLE trunc_stats_test;
131+
DROP TABLE trunc_stats_dist_test;

src/test/regress/expected/multi_extension.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,11 +1484,12 @@ SELECT * FROM multi_extension.print_extension_changes();
14841484
| function citus_is_primary_node() boolean
14851485
| function citus_stat_counters(oid) SETOF record
14861486
| function citus_stat_counters_reset(oid) void
1487+
| function citus_stat_user_tables() TABLE(relname regclass, n_tup_ins bigint, n_tup_upd bigint, n_tup_del bigint, n_tup_hot_upd bigint, n_tup_newpage_upd bigint, n_live_tup bigint, n_dead_tup bigint)
14871488
| function citus_unmark_object_distributed(oid,oid,integer,boolean) void
14881489
| function shard_name(regclass,bigint,boolean) text
14891490
| view citus_nodes
14901491
| view citus_stat_counters
1491-
(34 rows)
1492+
(35 rows)
14921493

14931494
-- Test downgrade to 13.1-1 from 13.2-1
14941495
ALTER EXTENSION citus UPDATE TO '13.2-1';

src/test/regress/expected/upgrade_list_citus_objects.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ ORDER BY 1;
189189
function citus_stat_tenants_local_internal(boolean)
190190
function citus_stat_tenants_local_reset()
191191
function citus_stat_tenants_reset()
192+
function citus_stat_user_tables()
192193
function citus_table_is_visible(oid)
193194
function citus_table_size(regclass)
194195
function citus_task_wait(bigint,citus_task_status)
@@ -394,6 +395,6 @@ ORDER BY 1;
394395
view citus_stat_tenants_local
395396
view pg_dist_shard_placement
396397
view time_partitions
397-
(363 rows)
398+
(364 rows)
398399

399400
DROP TABLE extension_basic_types;

src/test/regress/sql/citus_aggregated_stats.sql

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,53 @@ DROP TABLE current_check;
5151
DROP TABLE dist_current_check;
5252

5353
RESET SESSION AUTHORIZATION;
54+
RESET row_security;
55+
56+
-- compare pg_stat_user_tables with citus_stat_user_tables
57+
CREATE TABLE trunc_stats_test(id serial);
58+
CREATE TABLE trunc_stats_dist_test(id serial);
59+
SELECT create_distributed_table('trunc_stats_dist_test', 'id');
60+
61+
-- rollback a savepoint: this should count 4 inserts and have 2
62+
-- live tuples after commit (and 2 dead ones due to aborted subxact)
63+
BEGIN;
64+
65+
INSERT INTO trunc_stats_test DEFAULT VALUES;
66+
INSERT INTO trunc_stats_test DEFAULT VALUES;
67+
68+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
69+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
70+
71+
SAVEPOINT p1;
72+
73+
INSERT INTO trunc_stats_test DEFAULT VALUES;
74+
INSERT INTO trunc_stats_test DEFAULT VALUES;
75+
TRUNCATE trunc_stats_test;
76+
INSERT INTO trunc_stats_test DEFAULT VALUES;
77+
78+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
79+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
80+
TRUNCATE trunc_stats_dist_test;
81+
INSERT INTO trunc_stats_dist_test DEFAULT VALUES;
82+
83+
ROLLBACK TO SAVEPOINT p1;
84+
COMMIT;
85+
86+
\c - - - :worker_1_port
87+
SELECT pg_stat_force_next_flush();
88+
\c - - - :worker_2_port
89+
SELECT pg_stat_force_next_flush();
90+
\c - - - :master_port
91+
SELECT pg_stat_force_next_flush();
92+
93+
SELECT relname, n_tup_ins, n_live_tup, n_dead_tup
94+
FROM pg_stat_user_tables
95+
WHERE relname like 'trunc_stats%';
96+
97+
SELECT relname, n_tup_ins, n_live_tup, n_dead_tup
98+
FROM citus_stat_user_tables();
99+
54100
REVOKE ALL ON SCHEMA public FROM user1;
55101
DROP USER user1;
102+
DROP TABLE trunc_stats_test;
103+
DROP TABLE trunc_stats_dist_test;

0 commit comments

Comments
 (0)