PostgreSQL實用查詢SQL

2021-06-16 14:39:34 字數 4252 閱讀 6252

參考:

postgresql實用查詢sql

--檢視

資料庫  www.2cto.com  

select * from pg_database;

--檢視表空間

select * from pg_tablespace;

--檢視語言

select * from pg_language;

--檢視角色使用者

select * from pg_user;

select * from pg_shadow;

select * from pg_roles;

--檢視會話程序

select * from pg_stat_activity;

--檢視表

select * from pg_tables where schemaname = 'public';

--檢視表字段

select * from information_schema.columns where table_schema = 'public' and table_name = 'pf_vip_org';

--檢視檢視

select * from pg_views where schemaname = 'public';

select * from information_schema.views where table_schema = 'public';

--檢視觸發器

select * from information_schema.triggers;

--檢視序列

select * from information_schema.sequences where sequence_schema = 'public';

--檢視約束

select * from pg_constraint where contype = 'p'  

--u unique,p primary,f foreign,c check,t trigger,x exclusion

select a.relname as table_name,b.conname as constraint_name,b.contype as constraint_type from pg_class a,pg_constraint b where a.oid = b.conrelid and a.relname = 'cc';

--檢視索引

select * from pg_index ;

--檢視表上存在哪些索引以及大小

select relname,n.amname as index_type from pg_class m,pg_am n where m.relam = n.oid and m.oid in (

select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc');

select c.relname,c2.relname, c2.relpages*8 as size_kb

from pg_class c, pg_class c2, pg_index i

where c.relname = 'cc' and

c.oid = i.indrelid and

c2.oid = i.indexrelid

order by c2.relname; 

--檢視索引定義

select b.indexrelid from pg_class a,pg_index b where a.oid = b.indrelid and a.relname = 'cc';

select pg_get_indexdef(b.indexrelid);

--檢視過程函式定義

select oid,* from pg_proc where proname = 'insert_platform_action_exist'; --oid = 24610

select * from pg_get_functiondef(24610);

--檢視表大小(不含索引等資訊)

select pg_relation_size('cc');                         --368640 byte

select pg_size_pretty(pg_relation_size('cc'))   --360 kb

--檢視db大小

select pg_size_pretty(pg_database_size('smiletao'));   --12m

--檢視伺服器db執行狀態

[postgres@eyar ~]$ pg_ctl status -d $pgdata

pg_ctl: server is running (pid: 2373)

/home/postgres/bin/postgres "-d" "/database/pgdata" 

--檢視每個db的使用情況(讀,寫,快取,更新,事務等)

select * from pg_stat_database

--檢視索引的使用情況

select * from pg_stat_user_indexes;

--檢視表所對應的資料檔案路徑與大小

select pg_relation_filepath(oid), relpages from pg_class where relname = 'empsalary';

--檢視索引與相關欄位及大小

select n.nspname as schema_name,

r.rolname as table_owner,

bc.relname as table_name,

ic.relname as index_name,

a.attname  as column_name,

bc.relpages*8 as index_size_kb     

from pg_namespace n,

pg_class bc,             -- base class

pg_class ic,             -- index class

pg_index i,

pg_attribute a,           -- att in base

pg_roles r

where bc.relnamespace = n.oid

and i.indrelid = bc.oid

and i.indexrelid = ic.oid

and bc.relowner = r.oid

and i.indkey[0] = a.attnum

and i.indnatts = 1

and a.attrelid = bc.oid

and n.nspname = 'public'

and bc.relname = 'cc'

order by schema_name, table_name, index_name, attname;

--檢視pg鎖

select * from pg_locks;

備註:relpages*8 是實際所佔磁碟大小

--檢視表空間大小

select pg_tablespace_size('pg_default');

--檢視序列與表的對應關係

with fq_objects as (select c.oid,c.relname as fqname ,

c.relkind, c.relname as relation

from pg_class c join pg_namespace n on n.oid = c.relnamespace ),

sequences as (select oid,fqname from fq_objects where relkind = 's'), 

tables    as (select oid, fqname from fq_objects where relkind = 'r' ) 

select

s.fqname as sequence,

'->' as depends,

t.fqname as table

from

pg_depend d join sequences s on s.oid = d.objid 

join tables t on t.oid = d.refobjid 

where

d.deptype = 'a' and t.fqname = 'cc';

PostgreSQL的實用程式

postgresql的實用程式 1 使用者實用程式 createdb 建立乙個新的postgresql的資料庫 和sql語句 create database 相同 createuser 建立乙個新的postgresql的使用者 和sql語句 create user 相同 dropdb 刪除資料庫 d...

postgresql分頁查詢

資料庫中存了3000w條資料,兩種分頁查詢測試時間 第一種select from test table where i id 1000 limit 100 time 0.016s 第二種select from test table limit 100 offset 1000 time 0.003s ...

PostgreSQL的查詢優化

postgresql 的查詢優化 資料庫管理系統中的 sql執行,有多種多樣,從 sql語句型別上講,有 ddl dml dql dcl。不同語句,被資料庫引擎執行,其執行方式 複雜程度都不相同。其中,最為複雜的,是 dql,查詢語句。查詢語句的執行,在資料庫中,又可以分為 2個階段,一是查詢計畫的...