postgresql 的一些sql语句

文章介绍了在PostgreSQL中如何获取建表信息,包括创建自定义函数`tabledef`来检索表结构,以及查询数据库配置如TCP设置、连接数和最大连接数。此外,还涉及了外键的启用与禁用,以及查询外键信息的方法。同时,提到了表和视图的权限管理,以及SQL查询示例,如筛选当年数据和计算下一年的第一天。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. postgresql 获取建表信息

1.1 创建自定义函数

CREATE OR REPLACE FUNCTION tabledef(text,text) RETURNS text
LANGUAGE sql STRICT AS 
$$
WITH attrdef AS (
        SELECT n.nspname, c.relname, c.oid, pg_catalog.array_to_string(c.reloptions || array(select 'toast.' || x from pg_catalog.unnest(tc.reloptions) x), ', ') as relopts,
        c.relpersistence, a.attnum, a.attname, pg_catalog.format_type(a.atttypid, a.atttypmod) as atttype, 
        (SELECT substring(pg_catalog.pg_get_expr(d.adbin, d.adrelid, true) for 128) FROM pg_catalog.pg_attrdef d WHERE d.adrelid = a.attrelid AND d.adnum = a.attnum AND a.atthasdef) as attdefault,
        a.attnotnull, (SELECT c.collname FROM pg_catalog.pg_collation c, pg_catalog.pg_type t WHERE c.oid = a.attcollation AND t.oid = a.atttypid AND a.attcollation <> t.typcollation) as attcollation,
        a.attidentity, a.attgenerated 
        FROM pg_catalog.pg_attribute a 
        JOIN pg_catalog.pg_class c ON a.attrelid = c.oid 
        JOIN pg_catalog.pg_namespace n ON c.relnamespace = n.oid 
        LEFT JOIN pg_catalog.pg_class tc ON (c.reltoastrelid = tc.oid) 
        WHERE n.nspname = $1 AND c.relname = $2 AND a.attnum > 0 AND NOT a.attisdropped 
        ORDER BY a.attnum
    ), coldef AS (
        SELECT attrdef.nspname, attrdef.relname, attrdef.oid, attrdef.relopts, attrdef.relpersistence, pg_catalog.format('%I %s%s%s%s%s', attrdef.attname, attrdef.atttype, 
        case when attrdef.attcollation is null then '' else pg_catalog.format(' COLLATE %I', attrdef.attcollation) end, 
        case when attrdef.attnotnull then ' NOT NULL' else '' end, 
        case when attrdef.attdefault is null then '' else case when attrdef.attgenerated = 's' then pg_catalog.format(' GENERATED ALWAYS AS (%s) STORED', attrdef.attdefault) when attrdef.attgenerated <> '' then ' GENERATED AS NOT_IMPLEMENTED' else pg_catalog.format(' DEFAULT %s', attrdef.attdefault)  end  end,           
        case when attrdef.attidentity<>'' then pg_catalog.format(' GENERATED %s AS IDENTITY', case attrdef.attidentity when 'd' then 'BY DEFAULT' when 'a' then 'ALWAYS' else 'NOT_IMPLEMENTED' end)       else '' end ) as col_create_sql    
        FROM attrdef
        ORDER BY attrdef.attnum
    ), tabdef AS (
         SELECT coldef.nspname, coldef.relname, coldef.oid, coldef.relopts, coldef.relpersistence, concat(string_agg(coldef.col_create_sql, E',\n    ') , (select concat(E',\n    ',pg_get_constraintdef(oid)) from pg_constraint where contype='p' and conrelid = coldef.oid))  as cols_create_sql
         FROM coldef 
         GROUP BY coldef.nspname, coldef.relname, coldef.oid, coldef.relopts, coldef.relpersistence
    )
SELECT FORMAT( 'CREATE%s TABLE %I.%I%s%s%s;', 
                case tabdef.relpersistence when 't' then ' TEMP' when 'u' then ' UNLOGGED' else '' end, 
                tabdef.nspname, 
                tabdef.relname, 
                coalesce( (
                        SELECT FORMAT( E'\n    PARTITION OF %I.%I %s\n', pn.nspname, pc.relname, pg_get_expr(c.relpartbound, c.oid) ) 
                        FROM pg_class c 
                        JOIN pg_inherits i ON c.oid = i.inhrelid
                        JOIN pg_class pc ON pc.oid = i.inhparent
                        JOIN pg_namespace pn ON pn.oid = pc.relnamespace
                        WHERE c.oid = tabdef.oid ),
                        FORMAT( E' (\n    %s\n)', tabdef.cols_create_sql) 
                ),
                case when tabdef.relopts <> '' then format(' WITH (%s)', tabdef.relopts) else '' end,
                coalesce(E'\nPARTITION BY '||pg_get_partkeydef(tabdef.oid), '') 
            ) as table_create_sql
FROM tabdef
$$;

1.2 使用

select tabledef('public','t_sys_log');

在这里插入图片描述

2. 配置信息

-- 查看tcp设置
select * from pg_settings where name like '%tcp%';
-- 查看当前连接数
select count(1) from pg_stat_activity;
-- 查看最大连接数
show max_connections;

3. 外键相关

-- 禁用外键
alter table t_product disable trigger all;
-- 启用外键
alter table t_product enable trigger all;


-- 查询xxx数据库中的外键
SELECT
     tc.constraint_name, tc.table_name, kcu.column_name, 
     ccu.table_name AS foreign_table_name,
     ccu.column_name AS foreign_column_name,
     tc.is_deferrable,tc.initially_deferred
 FROM 
     information_schema.table_constraints AS tc 
     JOIN information_schema.key_column_usage AS kcu ON tc.constraint_name = kcu.constraint_name
     JOIN information_schema.constraint_column_usage AS ccu ON ccu.constraint_name = tc.constraint_name
 WHERE constraint_type = 'FOREIGN KEY' AND tc.table_catalog = 'xxx';

4.表、视图权限

-- xxx :用户名
select 'ALTER TABLE ' || table_name || ' OWNER TO xxx;' from information_schema.tables where table_schema='public';
select 'ALTER FUNCTION ' || routine_name || ' OWNER TO xxx;' from information_schema.routines where specific_schema = 'public' ORDER BY routine_name;

5. sql

#  查询 start_time 是当年的数据
select * fromm xxx where EXTRACT (YEAR FROM start_time)=EXTRACT (YEAR FROM now());

# 今年的第一天 + 1年
select (date_trunc('year',current_date) + interval'1 year')::DATE;
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值