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 1hJeAa-0001qO-3E for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 13:12:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hJeAY-0002cX-Rc for pgsql-general@arkaria.postgresql.org; Thu, 25 Apr 2019 13:12:06 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJeAY-0002Vu-AO for pgsql-general@lists.postgresql.org; Thu, 25 Apr 2019 13:12:06 +0000 Received: from mail-qk1-x744.google.com ([2607:f8b0:4864:20::744]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hJeAV-0002u7-2X for pgsql-general@postgresql.org; Thu, 25 Apr 2019 13:12:05 +0000 Received: by mail-qk1-x744.google.com with SMTP id k189so12969660qkc.0 for ; Thu, 25 Apr 2019 06:12:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to:user-agent; bh=iDr93U37ZIy4Ei+D4hqElvSalSZVv7crI9QtP0MDpYs=; b=bUL7t038Y1GwSDuL0U22mw6r19zG6a8b/uR2UXLHn/NXyquyE5JjjiuVL9nVk1QkgM xCkyBCDR4x+q2iZZw2F8NMS58beB/fXbQXGpoVMKtVjNeXjEIWGmxhx5Q8agDENOIGxw 5K7t8swnY5lWBhQ3U4fCYdCLaMks/ZQcnCB+Gt+hq0SjYTUBOtdhUVo0J9PHax3GQT8S KYhdHzkNUeSzrtO3avyevp9epBfEpMHBupcIqYkQQZZh71F7XoTRHq/FE3uuXeLzLprD aLulvG108VQ6or13L+89PtTyuPJbcwnrnnlLg4c79gRviOS8hrVxVxf+1aCXMq7knoQf Z4jA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=iDr93U37ZIy4Ei+D4hqElvSalSZVv7crI9QtP0MDpYs=; b=YFd8i28bxttFbz5tsDo6ZcpqCek/qibwpSusXT5rxGiaZGuz3fNiXEXm3S8G2h9uiP KnPNM2VEwv2Ngsh0cXjUtXEWB8nT6fwIPwlrYGOMH+tzx+34UCKb+gQmT5yRN+8Pddc2 tMWPm3KdJUBFMbZ9PbSmqKt6GmAT+x1BcGkUW2tHo6v1TUw1NnnzAOkfngNuDhMBEnhM oSA+RnVPr+lmsqFF7SUKJeza0N0F0pURAEab9J61ZfjMdUQmp16LYJJMyJelwA1946ca NyeHA82lHRHIwJKO26mKNt7371G7+6sujX1aw74FEE2bPdS0rjwfJpi3hf9PM1HvNZpL NmPw== X-Gm-Message-State: APjAAAXDosYVzTAvPwlCVK2ylLLTvFPqYWW4nj7tos2rPbWXvLEipMfK 1qZjpBNS9TtAGh2ED6D8cvAAcA== X-Google-Smtp-Source: APXvYqzTlxs8k0U7bFBb0u3NhgRYeTnGT4iuEK41xWoHdE9VkLwoi8Qwni4Mxz+c9n7I5DZ0rXlmWg== X-Received: by 2002:a37:9a44:: with SMTP id c65mr19791818qke.203.1556197922285; Thu, 25 Apr 2019 06:12:02 -0700 (PDT) Received: from nimloth.alvh.no-ip.org ([190.121.29.3]) by smtp.gmail.com with ESMTPSA id t124sm9584310qkh.29.2019.04.25.06.12.00 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Thu, 25 Apr 2019 06:12:01 -0700 (PDT) Received: by nimloth.alvh.no-ip.org (Postfix, from userid 1000) id E4BBC12081F; Thu, 25 Apr 2019 09:11:57 -0400 (-04) Date: Thu, 25 Apr 2019 09:11:57 -0400 From: Alvaro Herrera To: rihad Cc: pgsql-general General Subject: Re: Does "ON UPDATE" for foreign keys require index? Message-ID: <20190425131157.GA15634@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <6cb9e903-b7c7-23c1-65b4-5df8dbdfc1db@mail.ru> User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2019-Apr-25, rihad wrote: > 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. We have an optimization that if you update a row in a transaction just once, and the column is not modified, then it won't need to scan the referencing table. However, if you make two updates in a transaction, the optimization isn't smart enough to detect that the FK isn't invalidated, so a scan will occur. Therefore, if A is large [enough that you care about a seqscan on it] and you expect to be doing more than one UPDATE of B in the same transaction, then this could be noticeable. I suggest you run some tests, just to be sure. -- Álvaro Herrera https://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services