Fixed incorrect behaviour of the option deduplicate items after creating the index. #8969

pull/8994/head
Murtuza Zabuawala 2025-07-28 13:23:39 +05:30 committed by GitHub
parent 79aeb5204c
commit e73d8f4532
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
25 changed files with 436 additions and 120 deletions

View File

@ -6,6 +6,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=100)
TABLESPACE pg_default;
COMMENT ON INDEX public."Idx1_$%{}[]()&*^!@""'`\/#"

View File

@ -6,4 +6,5 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=100)
TABLESPACE pg_default;

View File

@ -0,0 +1,14 @@
-- Index: Idx3_$%{}[]()&*^!@"'`\/#
-- DROP INDEX IF EXISTS public."Idx3_$%{}[]()&*^!@""'`\/#";
CREATE UNIQUE INDEX IF NOT EXISTS "Idx3_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS LAST, lower(name) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;
COMMENT ON INDEX public."Idx3_$%{}[]()&*^!@""'`\/#"
IS 'Test Comment';

View File

@ -0,0 +1,16 @@
-- Index: Idx1_$%{}[]()&*^!@"'`\/#
-- DROP INDEX IF EXISTS public."Idx1_$%{}[]()&*^!@""'`\/#";
CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.test_table_for_indexes
CLUSTER ON "Idx1_$%{}[]()&*^!@""'`\/#";
COMMENT ON INDEX public."Idx1_$%{}[]()&*^!@""'`\/#"
IS 'Test Comment';

View File

@ -0,0 +1,13 @@
-- Index: Idx1_$%{}[]()&*^!@"'`\/#
-- DROP INDEX IF EXISTS public."Idx1_$%{}[]()&*^!@""'`\/#";
CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=100, deduplicate_items=True)
TABLESPACE pg_default;
COMMENT ON INDEX public."Idx1_$%{}[]()&*^!@""'`\/#"
IS 'Test Comment';

View File

@ -0,0 +1,10 @@
-- Index: Idx_$%{}[]()&*^!@"'`\/#
-- DROP INDEX IF EXISTS public."Idx_$%{}[]()&*^!@""'`\/#";
CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
WITH (fillfactor=100, deduplicate_items=True)
TABLESPACE pg_default;

View File

@ -0,0 +1,14 @@
-- Index: Idx3_$%{}[]()&*^!@"'`\/#
-- DROP INDEX IF EXISTS public."Idx3_$%{}[]()&*^!@""'`\/#";
CREATE UNIQUE INDEX IF NOT EXISTS "Idx3_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS LAST, lower(name) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;
COMMENT ON INDEX public."Idx3_$%{}[]()&*^!@""'`\/#"
IS 'Test Comment';

View File

@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.test_table_for_indexes

View File

@ -7,6 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=100, deduplicate_items=True)
TABLESPACE pg_default;
COMMENT ON INDEX public."Idx1_$%{}[]()&*^!@""'`\/#"

View File

@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS FIRST)
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id ASC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -7,4 +7,5 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=100, deduplicate_items=True)
TABLESPACE pg_default;

View File

@ -7,7 +7,7 @@ CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
(id DESC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS LAST)
INCLUDE(name, id)
NULLS NOT DISTINCT
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -5,7 +5,7 @@
CREATE UNIQUE INDEX IF NOT EXISTS "Idx3_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS LAST, lower(name) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -5,7 +5,7 @@
CREATE UNIQUE INDEX IF NOT EXISTS "Idx1_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS FIRST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default;
ALTER TABLE IF EXISTS public.test_table_for_indexes

View File

@ -5,7 +5,7 @@
CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS FIRST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -1,7 +1,7 @@
CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS FIRST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS FIRST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -5,7 +5,7 @@
CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -5,7 +5,7 @@
CREATE UNIQUE INDEX IF NOT EXISTS "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS LAST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -1,7 +1,7 @@
CREATE UNIQUE INDEX "Idx_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id DESC NULLS LAST, name COLLATE pg_catalog."POSIX" text_pattern_ops DESC NULLS LAST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -5,7 +5,7 @@
CREATE UNIQUE INDEX IF NOT EXISTS "Idx3_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS LAST, lower(name) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -1,7 +1,7 @@
CREATE UNIQUE INDEX "Idx3_$%{}[]()&*^!@""'`\/#"
ON public.test_table_for_indexes USING btree
(id ASC NULLS LAST, (lower(name)) COLLATE pg_catalog."POSIX" text_pattern_ops ASC NULLS LAST)
WITH (fillfactor=10)
WITH (fillfactor=10, deduplicate_items=True)
TABLESPACE pg_default
WHERE id < 100;

