PostgreSQL 17.12실제 DB 검증 · 2026-11-29

PostgreSQL 57014: 명령실행시간 초과로 작업을 취소합니다 (statement timeout)

쿼리가 statement_timeout보다 오래 걸려서 PostgreSQL이 취소했어요.

PostgreSQL 17.12
57014: 명령실행시간 초과로 작업을 취소합니다.
57014: canceling statement due to statement timeout

원인 바로가기

  1. 01역할에 걸린 타임아웃이 긴 리포트를 끊음
  2. 02SET statement_timeout = 5 는 5밀리초
  3. 03쓸 인덱스가 없어 느린 쿼리 (Seq Scan)

실제 DB 검증PostgreSQL 17.12에서 재현·해결 확인 · 2026-11-29 · 컨테이너 이미지 postgres:17

SQLSTATE 57014는 문장이 취소됐다는 뜻이에요. 메시지가 canceling statement due to statement timeout이면 원인은 statement_timeout 설정이에요. 그보다 오래 걸리는 문장은 중단되고 트랜잭션도 실패 상태가 돼요.

이 값은 세션, 역할(role), 데이터베이스, postgresql.conf 어디서든 올 수 있어요. 지금 적용된 값은 SHOW statement_timeout;으로 봐요. 아래 3가지 검증에서 제한이 어디서 왔는지와 어떻게 해결했는지 보여 드려요.

01역할에 걸린 타임아웃이 긴 리포트를 끊음

DBA가 ALTER ROLE lab_a SET statement_timeout = '1s'를 걸어 뒀어요. 5천만 건을 세는 리포트가 57014로 실패했어요. 세션에서 SHOW statement_timeout은 1s, pg_db_role_setting에는 lab_a에 {statement_timeout=1s}가 있었어요.

그 작업만 BEGIN; SET LOCAL statement_timeout = '60s'; ... COMMIT;으로 감싸니 끝까지 돌았어요(50000000). COMMIT 뒤 SHOW statement_timeout은 다시 1s라서, 나머지 쿼리는 역할 설정이 그대로 적용돼요.

사전 준비
사전 준비postgres 계정으로 실행
ALTER ROLE lab_a SET statement_timeout = '1s';
OUTPUT
ALTER ROLE
재현lab_a 계정으로 실행
SELECT count(*) AS n FROM generate_series(1, 50000000);
✕ 에러
ERROR:  57014: canceling statement due to statement timeout
확인lab_a 계정으로 실행
SHOW statement_timeout;
OUTPUT
 statement_timeout
-------------------
 1s
(1 row)
확인postgres 계정으로 실행
SELECT r.rolname, s.setconfig FROM pg_db_role_setting s JOIN pg_roles r ON r.oid = s.setrole WHERE r.rolname = 'lab_a';
OUTPUT
 rolname |       setconfig
---------+------------------------
 lab_a   | {statement_timeout=1s}
(1 row)

SET LOCAL은 트랜잭션이 끝날 때까지만 유효해요. 역할 전체 제한을 올리지 않고 배치 하나에만 시간을 더 줄 때 써요.

해결 후 재실행lab_a 계정으로 실행
BEGIN;
SET LOCAL statement_timeout = '60s';
SELECT count(*) AS n FROM generate_series(1, 50000000);
COMMIT;
SHOW statement_timeout;
✓ 성공
BEGIN
SET
    n
----------
 50000000
(1 row)

COMMIT
 statement_timeout
-------------------
 1s
(1 row)

02SET statement_timeout = 5 는 5밀리초

SET statement_timeout = 5 뒤에 500만 건 count가 57014로 실패했어요. SHOW statement_timeout은 5ms였어요. 단위 없는 숫자는 초가 아니라 밀리초예요.

SET statement_timeout = '5s'로 바꾸니 SHOW가 5s를 보여 줬고 count도 5000000이 나왔어요.

재현lab_a 계정으로 실행
SET statement_timeout = 5;
SELECT count(*) AS n FROM generate_series(1, 5000000);
✕ 에러
SET
ERROR:  57014: canceling statement due to statement timeout
확인lab_a 계정으로 실행
SET statement_timeout = 5;
SHOW statement_timeout;
OUTPUT
SET
 statement_timeout
