X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id C2B26D1B1CF for ; Thu, 1 Jul 2004 06:57:39 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 19204-04 for ; Thu, 1 Jul 2004 09:57:32 +0000 (GMT) Received: from mta04-svc.ntlworld.com (mta04-svc.ntlworld.com [62.253.162.44]) by svr1.postgresql.org (Postfix) with ESMTP id 5DADBD1B170 for ; Thu, 1 Jul 2004 06:57:27 -0300 (ADT) Received: from chezphil.org ([82.4.146.209]) by mta04-svc.ntlworld.com (InterMail vM.4.01.03.37 201-229-121-137-20020806) with ESMTP id <20040701095626.EFYS24958.mta04-svc.ntlworld.com@chezphil.org> for ; Thu, 1 Jul 2004 10:56:26 +0100 Received: from chezphil.org ([127.0.0.1]) by andorra with smtp (dmsmtpd 0.00001); Thu, 01 Jul 2004 10:57:29 +0100 Message-ID: <40E3E003.4050405@chezphil.org> Date: Thu, 01 Jul 2004 10:57:23 +0100 User-Agent: Mozilla Thunderbird 0.5 (X11/20040306) X-Accept-Language: en-us, en MIME-Version: 1.0 To: Kenneth Gonsalves , pgsql-sql@postgresql.org Subject: Re: finding if a foreign key is in use References: <0406290618410W.02481@thenilgiris.com> <7679232@chezphil.org> <0407010743031D.02481@thenilgiris.com> In-Reply-To: <0407010743031D.02481@thenilgiris.com> Content-Type: text/plain; charset="us-ascii"; format="flowed" Content-Transfer-Encoding: 7bit From: Phil Endecott X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.1 tagged_above=0.0 required=5.0 tests=RCVD_IN_SORBS X-Spam-Level: X-Archive-Number: 200407/4 X-Sequence-Number: 18256 >>>in my app i have a table where the id serves as a foreign key for >>>one or more other tables. if i want to delete a row in the table, >>>i currently search the other tables where this table is referenced >>>to see if the row is in use - and then, if not in use, permit >>>deletion. >>>Now if i want the delete button in my app to be disabled whenever >>>a row that is in use is selected, searching the database every time >>>would dramatically slow down the app. >> >>Basically you do have to do this search. But it won't be too slow if you >>create an index on the foreign key. > > pity. thought postgres would have some function like 'in_use' to tell when a > row that is used as a foreign key is in actual use and hence cannot be > deleted. surely, in a database of millions of records, it wouldnt have search > them all to find if the row is in use? It doesn't "search them all" if you have an index. If your database has a million records it needs to look at only 20 index entries, as 2^20 is about a million. (At least that's what I, naively, think it should do - anyone who knows more want to correct me?) --Phil.