赞
踩
名称 | 返回类型 | 描述 |
---|---|---|
pg_total_relation_size(regclass) | bigint | 指定表OID或表名使用的总磁盘空间,包括所有索引和TOAST数据。 |
pg_table_size(regclass) | bigint | 指定表OID或表名使用的磁盘空间,除去索引(但是包含TOAST,自由空间映射和可视映射) |
pg_indexes_size(regclass) | bigint | 关联指定表OID或表名的表索引的使用总磁盘空间 |
pg_database_size(name) | bigint | 指定名称的数据库使用的磁盘空间 |
pg_size_pretty(bigint) | text | 把字节计算的大小转换成一个人类易读的大小(使用KB 、MB 、GB 、TB) |
VACUUM regclass;
--释放具体的表(不会给操作系统,不会减少磁盘使用量)
VACUUM FULL regclass;
–立即释放磁盘空间给操作系统(可能锁表)
select pid, state, usename, query, query_start
from pg_stat_activity
where pid in (
select pid from pg_locks l
join pg_class t on l.relation = t.oid
and t.relkind = 'r');
select pid from pg_locks l join pg_class t on l.relation = t.oid where t.relkind = 'r' and t.relname = 'lockedtable';
SELECT pg_cancel_backend(pid);
SELECT
c.relname 表名称,
a.attname AS 字段名称,
col_description(a.attrelid,a.attnum) AS 注释,
format_type ( a.atttypid, a.atttypmod ) AS 类型,
CASE WHEN a.attnotnull='f' THEN '否' ELSE '是' END AS 是否必填,
a.attnum 序号
FROM
pg_class c
left join pg_attribute a on a.attrelid = c.oid
WHERE
a.attnum > 0
ORDER BY c.relname,a.attnum;
SELECT conname AS constraint_name, pg_get_constraintdef(con.oid) AS constraint_definition, relname AS table_name
FROM pg_constraint con
JOIN pg_class cls ON (cls.oid = con.conrelid AND cls.relkind = 'r') -- 只选择关系类型为 "r"(表)的对象
WHERE conname = '<constraint_name>';
string_agg(text,text)
SELECT a.id, a.db_name, string_agg(b.username, ',') FROM tbl_company a, tbl_user b WHERE a.id = b.company_id group by a.id;
regexp_split_to_table(string, pattern)
position(substring in string)
concat_ws(pattern,string1,string2,......)
例:
SELECT T::DATE 时间轴 FROM generate_series ( '2022-10-25' :: DATE, '2022-11-03' :: DATE, '1 days' ) AS T
WITH RECURSIVE a AS (
select xx,1 as level from table1 where id = '123'
union all
select d.xx,a.level +1 from table1 d join a on d.pid = a.id
) select * from a
select row_number() OVER(PARTITION by [分区字段] order by [排序字段]) 序号 from 表名
–示例,对成绩单中每个学生的成绩进行分组排序
select row_number() OVER(PARTITION by studentID order by core) 序号,name,core from schoolreport;
select 'Service' ~ '^[0-9\.]+$'
true-为数字
注意:where 条件的字段是约束的一部分
create unique index UN_tablename on tablename(name, phone,enable) where enable = '0';
raise notice '**%**',param; --param为占位符所在的变量
使用 EXPLAIN ANALYZE ....
待分析的SQL…查看执行时间及循环次数
再找出耗时较多的SQL进行修改
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。