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 3F5E253481 for ; Wed, 20 Apr 2005 09:41:58 -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 57792-08 for ; Wed, 20 Apr 2005 12:41:50 +0000 (GMT) Received: from mx-00.sil.at (mx-00.sil.at [62.116.68.196]) by svr1.postgresql.org (Postfix) with ESMTP id BB20A52A67 for ; Wed, 20 Apr 2005 09:41:48 -0300 (ADT) Received: (qmail-ldap/ctrl 74534 invoked from network); 20 Apr 2005 12:41:49 -0000 Received: from unknown (HELO jupiter.sonorys.at) ([213.235.243.42]) (envelope-sender ) by mx-00.sil.at (qmail-ldap-1.03) with SMTP for ; 20 Apr 2005 12:41:49 -0000 Received: from neptun.sonorys.at (neptun.sonorys.at [172.24.1.17]) by jupiter.sonorys.at (8.11.7+Sun/8.11.7) with ESMTP id j3KCfo129669 for ; Wed, 20 Apr 2005 14:41:50 +0200 (MEST) Received: by neptun.sonorys.at with Internet Mail Service (5.5.2653.19) id <2JD5WJJP>; Wed, 20 Apr 2005 14:41:50 +0200 Message-ID: From: =?ISO-8859-1?Q?K=D6PFERL_Robert?= To: pgsql-sql@postgresql.org Subject: Function to either return one or all records Date: Wed, 20 Apr 2005 14:41:50 +0200 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2653.19) Content-Type: text/plain; charset="ISO-8859-1" X-Virus-Scanned: by amavisd-new at hub.org X-Archive-Number: 200504/299 X-Sequence-Number: 21192 Hi all, I think I have got a usual problem. I'm asking here, however, because I wonder why it works this way. The problem is to write a fcn that eihter returns all records or just one/none filtered by some expression. For example get a value by id or return all values if the given id is null. For a table like this (id (PK) int | val int ) one would write a function like this: CREATE OR REPLACE FUNCTION getval(integer) RETURNS SETOF id_val_tbl AS $BODY$ select * from id_bal_tbl where ( $1 is null )or (id=$1 ); $BODY$ LANGUAGE 'sql' VOLATILE SECURITY DEFINER; It works fine, however an index is never used (if just one record is requested). The column id has a btree-Index but what aobut it. I'm wondering how this comes and how one can overcome this limit. I read that btree and null values do not like each other very well. And surely, if the Select is transformed reduced to select * from id_bal_tbl where (false)or (id=$1 ); the Index is used! What I don't get is: When expanding parameters in the SQL-function it will evaluate to the statement above. Bevore any data is looked up. Why is that? Thanks again in advance