Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gAr56-0007ZW-N5 for pgsql-general@arkaria.postgresql.org; Fri, 12 Oct 2018 06:37:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gAr54-0007Vl-Tk for pgsql-general@arkaria.postgresql.org; Fri, 12 Oct 2018 06:37:50 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1gAr54-0007Ve-Fn for pgsql-general@lists.postgresql.org; Fri, 12 Oct 2018 06:37:50 +0000 Received: from mail-ed1-x544.google.com ([2a00:1450:4864:20::544]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gAr51-00026Y-F0 for pgsql-general@postgresql.org; Fri, 12 Oct 2018 06:37:48 +0000 Received: by mail-ed1-x544.google.com with SMTP id g32-v6so10442705edg.13 for ; Thu, 11 Oct 2018 23:37:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=cbzFIm1YV/0Eq/pBtqxej0wgP9auHbBjDYaJ67neOD8=; b=cZM3IA9x7oFaKHZYIBOm9vxs59s2/6BMd2Y1f5I13Bi7dkxTfaQUx4n1V3CQpCKgR1 EQDgCfOWhMjR8Zh5UNx8/4HOP8qJ9niE40L/XoSz4STqpkIvFHDV/m0yc7AtkFdhrzqM TZkD9EA517y18AaV7j3wz+T3HYoGu3JRXWfbZxMadnmLyyxz03eCz1fIWkg0W7xwXz9e BHxJ1NXsN9UnzVCeygzAZyDcF0+c2KPxqstPKfriJWcW3BHN0wB7Fpck1Dwu8BRADmiO Xv/AYkqwSgG8/ZLEiw/lOKzr69CJCZY2ZcFP7TRMAGRhxREePrhjgoizG+AQRljqJkQ4 THqA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=cbzFIm1YV/0Eq/pBtqxej0wgP9auHbBjDYaJ67neOD8=; b=h1olDFEXym5Xrox/O+zky2hMtDFgOEDxZuVTZTp1/2O6ZO1lASCo+yW3pan55nvi5W JzGcaYNJvC8HoAf0NykviOBOlrS9LUPiH7wjwVLwfiXYyyYHqGOhXpdAbitqKvQp02dI 7zRk5VeWTz2hcAgj+jFIM/ua0XZW/WqtJZSJznRxjxI0csWe2WyswSs1xOvlU2kyu+ep 8NFQ02F3fZs9tHjSk6urd6Yq4Psz1XSqA+cn4nqK7C3YIEm/86MrTyeZFKGPfMH7f9Wz 2cxEH9Dv27PT6+9tBcbHDKDhdckAcUWBU8zhpXFfd9AuaOWr5S9ew245/7wvXmQA53OI u6qg== X-Gm-Message-State: ABuFfoj0s8NEYOFVpoIeglS9AuXnVErSFVRQ0AogDP8O7eN9OWpU7v/v pRk5kyofcAHSCjs9oD4OYIE= X-Google-Smtp-Source: ACcGV61MBuTNh2+deEe0HqMdRT/lk3bLbo9Nhno/8XSN1hiLZ2egQMCWd72vVX/pIEKHiFNNjkrlrA== X-Received: by 2002:a50:d2d4:: with SMTP id q20-v6mr7244725edg.212.1539326265055; Thu, 11 Oct 2018 23:37:45 -0700 (PDT) Received: from hollewijn.internal (217-19-30-105.dsl.cambrium.nl. [217.19.30.105]) by smtp.gmail.com with ESMTPSA id b14-v6sm243393edc.17.2018.10.11.23.37.44 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 11 Oct 2018 23:37:44 -0700 (PDT) Content-Type: text/plain; charset=us-ascii Mime-Version: 1.0 (Mac OS X Mail 11.5 \(3445.9.1\)) Subject: Re: Want to acquire lock on tables where primary of one table is foreign key on othere From: Alban Hertroys In-Reply-To: Date: Fri, 12 Oct 2018 08:37:42 +0200 Cc: Postgres General Content-Transfer-Encoding: quoted-printable Message-Id: References: To: Abhishek Tripathi X-Mailer: Apple Mail (2.3445.9.1) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk You forgot to CC the list. Also, top-posting is generally not = appreciated here. > On Thu, Oct 11, 2018 at 8:26 PM Alban Hertroys = wrote: > On Thu, 11 Oct 2018 at 16:38, Abhishek Tripathi > wrote: > > Actually I have acquired a "Select for Update" on a table whose id = is refrence as a foreign key on another table So I want those table = won't update until there is lock. Is it possible? Becuase postgres is = acquiring lock but AccessShare Lock which allow to write on those table = How I restrict this. >=20 > For what purpose do you want that? What is inadequate about the lock > that Postgres acquires? >=20 > Table locks are very rarely what you want, as it blocks all concurrent > access to the entire table, while that is only necessary for a few > rarely used corner cases; a foreign key update is not among those. > On Fri, Oct 12, 2018 at 10:33 AM Abhishek Tripathi = wrote: > Thank you for your response.=20 >=20 > I am explaining my situation there is table A on which I have taken a = row lock and the primary key of table A is related with table B as a = foreign key so automatically I can not insert new row with that foreign = key now the primary key of table B is used as foreign key in table C = and insertion can be done on table C. I just want to lock table C also = No insertion can be done on table C related to table B primary key. >=20 > On 12 Oct 2018, at 8:08, Abhishek Tripathi = wrote: >=20 > My bad sorry actually there is updation- >=20 > there is table A on which I have taken a row lock and the primary key = of table A is related with table B as a foreign key so automatically I = can not insert new row with that foreign key on table B that is fine = now table C any insertion can be done on table C. I just want to lock = table C also No insertion can be done on table C becuse table C primary = key is related to table B as a foreign key of B. So your tables (simplified) are something like this?: create table A (id primary key) create table B (id primary key, a_id references A (id)) create table C (id primary key, b_id references B (id)) And you insert a new value for b_id in C? That's not possible, unless = that value exists in table B, which in turn can't exist unless it's a_id = exists in table A. That's how foreign key constraints are designed, you don't need to do = manual locking for that to work. Alban Hertroys -- If you can't see the forest for the trees, cut the trees and you'll find there is no forest.