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 CC7E6D1B285 for ; Mon, 21 Jun 2004 02:35:11 -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 15385-01 for ; Mon, 21 Jun 2004 05:35:11 +0000 (GMT) Received: from megazone.bigpanda.com (megazone.bigpanda.com [64.147.171.210]) by svr1.postgresql.org (Postfix) with ESMTP id 46321D1B21B for ; Mon, 21 Jun 2004 02:35:09 -0300 (ADT) Received: by megazone.bigpanda.com (Postfix, from userid 1001) id 351FB35648; Sun, 20 Jun 2004 22:35:09 -0700 (PDT) Received: from localhost (localhost [127.0.0.1]) by megazone.bigpanda.com (Postfix) with ESMTP id 33A98352E4; Sun, 20 Jun 2004 22:35:09 -0700 (PDT) Date: Sun, 20 Jun 2004 22:35:09 -0700 (PDT) From: Stephan Szabo To: ctrl Cc: pgsql-sql@postgresql.org Subject: Re: help with Postgres function In-Reply-To: <46a31c4d.0406161349.38d3a34b@posting.google.com> Message-ID: <20040620221537.C69900@megazone.bigpanda.com> References: <46a31c4d.0406161349.38d3a34b@posting.google.com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.0 tagged_above=0.0 required=5.0 tests= X-Spam-Level: X-Archive-Number: 200406/177 X-Sequence-Number: 18122 On Wed, 16 Jun 2004, ctrl wrote: > CREATE OR REPLACE FUNCTION getNextWebsiteForCrawl(integer) RETURNS > website AS ' > DECLARE > my_record RECORD; > w website%rowtype; > count smallint; You can't safely use a variable named count and the count(*) expression below I think, so you'll want to rename this variable. > BEGIN > SELECT id, domain into my_record FROM websites WHERE crawl_status=1 > AND date(last_fetch) > (current_timestamp - interval ''$1 days'') I don't think that'll get you the interval you want. I think you want current_timestamp - $1 * interval '1 day' instead since the $1 isn't going to get replaced inside the string. > ORDER BY last_fetch LIMIT 1; > select count(*) into count from my_record; > if count > 0 then This isn't going to work either. my_record isn't some kind of recordset, it's a single variable so I the select count(*) from my_record doesn't make sense. Perhaps you could check for my_record.id being non-null. > w.id := my_record.id; > w.domain := my_record.domain; > update websites set crawl_status=2 where id = my_record.id; > end IF; > return w; If you couldn't get a value, what do you want this to return? Right now it returns a website where the values are null I believe. The function as written is not going to guarantee distinct results if used from multiple transactions concurrently. How are you planning to use the function? Many sequential calls from a single connection, random usage from multiple, etc?