Analyzing delay constraint PostgreSQL

When we operate on the data on a table, if there are constraints exist on the table, then the constraint is when it entered into force?
For example, when we conduct data migration will consider this issue, some migration tools have to migrate when the migration table constraints and data separately, otherwise the situation can not be imported data will appear. This is because the constraint can not be delayed due to .

So pg for the extension of the sentence constraints What principles?
1, when data is exported, the constraints are usually created in the data is written before. Avoid the constraints created after the first pour failed.
2, during use, PG provides a delayed detection constraint function:
2.1, allowing constraints extension sentence, (build tables, when construction constraints may specify, can subsequently be modified)
2.2, provided the extension judgment rule, at the end of the statement or the determining if the constrained end of the transaction. Constraints can be defined by modifying the rule set, or directly in the transaction.

Deals with the following parameters:

DEFERRABLE / NOT DEFERRABLE
these two options control whether the constraint can be delayed. A non-deferrable constraint will be checked immediately after every command, you can delay constraint checking will be deferred until the end of the transaction.

INITIALLY IMMEDIATE / INITIALLY DEFERRED
delay determination rule, if the constraint is INITIALLY IMMEDIATE, it will be checked after each statement. This is the default value. If the constraint is INITIALLY DEFERRED, it will only be checked at the end of the transaction.

We can set a delay in the transaction rules:

SET CONSTRAINTS { ALL | name [, ...] } { DEFERRED | IMMEDIATE }

Examples:
1, allowed delay determination

bill@bill=>create table tt1(id int primary key, c1 int);  
CREATE TABLE
bill@bill=>create table tt2(id int references tt1(id), c1 int);    
CREATE TABLE
bill@bill=>insert into tt2 values(1,1); 
ERROR:  insert or update on table "tt2" violates foreign key constraint "tt2_id_fkey"
DETAIL:  Key (id)=(1) is not present in table "tt1".

2, to allow the extension of sentence, and set the rules for the end of the transaction extended sentence

bill@bill=>create table tt3(id int references tt1(id) INITIALLY DEFERRED, c1 int);  
CREATE TABLE

- can be found in the transaction can insert data

bill@bill=>begin;
BEGIN
bill@bill=>insert into tt3 values(1,1);
INSERT 0 1

3, allowing the extension of negotiations, set to end the transaction after the transaction is open extended sentence

bill@bill=>alter table tt3 alter CONSTRAINT tt3_id_fkey deferrable;
ALTER TABLE
bill@bill=>begin;
BEGIN
bill@bill=>insert into tt3 values(1,1);
ERROR:  insert or update on table "tt3" violates foreign key constraint "tt3_id_fkey"
DETAIL:  Key (id)=(1) is not present in table "tt1".
bill@bill=>rollback ;
ROLLBACK

bill@bill=>begin;  
BEGIN

- Set the end of the transaction extended sentence, all constraints into force

bill@bill=>set constraints all  deferred;  
SET CONSTRAINTS
bill@bill=>insert into tt3 values(1,1);
INSERT 0 1
bill@bill=>rollback ;
ROLLBACK
发布了78 篇原创文章 · 获赞 13 · 访问量 7202

Guess you like

Origin blog.csdn.net/weixin_39540651/article/details/104036315