Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iXQmJ-0003oW-90 for pgsql-general@arkaria.postgresql.org; Wed, 20 Nov 2019 14:16:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iXQmI-0001AW-0J for pgsql-general@arkaria.postgresql.org; Wed, 20 Nov 2019 14:16:18 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iXQmH-0001AD-Kk for pgsql-general@lists.postgresql.org; Wed, 20 Nov 2019 14:16:17 +0000 Received: from mail-oi1-x236.google.com ([2607:f8b0:4864:20::236]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iXQmE-0003nb-Fz for pgsql-general@postgresql.org; Wed, 20 Nov 2019 14:16:16 +0000 Received: by mail-oi1-x236.google.com with SMTP id o12so12486324oic.9 for ; Wed, 20 Nov 2019 06:16:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=bMRwmFZv6tetZxOzlrR7/aOOqSajv6R0SJ1fHwKlDSw=; b=CDnGhdKXfQhNexgO8rb3xQqK4A5hBR56GEEaW34jNW7KfD8qSk2r0OzY+UKNqj/OpG /8LTtzu1lsogqgIgvVLTvAMbX3xMhhTXu18lTlR7z+T8RlOTMAml/9BUeRKeb0lLdy0v us1QTmAgzCNQztFnUyPTwhtFbbgmru8O/WWPklb5GgPvuj10LwR9In9eY/UeW6gcDnfk KkCleTuU14Ss0/VjoXMUutzs9h2GFfnCokr09M0BXuPjTmZjk9m7eZKOafs7/Mqt7Tap bSdCWscRlkZIkdVdYeWz4XA/gx8PhY+4dzXEZ14XFNtNIEQhfDNhdFQduvqWSjSjXEQw rqIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=bMRwmFZv6tetZxOzlrR7/aOOqSajv6R0SJ1fHwKlDSw=; b=M3zOLgHPGGkENyWYZswyNcCq458O1NeqC70nDXicE5PQbz8Rr48rMUSfzIwWgzELnz 9+htN9zj+6ztFRoUNTdI8+9K1hiaDswxhE05bKANDEJrwXtjOzoDlPi6g2WUv63F7cMz 86dJu9nw89/LVJwDAXJJzEtRMLEjTizezAFsI490eR2maV0Bw/3pWm32R5FqxXmKSCX4 TbJqpRZEwDIdfh1ZOC4CxkNQBkyQe8y9Dhz7Z0n+LWhCtqLfu2FJdMEdQjqGx68hivs/ UltQ/qy+M4YDY5y3YxUu7J8bNgwocbtef1AvVnSIMR/RVegP6L2UxCwn64w+gh3fSK3Y +U7w== X-Gm-Message-State: APjAAAXjoZz4qXk6MvfgP2oNSu2CdVhMY7/vn1hzUEqeS6WWLIQhkq3z WVWwILK3OdFezywDA6aNt6nEaSg690CuNzHnrSyThWMRFC0= X-Google-Smtp-Source: APXvYqx2eeE4/GCIzzBMSavT2MrJoDzhCBcFrM/6j+8hAcWpugeGZW9RZxbhDEZPaqh2JJCZr0kF7W5jUqutwWs9ItE= X-Received: by 2002:aca:6006:: with SMTP id u6mr3032473oib.137.1574259371611; Wed, 20 Nov 2019 06:16:11 -0800 (PST) MIME-Version: 1.0 From: Enrico Pirozzi Date: Wed, 20 Nov 2019 15:16:00 +0100 Message-ID: Subject: Foreign keys and locks. To: pgsql-general Content-Type: multipart/alternative; boundary="000000000000a11a120597c7d212" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --000000000000a11a120597c7d212 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Hi all, Today I found this behavior on two tables. I have 2 tables t2 and t3 , described as below: testdb=3D# \d t2 Tabella "public.t2" Colonna | Tipo | Ordinamento | Pu=C3=B2 essere null | Default ---------+---------+-------------+-----------------+--------- id | integer | | not null | value | integer | | | Indici: "t2_id_p_key" PRIMARY KEY, btree (id) "t2_rate_idx" btree (value) Referenziato da: TABLE "t3" CONSTRAINT "t3_roomrate_fkey" FOREIGN KEY (value) REFERENCES t2(id) ON UPDATE CASCADE ON DELETE CASCADE testdb=3D# \d t3 Tabella "public.t3" Colonna | Tipo | Ordinamento | Pu=C3=B2 essere null | Default ---------+---------+-------------+-----------------+--------- id | integer | | not null | value | integer | | | Indici: "t3_id_p_key" PRIMARY KEY, btree (id) Vincoli di integrit=C3=A0 referenziale "t3_roomrate_fkey" FOREIGN KEY (value) REFERENCES t2(id) ON UPDATE CASCADE ON DELETE CASCADE the two tables are populated with records that satisfy the referential integrity constraints. 1) If I execute testdb=3D# delete from t2 where id=3D1978800 ; it works; 2) but if I execute testdb=3D# delete from t2 where value=3D20342; postgresql applies locks and the query freezes. testdb=3D# SELECT locktype, relation::regclass, mode, transactionid AS tid, virtualtransaction AS vtid, l.pid, granted,a.query FROM pg_catalog.pg_locks l JOIN pg_stat_activity a on a.pid=3Dl.pid LEFT JOIN pg_catalog.pg_database db ON db.oid =3D l.database WHERE (db.datname =3D 'testdb') AND NOT l.pid =3D pg_backend_pid() and a.state =3D 'active'; locktype | relation | mode | tid | vtid | pid | granted | query ----------+-------------+------------------+-----+--------+------+---------= +----------------------------------- relation | t3_id_p_key | RowExclusiveLock | | 9/1475 | 8685 | t | delete from t2 where value=3D20342; relation | t3 | RowExclusiveLock | | 9/1475 | 8685 | t | delete from t2 where value=3D20342; relation | t2_rate_idx | RowExclusiveLock | | 9/1475 | 8685 | t | delete from t2 where value=3D20342; relation | t2_id_p_key | RowExclusiveLock | | 9/1475 | 8685 | t | delete from t2 where value=3D20342; relation | t2 | RowExclusiveLock | | 9/1475 | 8685 | t | delete from t2 where value=3D20342; I tried to check the problem on other tables but on other tables I did not find the same behavior and the second type of query works without problems. Any Ideas? Thanks in advance for your reply Enrico --000000000000a11a120597c7d212 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hi all,
Today I found this behavior on two tables.
<= br>I have 2 tables t2 and t3 , described as below:

testdb=3D# \d t2=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0Tabella "public.t2"
=C2=A0Colonna | =C2=A0Tipo =C2=A0 | = Ordinamento | Pu=C3=B2 essere null | Default
---------+---------+-------= ------+-----------------+---------
=C2=A0id =C2=A0 =C2=A0 =C2=A0| intege= r | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | not null =C2=A0 =C2=A0 =C2= =A0 =C2=A0|
=C2=A0value =C2=A0 | integer | =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |Indici:
=C2=A0 =C2=A0 "t2_id_p_key" PRIMARY KEY, btree (id)<= br>=C2=A0 =C2=A0 "t2_rate_idx" btree (value)
Referenziato da:<= br>=C2=A0 =C2=A0 TABLE "t3" CONSTRAINT "t3_roomrate_fkey&quo= t; FOREIGN KEY (value) REFERENCES t2(id) ON UPDATE CASCADE ON DELETE CASCAD= E

testdb=3D# \d t3
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Tabella "public.t3"
=C2=A0Colon= na | =C2=A0Tipo =C2=A0 | Ordinamento | Pu=C3=B2 essere null | Default
-= --------+---------+-------------+-----------------+---------
=C2=A0id = =C2=A0 =C2=A0 =C2=A0| integer | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 |= not null =C2=A0 =C2=A0 =C2=A0 =C2=A0|
=C2=A0value =C2=A0 | integer | = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 |
Indici:
=C2=A0 =C2=A0 "t3_id_p_key&q= uot; PRIMARY KEY, btree (id)
Vincoli di integrit=C3=A0 referenziale
= =C2=A0 =C2=A0 "t3_roomrate_fkey" FOREIGN KEY (value) REFERENCES t= 2(id) ON UPDATE CASCADE ON DELETE CASCADE

the two tables are populat= ed with records that satisfy the referential integrity constraints.

= 1) If I execute

