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 1t0y3N-00E2KL-Ro for pgsql-general@arkaria.postgresql.org; Wed, 16 Oct 2024 07:02:42 +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 1t0y3L-00Gz0z-Ks for pgsql-general@arkaria.postgresql.org; Wed, 16 Oct 2024 07:02:40 +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 1t0y3L-00Gz0p-8J for pgsql-general@lists.postgresql.org; Wed, 16 Oct 2024 07:02:39 +0000 Received: from mail-ed1-x541.google.com ([2a00:1450:4864:20::541]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1t0y3F-001JNr-2D for pgsql-general@postgresql.org; Wed, 16 Oct 2024 07:02:39 +0000 Received: by mail-ed1-x541.google.com with SMTP id 4fb4d7f45d1cf-5c948c41edeso6338816a12.1 for ; Wed, 16 Oct 2024 00:02:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729062152; x=1729666952; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=hWDLanHiLFP7E70pzMnvOLHj+jp5KwXAdOjmnt1CUF0=; b=VEr/U2A7V5edNu1EVVepJyFxNBlb+RgCfw5eezxWwYc3iSRv/tlxJDWsvoc+YKOY0A bTeUzCCyWHvPJclb+1dlWK/jhAFpKKwDLvc4IkxwQ0s1rZUZAUJzyX4VIzYkd4Ch4PXR hyuLIDhS7nExGBwJKmwTrq0oet2qR7/R+ejXjYlFgj40DQCRHd50Q2T7c90tQLsrk3aF FJ6IbvVq5QxKTJJxI5oOTtTE5pkzLoq+xv4MNYt3GZ+M7f5/1YKUjv8dmhwMaIB0hX/k sRAzSvcN1vlJ6mOLGB/NgLPVgdiwuveKbJ7DxRhc9MgsqZjBJwhwsyeH8ryMP9kD+TD8 toJg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729062152; x=1729666952; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hWDLanHiLFP7E70pzMnvOLHj+jp5KwXAdOjmnt1CUF0=; b=qPlcft9eTMEld7B2rLnxZUg6t2XhuHREP66KfqmJ1MSy/LmBA/qebctpQHPO/x+7mh 8c70SPLI4G2QoLMqKw9dxkHDwUsi/xZZ/1EsbifCSnV1uULxEJg/e/pyurx2k8J5nGNb 8tW38T35w0TE2NWA2Icdzn1lR4YTb1fbCmHfSmIMhP91KJV57HD4YY6mEWEvM5nJmkIC YuJTQOaxMdXSKqxlLBSVqjKk5O7/vm6Vyq11o4NYdTVTj3QpUFcMt8h2xrqUpxMoANxu guA4SCRnzHCJquKyazI+JI5DJtM5kdx4nbUDg8GbEjMvPwu/jehsgH6nFSYJaKRjqRkx 6Ysg== X-Gm-Message-State: AOJu0YxavHn1oczuDQjBSg2OeEGiAAWiP4TCetr7gWRj9v4Ql9Iqyc2R zx7itfu742KwIrjZs0WxWR/J/OueIG9f+Sc/uSY/M3GRmutvDgOjSLZMCmzfR7vIYFgN2YOmxt2 1Zwsp17396t4wrw+1KIxSaDXgCiw= X-Google-Smtp-Source: AGHT+IH7PQtIdlfoUz9UQlalS9j/tEv4YuzKlnQdsuz9RpW81dYmx1UwSznSJtoqQeaCJhor3YXdTUmPth4xhaFSO88= X-Received: by 2002:a05:6402:2748:b0:5c9:4a9b:31f9 with SMTP id 4fb4d7f45d1cf-5c94a9b5292mr13679566a12.35.1729062151545; Wed, 16 Oct 2024 00:02:31 -0700 (PDT) MIME-Version: 1.0 References: <7448286f-6868-4f77-b457-5a1a943d8576@aklaver.com> In-Reply-To: <7448286f-6868-4f77-b457-5a1a943d8576@aklaver.com> From: Vijaykumar Jain Date: Wed, 16 Oct 2024 12:32:18 +0530 Message-ID: Subject: Re: how to know if the sql will run a seq scan To: Adrian Klaver Cc: pgsql-general Content-Type: multipart/alternative; boundary="000000000000566cd5062492a731" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000566cd5062492a731 Content-Type: text/plain; charset="UTF-8" On Wed, 16 Oct 2024 at 02:59, Adrian Klaver wrote: > 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. > > /* 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 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 --000000000000566cd5062492a731 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


On = Wed, 16 Oct 2024 at 02:59, Adrian Klaver <adrian.klaver@aklaver.com> wrote:
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.


/*
postgres=3D# create = table t(col1 int) partition by list(col1);
CREATE TABLE
postgres=3D# create table t1(col1 int)
postgres-# ;
CR= EATE TABLE
postgres=3D# insert into t1 select 0 from generate_ser= ies(1, 100000) x;
INSERT 0 100000
postgres=3D# select r= elname,seq_scan,last_seq_scan, age(last_seq_scan, current_timestamp), seq_t= up_read from pg_stat_user_tables where relname =3D 't1';
= =C2=A0relname | seq_scan | last_seq_scan | age | seq_tup_read
---= ------+----------+---------------+-----+--------------
=C2=A0t1= =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 0 |=C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 0
(1 row)

postg= res=3D# alter table t1 add constraint col10 check (col1 =3D 0);
A= LTER TABLE
postgres=3D# select relname,seq_scan,last_seq_scan, ag= e(last_seq_scan, current_timestamp), seq_tup_read from pg_stat_user_tables = where relname =3D 't1';
=C2=A0relname | seq_scan |=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0last_seq_scan=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|= =C2=A0 =C2=A0 =C2=A0 =C2=A0age=C2=A0 =C2=A0 =C2=A0 =C2=A0 | seq_tup_read
---------+----------+-------------------------------+--------------= ----+--------------
=C2=A0t1=C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 = =C2=A0 =C2=A0 1 | 2024-10-16 06:46:28.641281+00 | -00:00:03.258432 |=C2=A0 = =C2=A0 =C2=A0 =C2=A0100000
(1 row)

postg= res=3D# -- this results in a seq scan , which is ok, but then when i attach= the partition it does a seq scan again
postgres=3D# alter table = t attach partition t1 for values in (0);=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0ALTER TABLE
postgres=3D# select= relname,seq_scan,last_seq_scan, age(last_seq_scan, current_timestamp), seq= _tup_read from pg_stat_user_tables where relname =3D 't1';
=C2=A0relname | seq_scan |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0last_seq_scan= =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A0 =C2=A0 =C2=A0age=C2=A0 =C2= =A0 =C2=A0 =C2=A0 | seq_tup_read
---------+----------+-----------= --------------------+------------------+--------------
=C2=A0t1= =C2=A0 =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 2 | 2024-10-16 06:46:59.5= 12201+00 | -00:00:02.498771 |=C2=A0 =C2=A0 =C2=A0 =C2=A0200000
(1= row)

postgres=3D# -- why , when there is a constr= aint that helps with the partition boundary/value

= postgres=3D# alter table t detach partition t1;
ALTER TABLE
=

postgres=3D# alter table t attach partition t1 for= values in (0);
ALTER TABLE
postgres=3D# select relname= ,seq_scan,last_seq_scan, age(last_seq_scan, current_timestamp), seq_tup_rea= d from pg_stat_user_tables where relname =3D 't1';
=C2=A0= relname | seq_scan |=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0last_seq_scan=C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0|=C2=A0 =C2=A0 =C2=A0 =C2=A0age=C2=A0 =C2=A0 =C2= =A0 =C2=A0 | seq_tup_read
---------+----------+------------------= -------------+------------------+--------------
=C2=A0t1=C2=A0 = =C2=A0 =C2=A0 |=C2=A0 =C2=A0 =C2=A0 =C2=A0 3 | 2024-10-16 06:54:28.780145+0= 0 | -00:00:03.358524 |=C2=A0 =C2=A0 =C2=A0 =C2=A0300000
(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.

*/

abo= ve is one of the cases i found.
my core question still was, how d= o i know which statement will cause a=C2=A0
full table rewrite
full table scan

how do i get to know that.= =C2=A0i know implictly=C2=A0i can use the above stat tables and pg_rel_file= path function etc to figure out the change in oid , update in seq count etc= .
but i want to pin point=C2=A0which statement made what change a= mong 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.=C2=A0

if i am not = clear, maybe ignore my question. i have some issues explaining things clear= ly, so i try to use demos.






=C2=A0
<= div dir=3D"ltr" class=3D"gmail_signature">
Thanks,
Vija= y

Open to work
Resume -=C2=A0Vijaykumar Jain
--000000000000566cd5062492a731--