Permalink
Browse files

wrapper around pg_catalog.pg_get_functiondef() that clears out search…

…_path, so that all variable references are schema-qualified. (yuck)
  • Loading branch information...
erichanson committed Feb 8, 2019
1 parent 85744a7 commit 8177647155171a958677b235e859837ce3f945ec
Showing with 28 additions and 1 deletion.
  1. +1 −1 .gitignore
  2. +27 −0 src/pg-extension/pg_catalog_get_defs/pg_get_typedef.sql
@@ -1,7 +1,6 @@
*.swp
*.swo
*.pyc

src/pg-extension/bundle/bundle--*.*.*.sql
src/pg-extension/email/email--*.*.*.sql
src/pg-extension/endpoint/endpoint--*.*.*.sql
@@ -10,3 +9,4 @@ src/pg-extension/filesystem/filesystem--*.*.*.sql
src/pg-extension/meta/meta--*.*.*.sql
src/pg-extension/widget/widget--*.*.*.sql
src/pg-extension/semantics/semantics--*.*.*.sql
src/pg-extension/pg_catalog_get_defs/pg_catalog_get_defs--*.*.*.sql
@@ -1,3 +1,24 @@
/*
* pg_catalog.pg_get_functiondef_no_searchpath(oid)
*
* wraps the pg_get_functiondef() function, eliminating the search path so that
* fully schema-qualified names are used on types, tables, views, etc.
*/

create or replace function pg_catalog.pg_get_functiondef_no_searchpath(oid) returns text
language plpgsql
as $$
declare
defn text;
begin
set local search_path=pg_catalog;
select into defn pg_catalog.pg_get_functiondef($1);
return defn;
end;
$$;



-- written by RhodiumToad on IRC in one hour :)

create or replace function get_typedef_enum(oid) returns text
@@ -6,6 +27,7 @@ create or replace function get_typedef_enum(oid) returns text
declare
defn text;
begin
set local search_path=pg_catalog;
select into defn
format('CREATE TYPE %s AS ENUM (%s)',
$1::regtype,
@@ -23,6 +45,7 @@ create or replace function get_typedef_composite(oid) returns text
declare
defn text;
begin
set local search_path=pg_catalog;
select into defn
format('CREATE TYPE %s AS (%s)',
$1::regtype,
@@ -52,6 +75,7 @@ create or replace function get_typedef_domain(oid) returns text
declare
defn text;
begin
set local search_path=pg_catalog;
select into defn
format('CREATE DOMAIN %s AS %s%s%s',
$1::regtype,
@@ -72,6 +96,7 @@ create or replace function get_typedef_range(oid) returns text
declare
defn text;
begin
set local search_path=pg_catalog;
select into defn
format('CREATE TYPE %s AS RANGE (%s)',
$1::regtype,
@@ -109,6 +134,7 @@ create or replace function get_typedef_base(oid) returns text
declare
defn text;
begin
set local search_path=pg_catalog;
select into defn
format('CREATE TYPE %s AS (%s)',
$1::regtype,
@@ -164,6 +190,7 @@ create function get_typedef(typid oid) returns text
declare
r record;
begin
set local search_path=pg_catalog;
select into r * from pg_type where oid = typid;
if not found then
raise exception 'unknown type oid %', typid;

0 comments on commit 8177647

Please sign in to comment.