pg.ddx.io  pgsql-sql@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Stefano Vita Finzi <stefano.vita@pronesis.it>
To: pgsql-sql@postgresql.org
Subject: plpgsql recursion
Date: Tue, 20 May 2003 18:53:08 +0200
Message-ID: <01a601c31ef0$4a628220$0600a8c0@stefano> (raw)

Greetings!
I have a table like:

node parent
   1       2
   2       3
   3       4

Since i traverse this table with a recursive function, i want to avoid
infinite recursion loop. I have wrote a function to check that a new record
does not create a circular dependency. The function i wrote is as follow:

CREATE OR REPLACE FUNCTION dba_test(INTEGER,INTEGER) RETURNS TEXT AS '
  DECLARE
    traversing ALIAS FOR $1;
    testing ALIAS FOR $2;
    t_rec RECORD;
  BEGIN
    FOR t_rec IN SELECT node,parent FROM dba_test WHERE parent = traversing
LOOP
      IF t_rec.node = testing THEN
        RETURN ''Circular'';
      ELSE
        PERFORM dba_test(t_rec.node,testing);
      END IF;
    END LOOP;
    RETURN ''ok'' || testing::text;
  END;
' LANGUAGE 'plpgsql';

I would use this function BEFORE inserting the new row. But if i try SELECT
dba_test(4,1); i don't have the result i expect. Can i you give me an hint
where am i wrong?

Thank you!

Stefano Vita Finzi
kluge@despammed.com




view thread (5+ messages)  latest in thread

Message-ID: <01a601c31ef0$4a628220$0600a8c0@stefano>
Permalink:  ../01a601c31ef0$4a628220$0600a8c0@stefano/
Also on:    postgresql.org/message-id/01a601c31ef0$4a628220$0600a8c0@stefano

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-sql@postgresql.org
  Cc: stefano.vita@pronesis.it
  Subject: Re: plpgsql recursion
  In-Reply-To: <01a601c31ef0$4a628220$0600a8c0@stefano>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox