Received: from maia.hub.org (unknown [200.46.204.183]) by mail.postgresql.org (Postfix) with ESMTP id 3834C632702 for ; Sun, 28 Mar 2010 04:02:27 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.183]) (amavisd-maia, port 10024) with ESMTP id 86091-04 for ; Sun, 28 Mar 2010 07:02:16 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from mail-ww0-f46.google.com (mail-ww0-f46.google.com [74.125.82.46]) by mail.postgresql.org (Postfix) with ESMTP id 638B26323DF for ; Sun, 28 Mar 2010 04:02:16 -0300 (ADT) Received: by wwe15 with SMTP id 15so7556721wwe.19 for ; Sun, 28 Mar 2010 00:02:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:received:message-id:subject:from:to:cc:content-type; bh=3Mo0/PraicDYSKHO7A+EXTzvc7Ug4V9Mt51rsAv+XX8=; b=RE5e32UTBu4/njMZX9QOFNZiHz+NnLPEHpelximx3rETX3HZhovdQTCmTRUKIFUvJu 5VFqSqg/aPDnswvD3tXfkk9kEw8Ph2ZL2mDPjYWh0+eL3OTdAfnVw1vkjzTVdaV3d0nj +s8A0SxMwBovhWKdD593JQDO8s/iq1Fq45zt8= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; b=AselDw0uU290uKoy+R7LTmZDf9LY5Mwi+t3c4n08J7AYMFyecbDymP6HKujFs71hGd O4Ae6rCg7mqarZTlrpvfuTDe7oHe5xk9hIjsgXK0QopBT2po3pX7Qww5PjyESkEkrL0Q F5Q94jGAXP16g7v9mMJKwKcb/LZdCZL69wA9s= MIME-Version: 1.0 Received: by 10.216.0.5 with HTTP; Sun, 28 Mar 2010 00:02:14 -0700 (PDT) In-Reply-To: <645d9d71003272351x766707c7sadde9aa77bc1c0d3@mail.gmail.com> References: <645d9d71003272351x766707c7sadde9aa77bc1c0d3@mail.gmail.com> Date: Sun, 28 Mar 2010 09:02:14 +0200 Received: by 10.216.89.193 with SMTP id c43mr1872413wef.221.1269759734777; Sun, 28 Mar 2010 00:02:14 -0700 (PDT) Message-ID: Subject: Re: Optimizer showing wrong rows in plan From: Szymon Guz To: Tadipathri Raghu Cc: pgsql-performance@postgresql.org Content-Type: multipart/alternative; boundary=0016e6d46d7df112830482d6f945 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-2.598 tagged_above=-10 required=5 tests=BAYES_00=-2.599, HTML_MESSAGE=0.001 X-Spam-Level: X-Archive-Number: 201003/350 X-Sequence-Number: 38175 --0016e6d46d7df112830482d6f945 Content-Type: text/plain; charset=UTF-8 2010/3/28 Tadipathri Raghu > Hi All, > > Example on optimizer > =============== > postgres=# create table test(id int); > CREATE TABLE > postgres=# insert into test VALUES (1); > INSERT 0 1 > postgres=# select * from test; > id > ---- > 1 > (1 row) > postgres=# explain select * from test; > QUERY PLAN > -------------------------------------------------------- > Seq Scan on test (cost=0.00..34.00 *rows=2400* width=4) > (1 row) > In the above, example the optimizer is retreiving those many rows where > there is only one row in that table. If i analyze am geting one row. > No, the optimizer is not retrieving anything, it just assumes that there are 2400 rows because that is the number of rows that exists in the statictics for this table. The optimizer just tries to find the best plan and to optimize the query plan for execution taking into consideration all information that can be found for this table (it also looks in the statistics information about rows from this table). > > postgres=# ANALYZE test; > ANALYZE > postgres=# explain select * from test; > QUERY PLAN > ---------------------------------------------------- > Seq Scan on test (cost=0.00..1.01 *rows=1* width=4) > (1 row) > > My question here is, what it retreiving as rows when there is no such. One > more thing, if i wont do analyze and run the explain plan for three or more > times, then catalogs getting updated automatically and resulting the correct > row as 1. > > Now ANALYZE changed the statistics for this table and now the planner knows that there is just one row. In the background there can work autovacuum so it changes rows automatically (the autovacuum work characteristic depends on the settings for the database). > Q2. Does explain , will update the catalogs automatically. > > No, explain doesn't update table's statistics. regards Szymon Guz --0016e6d46d7df112830482d6f945 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
2010/3/28 Tadipathri Raghu <= ;traghu.dba@gmail.com>
Hi All,
=C2=A0
Example on optimizer
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
postgres=3D# create table test(id int);
CREATE TABLE
postgres=3D= # insert into test VALUES (1);
INSERT 0 1
postgres=3D# select * from = test;
=C2=A0id
----
=C2=A0 1
(1 row)
postgres=3D# explain select * from test;
=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 QUERY PLAN
---------------------------= -----------------------------
=C2=A0Seq Scan on test=C2=A0 (cost=3D0.00.= .34.00 rows=3D2400 width=3D4)
(1 row)
In the above, example the optimizer is retreiving those many rows wher= e there is only one row in that table. If i analyze am geting one row.

No, the optimizer is not retrieving anyth= ing, it just assumes that there are 2400 rows because that is the number of= rows that exists in the statictics for this table. The optimizer just trie= s to find the best plan and to optimize the query plan for execution taking= into consideration all information that can be found for this table (it al= so looks in the statistics information about rows from this table).
=C2=A0
=C2=A0
postgres=3D# ANALYZE test;
ANALYZE
postgres=3D# explain select *= from test;
=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 QUERY PLAN
= ----------------------------------------------------
=C2=A0Seq Scan on t= est=C2=A0 (cost=3D0.00..1.01 rows=3D1 width=3D4)
(1 row)
=C2=A0
My question here is, what it retreiving as rows when there is no such.= One more thing, if i wont do analyze and run the explain plan for three or= more times, then catalogs getting updated automatically and resulting the = correct row as 1.
=C2=A0

Now ANALYZE changed the s= tatistics for this table and now the planner knows that there is just one r= ow. In the background there can work autovacuum so it changes rows automati= cally (the autovacuum work characteristic depends on the settings for the d= atabase).
=C2=A0
Q2. Does explain , will update the catalogs automatically.
=C2=A0

No, explain doesn't u= pdate table's statistics.


regar= ds
Szymon Guz
--0016e6d46d7df112830482d6f945--