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 1t175B-00EnAy-Gs for pgsql-general@arkaria.postgresql.org; Wed, 16 Oct 2024 16:41:10 +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 1t1759-005zO6-DN for pgsql-general@arkaria.postgresql.org; Wed, 16 Oct 2024 16:41:07 +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 1t1758-005zNG-Gc for pgsql-general@lists.postgresql.org; Wed, 16 Oct 2024 16:41:07 +0000 Received: from fhigh-a8-smtp.messagingengine.com ([103.168.172.159]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1t1754-001O0O-Nb for pgsql-general@postgresql.org; Wed, 16 Oct 2024 16:41:06 +0000 Received: from phl-compute-03.internal (phl-compute-03.phl.internal [10.202.2.43]) by mailfhigh.phl.internal (Postfix) with ESMTP id 5A7D711401BF; Wed, 16 Oct 2024 12:41:01 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-03.internal (MEProxy); Wed, 16 Oct 2024 12:41:01 -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=1729096861; x=1729183261; bh=aCuBppowKeRbhjnnxs/Oe3sPM1CfD3PTSJSx9pc2kxY=; b= nF0O2KyETGkYBWQ+Jt6CTJeQQi3q9V4JwoisJN5ablWEdNFBnwpOd6BN2tZ2H2ik rNWg8tsQaYmwjwPmy+/41AHbDsdjN8XIRDHxVLnrL/JMi+h1oADxKlIZAUEhIEEv 44HYg90Pgv1HFrFqY9555y8lhHaBNM1o+wtQJyTfp+a3trNS9Wb/Pj2xo0hY6Zzr anQfy4dHhZj94iopUx/xOd09B0/uTfvo00e3JUBBXCprgrwLNocJuDL7U82bswMC 4Sr3ki7ZPMNlGalTGDIY8jjCtad0ZOk/+nud5tDIWPdN8MykBkkjshrX/2uJHELU ecyIFvhTs2dz6NkWpVP4Qw== 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=1729096861; x= 1729183261; bh=aCuBppowKeRbhjnnxs/Oe3sPM1CfD3PTSJSx9pc2kxY=; b=h N7RoS/R38WWNI7TbE2YESKvERLlKOKt/IuYzoWF/baiRT70jGRL03588wbMDpVMM +jBMNE49inuzBlHCpw9D9rm3w/FYK+kzUGZTadjdWDLsJggz8hXiVArXNwcVfGID Ap+WhfVWydHEoBqSeCGzRmXBn2A6hS6X87q3KsTb2HAqktrKE6SPAaCLsgJTrgiN iyv3lznik7r0TK9XXjENhdFaCwA9dxTpJxjQVrw0dN0gPbT44QiwWeF+xo/c2xpK Ib+ypLLfYNXkDZ8HC1/8wCJeDzswuoLbckRriJY6VGKYZ+kHdza5nEGlsE0Kp10C 2ggyAs4Y4/O4RfNzoLrcQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrvdegledguddtfecutefuodetggdotefrod 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; Wed, 16 Oct 2024 12:41:00 -0400 (EDT) Message-ID: Date: Wed, 16 Oct 2024 09:40:59 -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: <7448286f-6868-4f77-b457-5a1a943d8576@aklaver.com> 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/16/24 00:02, Vijaykumar Jain wrote: > > > postgres=# create table t(col1 int) partition by list(col1); > CREATE TABLE > postgres=# create table t1(col1 int) > postgres-# ; > CREATE TABLE > postgres=# insert into t1 select 0 from generate_series(1, 100000) x; > INSERT 0 100000 > postgres=# select relname,seq_scan,last_seq_scan, age(last_seq_scan, > current_timestamp), seq_tup_read from pg_stat_user_tables where relname > = 't1'; >  relname | seq_scan | last_seq_scan | age | seq_tup_read > ---------+----------+---------------+-----+-------------- >  t1      |        0 |               |     |            0 > (1 row) > > postgres=# alter table t1 add constraint col10 check (col1 = 0); > ALTER TABLE > postgres=# select relname,seq_scan,last_seq_scan, age(last_seq_scan, > current_timestamp), seq_tup_read from pg_stat_user_tables where relname > = 't1'; >  relname | seq_scan |         last_seq_scan         |       age > | seq_tup_read > ---------+----------+-------------------------------+------------------+-------------- >  t1      |        1 | 2024-10-16 06:46:28.641281+00 | -00:00:03.258432 > |       100000 > (1 row) > > postgres=# -- this results in a seq scan , which is ok, but then when i > attach the partition it does a seq scan again > postgres=# alter table t attach partition t1 for values in (0); >                                                    ALTER TABLE > postgres=# select relname,seq_scan,last_seq_scan, age(last_seq_scan, > current_timestamp), seq_tup_read from pg_stat_user_tables where relname > = 't1'; >  relname | seq_scan |         last_seq_scan         |       age > | seq_tup_read > ---------+----------+-------------------------------+------------------+-------------- >  t1      |        2 | 2024-10-16 06:46:59.512201+00 | -00:00:02.498771 > |       200000 > (1 row) > > postgres=# -- why , when there is a constraint that helps with the > partition boundary/value > > postgres=# alter table t detach partition t1; > ALTER TABLE > > postgres=# alter table t attach partition t1 for values in (0); > ALTER TABLE > postgres=# select relname,seq_scan,last_seq_scan, age(last_seq_scan, > current_timestamp), seq_tup_read from pg_stat_user_tables where relname > = 't1'; >  relname | seq_scan |         last_seq_scan         |       age > | seq_tup_read > ---------+----------+-------------------------------+------------------+-------------- >  t1      |        3 | 2024-10-16 06:54:28.780145+00 | -00:00:03.358524 > |       300000 > (1 row) > > -- despite there being a constraint, it does a full table scan to attach > the partition. why ? note the tup read is full table of t1. > > */ > > above is one of the cases i found. > my core question still was, how do i know which statement will cause a > full table rewrite > full table scan I don't have time now to create an example, but I can point you at: https://www.postgresql.org/docs/current/ddl-partitioning.html#DDL-PARTITIONING-DECLARATIVE 5.12.2.2. Partition Maintenance "As an alternative to creating a new partition, it is sometimes more convenient to create a new table separate from the partition structure and attach it as a partition later. ... " Read the section starting above. > > how do i get to know that. i know implictly i can use the above stat > tables and pg_rel_filepath function etc to figure out the change in oid > , update in seq count etc. > but i want to pin point which statement made what change among 100 other > statements in production. > > I mean is there a way that a certain alter table will do a table rewrite > on disk and other alter table will not. > access exclusive lock on tables does not help answer that question. > > if i am not clear, maybe ignore my question. i have some issues > explaining things clearly, so i try to use demos. > > > > > > > > Thanks, > Vijay > > Open to work > Resume - Vijaykumar Jain -- Adrian Klaver adrian.klaver@aklaver.com