postgresql的一些固定用法

工作中用pg,有些语法和其他的并不一样,记录下。

  • 查看数据库中各个表(索引)大小
select
    (n.nspname::text),
    c.relname::text as relation,
    pg_size_pretty(pg_total_relation_size(c.oid::regclass)) as total_table_size,
    pg_size_pretty(pg_indexes_size(c.oid::regclass)) as total_index_size
from
    pg_class c
    left join pg_tablespace t on c.reltablespace = t.oid
    left join pg_namespace n on n.oid = c.relnamespace
where (n.nspname <> all (array['pg_catalog'::name, 'information_schema'::name, 'pg_toast'::name]))
    and c.relkind = 'r'::"char"
order by
    (pg_total_relation_size(c.oid::regclass))
    desc;
  • 查看各个数据库的大小
select pg_database.datname, pg_size_pretty(pg_database_size(pg_database.datname)) AS size from pg_database;
  • 查看表的各个字段
   select * from information_schema.columns where table_schema='test_schema' and table_name='test_table';
  • 关联更新
update
    tmp.order_table
set
    create_time = tmp.test_order.create_time
from
    tmp.test_order
where
    tmp.test_order.id = tmp.order_table.id;
  • 去掉字段的非空限制
alter table test.table_name alter column column_1 drop not null;
    原文作者:nizaikanwome
    原文地址: https://segmentfault.com/a/1190000020335857
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