View File

@ -1,36 +1,114 @@
SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey, indisclustered,
indisvalid, indisunique, indisprimary, n.nspname,indnatts,cls.reltablespace AS spcoid,
CASE WHEN (length(spcname::text) > 0 OR cls.relkind = 'I') THEN spcname ELSE
(SELECT sp.spcname FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace=sp.oid
WHERE dtb.oid = {{ did }}::oid)
END as spcname, conname,
tab.relname as tabname, indclass, con.oid AS conoid,
CASE WHEN contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description END AS description,
pg_catalog.pg_get_expr(indpred, indrelid, true) as indconstraint, contype, condeferrable, condeferred, amname,
(SELECT (CASE WHEN count(i.inhrelid) > 0 THEN true ELSE false END) FROM pg_inherits i WHERE i.inhrelid = cls.oid) as is_inherited,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fillfactor=([0-9]*)') AS fillfactor,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'gin_pending_list_limit=([0-9]*)') AS gin_pending_list_limit,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'pages_per_range=([0-9]*)') AS pages_per_range,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'buffering=([a-z]*)') AS buffering,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fastupdate=([a-z]*)')::boolean AS fastupdate,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'autosummarize=([a-z]*)')::boolean AS autosummarize,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'lists=([0-9]*)') AS lists
{% if datlastsysoid %}, (CASE WHEN cls.oid <= {{ datlastsysoid}}::oid THEN true ElSE false END) AS is_sys_idx {% endif %}
SELECT DISTINCT ON (cls.relname)
cls.oid,
cls.relname AS name,
idx.indrelid,
idx.indkey,
idx.indisclustered,
idx.indisvalid,
idx.indisunique,
idx.indisprimary,
n.nspname,
idx.indnatts,
COALESCE(cls.reltablespace, 0) AS spcoid,
COALESCE(
CASE
WHEN (cls.reltablespace <> 0 OR cls.relkind = 'I') THEN ta.spcname
ELSE (
SELECT sp.spcname
FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace = sp.oid
WHERE dtb.oid = {{ did }}::oid
LIMIT 1
)
END, 'pg_default'
) AS spcname,
con.conname,
tab.relname AS tabname,
idx.indclass,
con.oid AS conoid,
CASE
WHEN con.contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description
END AS description,
pg_catalog.pg_get_expr(idx.indpred, idx.indrelid, true) AS indconstraint,
con.contype,
con.condeferrable,
con.condeferred,
am.amname,
EXISTS (
SELECT 1 FROM pg_inherits inh WHERE inh.inhrelid = cls.oid
) AS is_inherited,
-- Options (with defaults per index type)
COALESCE(
substring(array_to_string(cls.reloptions, ',') FROM 'fillfactor=([0-9]*)')::int,
CASE am.amname
WHEN 'btree' THEN 100
WHEN 'gist' THEN 90
ELSE NULL
END
) AS fillfactor,
COALESCE(
CASE am.amname
WHEN 'gin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'gin_pending_list_limit=([0-9]*)')::int
ELSE NULL
END,
CASE am.amname WHEN 'gin' THEN 4096 * 1024 ELSE NULL END
) AS gin_pending_list_limit,
COALESCE(
CASE am.amname
WHEN 'brin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'pages_per_range=([0-9]*)')::int
ELSE NULL
END,
CASE am.amname WHEN 'brin' THEN 128 ELSE NULL END
) AS pages_per_range,
COALESCE(
CASE am.amname
WHEN 'gist' THEN substring(array_to_string(cls.reloptions, ',') FROM 'buffering=([a-z]*)')
ELSE NULL
END,
CASE am.amname WHEN 'gist' THEN 'auto' ELSE NULL END
) AS buffering,
COALESCE(
CASE am.amname
WHEN 'gin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'fastupdate=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'gin' THEN TRUE ELSE NULL END
) AS fastupdate,
COALESCE(
CASE am.amname
WHEN 'brin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'autosummarize=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'brin' THEN FALSE ELSE NULL END
) AS autosummarize,
COALESCE(
substring(array_to_string(cls.reloptions, ',') FROM 'lists=([0-9]*)')::int,
NULL
) AS lists
{% if datlastsysoid %}
, (cls.oid <= {{ datlastsysoid }}::oid) AS is_sys_idx
{% endif %}
FROM pg_catalog.pg_index idx
JOIN pg_catalog.pg_class cls ON cls.oid=indexrelid
JOIN pg_catalog.pg_class tab ON tab.oid=indrelid
LEFT OUTER JOIN pg_catalog.pg_tablespace ta on ta.oid=cls.reltablespace
JOIN pg_catalog.pg_namespace n ON n.oid=tab.relnamespace
JOIN pg_catalog.pg_am am ON am.oid=cls.relam
LEFT JOIN pg_catalog.pg_depend dep ON (dep.classid = cls.tableoid AND dep.objid = cls.oid AND dep.refobjsubid = '0' AND dep.refclassid=(SELECT oid FROM pg_catalog.pg_class WHERE relname='pg_constraint') AND dep.deptype='i')
LEFT OUTER JOIN pg_catalog.pg_constraint con ON (con.tableoid = dep.refclassid AND con.oid = dep.refobjid)
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}AND cls.oid = {{idx}}::OID {% endif %}
ORDER BY cls.relname
JOIN pg_catalog.pg_class cls ON cls.oid = idx.indexrelid
JOIN pg_catalog.pg_class tab ON tab.oid = idx.indrelid
LEFT JOIN pg_catalog.pg_tablespace ta ON ta.oid = cls.reltablespace
JOIN pg_catalog.pg_namespace n ON n.oid = tab.relnamespace
JOIN pg_catalog.pg_am am ON am.oid = cls.relam
LEFT JOIN pg_catalog.pg_depend dep ON dep.classid = cls.tableoid
AND dep.objid = cls.oid
AND dep.refobjsubid = 0
AND dep.refclassid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'pg_constraint')
AND dep.deptype = 'i'
LEFT JOIN pg_catalog.pg_constraint con ON con.tableoid = dep.refclassid AND con.oid = dep.refobjid
LEFT JOIN pg_catalog.pg_description des ON des.objoid = cls.oid AND des.classoid = 'pg_class'::regclass
LEFT JOIN pg_catalog.pg_description desp ON desp.objoid = con.oid AND desp.objsubid = 0 AND desp.classoid = 'pg_constraint'::regclass
WHERE idx.indrelid = {{ tid }}::OID
{% if not show_sys_objects %}
AND con.conname IS NULL
{% endif %}
{% if idx %}
AND cls.oid = {{ idx }}::OID
{% endif %}
ORDER BY cls.relname;

