Skip to content

Commit bc8a4ce

Browse files
committed
add more sanity checks
1 parent fc3a400 commit bc8a4ce

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

contrib/dump_stat/dump_stat--1.0.sql

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_operator(opid oid) RETURNS TEXT A
2929
into r;
3030

3131
if r is null then
32-
raise exception 'operator % does not exist', opid;
32+
raise exception '% is not a valid operator id', opid;
3333
end if;
3434

3535
if r.oprleft = 0 then
@@ -61,6 +61,10 @@ CREATE OR REPLACE FUNCTION to_schema_qualified_type(typid oid) RETURNS TEXT AS $
6161
on typnamespace = pg_namespace.oid
6262
where pg_type.oid = typid
6363
into result;
64+
65+
if result is null then
66+
raise exception '% is not a valid type id', typid;
67+
end if;
6468

6569
return result;
6670
END;
@@ -77,6 +81,10 @@ CREATE FUNCTION to_schema_qualified_relation(reloid oid) RETURNS TEXT AS $$
7781
on relnamespace = pg_namespace.oid
7882
where pg_class.oid = reloid
7983
into result;
84+
85+
if result is null then
86+
raise exception '% is not a valid relation id', reloid;
87+
end if;
8088

8189
return result;
8290
END;
@@ -172,6 +180,11 @@ CREATE FUNCTION to_namespace(nsp text) RETURNS OID AS $$
172180
from pg_catalog.pg_namespace
173181
where nspname = nsp
174182
into result;
183+
184+
if result is null then
185+
raise exception 'schema % does not exist',
186+
quote_literal(nsp);
187+
end if;
175188

176189
return result;
177190
END;
@@ -187,6 +200,10 @@ CREATE FUNCTION get_namespace(relation oid) RETURNS OID AS $$
187200
from pg_catalog.pg_class
188201
where oid = relation
189202
into result;
203+
204+
if result is null then
205+
raise exception 'relation % does not exist', relation;
206+
end if;
190207

191208
return result;
192209
END;
@@ -409,6 +426,9 @@ CREATE FUNCTION dump_statistic(schema_name text) RETURNS SETOF TEXT AS $$
409426
i text;
410427

411428
BEGIN
429+
-- validate schema name
430+
select to_namespace(schema_name);
431+
412432
for relid in
413433
select pg_class.oid
414434
from pg_catalog.pg_namespace
@@ -422,6 +442,11 @@ CREATE FUNCTION dump_statistic(schema_name text) RETURNS SETOF TEXT AS $$
422442
end loop;
423443

424444
return;
445+
446+
EXCEPTION
447+
when invalid_schema_name then
448+
raise exception 'schema % does not exist',
449+
quote_literal(schema_name);
425450
END;
426451
$$ LANGUAGE plpgsql;
427452

0 commit comments

Comments
 (0)