Skip to content

Commit 6b41a96

Browse files
committed
Add new function get_col_from_qualstats() to fix creating wrong syntax
1 parent 702f652 commit 6b41a96

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

pg_plan_advsr--0.1.sql

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,20 @@ IMMUTABLE
9393
RETURNS NULL ON NULL INPUT;
9494

9595
-- This function can use on PG14 or above with pg_qualstats
96+
CREATE OR REPLACE FUNCTION plan_repo.get_col_from_qualstats(bigint)
97+
RETURNS TEXT
98+
AS $$
99+
SELECT pg_catalog.quote_ident(a.attname)
100+
FROM pg_qualstats() q
101+
JOIN pg_catalog.pg_class c ON coalesce(q.lrelid, q.rrelid) = c.oid
102+
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
103+
AND a.attnum = coalesce(q.lattnum, q.rattnum)
104+
JOIN pg_catalog.pg_operator op ON op.oid = q.opno
105+
WHERE q.qualnodeid = $1
106+
AND q.qualid is not null;
107+
$$ LANGUAGE sql;
108+
109+
96110
CREATE OR REPLACE FUNCTION plan_repo.get_extstat(bigint)
97111
RETURNS TABLE (suggest text) AS $$
98112
with all_quals as (
@@ -101,7 +115,7 @@ RETURNS TABLE (suggest text) AS $$
101115
lrelid as rel,
102116
pg_catalog.quote_ident(n.nspname) || '.' ||
103117
pg_catalog.quote_ident(c.relname) as relname,
104-
pg_qualstats_get_idx_col(qualnodeid, true) as col
118+
plan_repo.get_col_from_qualstats(qualnodeid) as col
105119
FROM pg_qualstats() q
106120
JOIN pg_catalog.pg_class c ON q.lrelid = c.oid
107121
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
@@ -112,7 +126,7 @@ RETURNS TABLE (suggest text) AS $$
112126
rrelid as rel,
113127
pg_catalog.quote_ident(n.nspname) || '.' ||
114128
pg_catalog.quote_ident(c.relname) as relname,
115-
pg_qualstats_get_idx_col(qualnodeid, true) as col
129+
plan_repo.get_col_from_qualstats(qualnodeid) as col
116130
FROM pg_qualstats() q
117131
JOIN pg_catalog.pg_class c ON q.rrelid = c.oid
118132
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace

0 commit comments

Comments
 (0)