Thread: simple recursive function in plpgsql fails
ians=# SELECT version(); version ---------------------------------------------------------------PostgreSQL 7.1.3 on i686-pc-linux-gnu, compiled by GCC 2.95.4 My goal is to find the last occurance of a pattern in a string. As a helper function, I wrote this: DROP FUNCTION reverse(text); CREATE FUNCTION reverse(text) RETURNS text AS 'DECLARE str ALIAS FOR $1; BEGIN IF length(str) > 1 THEN RETURN reverse(substr(str, 2)) || substr(str, 1, 1); ELSE RETURN str; END IF; END;' LANGUAGE 'plpgsql' ians=# SELECT reverse('q');reverse ---------q (1 row) ians=# SELECT reverse('qw');reverse ---------wq (1 row) ians=# SELECT reverse('qwe');reverse ---------ewq (1 row) ians=# SELECT reverse('qwer');reverse ---------rerq (1 row) Ooops... ------------------------------------------------------------------------ Andrew G. Hammond drew@xyzzy.dhs.org <mailto:drew@xyzzy.dhs.org> http://xyzzy.dhs.org/~drew/ <http://xyzzy.dhs.org/%7Edrew/> 56 2A 54 EF 19 C0 3B 43 72 69 5B E3 69 5B A1 1F 613-389-5481 5CD3 62B0 254B DEB1 86E0 8959 093E F70A B457 84B1
Andrew Hammond <drew@xyzzy.dhs.org> writes: > [ problems with a recursive plpgsql function ] Yeah, there are some known bugs there :-(. AFAICS this cannot be fixed properly until we get around to doing the querytree restructuring that I keep ranting about --- in particular, making a clean distinction between plan tree and execution state. regards, tom lane