pg.ddx.io pgsql-sql@postgresql.org mailing list archive
help / color / mirror / Atom feedNeed indexes on inherited tables?
5+ messages / 3 participants
[nested] [flat]
* Need indexes on inherited tables?
@ 2004-06-26 19:29 Phil Endecott <spam_from_postgresql_sql@chezphil.org>
2004-06-26 21:06 ` Re: Need indexes on inherited tables? Franco Bruno Borghesi <franco@akyasociados.com.ar>
0 siblings, 1 reply; 5+ messages in thread
From: Phil Endecott @ 2004-06-26 19:29 UTC (permalink / raw)
To: pgsql-sql
Dear Postgresql experts,
I have a base table that declares a primary key spanning a couple of columns:
create table B (
id integer,
xx someothertype,
.....
primary key (id, xx)
);
and a number of derived tables that inherit from B:
create table T (
....
) inherits (B);
An index is automatically created for B because of the primary key.
If I search for something in T using the key columns, e.g. I do
select * from T where id=1 and xx=something;
will the index be used? Or must I explicity create an index on id and xx for T and each of the other derived tables?
Is it any different if I search in B and find rows that are actually in T?
(Slightly unrelated: does the index on (id,xx) help when I am searching only on id?)
Thanks for any insight anyone can offer.
--Phil.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Need indexes on inherited tables?
2004-06-26 19:29 Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
@ 2004-06-26 21:06 ` Franco Bruno Borghesi <franco@akyasociados.com.ar>
2004-06-26 22:30 ` Re: Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
0 siblings, 1 reply; 5+ messages in thread
From: Franco Bruno Borghesi @ 2004-06-26 21:06 UTC (permalink / raw)
To: Phil Endecott <spam_from_postgresql_sql@chezphil.org>; +Cc: pgsql-sql
Table T is not inheriting any indexes, neither the primary key
constraint. That means that the search is going to use an index scan on
table B and a sequential scan on table T (unless of course you add a
primary key constraint or an index on table T).
You can check this things doing:
->SET enable_seqscan TO off;
->EXPLAIN SELECT * FROM B WHERE id=5;
you'll see an index scan on table B and sequential scans on the other
tables.
Doing:
->SELECT C.relname AS table_name, C2.relname AS index_name FROM pg_index
I LEFT JOIN pg_class C ON (I.indrelid=C.oid) LEFT JOIN pg_class C2 ON
(C2.oid=I.indexrelid) WHERE C.relname ILIKE '<table_name>'
you can find out what indexes are available for table_name (or \d
<table_name> in psql).
On Sat, 2004-06-26 at 16:29, Phil Endecott wrote:
> Dear Postgresql experts,
>
> I have a base table that declares a primary key spanning a couple of columns:
>
> create table B (
> id integer,
> xx someothertype,
> .....
> primary key (id, xx)
> );
>
> and a number of derived tables that inherit from B:
>
> create table T (
> ....
> ) inherits (B);
>
> An index is automatically created for B because of the primary key.
>
> If I search for something in T using the key columns, e.g. I do
>
> select * from T where id=1 and xx=something;
>
> will the index be used? Or must I explicity create an index on id and xx for T and each of the other derived tables?
>
> Is it any different if I search in B and find rows that are actually in T?
>
> (Slightly unrelated: does the index on (id,xx) help when I am searching only on id?)
>
> Thanks for any insight anyone can offer.
>
> --Phil.
>
> ---------------------------(end of broadcast)---------------------------
> TIP 7: don't forget to increase your free space map settings
>
Attachments:
signature.asc (application/pgp-signature, 186B) download
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Need indexes on inherited tables?
2004-06-26 19:29 Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
2004-06-26 21:06 ` Re: Need indexes on inherited tables? Franco Bruno Borghesi <franco@akyasociados.com.ar>
@ 2004-06-26 22:30 ` Phil Endecott <spam_from_postgresql_sql@chezphil.org>
2004-06-26 23:37 ` Re: Need indexes on inherited tables? Karsten Hilbert <Karsten.Hilbert@gmx.net>
2004-06-28 16:30 ` Re: Need indexes on inherited tables? Franco Bruno Borghesi <franco@akyasociados.com.ar>
0 siblings, 2 replies; 5+ messages in thread
From: Phil Endecott @ 2004-06-26 22:30 UTC (permalink / raw)
To: Franco Bruno Borghesi <franco@akyasociados.com.ar>; pgsql-sql
I asked if derived tables use primary key indexes generated in the base tables that they inherit from.
Franco Bruno Borghesi replied:
> [the derived table] is not inheriting any indexes, neither the
> primary key constraint.
OK, thanks! I had come to the conclusion that it was not using the index, but I'm really surprised to hear that the primary key constraint that I define in a base table is not inherited. Are any constraints inherited? What happens if I declare a single-column primary key? What if I declare a "not null" constraint or a "check" constraint in a base table?
Having to replicate the constraints and indexes for each derived table is a pain - lots of error-prone typing - but there is a more serious problem: how can I ensure that these keys are unique across all of the derived tables? (i.e. if T1 and T2 inherit from B, and B's primary key is (id,xx), then I want there to be at most one row in (T1 union T2) that has any value of (id,xx).)
Is this a possible area for future enhancements?
Regards,
--Phil.
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Need indexes on inherited tables?
2004-06-26 19:29 Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
2004-06-26 21:06 ` Re: Need indexes on inherited tables? Franco Bruno Borghesi <franco@akyasociados.com.ar>
2004-06-26 22:30 ` Re: Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
@ 2004-06-26 23:37 ` Karsten Hilbert <Karsten.Hilbert@gmx.net>
1 sibling, 0 replies; 5+ messages in thread
From: Karsten Hilbert @ 2004-06-26 23:37 UTC (permalink / raw)
To: pgsql-sql
> Is this a possible area for future enhancements?
Yes.
Karsten
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: Need indexes on inherited tables?
2004-06-26 19:29 Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
2004-06-26 21:06 ` Re: Need indexes on inherited tables? Franco Bruno Borghesi <franco@akyasociados.com.ar>
2004-06-26 22:30 ` Re: Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
@ 2004-06-28 16:30 ` Franco Bruno Borghesi <franco@akyasociados.com.ar>
1 sibling, 0 replies; 5+ messages in thread
From: Franco Bruno Borghesi @ 2004-06-28 16:30 UTC (permalink / raw)
To: Phil Endecott <spam_from_postgresql_sql@chezphil.org>; +Cc: pgsql-sql
you can find more information here:
http://www.postgresql.org/docs/7.4/static/sql-createtable.html (under
the parameters section). For unique constraints, the only thing I can
think of is a table check constraint (SELECT count(pk)=0).
I agree with you, right now there are many inconveniences with
postgresql inheritance, that's why I never use inheritance this way.
I usually stick to the "standard" way, for example, I create a people
table, and for every people I want to insert in my database there is one
row in this table, plus one row with the same id/pk (which is also a fk)
in a specialized table (let's say students, teachers, whatever).
This simplifies things, because attribute
definitions/defaults/constraints are always in one table. Maybe you get
into some other troubles, but I find them less painfull :)
On Sat, 2004-06-26 at 19:30, Phil Endecott wrote:
> I asked if derived tables use primary key indexes generated in the base tables that they inherit from.
>
> Franco Bruno Borghesi replied:
> > [the derived table] is not inheriting any indexes, neither the
> > primary key constraint.
>
> OK, thanks! I had come to the conclusion that it was not using the index, but I'm really surprised to hear that the primary key constraint that I define in a base table is not inherited. Are any constraints inherited? What happens if I declare a single-column primary key? What if I declare a "not null" constraint or a "check" constraint in a base table?
>
> Having to replicate the constraints and indexes for each derived table is a pain - lots of error-prone typing - but there is a more serious problem: how can I ensure that these keys are unique across all of the derived tables? (i.e. if T1 and T2 inherit from B, and B's primary key is (id,xx), then I want there to be at most one row in (T1 union T2) that has any value of (id,xx).)
>
> Is this a possible area for future enhancements?
>
> Regards,
>
> --Phil.
>
>
>
Attachments:
smiley-3.png (image/png, 819B) download
view image
signature.asc (application/pgp-signature, 186B) download
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2004-06-28 16:30 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2004-06-26 19:29 Need indexes on inherited tables? Phil Endecott <spam_from_postgresql_sql@chezphil.org>
2004-06-26 21:06 ` Franco Bruno Borghesi <franco@akyasociados.com.ar>
2004-06-26 22:30 ` Phil Endecott <spam_from_postgresql_sql@chezphil.org>
2004-06-26 23:37 ` Karsten Hilbert <Karsten.Hilbert@gmx.net>
2004-06-28 16:30 ` Franco Bruno Borghesi <franco@akyasociados.com.ar>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox