프로그래밍

[postgres]유용한 쿼리

빨간드래곤 2016. 11. 30. 09:17
select * from pg_stat_activity;

- db 통계정보 보기
SELECT * FROM pg_stat_database;

- 테이블 통계정보 보기
select * from pg_stat_all_tables

- db사이즈 조회
select * from pg_size_pretty(pg_database_size('testDatabase'));

- tablespace size조회
select * from pg_size_pretty(pg_tablespace_size('pg_default'));

-현재 스키마 조회
select current_schema(); 

- table 목록 보기
postgresql: \d
postgresql: SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';

- db목록 보기
postgresql: \l
postgresql: SELECT datname FROM pg_database;

- 컬럼 내용 보기
postgresql: \d table
postgresql: SELECT column_name FROM information_schema.columns WHERE table_name ='table';

DESCRIBE TABLE
postgresql: \d+ table
postgresql: SELECT column_name FROM information_schema.columns WHERE table_name ='table'


- user 정보 보기

select * from pg_user where usename = CURRENT_USER;


- 현재 유저 정보 보기

select CURRENT_USER;


-function 내용보기

SELECT prosrc FROM pg_proc WHERE proname = 'partitioning_trigger_login';



-index 조회하기


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
SELECT i.relname as indname,
       i.relowner as indowner,
       idx.indrelid::regclass,
       am.amname as indam,
       idx.indkey,
       ARRAY(
       SELECT pg_get_indexdef(idx.indexrelid, k + 1, true)
       FROM generate_subscripts(idx.indkey, 1) as k
       ORDER BY k
       ) as indkey_names,
       idx.indexprs IS NOT NULL as indexprs,
       idx.indpred IS NOT NULL as indpred
FROM   pg_index as idx
JOIN   pg_class as i
ON     i.oid = idx.indexrelid
JOIN   pg_am as am
ON     i.relam = am.oid
and i.relname not like 'pg%';



-컴마 붙이기

 select to_char(123456789, 'FM999,999,990'); 


-index table space 변경

ALTER INDEX tb_ham_log_login_2013_11_first_idx1 SET TABLESPACE tbs_ham