Skip to content

Commit aa5766b

Browse files
authored
Add a check "Don't use table inheritance" [pgwiki] (#112)
* Add a check "Don't use table inheritance" [pgwiki] * Update readme
1 parent a8042f4 commit aa5766b

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ For more information please see [PostgreSQL Versioning Policy](https://www.postg
6666
36. Tables where the primary key columns are not first ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/tables_where_primary_key_columns_not_first.sql)).
6767
37. Tables that have all columns besides the primary key that are nullable ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/tables_where_all_columns_nullable_except_pk.sql)).
6868
38. Columns with [char(n) type](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don't_use_char(n)) ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/columns_with_char_type.sql)).
69+
39. Tables with [inheritance](https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_table_inheritance) ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/tables_with_inheritance.sql)).
6970

7071
## Local development
7172

sql/tables_with_inheritance.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 use table inheritance.
9+
-- Never use table inheritance. This is a completely bad idea.
10+
--
11+
-- See https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_table_inheritance
12+
-- Not applicable to partitioned tables
13+
select
14+
pc.oid::regclass::text as table_name,
15+
pg_table_size(pc.oid) as table_size
16+
from
17+
pg_catalog.pg_class pc
18+
inner join pg_catalog.pg_inherits i on i.inhrelid = pc.oid /* child table oid */
19+
inner join pg_catalog.pg_namespace nsp on nsp.oid = pc.relnamespace
20+
where
21+
pc.relkind = 'r' and
22+
nsp.nspname = :schema_name_param::text
23+
order by table_name;

0 commit comments

Comments
 (0)