-
-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathindexes.sql.ts
More file actions
51 lines (50 loc) · 2.23 KB
/
Copy pathindexes.sql.ts
File metadata and controls
51 lines (50 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { literal } from 'pg-format'
import type { SQLQueryPropsWithSchemaFilterAndIdsFilter } from './common.js'
export const INDEXES_SQL = (props: SQLQueryPropsWithSchemaFilterAndIdsFilter) => /* SQL */ `
SELECT
idx.indexrelid::int8 AS id,
idx.indrelid::int8 AS table_id,
n.nspname AS schema,
idx.indnatts AS number_of_attributes,
idx.indnkeyatts AS number_of_key_attributes,
idx.indisunique AS is_unique,
idx.indisprimary AS is_primary,
idx.indisexclusion AS is_exclusion,
idx.indimmediate AS is_immediate,
idx.indisclustered AS is_clustered,
idx.indisvalid AS is_valid,
idx.indcheckxmin AS check_xmin,
idx.indisready AS is_ready,
idx.indislive AS is_live,
idx.indisreplident AS is_replica_identity,
idx.indkey::smallint[] AS key_attributes,
idx.indcollation::integer[] AS collation,
idx.indclass::integer[] AS class,
idx.indoption::smallint[] AS options,
idx.indpred AS index_predicate,
obj_description(idx.indexrelid, 'pg_class') AS comment,
ix.indexdef as index_definition,
am.amname AS access_method,
jsonb_agg(
jsonb_build_object(
'attribute_number', a.attnum,
'attribute_name', a.attname,
'data_type', format_type(a.atttypid, a.atttypmod)
)
ORDER BY a.attnum
) AS index_attributes
FROM
pg_index idx
JOIN pg_class c ON c.oid = idx.indexrelid
JOIN pg_namespace n ON c.relnamespace = n.oid
JOIN pg_am am ON c.relam = am.oid
JOIN pg_attribute a ON a.attrelid = c.oid AND a.attnum = ANY(idx.indkey)
JOIN pg_indexes ix ON c.relname = ix.indexname AND n.nspname = ix.schemaname
WHERE
${props.schemaFilter ? `n.nspname ${props.schemaFilter}` : 'true'}
${props.idsFilter ? `AND idx.indexrelid ${props.idsFilter}` : ''}
GROUP BY
idx.indexrelid, idx.indrelid, n.nspname, idx.indnatts, idx.indnkeyatts, idx.indisunique, idx.indisprimary, idx.indisexclusion, idx.indimmediate, idx.indisclustered, idx.indisvalid, idx.indcheckxmin, idx.indisready, idx.indislive, idx.indisreplident, idx.indkey, idx.indcollation, idx.indclass, idx.indoption, idx.indexprs, idx.indpred, ix.indexdef, am.amname
${props.limit ? `limit ${literal(props.limit)}` : ''}
${props.offset ? `offset ${literal(props.offset)}` : ''}
`