SlideShare a Scribd company logo
1 of 14
Download to read offline
이지민
2019.08.23
DB Lock을 줄이는 5가지 팁
1. Default 컬럼을 추가할 때는 조심하세요.
Postgres SQL 10버전 이하에서는 컬럼을 추가할 때, 테이블을 새로 만들어냅니다.
그래서 테이블이 모두 복사될 때까지 ACCESS EXCLUSIVE 잠금이 발생해요.
1. Default 컬럼 추가 주의사항
1. Default 컬럼 추가 주의사항
ALTER TABLE items ADD COLUMN updated_timestamp timestamptz DEFAULT now();
ALTER TABLE items ADD COLUMN updated_timestamp timestamptz;
UPDATE items SET updated_timestamp = now();
do { numRowsUpdted = executeUpdate(
"UPDATE items SET updated_timestamp = ? " + "WHERE ctid IN (SELECT ctid
FROM items WHERE updated_timestamp IS NULL LIMIT 5000)", now);
} while (numRowsUpdted > 0);
안좋은 사례
좋은 사례
ALTER TABLE items ADD COLUMN deleted DEFAULT null
2. Lock TimeOut을 사용하세요.
1. Default 컬럼 추가 주의사항2. Lock Timeout
쿼리 실행이 지연되어, 오랜시간동안 트랜잭션 경합이 발생할까 두려울 때가 있습니다.
이런 경우, Lock Timeout을 사용하세요.
SET lock_timeout TO ’2s’
ALTER TABLE items ADD COLUMN updated_timestamp timestamptz DEFAULT now();
1. Default 컬럼 추가 주의사항2. Lock Timeout
타임아웃 기간동안 Lock이 해제되지 않는다면, 쿼리가 강제 종료됩니다.
3. 인덱스 추가 시, CONCURRENTLY 옵션을 추가해주세요.
3. 인덱스 추가 주의사항
CREATE INDEX idx_items__brand_id ON items USING brand_id;
CREATE INDEX CONCURRENTLY idx_items__brand_id
ON items USING brand_id;
안좋은 사례
좋은 사례
INDEX 생성 쿼리는 INSERT / UPDATE / DELETE 쿼리를 모두 막아요.
COUNCURRENTLY 옵션을 추가하면 DDL 명령문만 막습니다.
4. Lock을 발생시키는 쿼리는 최대한 늦게 실행해요.
4. Lock 쿼리 호출 시점 주의사항
BEGIN;
-- 기존 테이블 스키마를 그대로 복사합니다
CREATE TABLE temp_items (LIKE items INCLUDING ALL);
-- 테이블에 대량의 데이터를 밀어넣는 쿼리를 실행합니다
COPY items_new FROM 'new_item.csv' WITH CSV
-- 기존 테이블을 삭제하고, 새로운 테이블명으로 이름을 변경합니다
DROP TABLE items;
ALTER TABLE temp_items RENAME TO items;
COMMIT;
BEGIN;
-- item 테이블의 모든 읽기 / 쓰기가 Block 됩니다
TRUNCATE items;
-- 그리고 테이블에 대량의 데이터를 밀어넣는 작업은 시간이 많이 소요됩니다
COPY items FROM ‘new_item.csv' WITH CSV
COMMIT;
안좋은 사례
좋은 사례
5. VACCUM FULL은 절대 사용하지말아요.
1. Default 컬럼 추가 주의사항5. VACCUM FULL
PostgreSQL에서는 디스크 정리를 위하여 주기적으로 VACCUM을 실행해야합니다.
VACCUM FULL은 유효한 데이터들을 새로운 테이블에 다시 씁니다.
이 때, 테이블 전체 잠금이 발생할 수 있어서 절대 사용하면 안돼요.
1. Default 컬럼을 추가할 때는 조심하세요.
2. Lock TimeOut을 사용하세요.
3. 인덱스 추가 시, CONCURRENTLY 옵션을 추가해주세요.
4. Lock을 발생시키는 쿼리는 최대한 늦게 실행해요.
5. VACCUM FULL은 절대 사용하지말아요.
정리

More Related Content

What's hot

webservice scaling for newbie
webservice scaling for newbiewebservice scaling for newbie
webservice scaling for newbieDaeMyung Kang
 
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재PgDay.Seoul
 
GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)
GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)
GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)オラクルエンジニア通信
 
엘라스틱서치, 로그스태시, 키바나
엘라스틱서치, 로그스태시, 키바나엘라스틱서치, 로그스태시, 키바나
엘라스틱서치, 로그스태시, 키바나종민 김
 
How to build massive service for advance
How to build massive service for advanceHow to build massive service for advance
How to build massive service for advanceDaeMyung Kang
 
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유Hyojun Jeon
 
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2Tanel Poder
 
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and OptimizationPgDay.Seoul
 
