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 1hJahQ-00053U-A4 for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 09:29:49 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hJahM-000192-G6 for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 09:29:44 +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 1hJahL-000172-Ug for pgsql-general@lists.postgresql.org; Thu, 25 Apr 2019 09:29:44 +0000 Received: from smtp5.mail.ru ([94.100.179.24]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJahE-0003FO-FY for pgsql-general@postgresql.org; Thu, 25 Apr 2019 09:29:43 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mail.ru; s=mail2; h=Content-Type:MIME-Version:Date:Message-ID:Reply-To:Subject:From:To; bh=O/hw/5C86LP2yVXjJ1AhpRltBD5k9PxCa44GDKTcy98=; b=LbON5kuSs/5X+USc5XtUtmV1GAI4CguMOztsqQgww+WiELFk3jNjtVLuSErgd54SZaTNCWbGzkS5BqjAxW9kupGZzxq4EBPBazCDGUm+9byrvfXIL6gHjlBzaXgeF0iS65SR8KmptUtNkTkrRBpcRTktIy1MvMxbb2tsl/i66OM=; Received: by smtp5.mail.ru with esmtpa (envelope-from ) id 1hJahB-0007XA-Sj for pgsql-general@postgresql.org; Thu, 25 Apr 2019 12:29:34 +0300 To: pgsql-general General From: rihad Subject: Does "ON UPDATE" for foreign keys require index? Reply-To: rihad Message-ID: <6cb9e903-b7c7-23c1-65b4-5df8dbdfc1db@mail.ru> Date: Thu, 25 Apr 2019 13:29:31 +0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="------------0D24ABF158912932A94DF83E" Content-Language: en-US Authentication-Results: smtp5.mail.ru; auth=pass smtp.auth=rihad@mail.ru smtp.mailfrom=rihad@mail.ru X-77F55803: 0014004E1F3277295A78504BD2AC2941B9F49188DF33F39BF677A30490DEA358F1F2C0712EF4B41CC9280197616BE6D8 X-7FA49CB5: 0D63561A33F958A501E7B1838D847C1D5C8C0FF395B3D35FE8B7A1C0147988FF8941B15DA834481FA18204E546F3947CEDCF5861DED71B2F389733CBF5DBD5E9C8A9BA7A39EFB7666BA297DBC24807EA117882F44604297287769387670735209ECD01F8117BC8BEA471835C12D1D977C4224003CC8364767815B9869FA544D8D32BA5DBAC0009BE9E8FC8737B5C2249CB2598FF67A25C423AA81AA40904B5D9CF19DD082D7633A093541453170D46FCD81D268191BDAD3D78DA827A17800CE7ADE5BB8891CD3BFCCD04E86FAF290E2DBBC930A3941E20C675ECD9A6C639B01B78DA827A17800CE7737B215068CA9314670C1BFB1D74B17075ECD9A6C639B01B4E70A05D1297E1BBC6867C52282FAC8519DC0BE04022C72727F269C8F02392CD5571747095F342E88FB05168BE4CE3AF X-Mailru-Sender: B19AB4DAF1EA2FB53C6BD2985D337C62AFBBADDA466739CCFEACD8100559C6347942E65D3B0E2994ABBFE0CF075BC50BFB559BB5D741EB96004FA9CEFBA991DA3A5DB60FBEB33A8A0DA7A0AF5A3A8387 X-Mras: OK List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------0D24ABF158912932A94DF83E Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi. Say I have column A.b_id which references B.id (which is a primary key) and as such it is declared as a foreign key constraint. A.b_id has no index because it doesn't need one. What happens when table B's rows are modified (but never deleted)? Will PG still have have to scan A fo find A.b_id to do nothing with it? )) B.id itself is never modified, it's just a normal serial value typically used for id's. The docs are a bit ambiguous: > Since a DELETE of a row from the referenced table *or an **UPDATE**of > a referenced column* will require a scan of the referencing table for > rows matching the old value, it is often a good idea to index the > referencing columns too. Because this is not always needed, and there > are many choices available on how to index, declaration of a foreign > key constraint does not automatically create an index on the > referencing columns. > > https://www.postgresql.org/docs/9.6/ddl-constraints.html --------------0D24ABF158912932A94DF83E Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit

Hi. Say I have column A.b_id which references B.id (which is a primary key) and as such it is declared as a foreign key constraint. A.b_id has no index because it doesn't need one. What happens when table B's rows are modified (but never deleted)? Will PG still have have to scan A fo find A.b_id to do nothing with it? )) B.id itself is never modified, it's just a normal serial value typically used for id's.


The docs are a bit ambiguous:

Since a DELETE of a row from the referenced table or an UPDATE of a referenced column will require a scan of the referencing table for rows matching the old value, it is often a good idea to index the referencing columns too. Because this is not always needed, and there are many choices available on how to index, declaration of a foreign key constraint does not automatically create an index on the referencing columns.



https://www.postgresql.org/docs/9.6/ddl-constraints.html

--------------0D24ABF158912932A94DF83E--