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 1t0o63-00DI28-Vd for pgsql-general@arkaria.postgresql.org; Tue, 15 Oct 2024 20:24:48 +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 1t0o61-00AzZ5-FV for pgsql-general@arkaria.postgresql.org; Tue, 15 Oct 2024 20:24:45 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t0o60-00AzYd-Hm for pgsql-general@lists.postgresql.org; Tue, 15 Oct 2024 20:24:45 +0000 Received: from fout-b5-smtp.messagingengine.com ([202.12.124.148]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t0o5t-001EnP-G9 for pgsql-general@postgresql.org; Tue, 15 Oct 2024 20:24:44 +0000 Received: from phl-compute-08.internal (phl-compute-08.phl.internal [10.202.2.48]) by mailfout.stl.internal (Postfix) with ESMTP id 1830911400B8; Tue, 15 Oct 2024 16:24:35 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-08.internal (MEProxy); Tue, 15 Oct 2024 16:24:35 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= 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=1729023874; x=1729110274; bh=2rxsYugHFXp/0C8u/OQe4jUobByBDiM1LJPkeN1e3L0=; b= SQ6Z+sy4kclW2C+lOt2XbvwKovXAtjnGQjsbaMCxeELnSRLeGm5N5qXlukdYKs42 jCytGSleeNntMtv5mlIuLGnGYp/iaSR2sCB+IbTy2d24gR4D1fLb7MuaLtfaUG73 LgkBUZ8rgbVj+p7pBCU+mXnmTQRf0VqXAUHmmf6FweUqlPUIwHLHN/6j7T+nbCrk r3l0513x+ug+WufsqS9OPIfq9ZZ6mPUk3QEsW+pd69a4A/H5sWMB4DybIv8SDSad HI2EO80OQxDS3qnuxHUWJslH8gL9FkSQ5K+jaLp7FFae7nUeUQckxouuSJ4cwd0e jz2pxMkPS/CtGUCZCbuhVg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=1729023874; x= 1729110274; bh=2rxsYugHFXp/0C8u/OQe4jUobByBDiM1LJPkeN1e3L0=; b=j t9J/Z9fUlZqg1S6Dn9A42nCrMsGflDKfVfo/P460kBCxQcPzvlEYfPXi0uaJVTEd SxVDD50orqO6ISA3K/CP7EMjp3kkTS+RhY7/YhVPHCosQV3rTyFi48zZ6cCONO4Q 6aze2hPtDUllG/FJVVzNSTIgMqwEWw+A4U0zwZJGlXP4HZF3vEmgo87yzgiVc+rN khBhR2qXnAu75C4ugRxvKq//IbM6QsJB4bVNddedBqNwQPldniwnqD2lHZgr3ijq IPoQp8AVGAS04vF1RLc04uUyKcNECM8V7O+aYL5nBy87ilcPsCdWQfKQNZWAyEE/ GwBbxy9wNtYVwWNo5QQQA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdegjedgudegkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpggftfghnshhusghstghrihgsvgdp uffrtefokffrpgfnqfghnecuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgg gfuffvfhfhjggtgfesthekredttddvjeenucfhrhhomheptegurhhirghnucfmlhgrvhgv rhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrg htthgvrhhnpeevjeeuledvhefhgffgieefgfetgfeltdetvdehfffhvdffveejfeelieeh ueehhfenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgpdhgihhthhhusgdrtg homhenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegr ughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmpdhnsggprhgtphhtthhope dvpdhmohguvgepshhmthhpohhuthdprhgtphhtthhopehvihhjrgihkhhumhgrrhhjrghi nhdrghhithhhuhgssehgmhgrihhlrdgtohhmpdhrtghpthhtohepphhgshhqlhdqghgvnh gvrhgrlhesphhoshhtghhrvghsqhhlrdhorhhg X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Tue, 15 Oct 2024 16:24:34 -0400 (EDT) Message-ID: Date: Tue, 15 Oct 2024 13:24:33 -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 , 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 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