testdb=3D# delete from t2 where id=3D1978800 ;
<= br>it =C2=A0works;

2) but if I execute
testdb=3D# delete from t2= where value=3D20342;

postgresql applies locks and the query freezes= .

testdb=3D# SELECT locktype, relation::regclass, mode, transactioni= d AS tid,
virtualtransaction AS vtid, l.pid, granted,a.query
FROM pg_= catalog.pg_locks l
JOIN pg_stat_activity a on a.pid=3Dl.pid
LEFT JOI= N pg_catalog.pg_database db
ON db.oid =3D l.database WHERE (db.datname = =3D 'testdb')
AND NOT l.pid =3D pg_backend_pid()
and a.state = =3D 'active';
=C2=A0locktype | =C2=A0relation =C2=A0 | =C2=A0 = =C2=A0 =C2=A0 mode =C2=A0 =C2=A0 =C2=A0 | tid | =C2=A0vtid =C2=A0| pid =C2= =A0| granted | =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 query =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0
----------+-------------+= ------------------+-----+--------+------+---------+------------------------= -----------
=C2=A0relation | t3_id_p_key | RowExclusiveLock | =C2=A0 =C2= =A0 | 9/1475 | 8685 | t =C2=A0 =C2=A0 =C2=A0 | delete from t2 where value= =3D20342;
=C2=A0relation | t3 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| RowExc= lusiveLock | =C2=A0 =C2=A0 | 9/1475 | 8685 | t =C2=A0 =C2=A0 =C2=A0 | delet= e from t2 where value=3D20342;
=C2=A0relation | t2_rate_idx | RowExclusi= veLock | =C2=A0 =C2=A0 | 9/1475 | 8685 | t =C2=A0 =C2=A0 =C2=A0 | delete fr= om t2 where value=3D20342;
=C2=A0relation | t2_id_p_key | RowExclusiveLo= ck | =C2=A0 =C2=A0 | 9/1475 | 8685 | t =C2=A0 =C2=A0 =C2=A0 | delete from t= 2 where value=3D20342;
=C2=A0relation | t2 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0| RowExclusiveLock | =C2=A0 =C2=A0 | 9/1475 | 8685 | t =C2=A0 =C2=A0 = =C2=A0 | delete from t2 where value=3D20342;


I tried to check th= e problem on other tables but=C2=A0on other tables I did not find the same = behavior
and the second type of query works without problems.

An= y Ideas?

Thanks in advance for your reply

Enrico

--000000000000a11a120597c7d212--