Skip to content

Commit e2d7c33

Browse files
DGilfanovamfvanek
andauthored
Add check "Don't use varchar(n) by default" (#89)
* Add check "Don't use varchar(n) by default" * Apply suggestions from code review --------- Co-authored-by: Ivan Vakhrushev <[email protected]>
1 parent 34c1fd3 commit e2d7c33

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ For more information please see [PostgreSQL Versioning Policy](https://www.postg
5151
27. Objects whose names do not follow naming convention ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/objects_not_following_naming_convention.sql)).
5252
28. Columns whose names do not follow naming convention ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/columns_not_following_naming_convention.sql)).
5353
29. Primary keys with varchar columns instead of uuids ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/primary_keys_with_varchar.sql)).
54+
30. Columns with [varchar(n)](https://www.postgresql.org/docs/current/datatype-character.html) type ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/columns_with_fixed_length_varchar.sql)).
5455

5556
## Local development
5657

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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 columns of type 'varchar(n)'.
9+
--
10+
-- See also https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_varchar.28n.29_by_default
11+
select
12+
t.oid::regclass::text as table_name,
13+
col.attnotnull as column_not_null,
14+
quote_ident(col.attname) as column_name
15+
from
16+
pg_catalog.pg_class t
17+
inner join pg_catalog.pg_namespace nsp on nsp.oid = t.relnamespace
18+
inner join pg_catalog.pg_attribute col on col.attrelid = t.oid
19+
where
20+
t.relkind in ('r', 'p') and
21+
not t.relispartition and
22+
col.attnum > 0 and /* to filter out system columns such as oid, ctid, xmin, xmax, etc. */
23+
not col.attisdropped and
24+
col.atttypid = 'varchar'::regtype and
25+
col.atttypmod > 0 and /* only for fixed length varchar (not varchar without n) */
26+
nsp.nspname = :schema_name_param::text
27+
order by table_name, column_name;

0 commit comments

Comments
 (0)