SlideShare a Scribd company logo
PostgreSQL 11 새기능 소개
김상기
이 저작물은 크리에이티브 커먼즈 저작자표시 4.0 국제 라이선스에 따라 이용할 수 있습니다.
2
1. 마이그레이션 고려 사항
1.1. pg_dump --create
데이터베이스 생성 구문은 pg_dump 명령으로 만든다.
이때 encoding, lc_collate, lc_ctype 값은 무조건 덤프된다.
1.2. 함수 이름과 같은 칼럼 이름을 함수 호출 형식으로 사용될 경우, 함수 우선
1.3. 테이블이나 도메인에 정의된 제약 조건 이름은 유일해야 한다.
기존 중복된 이름 제약 조건 찾는 쿼리
select conrelid::regclass,conname,count(*) from pg_constraint
group by conrelid,conname having count(*) > 1;
1.4. to_number('12345', '9,999') → 1234(O), 1345(X)
11 버전 이전 버전
select row_to_json(x)
from (select *, row_to_json(null) from
generate_series(1, 1)) x;
row_to_json
---------------------------------------
{"generate_series":1,"row_to_json":null}
select row_to_json(x)
from (select *, row_to_json(null) from
generate_series(1, 1)) x;
row_to_json
-------------
3
1. 마이그레이션 고려 사항
1.5. xmltable, xpath 함수의 상대 경로 기준 바뀜
1.6. 시스템 카탈로그 변경 사항
1.7. 서버 환경 설정 매개 변수 변경 사항
replacement_sort_tuples 삭제 됨, jit=off 추가 됨, (외 14개 추가, 지면 관계상 생략)
11 버전 이전 버전
select xpath('r', '<d><r>a</r></d>');
xpath
-------
{}
select xpath('r', '<d><r>a</r></d>');
xpath
------------
{<r>a</r>}
삭제된 칼럼 추가된 칼럼
pg_class.relhaspkey
pg_proc.proisagg
pg_proc.proiswindow
pg_class.relrewrite
pg_proc.prokind
pg_attribute.atthasmissing
pg_index.indnkeyatts
pg_aggregate.aggfinalmodify
pg_aggregate.aggmfinalmodify
pg_partitioned_table.partdefid
pg_publication.pubtruncate
4
2. 프로시져
2.1. 핵심은 코드 블럭 내 commit, rollback이 가능하다는 것
* 이전 버전
(10) postgres@postgres=# do $$
begin
loop
perform pg_sleep(1);
insert into t values (1);
commit;
end loop;
end;
$$;
오류: PL/pgSQL의 트랜잭션을 시작/종료할 수 없음
힌트: 대신 BEGIN 블록을 EXCEPTION 절과 함께 사용하십시오.
구문: PL/pgSQL 함수 "inline_code_block" 의 6번째 SQL 문
* 11 버전
(11) postgres@postgres=# do $$ …
^CCancel request sent
오류: 사용자 요청에 의해 작업을 취소합니다.
구문: SQL 구문: "SELECT pg_sleep(1)"
PL/pgSQL 함수 "inline_code_block" 의 4번째 PERFORM
(11) postgres@postgres=# select * from t;
a
---
1
1
5
2. 프로시져
2.2. 함수와 프로시져 비교
(11) postgres@postgres=# create function insert_t() returns void
language plpgsql as $$
begin
loop
perform pg_sleep(1); insert into t values (1); commit;
end loop;
end;
$$;
(11) postgres@postgres=# select insert_t();
오류: 잘못된 트랜잭션 마침
구문: PL/pgSQL 함수 "insert_t()" 의 6번째 COMMIT
(11) postgres@postgres=# drop function insert_t; -- 같은 이름의 프로시져를 만들 수 없음
DROP FUNCTION
(11) postgres@postgres=# create procedure insert_t()
language plpgsql as $$
… 본문은 같은 코드
CREATE PROCEDURE
(11) postgres@postgres=# call insert_t();
^CCancel request sent
오류: 사용자 요청에 의해 작업을 취소합니다.
구문: SQL 구문: "SELECT pg_sleep(1)"
PL/pgSQL 함수 "insert_t()" 의 4번째 PERFORM
(11) postgres@postgres=# select * from t;
a
---
1
1
6
2. 프로시져
2.3. 출력(INOUT 인자 모드)
●
출력은 스칼라 반환만 허용
– 테이블이나, SETOF RECORD를 반환할 방법이 없음
refcursor (참조 커서)를 반환하고, FETCH 로 풀 수는 있음
2.4. 한계 (앞으로 개선 되어야 할 부분)
●
전통적으로 함수와 프로시져 용어를 함께 썼던 개념 분리 작업
- 연산자, 트리거에서 하위 호환성 때문에, PROCEDURE 여전히 허용
●
공식 문서를 잘 읽어야 함
●
코드 내 procedure 가 정말 procedure인지 확인 해야 함
●
drop function 대신에 drop routine
●
TABLE 반환 프로시져 지원
7
3. JIT 짜깁기 (Just-In Time compilation)
3.1. 개요
●
문자열로 된 쿼리를 실시간으로 바이트 코드로 바꿔(실시간 컴파일) 반복 연산
속도를 높인다. (기본 컴파일러 = llvm)
●
집계 함수, WHERE 절 뒤 조건절 연산, ORDER BY, CREATE INDEX 빨라짐
3.2. 지원 서버 만들기
●
configure --with-llvm
(그러나, CentOS 7.X 에서 그리 쉽게 만들어지지 않는다!)
llvm, cmake 패키지 설치, ᅟsrc/Makefile.global 파일 수정 필요
3.3. 환경 설정 변수
(11) postgres@postgres=# show jit<tab>
jit jit_expressions jit_provider
jit_above_cost jit_inline_above_cost jit_tuple_deforming
jit_debugging_support jit_optimize_above_cost
jit_dump_bitcode jit_profiling_support
● jit = on|off
●
jit_above_cost = 100000 : 기본값, 비용이 100000 이상일 때 jit 짜깁기 함
8
3. JIT 짜깁기 compilation
3.4. 사용
(11) postgres@postgres=# SET jit=on;
(11) postgres@postgres=# EXPLAIN ANALYZE SELECT sum(a.attnum)
FROM pg_attribute a, pg_attribute b;
QUERY PLAN
------------------------------------------------------------------------------
Aggregate (cost=100933.28..100933.29 rows=1 width=8) (actual time=1151.35...
-> Nested Loop (cost=0.00..84137.12 rows=6718464 width=2) (actual time...
...
Planning Time: 0.099 ms
JIT:
Functions: 6
Options: Inlining false, Optimization false, Expressions true, Deforming true
Timing: Generation 0.645 ms, Inlining 0.000 ms, Optimization 0.284 ms,
Emission 4.632 ms, Total 5.561 ms
Execution Time: 1152.101 ms
(11) postgres@postgres=# SET jit=off;
(11) postgres@postgres=# EXPLAIN ANALYZE SELECT sum(a.attnum) ... -- 같은 쿼리
QUERY PLAN
--------------------------------------------------------------------------------
Aggregate (cost=100933.28..100933.29 rows=1 width=8) (actual time=1245.782...
-> Nested Loop (cost=0.00..84137.12 rows=6718464 width=2) (actual time=...
...
Planning Time: 0.101 ms
Execution Time: 1245.834 ms
9
4. 파티션
4.1. 해시 파티션
(11) postgres@postgres=# create table users (userid varchar(20) , username varchar(40))
PARTITION BY HASH(userid);
(11) postgres@postgres=# create table users1 partition of users for values
WITH (MODULUS 3, REMAINDER 0);
(11) postgres@postgres=# create table users2 partition of users for values
WITH (MODULUS 3, REMAINDER 1);
(11) postgres@postgres=# create table users3 partition of users for values
WITH (MODULUS 3, REMAINDER 2);
(11) postgres@postgres=# d+ users
"public.users" 테이블
필드명 | 종류 | Collation | NULL허용 | 초기값 | 스토리지 | 통계수집량 | 설명
----------+-----------------------+-----------+----------+--------+----------+------------+------
userid | character varying(20) | | | | extended | |
username | character varying(40) | | | | extended | |
파티션 키: HASH (userid)
파티션들: users1 FOR VALUES WITH (modulus 3, remainder 0),
users2 FOR VALUES WITH (modulus 3, remainder 1),
users3 FOR VALUES WITH (modulus 3, remainder 2)
(11) postgres@postgres=# insert into users values ('user1','name1'),
('user2','name2'),('user3','name3');
(11) postgres@postgres=# select tableoid::regclass, * from users;
tableoid | userid | username
----------+--------+----------
users2 | user2 | name2
users3 | user1 | name1
users3 | user3 | name3
(3개 행)
10
4. 파티션
4.2. DEFAULT 하위 테이블
●
목록이나, 범위 파티션에서 기타 자료 보관용.
●
VALUES 대신에 DEFAULT
create table … partition of … DEFAULT
4.3. 파티션 키 UPDATE 명령
(11) postgres@postgres=# update users set userid = 'user4'
where userid = 'user2';
(11) postgres@postgres=# select tableoid::regclass, * from users;
tableoid | userid | username
----------+--------+----------
users3 | user1 | name1
users3 | user4 | name2
users3 | user3 | name3
(3개 행)
4.4. 제약 조건과 인덱스
●
이제 상위 테이블에서 지정하면 하위 테이블 만들 때 자동으로 만든다.
●
alter table attach 에서도 자동으로 만든다.
●
자동으로 만들어진 하위 테이블의 제약 조건과 인덱스는 개별 삭제 할 수 없다.
11
5. 병렬 쿼리
●
인덱스 만들 때, CREATE TABLE AS SELECT, SELECT INTO, 구체화한 뷰(mview)
만들 때.
●
hash join 에서.
●
union all 에서.
6. CREATE INDEX … INCLUDE
(11) postgres@postgres=# d addrcode
"public.addrcode" 테이블
필드명 | 종류 | Collation | NULL허용 | 초기값
--------+------+-----------+----------+--------
addrid | text | | not null |
aname | text | | |
upid | text | | |
인덱스들:
"addrcode_pkey" PRIMARY KEY, btree (addrid)
(11) postgres@postgres=# create index addrcode_upid_i on addrcode (upid)
INCLUDE (addrid, aname);
(11) postgres@postgres=# explain select addrid,aname from addrcode where upid = '3200';
QUERY PLAN
----------------------------------------------------------------------------------------
Index Only Scan using addrcode_upid_i on addrcode (cost=0.28..14.50 rows=14 width=26)
Index Cond: (upid = '3200'::text)
12
7. 그외
●
initdb --wal-segsize=1024, pg_resetwal --wal-segsize=1024
●
ecpg -C ORACLE
●
pg_verify_checksums
●
PL/Python 용 jsonb 형변환자, CREATE EXTENSION jsonb_plpython
●
새로운 내장 롤: pg_read_server_files, pg_write_server_files, pg_execute_server_program
●
PgDoc: sgml → xml
●
LOCK TABLE 뷰 …
●
VACUUM 테이블1, 테이블2, …
● pgsql : gdesc, exit, quit
13
7. 관련 링크
●
https://www.postgresql.org/about/press/presskit11/kr/
●
http://postgresql.kr/docs/11/release-11.html
●
https://momjian.us/main/writings/pgsql/features.pdf
●
https://h50146.www5.hpe.com/products/software/oe/linux/mainstream/su
pport/lcc/pdf/PostgreSQL_11_New_Features_beta1_en_20180525-1.pdf
<나는 기다립니다…>다비드 칼리 지음, 세르주 블로크 그림, 안수연 옮김, 문학동네어린이 펴냄, 2007 마지막 쪽
사진 출처: http://uple.net/2009

More Related Content

PDF
[Pgday.Seoul 2017] 8. PostgreSQL 10 새기능 소개 - 김상기
PDF
[Pgday.Seoul 2020] SQL Tuning
PDF
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
PDF
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자
PPTX
PostgreSql vaccum
PDF
Pgday bdr 천정대
PDF
Pg 클러스터 기반의 구성 및 이전, 그리고 인덱스 클러스터링
PDF
[2015-06-26] Oracle 성능 최적화 및 품질 고도화 3
[Pgday.Seoul 2017] 8. PostgreSQL 10 새기능 소개 - 김상기
[Pgday.Seoul 2020] SQL Tuning
[Pgday.Seoul 2021] 1. 예제로 살펴보는 포스트그레스큐엘의 독특한 SQL
[Pgday.Seoul 2017] 7. PostgreSQL DB Tuning 기업사례 - 송춘자
PostgreSql vaccum
Pgday bdr 천정대
Pg 클러스터 기반의 구성 및 이전, 그리고 인덱스 클러스터링
[2015-06-26] Oracle 성능 최적화 및 품질 고도화 3

What's hot (20)

PDF
[2015-06-12] Oracle 성능 최적화 및 품질 고도화 1
PDF
제 5회 엑셈 수요 세미나 자료 연구컨텐츠팀
PDF
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
PDF
[2015-06-19] Oracle 성능 최적화 및 품질 고도화 2
PDF
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
PDF
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
PDF
파이썬 데이터베이스 연결 1탄
PDF
TABLE ACCESS 패턴을 이용한 SQL 튜닝_Wh oracle
PDF
파이썬 데이터베이스 연결 2탄
PDF
Amazon aurora 2
PDF
Oracle Query Optimizer 관련 Parameter_OracleParameter
PDF
Result Cache 동작원리 및 활용방안_Wh oracle
PDF
KEEP BUFFER 활용 방안_Wh oracle
PDF
제 7회 엑셈 수요 세미나 자료 연구컨텐츠팀
PDF
Bind Peeking 한계에 따른 Adaptive Cursor Sharing 등장_Wh oracle
PDF
SQL Profile을 이용한 SQL Plan 변경_Wh oracle
PDF
Federated Engine 실무적용사례
PDF
Pg day seoul 2016 session_02_v1.0_ff
PDF
PostgreSQL Deep Internal
PDF
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
[2015-06-12] Oracle 성능 최적화 및 품질 고도화 1
제 5회 엑셈 수요 세미나 자료 연구컨텐츠팀
제 10회 엑셈 수요 세미나 자료 연구컨텐츠팀
[2015-06-19] Oracle 성능 최적화 및 품질 고도화 2
[2015-07-10-윤석준] Oracle 성능 관리 & v$sysstat
제 9회 엑셈 수요 세미나 자료 연구컨텐츠팀
파이썬 데이터베이스 연결 1탄
TABLE ACCESS 패턴을 이용한 SQL 튜닝_Wh oracle
파이썬 데이터베이스 연결 2탄
Amazon aurora 2
Oracle Query Optimizer 관련 Parameter_OracleParameter
Result Cache 동작원리 및 활용방안_Wh oracle
KEEP BUFFER 활용 방안_Wh oracle
제 7회 엑셈 수요 세미나 자료 연구컨텐츠팀
Bind Peeking 한계에 따른 Adaptive Cursor Sharing 등장_Wh oracle
SQL Profile을 이용한 SQL Plan 변경_Wh oracle
Federated Engine 실무적용사례
Pg day seoul 2016 session_02_v1.0_ff
PostgreSQL Deep Internal
[2015 07-06-윤석준] Oracle 성능 최적화 및 품질 고도화 4
Ad

Similar to [Pgday.Seoul 2018] PostgreSQL 11 새 기능 소개 (20)

PDF
PostgreSQL로 배우는 SQL 기초
PPTX
파이썬 스터디 15장
PDF
배치 프로그램에서 튜닝대상 SQL 추출하기_Wh oracle
PDF
ES6 for Node.js Study 2주차
PDF
EcmaScript6(2015) Overview
PPT
스프링처럼 JDBC 리팩터링하기
PDF
#17.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
PDF
Scala for play
PDF
From MSSQL to MySQL
PDF
2014.07.26 KSUG와 지앤선이 함께하는 테크니컬 세미나 - 나의 첫번째 자바8 람다식 (정대원)
PPTX
Clean code
PPTX
데이타베이스 기본튜닝
PDF
Basic git-commands
PPT
ecdevday8 웹개발자의 약한고리 SQL 뛰어넘기
PPTX
20150212 c++11 features used in crow
PDF
Presto User & Admin Guide
PDF
모델링 연습 리뷰
PDF
[오픈소스컨설팅]MyBatis Basic
PostgreSQL로 배우는 SQL 기초
파이썬 스터디 15장
배치 프로그램에서 튜닝대상 SQL 추출하기_Wh oracle
ES6 for Node.js Study 2주차
EcmaScript6(2015) Overview
스프링처럼 JDBC 리팩터링하기
#17.스프링프레임워크 & 마이바티스 (Spring Framework, MyBatis)_국비지원IT학원/실업자/재직자환급교육/자바/스프링/...
Scala for play
From MSSQL to MySQL
2014.07.26 KSUG와 지앤선이 함께하는 테크니컬 세미나 - 나의 첫번째 자바8 람다식 (정대원)
Clean code
데이타베이스 기본튜닝
Basic git-commands
ecdevday8 웹개발자의 약한고리 SQL 뛰어넘기
20150212 c++11 features used in crow
Presto User & Admin Guide
모델링 연습 리뷰
[오픈소스컨설팅]MyBatis Basic
Ad

More from PgDay.Seoul (20)

PDF
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정
PDF
[pgday.Seoul 2022] POSTGRES 테스트코드로 기여하기 - 이동욱
PDF
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
PDF
[pgday.Seoul 2022] PostgreSQL with Google Cloud
PDF
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
PDF
[Pgday.Seoul 2020] 포스트그레스큐엘 자국어화 이야기
PDF
[Pgday.Seoul 2019] AppOS 고성능 I/O 확장 모듈로 성능 10배 향상시키기
PDF
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
PDF
[Pgday.Seoul 2019] Advanced FDW
PDF
[Pgday.Seoul 2018] PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha
PDF
[Pgday.Seoul 2018] PostgreSQL Authentication with FreeIPA
PDF
[Pgday.Seoul 2018] 이기종 DB에서 PostgreSQL로의 Migration을 위한 DB2PG
PDF
[Pgday.Seoul 2018] AWS Cloud 환경에서 PostgreSQL 구축하기
PDF
[Pgday.Seoul 2018] Greenplum의 노드 분산 설계
PDF
[Pgday.Seoul 2018] replacing oracle with edb postgres
PDF
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
PDF
[Pgday.Seoul 2017] 5. 테드폴허브(올챙이) PostgreSQL 확장하기 - 조현종
PDF
[Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진
PDF
[Pgday.Seoul 2017] 4. Composite Type/JSON 파라미터를 활용한 TVP구현(with C#, JAVA) - 지현명
PDF
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오
[pgday.Seoul 2022] 서비스개편시 PostgreSQL 도입기 - 진소린 & 김태정
[pgday.Seoul 2022] POSTGRES 테스트코드로 기여하기 - 이동욱
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL with Google Cloud
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2020] 포스트그레스큐엘 자국어화 이야기
[Pgday.Seoul 2019] AppOS 고성능 I/O 확장 모듈로 성능 10배 향상시키기
[Pgday.Seoul 2019] Citus를 이용한 분산 데이터베이스
[Pgday.Seoul 2019] Advanced FDW
[Pgday.Seoul 2018] PostgreSQL 성능을 위해 개발된 라이브러리 OS 소개 apposha
[Pgday.Seoul 2018] PostgreSQL Authentication with FreeIPA
[Pgday.Seoul 2018] 이기종 DB에서 PostgreSQL로의 Migration을 위한 DB2PG
[Pgday.Seoul 2018] AWS Cloud 환경에서 PostgreSQL 구축하기
[Pgday.Seoul 2018] Greenplum의 노드 분산 설계
[Pgday.Seoul 2018] replacing oracle with edb postgres
[Pgday.Seoul 2017] 6. GIN vs GiST 인덱스 이야기 - 박진우
[Pgday.Seoul 2017] 5. 테드폴허브(올챙이) PostgreSQL 확장하기 - 조현종
[Pgday.Seoul 2017] 1. PostGIS의 사례로 본 PostgreSQL 확장 - 장병진
[Pgday.Seoul 2017] 4. Composite Type/JSON 파라미터를 활용한 TVP구현(with C#, JAVA) - 지현명
[Pgday.Seoul 2017] 3. PostgreSQL WAL Buffers, Clog Buffers Deep Dive - 이근오

[Pgday.Seoul 2018] PostgreSQL 11 새 기능 소개

  • 1. PostgreSQL 11 새기능 소개 김상기 이 저작물은 크리에이티브 커먼즈 저작자표시 4.0 국제 라이선스에 따라 이용할 수 있습니다.
  • 2. 2 1. 마이그레이션 고려 사항 1.1. pg_dump --create 데이터베이스 생성 구문은 pg_dump 명령으로 만든다. 이때 encoding, lc_collate, lc_ctype 값은 무조건 덤프된다. 1.2. 함수 이름과 같은 칼럼 이름을 함수 호출 형식으로 사용될 경우, 함수 우선 1.3. 테이블이나 도메인에 정의된 제약 조건 이름은 유일해야 한다. 기존 중복된 이름 제약 조건 찾는 쿼리 select conrelid::regclass,conname,count(*) from pg_constraint group by conrelid,conname having count(*) > 1; 1.4. to_number('12345', '9,999') → 1234(O), 1345(X) 11 버전 이전 버전 select row_to_json(x) from (select *, row_to_json(null) from generate_series(1, 1)) x; row_to_json --------------------------------------- {"generate_series":1,"row_to_json":null} select row_to_json(x) from (select *, row_to_json(null) from generate_series(1, 1)) x; row_to_json -------------
  • 3. 3 1. 마이그레이션 고려 사항 1.5. xmltable, xpath 함수의 상대 경로 기준 바뀜 1.6. 시스템 카탈로그 변경 사항 1.7. 서버 환경 설정 매개 변수 변경 사항 replacement_sort_tuples 삭제 됨, jit=off 추가 됨, (외 14개 추가, 지면 관계상 생략) 11 버전 이전 버전 select xpath('r', '<d><r>a</r></d>'); xpath ------- {} select xpath('r', '<d><r>a</r></d>'); xpath ------------ {<r>a</r>} 삭제된 칼럼 추가된 칼럼 pg_class.relhaspkey pg_proc.proisagg pg_proc.proiswindow pg_class.relrewrite pg_proc.prokind pg_attribute.atthasmissing pg_index.indnkeyatts pg_aggregate.aggfinalmodify pg_aggregate.aggmfinalmodify pg_partitioned_table.partdefid pg_publication.pubtruncate
  • 4. 4 2. 프로시져 2.1. 핵심은 코드 블럭 내 commit, rollback이 가능하다는 것 * 이전 버전 (10) postgres@postgres=# do $$ begin loop perform pg_sleep(1); insert into t values (1); commit; end loop; end; $$; 오류: PL/pgSQL의 트랜잭션을 시작/종료할 수 없음 힌트: 대신 BEGIN 블록을 EXCEPTION 절과 함께 사용하십시오. 구문: PL/pgSQL 함수 "inline_code_block" 의 6번째 SQL 문 * 11 버전 (11) postgres@postgres=# do $$ … ^CCancel request sent 오류: 사용자 요청에 의해 작업을 취소합니다. 구문: SQL 구문: "SELECT pg_sleep(1)" PL/pgSQL 함수 "inline_code_block" 의 4번째 PERFORM (11) postgres@postgres=# select * from t; a --- 1 1
  • 5. 5 2. 프로시져 2.2. 함수와 프로시져 비교 (11) postgres@postgres=# create function insert_t() returns void language plpgsql as $$ begin loop perform pg_sleep(1); insert into t values (1); commit; end loop; end; $$; (11) postgres@postgres=# select insert_t(); 오류: 잘못된 트랜잭션 마침 구문: PL/pgSQL 함수 "insert_t()" 의 6번째 COMMIT (11) postgres@postgres=# drop function insert_t; -- 같은 이름의 프로시져를 만들 수 없음 DROP FUNCTION (11) postgres@postgres=# create procedure insert_t() language plpgsql as $$ … 본문은 같은 코드 CREATE PROCEDURE (11) postgres@postgres=# call insert_t(); ^CCancel request sent 오류: 사용자 요청에 의해 작업을 취소합니다. 구문: SQL 구문: "SELECT pg_sleep(1)" PL/pgSQL 함수 "insert_t()" 의 4번째 PERFORM (11) postgres@postgres=# select * from t; a --- 1 1
  • 6. 6 2. 프로시져 2.3. 출력(INOUT 인자 모드) ● 출력은 스칼라 반환만 허용 – 테이블이나, SETOF RECORD를 반환할 방법이 없음 refcursor (참조 커서)를 반환하고, FETCH 로 풀 수는 있음 2.4. 한계 (앞으로 개선 되어야 할 부분) ● 전통적으로 함수와 프로시져 용어를 함께 썼던 개념 분리 작업 - 연산자, 트리거에서 하위 호환성 때문에, PROCEDURE 여전히 허용 ● 공식 문서를 잘 읽어야 함 ● 코드 내 procedure 가 정말 procedure인지 확인 해야 함 ● drop function 대신에 drop routine ● TABLE 반환 프로시져 지원
  • 7. 7 3. JIT 짜깁기 (Just-In Time compilation) 3.1. 개요 ● 문자열로 된 쿼리를 실시간으로 바이트 코드로 바꿔(실시간 컴파일) 반복 연산 속도를 높인다. (기본 컴파일러 = llvm) ● 집계 함수, WHERE 절 뒤 조건절 연산, ORDER BY, CREATE INDEX 빨라짐 3.2. 지원 서버 만들기 ● configure --with-llvm (그러나, CentOS 7.X 에서 그리 쉽게 만들어지지 않는다!) llvm, cmake 패키지 설치, ᅟsrc/Makefile.global 파일 수정 필요 3.3. 환경 설정 변수 (11) postgres@postgres=# show jit<tab> jit jit_expressions jit_provider jit_above_cost jit_inline_above_cost jit_tuple_deforming jit_debugging_support jit_optimize_above_cost jit_dump_bitcode jit_profiling_support ● jit = on|off ● jit_above_cost = 100000 : 기본값, 비용이 100000 이상일 때 jit 짜깁기 함
  • 8. 8 3. JIT 짜깁기 compilation 3.4. 사용 (11) postgres@postgres=# SET jit=on; (11) postgres@postgres=# EXPLAIN ANALYZE SELECT sum(a.attnum) FROM pg_attribute a, pg_attribute b; QUERY PLAN ------------------------------------------------------------------------------ Aggregate (cost=100933.28..100933.29 rows=1 width=8) (actual time=1151.35... -> Nested Loop (cost=0.00..84137.12 rows=6718464 width=2) (actual time... ... Planning Time: 0.099 ms JIT: Functions: 6 Options: Inlining false, Optimization false, Expressions true, Deforming true Timing: Generation 0.645 ms, Inlining 0.000 ms, Optimization 0.284 ms, Emission 4.632 ms, Total 5.561 ms Execution Time: 1152.101 ms (11) postgres@postgres=# SET jit=off; (11) postgres@postgres=# EXPLAIN ANALYZE SELECT sum(a.attnum) ... -- 같은 쿼리 QUERY PLAN -------------------------------------------------------------------------------- Aggregate (cost=100933.28..100933.29 rows=1 width=8) (actual time=1245.782... -> Nested Loop (cost=0.00..84137.12 rows=6718464 width=2) (actual time=... ... Planning Time: 0.101 ms Execution Time: 1245.834 ms
  • 9. 9 4. 파티션 4.1. 해시 파티션 (11) postgres@postgres=# create table users (userid varchar(20) , username varchar(40)) PARTITION BY HASH(userid); (11) postgres@postgres=# create table users1 partition of users for values WITH (MODULUS 3, REMAINDER 0); (11) postgres@postgres=# create table users2 partition of users for values WITH (MODULUS 3, REMAINDER 1); (11) postgres@postgres=# create table users3 partition of users for values WITH (MODULUS 3, REMAINDER 2); (11) postgres@postgres=# d+ users "public.users" 테이블 필드명 | 종류 | Collation | NULL허용 | 초기값 | 스토리지 | 통계수집량 | 설명 ----------+-----------------------+-----------+----------+--------+----------+------------+------ userid | character varying(20) | | | | extended | | username | character varying(40) | | | | extended | | 파티션 키: HASH (userid) 파티션들: users1 FOR VALUES WITH (modulus 3, remainder 0), users2 FOR VALUES WITH (modulus 3, remainder 1), users3 FOR VALUES WITH (modulus 3, remainder 2) (11) postgres@postgres=# insert into users values ('user1','name1'), ('user2','name2'),('user3','name3'); (11) postgres@postgres=# select tableoid::regclass, * from users; tableoid | userid | username ----------+--------+---------- users2 | user2 | name2 users3 | user1 | name1 users3 | user3 | name3 (3개 행)
  • 10. 10 4. 파티션 4.2. DEFAULT 하위 테이블 ● 목록이나, 범위 파티션에서 기타 자료 보관용. ● VALUES 대신에 DEFAULT create table … partition of … DEFAULT 4.3. 파티션 키 UPDATE 명령 (11) postgres@postgres=# update users set userid = 'user4' where userid = 'user2'; (11) postgres@postgres=# select tableoid::regclass, * from users; tableoid | userid | username ----------+--------+---------- users3 | user1 | name1 users3 | user4 | name2 users3 | user3 | name3 (3개 행) 4.4. 제약 조건과 인덱스 ● 이제 상위 테이블에서 지정하면 하위 테이블 만들 때 자동으로 만든다. ● alter table attach 에서도 자동으로 만든다. ● 자동으로 만들어진 하위 테이블의 제약 조건과 인덱스는 개별 삭제 할 수 없다.
  • 11. 11 5. 병렬 쿼리 ● 인덱스 만들 때, CREATE TABLE AS SELECT, SELECT INTO, 구체화한 뷰(mview) 만들 때. ● hash join 에서. ● union all 에서. 6. CREATE INDEX … INCLUDE (11) postgres@postgres=# d addrcode "public.addrcode" 테이블 필드명 | 종류 | Collation | NULL허용 | 초기값 --------+------+-----------+----------+-------- addrid | text | | not null | aname | text | | | upid | text | | | 인덱스들: "addrcode_pkey" PRIMARY KEY, btree (addrid) (11) postgres@postgres=# create index addrcode_upid_i on addrcode (upid) INCLUDE (addrid, aname); (11) postgres@postgres=# explain select addrid,aname from addrcode where upid = '3200'; QUERY PLAN ---------------------------------------------------------------------------------------- Index Only Scan using addrcode_upid_i on addrcode (cost=0.28..14.50 rows=14 width=26) Index Cond: (upid = '3200'::text)
  • 12. 12 7. 그외 ● initdb --wal-segsize=1024, pg_resetwal --wal-segsize=1024 ● ecpg -C ORACLE ● pg_verify_checksums ● PL/Python 용 jsonb 형변환자, CREATE EXTENSION jsonb_plpython ● 새로운 내장 롤: pg_read_server_files, pg_write_server_files, pg_execute_server_program ● PgDoc: sgml → xml ● LOCK TABLE 뷰 … ● VACUUM 테이블1, 테이블2, … ● pgsql : gdesc, exit, quit