Skip to content

Commit c4d1ab3

Browse files
committed
catch all errors during execution
1 parent 4421c56 commit c4d1ab3

File tree

3 files changed

+49
-14
lines changed

3 files changed

+49
-14
lines changed

index_advisor--0.2.0.sql

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ declare
1717
prepared_statement_name text = 'index_advisor_working_statement';
1818
hypopg_schema_name text = (select extnamespace::regnamespace::text from pg_extension where extname = 'hypopg');
1919
explain_plan_statement text;
20+
error_message text;
2021
rec record;
2122
plan_initial jsonb;
2223
plan_final jsonb;
@@ -35,17 +36,12 @@ begin
3536
-- Remove trailing semicolon
3637
query := regexp_replace(query, ';\s*$', '');
3738

38-
-- Disallow multiple statements
39-
if query ilike '%;%' then
40-
return query values (
41-
null::jsonb,
42-
null::jsonb,
43-
null::jsonb,
44-
null::jsonb,
45-
array[]::text[],
46-
array['Query must not contain a semicolon']::text[]
47-
);
48-
else
39+
begin
40+
-- Disallow multiple statements
41+
if query ilike '%;%' then
42+
raise exception 'Query must not contain a semicolon';
43+
end if;
44+
4945
-- Hack to support PostgREST because the prepared statement for args incorrectly defaults to text
5046
query := replace(query, 'WITH pgrst_payload AS (SELECT $1 AS json_data)', 'WITH pgrst_payload AS (SELECT $1::json AS json_data)');
5147

@@ -87,7 +83,7 @@ begin
8783
select
8884
distinct objid as oid
8985
from
90-
pg_depend
86+
pg_catalog.pg_depend
9187
where
9288
deptype = 'e'
9389
)
@@ -108,7 +104,7 @@ begin
108104
on pc.oid = pa.attrelid
109105
left join extension_regclass er
110106
on pc.oid = er.oid
111-
left join pg_index pi
107+
left join pg_catalog.pg_index pi
112108
on pc.oid = pi.indrelid
113109
and (select array_agg(x) from unnest(pi.indkey) v(x)) = array[pa.attnum]
114110
and pi.indexprs is null -- ignore expression indexes
@@ -174,8 +170,21 @@ begin
174170
statements::text[],
175171
array[]::text[]
176172
);
173+
return;
177174

178-
end if;
175+
exception when others then
176+
get stacked diagnostics error_message = MESSAGE_TEXT;
177+
178+
return query values (
179+
null::jsonb,
180+
null::jsonb,
181+
null::jsonb,
182+
null::jsonb,
183+
array[]::text[],
184+
array[error_message]::text[]
185+
);
186+
return;
187+
end;
179188

180189
end;
181190
$$;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
begin;
2+
-- Semicolons should be allowed in comments because they are common in prep stmts
3+
create extension index_advisor version '0.2.0' cascade;
4+
NOTICE: installing required extension "hypopg"
5+
select * from index_advisor(
6+
'SELECT concat(schemaname, $1, tablename, $2, policyname) as policy
7+
FROM pg_policies
8+
ORDER BY 1 desc'
9+
);
10+
startup_cost_before | startup_cost_after | total_cost_before | total_cost_after | index_statements | errors
11+
---------------------+--------------------+-------------------+------------------+------------------+---------------------------------------------------
12+
| | | | {} | {"could not determine data type of parameter $1"}
13+
(1 row)
14+
15+
rollback;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
begin;
2+
-- Semicolons should be allowed in comments because they are common in prep stmts
3+
create extension index_advisor version '0.2.0' cascade;
4+
5+
select * from index_advisor(
6+
'SELECT concat(schemaname, $1, tablename, $2, policyname) as policy
7+
FROM pg_policies
8+
ORDER BY 1 desc'
9+
);
10+
11+
rollback;

0 commit comments

Comments
 (0)