Skip to content

Commit 8739c65

Browse files
authored
Add ordering to check_not_valid_constraints.sql (#37)
* Add command for Windows * Add new checks to documentation * Rewrite query to return schema-qualified table name * Added CONTRIBUTING.md
1 parent 7d9e0c6 commit 8739c65

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

CONTRIBUTING.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Implementing a new check
2+
3+
### Write a new SQL query
4+
5+
Each database structure check starts with an SQL query to the pg_catalog.
6+
7+
1. [SQLFluff](https://github.com/sqlfluff/sqlfluff) is used as a linter for all sql queries
8+
2. All queries should be schema-aware, i.e. we filter out database objects on schema basis:
9+
```sql
10+
where
11+
nsp.nspname = :schema_name_param::text
12+
```
13+
3. All tables and indexes names in the query results should be schema-qualified.
14+
We use `::regclass` on `oid` for that.
15+
```sql
16+
select
17+
psui.relid::regclass::text as table_name,
18+
psui.indexrelid::regclass::text as index_name,
19+
```

README.md

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# pg-index-health-sql
2+
23
**pg-index-health-sql** is a set of sql-queries for analyzing and maintaining indexes and tables health in Postgresql databases.
34

45
[![Lint Code Base](https://github.com/mfvanek/pg-index-health-sql/actions/workflows/linter.yml/badge.svg)](https://github.com/mfvanek/pg-index-health-sql/actions/workflows/linter.yml)
@@ -8,18 +9,21 @@
89
[![Files](https://tokei.rs/b1/github/mfvanek/pg-index-health-sql?category=files)](https://github.com/mfvanek/pg-index-health-sql)
910

1011
## Supported PostgreSQL versions
12+
1113
[![PostgreSQL 12](https://img.shields.io/badge/PostgreSQL-12-green.svg)](https://www.postgresql.org/about/news/1976/ "PostgreSQL 12")
1214
[![PostgreSQL 13](https://img.shields.io/badge/PostgreSQL-13-green.svg)](https://www.postgresql.org/about/news/postgresql-13-released-2077/ "PostgreSQL 13")
1315
[![PostgreSQL 14](https://img.shields.io/badge/PostgreSQL-14-green.svg)](https://www.postgresql.org/about/news/postgresql-14-released-2318/ "PostgreSQL 14")
1416
[![PostgreSQL 15](https://img.shields.io/badge/PostgreSQL-15-green.svg)](https://www.postgresql.org/about/news/postgresql-15-released-2526/ "PostgreSQL 15")
1517
[![PostgreSQL 16](https://img.shields.io/badge/PostgreSQL-16-green.svg)](https://www.postgresql.org/about/news/postgresql-16-released-2715/ "PostgreSQL 16")
1618

1719
### Support for previous versions of PostgreSQL
20+
1821
Compatibility with PostgreSQL versions **9.6**, **10** and **11** is no longer guaranteed, but it is very likely.
1922
We focus only on the currently maintained versions of PostgreSQL.
2023
For more information please see [PostgreSQL Versioning Policy](https://www.postgresql.org/support/versioning/).
2124

2225
## Available checks
26+
2327
**pg-index-health-sql** allows you to detect the following problems:
2428
1. Invalid (broken) indexes ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/invalid_indexes.sql)).
2529
2. Duplicated (completely identical) indexes ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/duplicated_indexes.sql)).
@@ -36,10 +40,18 @@ For more information please see [PostgreSQL Versioning Policy](https://www.postg
3640
13. Columns with [json](https://www.postgresql.org/docs/current/datatype-json.html) type ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/columns_with_json_type.sql)).
3741
14. Columns of [serial types](https://www.postgresql.org/docs/current/datatype-numeric.html#DATATYPE-SERIAL) that are not primary keys ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/non_primary_key_columns_with_serial_types.sql)).
3842
15. Functions without [description](https://www.postgresql.org/docs/current/sql-comment.html) ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/functions_without_description.sql)).
43+
16. Indexes [with boolean](https://habr.com/ru/companies/tensor/articles/488104/) ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/indexes_with_boolean.sql))
44+
17. Tables with [not valid constraints](https://habr.com/ru/articles/800121/) ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/check_not_valid_constraints.sql))
45+
18. B-tree indexes [on array columns](https://habr.com/ru/articles/800121/) ([sql](https://github.com/mfvanek/pg-index-health-sql/blob/master/sql/btree_indexes_on_array_columns.sql))
3946

4047
## Local development
48+
4149
### Linting
42-
To run super-linter locally
50+
51+
#### macOS/Linux
52+
53+
To run super-linter locally:
54+
4355
```shell
4456
docker run \
4557
-e RUN_LOCAL=true \
@@ -48,3 +60,16 @@ docker run \
4860
-v $(pwd):/tmp/lint \
4961
ghcr.io/super-linter/super-linter:slim-v6
5062
```
63+
64+
#### Windows
65+
66+
Use `cmd` on Windows:
67+
68+
```shell
69+
docker run ^
70+
-e RUN_LOCAL=true ^
71+
-e USE_FIND_ALGORITHM=true ^
72+
-e VALIDATE_SQLFLUFF=true ^
73+
-v "%cd%":/tmp/lint ^
74+
ghcr.io/super-linter/super-linter:slim-v6
75+
```

sql/check_not_valid_constraints.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*/
77

88
select
9-
t.relname as table_name, -- Name of the table
9+
c.conrelid::regclass as table_name, -- Name of the table
1010
c.conname as constraint_name, -- Name of the constraint
1111
c.contype as constraint_type -- Type of the constraint
1212
from
@@ -16,4 +16,5 @@ from
1616
where
1717
not c.convalidated -- Constraints that have not yet been validated
1818
and c.contype in ('c', 'f') -- Focus on check and foreign key constraints
19-
and n.nspname = :schema_name_param::text; -- Make the query schema-aware
19+
and n.nspname = :schema_name_param::text -- Make the query schema-aware
20+
order by c.conrelid::regclass::text, c.conname;

0 commit comments

Comments
 (0)