누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...
누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...
누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...Amazon Web Services Korea
 
Ssd 성능시험 cubrid mysql
Ssd 성능시험 cubrid mysqlSsd 성능시험 cubrid mysql
Ssd 성능시험 cubrid mysqlswkim79
 
AWS Aurora 운영사례 (by 배은미)
AWS Aurora 운영사례 (by 배은미)AWS Aurora 운영사례 (by 배은미)
AWS Aurora 운영사례 (by 배은미)I Goo Lee.
 
[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニング
[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニング[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニング
[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニングオラクルエンジニア通信
 
Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]
Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]
Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]オラクルエンジニア通信
 
검색엔진이 데이터를 다루는 법 김종민
검색엔진이 데이터를 다루는 법 김종민검색엔진이 데이터를 다루는 법 김종민
검색엔진이 데이터를 다루는 법 김종민종민 김
 
What You Should Know About WebLogic Server 12c (12.2.1.2) #oow2015 #otntour2...
What You Should Know About WebLogic Server 12c (12.2.1.2)  #oow2015 #otntour2...What You Should Know About WebLogic Server 12c (12.2.1.2)  #oow2015 #otntour2...
What You Should Know About WebLogic Server 12c (12.2.1.2) #oow2015 #otntour2...Frank Munz
 
New availability features in oracle rac 12c release 2 anair ss
New availability features in oracle rac 12c release 2 anair   ssNew availability features in oracle rac 12c release 2 anair   ss
New availability features in oracle rac 12c release 2 anair ssAnil Nair
 
개발자를 위한 (블로그) 글쓰기 intro
개발자를 위한 (블로그) 글쓰기 intro개발자를 위한 (블로그) 글쓰기 intro
개발자를 위한 (블로그) 글쓰기 introSeongyun Byeon
 
Pgday bdr 천정대
Pgday bdr 천정대Pgday bdr 천정대
Pgday bdr 천정대PgDay.Seoul
 
【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]
【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]
【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]オラクルエンジニア通信
 

What's hot (20)

webservice scaling for newbie
webservice scaling for newbiewebservice scaling for newbie
webservice scaling for newbie
 
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
[pgday.Seoul 2022] PostgreSQL구조 - 윤성재
 
GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)
GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)
GoldenGateテクニカルセミナー3「Oracle GoldenGate Technical Deep Dive」(2016/5/11)
 
엘라스틱서치, 로그스태시, 키바나
엘라스틱서치, 로그스태시, 키바나엘라스틱서치, 로그스태시, 키바나
엘라스틱서치, 로그스태시, 키바나
 
How to build massive service for advance
How to build massive service for advanceHow to build massive service for advance
How to build massive service for advance
 
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유
[NDC18] 야생의 땅 듀랑고의 데이터 엔지니어링 이야기: 로그 시스템 구축 경험 공유
 
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
Tanel Poder - Troubleshooting Complex Oracle Performance Issues - Part 2
 
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
[Pgday.Seoul 2021] 2. Porting Oracle UDF and Optimization
 
누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...
누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...
누가 내 엔터프라이즈 고객을 클라우드로 옮겼을까?-양승호, Head of Cloud Modernization,AWS::AWS 마이그레이션 ...
 
Ssd 성능시험 cubrid mysql
Ssd 성능시험 cubrid mysqlSsd 성능시험 cubrid mysql
Ssd 성능시험 cubrid mysql
 
Oracle GoldenGate導入Tips
Oracle GoldenGate導入TipsOracle GoldenGate導入Tips
Oracle GoldenGate導入Tips
 
AWS Aurora 운영사례 (by 배은미)
AWS Aurora 운영사례 (by 배은미)AWS Aurora 운영사례 (by 배은미)
AWS Aurora 운영사례 (by 배은미)
 
[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニング
[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニング[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニング
[Oracle DBA & Developer Day 2014] しばちょう先生による特別講義! RMANの運用と高速化チューニング
 
Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]
Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]
Oracle Essbase for Oracle Cloud Infrastructure Marketplace のご紹介[2021年2月版]
 
검색엔진이 데이터를 다루는 법 김종민
검색엔진이 데이터를 다루는 법 김종민검색엔진이 데이터를 다루는 법 김종민
검색엔진이 데이터를 다루는 법 김종민
 
What You Should Know About WebLogic Server 12c (12.2.1.2) #oow2015 #otntour2...
What You Should Know About WebLogic Server 12c (12.2.1.2)  #oow2015 #otntour2...What You Should Know About WebLogic Server 12c (12.2.1.2)  #oow2015 #otntour2...
What You Should Know About WebLogic Server 12c (12.2.1.2) #oow2015 #otntour2...
 
New availability features in oracle rac 12c release 2 anair ss
New availability features in oracle rac 12c release 2 anair   ssNew availability features in oracle rac 12c release 2 anair   ss
New availability features in oracle rac 12c release 2 anair ss
 
개발자를 위한 (블로그) 글쓰기 intro
개발자를 위한 (블로그) 글쓰기 intro개발자를 위한 (블로그) 글쓰기 intro
개발자를 위한 (블로그) 글쓰기 intro
 
Pgday bdr 천정대
Pgday bdr 천정대Pgday bdr 천정대
Pgday bdr 천정대
 
【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]
【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]
【旧版】Oracle Database Cloud Service:サービス概要のご紹介 [2021年7月版]
 

