Re: help with Postgres function

From: Stephan Szabo <sszabo(at)megazone(dot)bigpanda(dot)com>
To: ctrl <ctrl(at)altonsys(dot)com>
Cc: pgsql-sql(at)postgresql(dot)org
Subject: Re: help with Postgres function
Date: 2004-06-21 05:35:09
Message-ID: 20040620221537.C69900@megazone.bigpanda.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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?

In response to

Browse pgsql-sql by date

  From Date Subject
Next Message Pradeepkumar, Pyatalo (IE10) 2004-06-21 06:21:19 Function Parameters - need help !!!
Previous Message Josh Berkus 2004-06-21 04:57:57 Re: question about which column(s) are the right foreign key