recursive text construction in plpgsql?

Started by Frank Milesover 24 years ago3 messagesgeneral
Jump to latest
#1Frank Miles
fpm@u.washington.edu

The simple recursive function:

--
DROP FUNCTION testRecurse(int,int);
CREATE FUNCTION testRecurse(int,int) RETURNS text AS '
DECLARE
rslt text;
BEGIN
IF $1 = 0 THEN
rslt= CAST($2 AS TEXT);
ELSE
rslt= CAST($1 AS TEXT) || '','' || testRecurse($1 - 1, $2);
END IF;
RETURN rslt;
END;
' LANGUAGE 'plpgsql';
--

does not give the result I expect. For example, for:
SELECT testRecurse(4,3);
it seems to me that the result should be:
4,3,2,1,3
instead of what is returned:
1,1,1,1,3

Is this supposed to work in 7.1.3?

-frank

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Frank Miles (#1)
Re: recursive text construction in plpgsql?

Frank Miles <fpm@u.washington.edu> writes:

The simple recursive function:
[ doesn't work ]

Hmm. In development sources I get

regression=# SELECT testRecurse(4,3);
NOTICE: Error occurred while executing PL/pgSQL function testrecurse
NOTICE: line 7 at assignment
ERROR: MemoryContextAlloc: invalid request size 2139062147

Looks like you have found a real bug, but I don't know what it is yet...

regards, tom lane

#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Tom Lane (#2)
Re: recursive text construction in plpgsql?

I said:

Frank Miles <fpm@u.washington.edu> writes:

The simple recursive function:
[ doesn't work ]

Looks like you have found a real bug, but I don't know what it is yet...

Now I do :-(. FunctionCache needs to be a read-only data structure;
it can't contain the argument block that's built for an individual call,
because the same expression tree might be invoked recursively while
filling in the arguments for a function. I'm surprised no one has
noticed this before.

I'll fix it for 7.2...

regards, tom lane