View File

@ -1,37 +1,120 @@
SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey, indisclustered,
indisvalid, indisunique, indisprimary, n.nspname,indnatts,cls.reltablespace AS spcoid,
CASE WHEN (length(spcname::text) > 0 OR cls.relkind = 'I') THEN spcname ELSE
(SELECT sp.spcname FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace=sp.oid
WHERE dtb.oid = {{ did }}::oid)
END as spcname, conname,
tab.relname as tabname, indclass, con.oid AS conoid,
CASE WHEN contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description END AS description,
pg_catalog.pg_get_expr(indpred, indrelid, true) as indconstraint, contype, condeferrable, condeferred, amname,
(SELECT (CASE WHEN count(i.inhrelid) > 0 THEN true ELSE false END) FROM pg_inherits i WHERE i.inhrelid = cls.oid) as is_inherited,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fillfactor=([0-9]*)') AS fillfactor,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'deduplicate_items=([a-z]*)')::boolean AS deduplicate_items,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'gin_pending_list_limit=([0-9]*)') AS gin_pending_list_limit,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'pages_per_range=([0-9]*)') AS pages_per_range,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'buffering=([a-z]*)') AS buffering,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fastupdate=([a-z]*)')::boolean AS fastupdate,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'autosummarize=([a-z]*)')::boolean AS autosummarize,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'lists=([0-9]*)') AS lists
{% if datlastsysoid %}, (CASE WHEN cls.oid <= {{ datlastsysoid}}::oid THEN true ElSE false END) AS is_sys_idx {% endif %}
SELECT DISTINCT ON (cls.relname)
cls.oid,
cls.relname AS name,
idx.indrelid,
idx.indkey,
idx.indisclustered,
idx.indisvalid,
idx.indisunique,
idx.indisprimary,
n.nspname,
idx.indnatts,
COALESCE(cls.reltablespace, 0) AS spcoid,
COALESCE(
CASE
WHEN (cls.reltablespace <> 0 OR cls.relkind = 'I') THEN ta.spcname
ELSE (
SELECT sp.spcname
FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace = sp.oid
WHERE dtb.oid = {{ did }}::oid
LIMIT 1
)
END, 'pg_default'
) AS spcname,
con.conname,
tab.relname AS tabname,
idx.indclass,
con.oid AS conoid,
CASE
WHEN con.contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description
END AS description,
pg_catalog.pg_get_expr(idx.indpred, idx.indrelid, true) AS indconstraint,
con.contype,
con.condeferrable,
con.condeferred,
am.amname,
EXISTS (
SELECT 1 FROM pg_inherits inh WHERE inh.inhrelid = cls.oid
) AS is_inherited,
-- Options (with defaults per index type)
COALESCE(
substring(array_to_string(cls.reloptions, ',') FROM 'fillfactor=([0-9]*)')::int,
CASE am.amname
WHEN 'btree' THEN 100
WHEN 'gist' THEN 90
ELSE NULL
END
) AS fillfactor,
COALESCE(
CASE am.amname
WHEN 'btree' THEN substring(array_to_string(cls.reloptions, ',') FROM 'deduplicate_items=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'btree' THEN TRUE ELSE NULL END
) AS deduplicate_items,
COALESCE(
CASE am.amname
WHEN 'gin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'gin_pending_list_limit=([0-9]*)')::int
ELSE NULL
END,
CASE am.amname WHEN 'gin' THEN 4096 * 1024 ELSE NULL END
) AS gin_pending_list_limit,
COALESCE(
CASE am.amname
WHEN 'brin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'pages_per_range=([0-9]*)')::int
ELSE NULL
END,
CASE am.amname WHEN 'brin' THEN 128 ELSE NULL END
) AS pages_per_range,
COALESCE(
CASE am.amname
WHEN 'gist' THEN substring(array_to_string(cls.reloptions, ',') FROM 'buffering=([a-z]*)')
ELSE NULL
END,
CASE am.amname WHEN 'gist' THEN 'auto' ELSE NULL END
) AS buffering,
COALESCE(
CASE am.amname
WHEN 'gin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'fastupdate=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'gin' THEN TRUE ELSE NULL END
) AS fastupdate,
COALESCE(
CASE am.amname
WHEN 'brin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'autosummarize=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'brin' THEN FALSE ELSE NULL END
) AS autosummarize,
COALESCE(
substring(array_to_string(cls.reloptions, ',') FROM 'lists=([0-9]*)')::int,
NULL
) AS lists
{% if datlastsysoid %}
, (cls.oid <= {{ datlastsysoid }}::oid) AS is_sys_idx
{% endif %}
FROM pg_catalog.pg_index idx
JOIN pg_catalog.pg_class cls ON cls.oid=indexrelid
JOIN pg_catalog.pg_class tab ON tab.oid=indrelid
LEFT OUTER JOIN pg_catalog.pg_tablespace ta on ta.oid=cls.reltablespace
JOIN pg_catalog.pg_namespace n ON n.oid=tab.relnamespace
JOIN pg_catalog.pg_am am ON am.oid=cls.relam
LEFT JOIN pg_catalog.pg_depend dep ON (dep.classid = cls.tableoid AND dep.objid = cls.oid AND dep.refobjsubid = '0' AND dep.refclassid=(SELECT oid FROM pg_catalog.pg_class WHERE relname='pg_constraint') AND dep.deptype='i')
LEFT OUTER JOIN pg_catalog.pg_constraint con ON (con.tableoid = dep.refclassid AND con.oid = dep.refobjid)
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}AND cls.oid = {{idx}}::OID {% endif %}
ORDER BY cls.relname
JOIN pg_catalog.pg_class cls ON cls.oid = idx.indexrelid
JOIN pg_catalog.pg_class tab ON tab.oid = idx.indrelid
LEFT JOIN pg_catalog.pg_tablespace ta ON ta.oid = cls.reltablespace
JOIN pg_catalog.pg_namespace n ON n.oid = tab.relnamespace
JOIN pg_catalog.pg_am am ON am.oid = cls.relam
LEFT JOIN pg_catalog.pg_depend dep ON dep.classid = cls.tableoid
AND dep.objid = cls.oid
AND dep.refobjsubid = 0
AND dep.refclassid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'pg_constraint')
AND dep.deptype = 'i'
LEFT JOIN pg_catalog.pg_constraint con ON con.tableoid = dep.refclassid AND con.oid = dep.refobjid
LEFT JOIN pg_catalog.pg_description des ON des.objoid = cls.oid AND des.classoid = 'pg_class'::regclass
LEFT JOIN pg_catalog.pg_description desp ON desp.objoid = con.oid AND desp.objsubid = 0 AND desp.classoid = 'pg_constraint'::regclass
WHERE idx.indrelid = {{ tid }}::OID
{% if not show_sys_objects %}
AND con.conname IS NULL
{% endif %}
{% if idx %}
AND cls.oid = {{ idx }}::OID
{% endif %}
ORDER BY cls.relname;

