update x=x+1; ? |
글쓴이 : PostgresDBA
날짜 : 2015-11-24 (화) 11:18
조회 : 15243
|
|
create table x(x int primary key );
insert into x values(100);
insert into x values(101);
insert into x values(102);
update x set x=x+1;
ERROR: duplicate key value violates unique constraint "x_pkey"
DETAIL: Key (x)=(101) already exists.
오라클 사용자라면 pk dup 에러에 의아해 할것입니다.
PostgreSQL/PPAS 는 기본적으로 해당로우 변경시 그 즉시 중복체크를 수행하므로 에러가 나게 됩니다.
오라클 처럼 에러가 안나게 하려면,
아래 처럼 테이블 선언시 아래 속성을 추가해서 만들면 됩니다.
alter 문을 이용한 추후 변경은 안되니 주의하세요.
CREATE TABLE x
(
x integer NOT NULL,
CONSTRAINT x_pkey PRIMARY KEY (x)
DEFERRABLE INITIALLY IMMEDIATE
) 벗뜨.. 성능이 immediate checking 보다는 나쁘다네요.
When a UNIQUE or PRIMARY KEY constraint is not deferrable, PostgreSQL checks for uniqueness immediately whenever a row is inserted or modified. The SQL standard says that uniqueness should be enforced only at the end of the statement; this makes a difference when, for example, a single command updates multiple key values. To obtain standard-compliant behavior, declare the constraint as DEFERRABLE but not deferred (i.e., INITIALLY IMMEDIATE). Be aware that this can be significantly slower than immediate uniqueness checking. |










