X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 2718E9DCA1C for ; Wed, 11 Jan 2006 19:22:02 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 15356-04 for ; Wed, 11 Jan 2006 19:22:04 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id 81A669DCA10 for ; Wed, 11 Jan 2006 19:21:59 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.3/8.13.3) with ESMTP id k0BNLwYj008987 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Wed, 11 Jan 2006 16:22:00 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.4/8.13.4) with ESMTP id k0BNLvra088936; Wed, 11 Jan 2006 16:21:57 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.4/8.13.4/Submit) id k0BNLvx7088935; Wed, 11 Jan 2006 16:21:57 -0700 (MST) (envelope-from mfuhr) Date: Wed, 11 Jan 2006 16:21:57 -0700 From: Michael Fuhr To: Burak Seydioglu Cc: pgsql-performance@postgresql.org Subject: Re: indexes on primary and foreign keys Message-ID: <20060111232157.GA88829@winnie.fuhr.org> References: <1b8a973c0601111438m3e0b1cc0hf9c387a4a1d6791d@mail.gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1b8a973c0601111438m3e0b1cc0hf9c387a4a1d6791d@mail.gmail.com> X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, score=0.091 required=5 tests=[AWL=0.091] X-Spam-Score: 0.091 X-Spam-Level: X-Archive-Number: 200601/134 X-Sequence-Number: 16612 On Wed, Jan 11, 2006 at 02:38:42PM -0800, Burak Seydioglu wrote: > I do a load of sql joins using primary and foreign keys. What i would like > to know if PostgreSQL creates indexes on these columns automatically (in > addition to using them to maintain referential integrity) or do I have to > create an index manually on these columns as indicated below? > > CREATE TABLE cities ( > city_id integer primary key, > city_name varchar(50) > ); > > CREATE INDEX city_id_index ON cities(city_id); PostgreSQL automatically creates indexes on primary keys. If you run the above CREATE TABLE statement in psql you should see a message to that effect: NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "cities_pkey" for table "cities" If you look at the table definition you should see the primary key's index: test=> \d cities Table "public.cities" Column | Type | Modifiers -----------+-----------------------+----------- city_id | integer | not null city_name | character varying(50) | Indexes: "cities_pkey" PRIMARY KEY, btree (city_id) So you don't need to create another index on cities.city_id. However, PostgreSQL doesn't automatically create an index on the referring column of a foreign key constraint, so if you have another table like CREATE TABLE districts ( district_id integer PRIMARY KEY, district_name varchar(50), city_id integer REFERENCES cities ); then you won't automatically get an index on districts.city_id. It's generally a good idea to create one; failure to do so can cause deletes and updates on the referred-to table (cities) to be slow because referential integrity checks would have to do sequential scans on the referring table (districts). Indeed, performance problems for exactly this reason occasionally come up in the mailing lists. -- Michael Fuhr