View File

@ -1,37 +1,121 @@
SELECT DISTINCT ON(cls.relname) cls.oid, cls.relname as name, indrelid, indkey, indisclustered,
indisvalid, indisunique, indisprimary, n.nspname,indnatts,cls.reltablespace AS spcoid, indnullsnotdistinct,
CASE WHEN (length(spcname::text) > 0 OR cls.relkind = 'I') THEN spcname ELSE
(SELECT sp.spcname FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace=sp.oid
WHERE dtb.oid = {{ did }}::oid)
END as spcname, conname,
tab.relname as tabname, indclass, con.oid AS conoid,
CASE WHEN contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description END AS description,
pg_catalog.pg_get_expr(indpred, indrelid, true) as indconstraint, contype, condeferrable, condeferred, amname,
(SELECT (CASE WHEN count(i.inhrelid) > 0 THEN true ELSE false END) FROM pg_inherits i WHERE i.inhrelid = cls.oid) as is_inherited,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fillfactor=([0-9]*)') AS fillfactor,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'deduplicate_items=([a-z]*)')::boolean AS deduplicate_items,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'gin_pending_list_limit=([0-9]*)') AS gin_pending_list_limit,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'pages_per_range=([0-9]*)') AS pages_per_range,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'buffering=([a-z]*)') AS buffering,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'fastupdate=([a-z]*)')::boolean AS fastupdate,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'autosummarize=([a-z]*)')::boolean AS autosummarize,
substring(pg_catalog.array_to_string(cls.reloptions, ',') from 'lists=([0-9]*)') AS lists
{% if datlastsysoid %}, (CASE WHEN cls.oid <= {{ datlastsysoid}}::oid THEN true ElSE false END) AS is_sys_idx {% endif %}
SELECT DISTINCT ON (cls.relname)
cls.oid,
cls.relname AS name,
idx.indrelid,
idx.indkey,
idx.indisclustered,
idx.indisvalid,
idx.indisunique,
idx.indisprimary,
n.nspname,
idx.indnatts,
COALESCE(cls.reltablespace, 0) AS spcoid,
idx.indnullsnotdistinct,
COALESCE(
CASE
WHEN (cls.reltablespace <> 0 OR cls.relkind = 'I') THEN ta.spcname
ELSE (
SELECT sp.spcname
FROM pg_catalog.pg_database dtb
JOIN pg_catalog.pg_tablespace sp ON dtb.dattablespace = sp.oid
WHERE dtb.oid = {{ did }}::oid
LIMIT 1
)
END, 'pg_default'
) AS spcname,
con.conname,
tab.relname AS tabname,
idx.indclass,
con.oid AS conoid,
CASE
WHEN con.contype IN ('p', 'u', 'x') THEN desp.description
ELSE des.description
END AS description,
pg_catalog.pg_get_expr(idx.indpred, idx.indrelid, true) AS indconstraint,
con.contype,
con.condeferrable,
con.condeferred,
am.amname,
EXISTS (
SELECT 1 FROM pg_inherits inh WHERE inh.inhrelid = cls.oid
) AS is_inherited,
-- Options (with defaults per index type)
COALESCE(
substring(array_to_string(cls.reloptions, ',') FROM 'fillfactor=([0-9]*)')::int,
CASE am.amname
WHEN 'btree' THEN 100
WHEN 'gist' THEN 90
ELSE NULL
END
) AS fillfactor,
COALESCE(
CASE am.amname
WHEN 'btree' THEN substring(array_to_string(cls.reloptions, ',') FROM 'deduplicate_items=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'btree' THEN TRUE ELSE NULL END
) AS deduplicate_items,
COALESCE(
CASE am.amname
WHEN 'gin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'gin_pending_list_limit=([0-9]*)')::int
ELSE NULL
END,
CASE am.amname WHEN 'gin' THEN 4096 * 1024 ELSE NULL END
) AS gin_pending_list_limit,
COALESCE(
CASE am.amname
WHEN 'brin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'pages_per_range=([0-9]*)')::int
ELSE NULL
END,
CASE am.amname WHEN 'brin' THEN 128 ELSE NULL END
) AS pages_per_range,
COALESCE(
CASE am.amname
WHEN 'gist' THEN substring(array_to_string(cls.reloptions, ',') FROM 'buffering=([a-z]*)')
ELSE NULL
END,
CASE am.amname WHEN 'gist' THEN 'auto' ELSE NULL END
) AS buffering,
COALESCE(
CASE am.amname
WHEN 'gin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'fastupdate=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'gin' THEN TRUE ELSE NULL END
) AS fastupdate,
COALESCE(
CASE am.amname
WHEN 'brin' THEN substring(array_to_string(cls.reloptions, ',') FROM 'autosummarize=([a-z]*)')::boolean
ELSE NULL
END,
CASE am.amname WHEN 'brin' THEN FALSE ELSE NULL END
) AS autosummarize,
COALESCE(
substring(array_to_string(cls.reloptions, ',') FROM 'lists=([0-9]*)')::int,
NULL
) AS lists
{% if datlastsysoid %}
, (cls.oid <= {{ datlastsysoid }}::oid) AS is_sys_idx
{% endif %}
FROM pg_catalog.pg_index idx
JOIN pg_catalog.pg_class cls ON cls.oid=indexrelid
JOIN pg_catalog.pg_class tab ON tab.oid=indrelid
LEFT OUTER JOIN pg_catalog.pg_tablespace ta on ta.oid=cls.reltablespace
JOIN pg_catalog.pg_namespace n ON n.oid=tab.relnamespace
JOIN pg_catalog.pg_am am ON am.oid=cls.relam
LEFT JOIN pg_catalog.pg_depend dep ON (dep.classid = cls.tableoid AND dep.objid = cls.oid AND dep.refobjsubid = '0' AND dep.refclassid=(SELECT oid FROM pg_catalog.pg_class WHERE relname='pg_constraint') AND dep.deptype='i')
LEFT OUTER JOIN pg_catalog.pg_constraint con ON (con.tableoid = dep.refclassid AND con.oid = dep.refobjid)
LEFT OUTER JOIN pg_catalog.pg_description des ON (des.objoid=cls.oid AND des.classoid='pg_class'::regclass)
LEFT OUTER JOIN pg_catalog.pg_description desp ON (desp.objoid=con.oid AND desp.objsubid = 0 AND desp.classoid='pg_constraint'::regclass)
WHERE indrelid = {{tid}}::OID
{% if not show_sys_objects %}
AND conname is NULL
{% endif %}
{% if idx %}AND cls.oid = {{idx}}::OID {% endif %}
ORDER BY cls.relname
JOIN pg_catalog.pg_class cls ON cls.oid = idx.indexrelid
JOIN pg_catalog.pg_class tab ON tab.oid = idx.indrelid
LEFT JOIN pg_catalog.pg_tablespace ta ON ta.oid = cls.reltablespace
JOIN pg_catalog.pg_namespace n ON n.oid = tab.relnamespace
JOIN pg_catalog.pg_am am ON am.oid = cls.relam
LEFT JOIN pg_catalog.pg_depend dep ON dep.classid = cls.tableoid
AND dep.objid = cls.oid
AND dep.refobjsubid = 0
AND dep.refclassid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'pg_constraint')
AND dep.deptype = 'i'
LEFT JOIN pg_catalog.pg_constraint con ON con.tableoid = dep.refclassid AND con.oid = dep.refobjid
LEFT JOIN pg_catalog.pg_description des ON des.objoid = cls.oid AND des.classoid = 'pg_class'::regclass
LEFT JOIN pg_catalog.pg_description desp ON desp.objoid = con.oid AND desp.objsubid = 0 AND desp.classoid = 'pg_constraint'::regclass
WHERE idx.indrelid = {{ tid }}::OID
{% if not show_sys_objects %}
AND con.conname IS NULL
{% endif %}
{% if idx %}
AND cls.oid = {{ idx }}::OID
{% endif %}
ORDER BY cls.relname;