[29DCF] PostgreSQL에서 DB Lock을 줄이는 5가지 팁

  • 2. 1. Default 컬럼을 추가할 때는 조심하세요.
  • 3. Postgres SQL 10버전 이하에서는 컬럼을 추가할 때, 테이블을 새로 만들어냅니다. 그래서 테이블이 모두 복사될 때까지 ACCESS EXCLUSIVE 잠금이 발생해요. 1. Default 컬럼 추가 주의사항
  • 4. 1. Default 컬럼 추가 주의사항 ALTER TABLE items ADD COLUMN updated_timestamp timestamptz DEFAULT now(); ALTER TABLE items ADD COLUMN updated_timestamp timestamptz; UPDATE items SET updated_timestamp = now(); do { numRowsUpdted = executeUpdate( "UPDATE items SET updated_timestamp = ? " + "WHERE ctid IN (SELECT ctid FROM items WHERE updated_timestamp IS NULL LIMIT 5000)", now); } while (numRowsUpdted > 0); 안좋은 사례 좋은 사례 ALTER TABLE items ADD COLUMN deleted DEFAULT null
  • 5. 2. Lock TimeOut을 사용하세요.
  • 6. 1. Default 컬럼 추가 주의사항2. Lock Timeout 쿼리 실행이 지연되어, 오랜시간동안 트랜잭션 경합이 발생할까 두려울 때가 있습니다. 이런 경우, Lock Timeout을 사용하세요.
  • 7. SET lock_timeout TO ’2s’ ALTER TABLE items ADD COLUMN updated_timestamp timestamptz DEFAULT now(); 1. Default 컬럼 추가 주의사항2. Lock Timeout 타임아웃 기간동안 Lock이 해제되지 않는다면, 쿼리가 강제 종료됩니다.
  • 8. 3. 인덱스 추가 시, CONCURRENTLY 옵션을 추가해주세요.
  • 9. 3. 인덱스 추가 주의사항 CREATE INDEX idx_items__brand_id ON items USING brand_id; CREATE INDEX CONCURRENTLY idx_items__brand_id ON items USING brand_id; 안좋은 사례 좋은 사례 INDEX 생성 쿼리는 INSERT / UPDATE / DELETE 쿼리를 모두 막아요. COUNCURRENTLY 옵션을 추가하면 DDL 명령문만 막습니다.
  • 10. 4. Lock을 발생시키는 쿼리는 최대한 늦게 실행해요.
  • 11. 4. Lock 쿼리 호출 시점 주의사항 BEGIN; -- 기존 테이블 스키마를 그대로 복사합니다 CREATE TABLE temp_items (LIKE items INCLUDING ALL); -- 테이블에 대량의 데이터를 밀어넣는 쿼리를 실행합니다 COPY items_new FROM 'new_item.csv' WITH CSV -- 기존 테이블을 삭제하고, 새로운 테이블명으로 이름을 변경합니다 DROP TABLE items; ALTER TABLE temp_items RENAME TO items; COMMIT; BEGIN; -- item 테이블의 모든 읽기 / 쓰기가 Block 됩니다 TRUNCATE items; -- 그리고 테이블에 대량의 데이터를 밀어넣는 작업은 시간이 많이 소요됩니다 COPY items FROM ‘new_item.csv' WITH CSV COMMIT; 안좋은 사례 좋은 사례
  • 12. 5. VACCUM FULL은 절대 사용하지말아요.
  • 13. 1. Default 컬럼 추가 주의사항5. VACCUM FULL PostgreSQL에서는 디스크 정리를 위하여 주기적으로 VACCUM을 실행해야합니다. VACCUM FULL은 유효한 데이터들을 새로운 테이블에 다시 씁니다. 이 때, 테이블 전체 잠금이 발생할 수 있어서 절대 사용하면 안돼요.
  • 14. 1. Default 컬럼을 추가할 때는 조심하세요. 2. Lock TimeOut을 사용하세요. 3. 인덱스 추가 시, CONCURRENTLY 옵션을 추가해주세요. 4. Lock을 발생시키는 쿼리는 최대한 늦게 실행해요. 5. VACCUM FULL은 절대 사용하지말아요. 정리