| | |
| | | ----------------------------------------------------------------------------------------------------- |
| | | |
| | | show max_connections; |
| | | select count(1) from pg_stat_activity; |
| | | ----------------------------------------------------------------------------------------------------- a.查询表结构 |
| | | select * from pg_tables; |
| | | select * from pg_class order by relnamespace; |
| | | select relnamespace,relkind,relname from pg_class where relnamespace in (select oid from pg_namespace) and relkind='r' order by 1,2; |
| | | |
| | | |
| | | select c.relname as "表名", cast(obj_description(c.oid) as varchar) as "表名描述", |
| | | a.attnum as "序号", a.attname as "列名", |
| | | concat_ws('', t.typname, SUBSTRING(format_type(a.atttypid, a.atttypmod) from '\(.*\)')) as "字段类型", |
| | | d.description as "备注" |
| | | from pg_attribute a |
| | | left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum |
| | | left join pg_class c on a.attrelid = c.oid |
| | | left join pg_type t on a.atttypid = t.oid |
| | | where a.attnum >= 0 and c.relname like 'sys_%' and obj_description(c.oid) is not null --and relnamespace=20582 |
| | | order by c.relname desc, a.attnum asc; |
| | | ----------------------------------------------------------------------------------------------------- b.GUID生成函数 |
| | | CREATE or REPLACE FUNCTION new_guid() |
| | | RETURNS "pg_catalog"."varchar" AS $BODY$ |
| | | DECLARE |
| | | v_seed_value varchar(32); |
| | | BEGIN |
| | | select md5(inet_client_addr()::varchar || timeofday() || inet_server_addr()::varchar || to_hex(inet_client_port())) |
| | | into v_seed_value; |
| | | |
| | | return (substr(v_seed_value,1,8) || '-' || |
| | | substr(v_seed_value,9,4) || '-' || |
| | | substr(v_seed_value,13,4) || '-' || |
| | | substr(v_seed_value,17,4) || '-' || |
| | | substr(v_seed_value,21,12)); |
| | | END; |
| | | $BODY$ LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER; |
| | | |
| | | select new_guid(); |
| | | ----------------------------------------------------------------------------------------------------- 1.菜单表 |
| | | -- DROP TABLE IF EXISTS lf.sys_menu; |
| | | create table lf.sys_menu( |
| | |
| | | select a.* from lf.sys_dir a, rs b where a.pid=b.id |
| | | ) |
| | | select * FROM rs order by order_num; |
| | | ----------------------------------------------------------------------------------------------------- a.查询连接数 |
| | | show max_connections; |
| | | select count(1) from pg_stat_activity; |
| | | ----------------------------------------------------------------------------------------------------- b.查询表结构 |
| | | select * from pg_tables; |
| | | select * from pg_class order by relnamespace; |
| | | select relnamespace,relkind,relname from pg_class where relnamespace in (select oid from pg_namespace) and relkind='r' order by 1,2; |
| | | |
| | | |
| | | select c.relname as "表名", cast(obj_description(c.oid) as varchar) as "表名描述", |
| | | a.attnum as "序号", a.attname as "列名", |
| | | concat_ws('', t.typname, SUBSTRING(format_type(a.atttypid, a.atttypmod) from '\(.*\)')) as "字段类型", |
| | | d.description as "备注" |
| | | from pg_attribute a |
| | | left join pg_description d on d.objoid = a.attrelid and d.objsubid = a.attnum |
| | | left join pg_class c on a.attrelid = c.oid |
| | | left join pg_type t on a.atttypid = t.oid |
| | | where a.attnum >= 0 and c.relname like 'sys_%' and obj_description(c.oid) is not null --and relnamespace=20582 |
| | | order by c.relname desc, a.attnum asc; |
| | | ----------------------------------------------------------------------------------------------------- c.GUID生成函数 |
| | | CREATE or REPLACE FUNCTION new_guid() |
| | | RETURNS "pg_catalog"."varchar" AS $BODY$ |
| | | DECLARE |
| | | v_seed_value varchar(32); |
| | | BEGIN |
| | | select md5(inet_client_addr()::varchar || timeofday() || inet_server_addr()::varchar || to_hex(inet_client_port())) |
| | | into v_seed_value; |
| | | |
| | | return (substr(v_seed_value,1,8) || '-' || |
| | | substr(v_seed_value,9,4) || '-' || |
| | | substr(v_seed_value,13,4) || '-' || |
| | | substr(v_seed_value,17,4) || '-' || |
| | | substr(v_seed_value,21,12)); |
| | | END; |
| | | $BODY$ LANGUAGE 'plpgsql' VOLATILE SECURITY DEFINER; |
| | | |
| | | select new_guid(); |
| | | ----------------------------------------------------------------------------------------------------- d.递归查询函数 |
| | | -- drop function rec_query_dep(id integer, tab varchar); |
| | | create or replace function fn_rec_query(id integer, tab varchar) |
| | | returns varchar as $$ |
| | | declare |
| | | str varchar = ''; |
| | | rec varchar = ''; |
| | | begin |
| | | for rec in execute 'with recursive rs as(' || |
| | | 'select id,pid,name from lf.sys_' || tab || ' where id=' || id || |
| | | ' union select a.id,a.pid,a.name from lf.sys_' || tab || ' a, rs b where a.id=b.pid '|| |
| | | ') select name from rs order by id' |
| | | loop |
| | | str = str || '\' || rec; |
| | | end loop; |
| | | |
| | | if (char_length(str) > 1) then |
| | | str = substring(str, 2); |
| | | end if; |
| | | |
| | | return str; |
| | | end; |
| | | $$ language plpgsql; |
| | | |
| | | select fn_rec_query(1, 'dep'); |
| | | select fn_rec_query(21, 'dep'); |
| | | select * from lf.sys_dep order by id; |
| | | |
| | | select fn_rec_query(10, 'dir'); |
| | | select fn_rec_query(28, 'dir'); |
| | | select * from lf.sys_dir order by id; |
| | | ----------------------------------------------------------------------------------------------------- e. |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | ----------------------------------------------------------------------------------------------------- |
| | | |
| | | |
| | | |