You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add exclusion constraints to analyze/read_schema (#658)
This PR adds support for displaying exclusion constraints in the output
of `analyze`/`read_schema`.
Attributes:
* `name`: name of the constraint
* `method`: selected index method (`exclude.index_method` option)
* `columns`: the list of columns included in the constraint
* `predicate`: conditional expression if partials index
* `definition`: constraint definition
```
go run . analyze
{
...
"excludeConstraints": {
"forbid_duplicated_titles": {
"name": "forbid_duplicated_titles",
"method": "btree",
"predicate": "(title IS NOT NULL)",
"columns": [
"title"
],
"definition": "EXCLUDE USING btree (title WITH =) WHERE ((title IS NOT NULL))"
}
},
...
}
```
Furthermore, I added a new attribute called `exclusion` to indexes to
show if the index belongs to an exclusion constraint.
replace(reverse(split_part(reverse(pi.indexrelid::regclass::text), '.', 1)), '"', '') AS name, pi.indisunique, array_agg(a.attname) AS columns, pg_get_expr(pi.indpred, t.oid) AS predicate, am.amnameAS method, pg_get_indexdef(pi.indexrelid) AS definition
210
+
replace(reverse(split_part(reverse(pi.indexrelid::regclass::text), '.', 1)), '"', '') AS name, pi.indisunique, pi.indisexclusion, array_agg(a.attname) AS columns, pg_get_expr(pi.indpred, t.oid) AS predicate, am.amnameAS method, pg_get_indexdef(pi.indexrelid) AS definition
211
211
FROM pg_index pi
212
212
JOIN pg_attribute a ONa.attrelid=pi.indrelid
213
213
ANDa.attnum= ANY (pi.indkey)
@@ -234,56 +234,69 @@ BEGIN
234
234
ANDuc_attr.attnum= ANY (uc_constraint.conkey)
235
235
WHERE
236
236
uc_constraint.conrelid=t.oid
237
-
ANDuc_constraint.contype='u'GROUP BYuc_constraint.oid, uc_constraint.conname) AS uc_details), 'foreignKeys', (
237
+
ANDuc_constraint.contype='u'GROUP BYuc_constraint.oid, uc_constraint.conname) AS uc_details), 'excludeConstraints', (
fk_info.connameAS conname, fk_info.columnsAS columns, fk_info.relnameAS referencedTable, array_agg(ref_attr.attnameORDER BYref_attr.attname) AS referencedColumns, CASE WHEN fk_info.confmatchtype='f' THEN
243
-
'FULL'
244
-
WHEN fk_info.confmatchtype='p' THEN
245
-
'PARTIAL'
246
-
WHEN fk_info.confmatchtype='s' THEN
247
-
'SIMPLE'
248
-
END AS matchType, CASE WHEN fk_info.confdeltype='a' THEN
249
-
'NO ACTION'
250
-
WHEN fk_info.confdeltype='r' THEN
251
-
'RESTRICT'
252
-
WHEN fk_info.confdeltype='c' THEN
253
-
'CASCADE'
254
-
WHEN fk_info.confdeltype='d' THEN
255
-
'SET DEFAULT'
256
-
WHEN fk_info.confdeltype='n' THEN
257
-
'SET NULL'
258
-
END AS onDelete, CASE WHEN fk_info.confupdtype='a' THEN
259
-
'NO ACTION'
260
-
WHEN fk_info.confupdtype='r' THEN
261
-
'RESTRICT'
262
-
WHEN fk_info.confupdtype='c' THEN
263
-
'CASCADE'
264
-
WHEN fk_info.confupdtype='d' THEN
265
-
'SET DEFAULT'
266
-
WHEN fk_info.confupdtype='n' THEN
267
-
'SET NULL'
268
-
END AS onUpdate FROM (
242
+
xc_constraint.conname, array_agg(xc_attr.attnameORDER BYxc_constraint.conkey::int[]) AS columns, pg_get_expr(pi.indpred, t.oid) AS predicate, am.amnameAS method, pg_get_constraintdef(xc_constraint.oid) AS definition FROM pg_constraint AS xc_constraint
fk_info.connameAS conname, fk_info.columnsAS columns, fk_info.relnameAS referencedTable, array_agg(ref_attr.attnameORDER BYref_attr.attname) AS referencedColumns, CASE WHEN fk_info.confmatchtype='f' THEN
256
+
'FULL'
257
+
WHEN fk_info.confmatchtype='p' THEN
258
+
'PARTIAL'
259
+
WHEN fk_info.confmatchtype='s' THEN
260
+
'SIMPLE'
261
+
END AS matchType, CASE WHEN fk_info.confdeltype='a' THEN
262
+
'NO ACTION'
263
+
WHEN fk_info.confdeltype='r' THEN
264
+
'RESTRICT'
265
+
WHEN fk_info.confdeltype='c' THEN
266
+
'CASCADE'
267
+
WHEN fk_info.confdeltype='d' THEN
268
+
'SET DEFAULT'
269
+
WHEN fk_info.confdeltype='n' THEN
270
+
'SET NULL'
271
+
END AS onDelete, CASE WHEN fk_info.confupdtype='a' THEN
272
+
'NO ACTION'
273
+
WHEN fk_info.confupdtype='r' THEN
274
+
'RESTRICT'
275
+
WHEN fk_info.confupdtype='c' THEN
276
+
'CASCADE'
277
+
WHEN fk_info.confupdtype='d' THEN
278
+
'SET DEFAULT'
279
+
WHEN fk_info.confupdtype='n' THEN
280
+
'SET NULL'
281
+
END AS onUpdate FROM (
282
+
SELECT
283
+
fk_constraint.conname, fk_constraint.conrelid, fk_constraint.confrelid, fk_constraint.confkey, fk_cl.relname, fk_constraint.confmatchtype, fk_constraint.confdeltype, fk_constraint.confupdtype, array_agg(fk_attr.attnameORDER BYfk_attr.attname) AS columns FROM pg_constraint AS fk_constraint
284
+
INNER JOIN pg_class fk_cl ONfk_constraint.confrelid=fk_cl.oid-- join the referenced table
0 commit comments