简体   繁体   中英

Does postgres “SELECT FOR UPDATE” restrict to create new foreign key references ?

There are two tables, one is referenced by another with foreign key constraint.

CREATE TABLE record
(
  id                    UUID PRIMARY KEY  NOT NULL
);    

CREATE TABLE task
(
  id                    UUID PRIMARY KEY  NOT NULL,
  record_id       UUID,
  CONSTRAINT fk_task_record_id FOREIGN KEY (record_id) 
  REFERENCES record (id)
);

The question is : if lock on record row is aquired by

SELECT * FROM record where id = 'A' FOR UPDATE OF record

Could I create new foreign key in task table and reference this very record row ?

INSERT INTO task VALUES ('someId', 'A');

Does Postgres prevent creating new foreign key references on tables locked by SELECT FOR UPDATE OF ?

No, it does not.

select .. for update only blocks changes (update, delete) to that row.

It does not prevent other transactions from reading that row and that's what is required to insert a row referencing the locked row.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM