Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t0p71-00DNBE-L0 for pgsql-general@arkaria.postgresql.org; Tue, 15 Oct 2024 21:29:52 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1t0p6z-00Bib8-W2 for pgsql-general@arkaria.postgresql.org; Tue, 15 Oct 2024 21:29:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t0p6y-00Biay-VA for pgsql-general@lists.postgresql.org; Tue, 15 Oct 2024 21:29:50 +0000 Received: from fout-b6-smtp.messagingengine.com ([202.12.124.149]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t0p6w-0014DR-1I for pgsql-general@postgresql.org; Tue, 15 Oct 2024 21:29:47 +0000 Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfout.stl.internal (Postfix) with ESMTP id 3DFA511400D8; Tue, 15 Oct 2024 17:29:44 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-08.internal (MEProxy); Tue, 15 Oct 2024 17:29:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:content-type:date :date:from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to; s=fm1; t=1729027784; x=1729114184; bh=fgDeUsc8lJa4YALDKONYohsgulLQdBwUCPFmR2fc2lY=; b= nGq5ejwvc5QhU3b3Vc1ZP0Tew5RxoX3nqwerN4c6+23QbqJ3/pxPBWAt2GWOGHzo SH5Mqn2jjJPcLCiqxw3bKg/ZWhD2s1GUzw3shObLEk2MyymEws2YKYoYJxv5dAI3 c5b/NdWZZ03vseWv8gCCm/Bfz/yFaQSuF4O0kneDIVb3rAYtnQWcwQtdXYSqWqyQ 8UL7PqpPCrKEWm7ygJO0yrbYQ0hQ/JMtL2JZr5kRB80nYG+QszYK49EJdwKQ24TB 3IQwW5A6HHkCEB4Wgy/8II9UNy+CBybUiejzqsh43V7h6muNGpgcNTJ3OWxZMxY3 2KN/pWUiJkm/E46hy8T8Xg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :references:reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1729027784; x= 1729114184; bh=fgDeUsc8lJa4YALDKONYohsgulLQdBwUCPFmR2fc2lY=; b=B AGHQIA3o3TgZ9G65ncNVIRVNgD+YbB88ktK2s0jUDlQjSUf31cTtfREhY8Df9YkZ BrXLPZ8XsnkK52KTtZoO0WbbRDdvP0A/iGgacOj3z2XCfFqLcXWYfTCzaoD3xfFP mHneVpYKaXWnh7mN7+dbj/Z1yV4yj+e9PeM/62BoVUVZxpdHb9U/aV4j7F6VqQoO Yb54yKJ2Ft62oCPDHGzQz4etpDxvfIk3G4oWtse9BqTnUMCA0BnbeJE4sKrICig5 8GZUaAZWsfARFHEVBstZtmKyfBEU2y2C/XFfbLR89/gVv97gm3cajIupIDKfKLNC tWYj58WkS8T78cTrGIkLw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdegjedgudeiudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgg gfuffvvehfhfgjtgfgsehtkeertddtvdejnecuhfhrohhmpeetughrihgrnhcumfhlrghv vghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuggftrf grthhtvghrnhepiefhveetvdefgfefudefvddtgefgkedvtedtleekgedtffeukeefffet veelgfdtnecuffhomhgrihhnpehpohhsthhgrhgvshhqlhdrohhrghdpghhithhhuhgsrd gtohhmnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomhep rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomhdpnhgspghrtghpthhtoh epvddpmhhouggvpehsmhhtphhouhhtpdhrtghpthhtohepvhhijhgrhihkuhhmrghrjhgr ihhnrdhgihhthhhusgesghhmrghilhdrtghomhdprhgtphhtthhopehpghhsqhhlqdhgvg hnvghrrghlsehpohhsthhgrhgvshhqlhdrohhrgh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 15 Oct 2024 17:29:42 -0400 (EDT) Message-ID: <7448286f-6868-4f77-b457-5a1a943d8576@aklaver.com> Date: Tue, 15 Oct 2024 14:29:39 -0700 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: how to know if the sql will run a seq scan To: Vijaykumar Jain Cc: pgsql-general References: Content-Language: en-US From: Adrian Klaver In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 10/15/24 13:50, Vijaykumar Jain wrote: > Sorry top posting, coz Gmail app on phone. > > Yeah, my point was for example we have a large table and we are > attaching a table as a partition. Now it will scan the whole table to > validate the constraint and that will create all sorts of problems. Now you have changed the problem description. To get a proper answer you will need to provide a more detailed description of what you are doing with the following information: 1) Postgres version. 2) Definition of 'large'. 3) The command/process being used to create the partition. 4) The actual constraint definition. 5) The table definition. > I understand the benefit of not valid constraint and then validating > constraint to reduce blocking. > But yeah monitoring locks for the statement should give me good enough > hint of what will happen. > > Thanks for your reply. It helps. > > > > On Wed, Oct 16, 2024, 1:54 AM Adrian Klaver > wrote: > > On 10/15/24 12:50, Vijaykumar Jain wrote: > > > > Hi, > > > > tl;dr > > I am trying to learn what sql can result in a full seq scan. > > > > Basically there is a lot of info on the internet of what ddl > change may > > take an access exclusive lock while running a seq scan and hold > for long. > >   And for some cases we can make use of > > "not valid" constraint and then run a validate constraint as work > > arounds to avoid long exclusive locks etc. > > but how do we check the same. i mean for dmls there is a explain/ > > auto_explain. > > > > but for DDLs, how do we check the same. > > i tried to isolate my setup and use pg_stat_user_tables and > monitor the > > same, which helped, but it is not useful as it does not link me > to what > > process/command invoked the seq scan. > > > > am i clear in my question ? > > > > if yes, > > how do i log an alter table that may or may not do a seq scan, > that may > > or may not rewrite the table file on disk etc. > > its a useless question, i am just playing with it for building > > knowledge, no requirement as such. > > Look at the docs: > > https://www.postgresql.org/docs/current/sql-altertable.html > > > "Scanning a large table to verify a new foreign key or check constraint > can take a long time, and other updates to the table are locked out > until the ALTER TABLE ADD CONSTRAINT command is committed. The main > purpose of the NOT VALID constraint option is to reduce the impact of > adding a constraint on concurrent updates. With NOT VALID, the ADD > CONSTRAINT command does not scan the table and can be committed > immediately. After that, a VALIDATE CONSTRAINT command can be issued to > verify that existing rows satisfy the constraint. The validation step > does not need to lock out concurrent updates, since it knows that other > transactions will be enforcing the constraint for rows that they insert > or update; only pre-existing rows need to be checked. Hence, validation > acquires only a SHARE UPDATE EXCLUSIVE lock on the table being altered. > (If the constraint is a foreign key then a ROW SHARE lock is also > required on the table referenced by the constraint.) In addition to > improving concurrency, it can be useful to use NOT VALID and VALIDATE > CONSTRAINT in cases where the table is known to contain pre-existing > violations. Once the constraint is in place, no new violations can be > inserted, and the existing problems can be corrected at leisure until > VALIDATE CONSTRAINT finally succeeds." > > > > -- > > Thanks, > > Vijay > > > > Open to work > > Resume - Vijaykumar Jain > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com