Skip to content

Commit 949799b

Browse files
authored
Add a check to find tables with zero or one column (#80)
1 parent 438c1ab commit 949799b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2019-2025. Ivan Vakhrushev and others.
3+
* https://github.com/mfvanek/pg-index-health-sql
4+
*
5+
* Licensed under the Apache License 2.0
6+
*/
7+
8+
-- Finds tables that have zero or one column.
9+
-- This usually indicates a poor database design.
10+
-- If you really need a table with one column, for example, as a global index for partitioned tables,
11+
-- just ignore the results of this check.
12+
--
13+
-- Based on https://github.com/sdblist/db_verifier/blob/main/shards/r1002.sql
14+
select
15+
pc.oid::regclass::text as table_name,
16+
pg_table_size(pc.oid) as table_size,
17+
coalesce(array_agg(a.attname::text || ',' || a.attnotnull::text order by a.attnum) filter (where a.attname is not null), '{}') as columns
18+
from
19+
pg_catalog.pg_class pc
20+
inner join pg_catalog.pg_namespace nsp on nsp.oid = pc.relnamespace
21+
left join pg_catalog.pg_attribute a on a.attrelid = pc.oid and a.attnum > 0 and not a.attisdropped
22+
where
23+
pc.relkind in ('r', 'p') and
24+
not pc.relispartition and
25+
nsp.nspname = :schema_name_param::text
26+
group by pc.oid
27+
having count(a.attname) <= 1
28+
order by table_name;

0 commit comments

Comments
 (0)