Skip to content

Commit

Permalink
Add new function get_col_from_qualstats() to fix creating wrong syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
yamatattsu committed May 27, 2024
1 parent 702f652 commit 6b41a96
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions pg_plan_advsr--0.1.sql
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ IMMUTABLE
RETURNS NULL ON NULL INPUT;

-- This function can use on PG14 or above with pg_qualstats
CREATE OR REPLACE FUNCTION plan_repo.get_col_from_qualstats(bigint)
RETURNS TEXT
AS $$
SELECT pg_catalog.quote_ident(a.attname)
FROM pg_qualstats() q
JOIN pg_catalog.pg_class c ON coalesce(q.lrelid, q.rrelid) = c.oid
JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
AND a.attnum = coalesce(q.lattnum, q.rattnum)
JOIN pg_catalog.pg_operator op ON op.oid = q.opno
WHERE q.qualnodeid = $1
AND q.qualid is not null;
$$ LANGUAGE sql;


CREATE OR REPLACE FUNCTION plan_repo.get_extstat(bigint)
RETURNS TABLE (suggest text) AS $$
with all_quals as (
Expand All @@ -101,7 +115,7 @@ RETURNS TABLE (suggest text) AS $$
lrelid as rel,
pg_catalog.quote_ident(n.nspname) || '.' ||
pg_catalog.quote_ident(c.relname) as relname,
pg_qualstats_get_idx_col(qualnodeid, true) as col
plan_repo.get_col_from_qualstats(qualnodeid) as col
FROM pg_qualstats() q
JOIN pg_catalog.pg_class c ON q.lrelid = c.oid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
Expand All @@ -112,7 +126,7 @@ RETURNS TABLE (suggest text) AS $$
rrelid as rel,
pg_catalog.quote_ident(n.nspname) || '.' ||
pg_catalog.quote_ident(c.relname) as relname,
pg_qualstats_get_idx_col(qualnodeid, true) as col
plan_repo.get_col_from_qualstats(qualnodeid) as col
FROM pg_qualstats() q
JOIN pg_catalog.pg_class c ON q.rrelid = c.oid
JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
Expand Down

0 comments on commit 6b41a96

Please sign in to comment.