Need indexes on inherited tables?

Поиск
Список
Период
Сортировка
Искать

Need indexes on inherited tables?

От:
"Phil Endecott" <spam_from_postgresql_sql@chezphil.org>
Дата:
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.

Re: Need indexes on inherited tables?

От:
Franco Bruno Borghesi <franco@akyasociados.com.ar>
Дата:
 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

Re: Need indexes on inherited tables?

От:
"Phil Endecott" <spam_from_postgresql_sql@chezphil.org>
Дата:
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.


Re: Need indexes on inherited tables?

От:
Karsten Hilbert <Karsten.Hilbert@gmx.net>
Дата:
> 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

Re: Need indexes on inherited tables?

От:
Franco Bruno Borghesi <franco@akyasociados.com.ar>
Дата:
 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.

FAQ