-------------------
 5ms
(1 row)
해결 후 재실행lab_a 계정으로 실행
SET statement_timeout = '5s';
SHOW statement_timeout;
SELECT count(*) AS n FROM generate_series(1, 5000000);
✓ 성공
SET
 statement_timeout
-------------------
 5s
(1 row)

    n
---------
 5000000
(1 row)

03쓸 인덱스가 없어 느린 쿼리 (Seq Scan)

members는 200만 행이에요. 타임아웃 100ms에서 WHERE lower(email) = '...'가 57014로 실패했어요. EXPLAIN을 보면 Parallel Seq Scan on members에 lower(email) 필터라 모든 행을 읽고 있었어요.

CREATE INDEX members_lower_email_idx ON members (lower(email)) 뒤에는 실행 계획이 Index Scan using members_lower_email_idx가 됐고, 같은 100ms 제한 안에서 id 1999999가 나왔어요.

사전 준비
사전 준비lab_a 계정으로 실행
CREATE TABLE members (id int PRIMARY KEY, email text NOT NULL);
INSERT INTO members SELECT g, 'User' || g || '@Example.com' FROM generate_series(1, 2000000) g;
ANALYZE members;
OUTPUT
CREATE TABLE
INSERT 0 2000000
ANALYZE
재현lab_a 계정으로 실행
SET statement_timeout = '100ms';
SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
✕ 에러
SET
ERROR:  57014: canceling statement due to statement timeout
확인lab_a 계정으로 실행
EXPLAIN (COSTS OFF) SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
OUTPUT
                            QUERY PLAN
------------------------------------------------------------------
 Gather
   Workers Planned: 2
   ->  Parallel Seq Scan on members
         Filter: (lower(email) = 'user1999999@example.com'::text)
(4 rows)
해결lab_a 계정으로 실행
CREATE INDEX members_lower_email_idx ON members (lower(email));
ANALYZE members;
OUTPUT
CREATE INDEX
ANALYZE

email에만 인덱스가 있으면 lower(email) 조건에는 안 쓰여요. 같은 식으로 인덱스를 만들어야 해요.

해결 후 재실행lab_a 계정으로 실행
SET statement_timeout = '100ms';
EXPLAIN (COSTS OFF) SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
SELECT id FROM members WHERE lower(email) = 'user1999999@example.com';
✓ 성공
SET
                           QUERY PLAN
----------------------------------------------------------------
 Index Scan using members_lower_email_idx on members
   Index Cond: (lower(email) = 'user1999999@example.com'::text)
(2 rows)

   id
---------
 1999999
(1 row)

자주 묻는 질문

canceling statement due to statement timeout 해결 방법은요?

SHOW statement_timeout으로 제한이 어디서 왔는지 보고, 쿼리를 빠르게 하거나(세 번째 경우의 인덱스) 그 작업에만 SET LOCAL statement_timeout으로 시간을 더 줘요(첫 번째 경우).

내 계정에 statement_timeout이 어디 걸려 있는지 어떻게 봐요?

역할 설정은 pg_db_role_setting에 있어요. 첫 번째 경우 lab_a에 {statement_timeout=1s}로 나왔어요.

statement_timeout 단위가 초예요, 밀리초예요?

단위 없는 숫자는 밀리초예요. SET statement_timeout = 5는 5ms였어요. 초는 '5s'처럼 써요.

쿼리 하나만 타임아웃을 늘리려면요?

트랜잭션 안에서 SET LOCAL statement_timeout = '60s'를 써요. 첫 번째 경우 COMMIT 뒤에는 다시 1s로 돌아왔어요.

검증 방법

시나리오마다 새 DB 사용자를 만들어 시작해요. 스크립트가 사전 준비 → 에러 재현(에러 코드 일치 확인) → 해결 → 원래 문장 재실행 순서로 실행하고, 화면의 출력은 DB가 실제로 낸 결과를 스크립트가 그대로 저장한 거예요.

lab_a, lab_b는 테스트용 일반 역할(같은 이름의 스키마 소유), postgres는 슈퍼유저예요. PostgreSQL 17.12