Wrong stats for empty tables

From: "Emmanuel Cecchet" <Emmanuel(dot)Cecchet(at)asterdata(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Cc: "manu(at)frogthinker(dot)org" <manu(at)frogthinker(dot)org>
Subject: Wrong stats for empty tables
Date: 2009-05-05 16:27:55
Message-ID: 43826FCDC252204EA7823B2E7CF3CCEC06CBE567@Pandora.AsterData.local
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Here is an example showing the problem:

Welcome to psql 8.3.6, the PostgreSQL interactive terminal.
manu=# create table foo (x int);
CREATE TABLE
manu=# explain select * from foo;
QUERY PLAN
-------------------------------------------------------
Seq Scan on foo (cost=0.00..34.00 rows=2400 width=4)
(1 row)

manu=# analyze foo;
ANALYZE
manu=# explain select * from foo;
QUERY PLAN
-------------------------------------------------------
Seq Scan on foo (cost=0.00..34.00 rows=2400 width=4)
(1 row)

manu=# insert into foo values (1);
INSERT 0 1
manu=# analyze foo;
ANALYZE
manu=# explain select * from foo;
QUERY PLAN
---------------------------------------------------
Seq Scan on foo (cost=0.00..1.01 rows=1 width=4)
(1 row)

Now a possible cause for this might be the relpages attribute in pg_class (the default value 0 does not seem to be interpreted correctly):

manu=# create table bar(x int);
CREATE TABLE
manu=# explain select * from bar;
QUERY PLAN
-------------------------------------------------------
Seq Scan on bar (cost=0.00..34.00 rows=2400 width=4)
(1 row)

manu=# select relpages from pg_class where relname='bar';
relpages
----------
0
(1 row)
manu=# update pg_class set relpages=1 where relname='bar';
UPDATE 1
manu=# explain select * from bar;
QUERY PLAN
---------------------------------------------------
Seq Scan on bar (cost=0.00..0.00 rows=1 width=4)
(1 row)

This is a real problem if you have a lot of empty child tables. Postgres will not optimize correctly queries in the presence of empty child tables.

Is this a bug?

Thanks for your help,
Emmanuel
--
Emmanuel Cecchet
Aster Data Systems
Web: http://www.asterdata.com

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2009-05-05 16:51:46 Re: bytea vs. pg_dump
Previous Message Archana Sundararam 2009-05-05 16:26:47 Re: ALTER TABLE should change respective views