Recursive SQL functions

From: Peter Eisentraut <peter_e(at)gmx(dot)net>
To: PostgreSQL Development <pgsql-hackers(at)postgresql(dot)org>
Subject: Recursive SQL functions
Date: 2001-10-13 18:15:16
Message-ID: Pine.LNX.4.30.0110122157060.648-100000@peter.localdomain
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

While looking to implement the ODBC replace() function (replace occurences
of $2 in $1 by $3), I found that it could be expressed as:

CREATE FUNCTION replace(text, text, text) RETURNS text AS '
select
case when position($2 in $1) = 0 or char_length($2) = 0
then $1
else substring($1 from 1 for position($2 in $1) - 1)
|| $3
|| replace(substring($1 from position($2 in $1) + char_length($2)), $2, $3)
end;
' LANGUAGE SQL WITH (isstrict);

Now this command doesn't actually work because it requires the replace()
function to exist already. But it does work if one first creates a stub
replace() function and then uses CREATE OR REPLACE.

(So much about the claim that procedural languages are a security hole
because they allow infinite loops.)

I was wondering whether, as a future project, we could make this more
convenient by parsing the body of the function with the binding of the
function already in effect.

Comments?

--
Peter Eisentraut peter_e(at)gmx(dot)net http://funkturm.homeip.net/~peter

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Peter Eisentraut 2001-10-13 18:15:39 Re: EXTRACT broken
Previous Message Peter Eisentraut 2001-10-13 18:14:19 Re: Pre-forking backend