当前位置:   article > 正文

Postgresql查看自己创建的函数、过程、触发器函数_pg 查看说有的自定义函数

pg 查看说有的自定义函数

1 首先获取用户oid

注意这里指的是每个数据库对应一个用户,具体操作方法请参阅再谈PostgreSQL创建数据库.
在数据库用户创建完成后执行下面的命令获取数据库用户oid并记录.
注意获取数据库用户oid只能使用postgres用户登录数据库.

psql -h host -U postgres;
  • 1
select oid from pg_authid where rolname='core';
```# 2 查看数据库用户创建的函数、过程、触发器函数```bash
psql -h host -U postgres;
  • 1
  • 2
  • 3

2 查看自己创建的函数、过程、触发器函数

psql -h host -U 数据库用户 -d 数据库;
  • 1
select * from pg_proc where proowner=187178
  • 1

3 查看自己创建的表

psql -h host -U 数据库用户 -d 数据库;
  • 1
select * from pg_tables where tableowner='数据库用户'
  • 1

4 清除所有自己创建的函数

do $$
	declare 
		v_owner oid;
		v_rec record;
		v_rec1 record;
		v_argtypes text;
	begin
		v_owner := 16385; --用postgres用户查询获取 select oid from pg_authid where rolname='数据库用户名';
		for v_rec in select oid,proname,pronargs,proargtypes,prorettype from pg_proc as t1 where proowner=v_owner loop
			v_argtypes := '';
			if( 2279 = v_rec.prorettype ) then --触发器函数
				for v_rec1 in select t2.relname, t1.tgname 
					from pg_trigger as t1
					inner join pg_class as t2 on t2.oid = t1.tgrelid
					where tgfoid=v_rec.oid loop
						execute format('drop trigger if exists %s on %s;',v_rec1.tgname, v_rec1.relname);
						execute format('drop function if exists %s();',v_rec.proname);
						--raise notice  '%',  format('drop trigger if exists %s on %s;',v_rec1.tgname, v_rec1.relname);
						--raise notice  '%',  format('drop function if exists %s();',v_rec.proname);
				end loop;
			else
				if( v_rec.pronargs > 0 ) then --有参数的函数
					for i in 1..array_length(v_rec.proargtypes,1) loop
						v_argtypes := format('%s%s,',v_argtypes,(select typname from pg_type where oid=v_rec.proargtypes[i-1]));			
					end loop;
					v_argtypes := regexp_replace(v_argtypes,',$','');
					execute format('drop function if exists %s(%s);',v_rec.proname,v_argtypes);		
					--raise notice  '%',  format('drop function if exists %s(%s);%s',v_rec.proname,v_argtypes,v_rec.proargtypes);		
				else
					execute format('drop function if exists %s();',v_rec.proname);	
					--raise notice  '%',  format('drop function if exists %s();',v_rec.proname);
				end if;
			end if;
		end loop;
	end;
$$;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/786025
推荐阅读
相关标签
  

闽ICP备14008679号