X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id B2C5052A04 for ; Tue, 28 Jun 2005 16:39:28 -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 31227-01 for ; Tue, 28 Jun 2005 19:39:27 +0000 (GMT) Received: from smtp1.libero.it (smtp1.libero.it [193.70.192.51]) by svr1.postgresql.org (Postfix) with ESMTP id E5731529E7 for ; Tue, 28 Jun 2005 16:39:26 -0300 (ADT) Received: from localhost (172.16.1.47) by smtp1.libero.it (7.0.027-DD01) id 42C16E9D0000BEAA; Tue, 28 Jun 2005 21:38:53 +0200 Received: from [151.25.86.70] (151.25.86.70) by smtp2.libero.it (7.0.027-DD01) (authenticated as tdezotti@inwind.it) id 41BF65E408AB6917; Tue, 28 Jun 2005 21:39:09 +0200 Message-ID: <42C1A5EF.7070505@streppone.it> Date: Tue, 28 Jun 2005 21:33:03 +0200 From: Cosimo Streppone User-Agent: Mozilla Thunderbird 1.0.2 (X11/20050317) X-Accept-Language: it, it-it, en-us, en MIME-Version: 1.0 To: John A Meinel Cc: Postgresql Performance list Subject: Re: tricky query References: <6EE64EF3AB31D5448D0007DD34EEB3415C2BEC@Herge.rcsinc.local> <42C167C6.5070108@arbash-meinel.com> <42C16FE4.2020803@arbash-meinel.com> In-Reply-To: <42C16FE4.2020803@arbash-meinel.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at libero.it serv7 X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.034 tagged_above=0 required=5 tests=AWL X-Spam-Level: X-Archive-Number: 200506/572 X-Sequence-Number: 13209 John A Meinel wrote: > John A Meinel wrote: >> Merlin Moncure wrote: >> >>> I need the smallest integer that is greater than zero that is not in the >>> column of a table. In other words, if an 'id' column has values >>> 1,2,3,4,6 and 7, I need a query that returns the value of 5. >> >> [...] > > Well, I was able to improve it to using appropriate index scans. > Here is the query: > > SELECT t1.id+1 as id_new FROM id_test t1 > WHERE NOT EXISTS > (SELECT t2.id FROM id_test t2 WHERE t2.id = t1.id+1) > ORDER BY t1.id LIMIT 1; I'm very interested in this "tricky query". Sorry John, but if I populate the `id_test' relation with only 4 tuples with id values (10, 11, 12, 13), the result of this query is: cosimo=> create table id_test (id integer primary key); NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'id_test_pkey' for table 'id_test' CREATE TABLE cosimo=> insert into id_test values (10); -- and 11, 12, 13, 14 INSERT 7457570 1 INSERT 7457571 1 INSERT 7457572 1 INSERT 7457573 1 INSERT 7457574 1 cosimo=> SELECT t1.id+1 as id_new FROM id_test t1 WHERE NOT EXISTS (SELECT t2.id FROM id_test t2 WHERE t2.id = t1.id+1) ORDER BY t1.id LIMIT 1; id_new -------- 15 (1 row) which if I understand correctly, is the wrong answer to the problem. At this point, I'm starting to think I need some sleep... :-) -- Cosimo