Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VJ5Gn-0004nq-A1 for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Sep 2013 17:29:01 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1VJ5Gm-0007KO-Po for pgsql-bugs@arkaria.postgresql.org; Mon, 09 Sep 2013 17:29:00 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VJ5Gm-0007KI-81 for pgsql-bugs@postgresql.org; Mon, 09 Sep 2013 17:29:00 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1VJ5Gj-00012Y-QC for pgsql-bugs@postgresql.org; Mon, 09 Sep 2013 17:28:59 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id r89HSsck022639; Mon, 9 Sep 2013 13:28:54 -0400 From: Tom Lane To: tvees@davincigroep.nl cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8441: Recursive function in plpgsql does not seem to handle null values correctly In-reply-to: References: Comments: In-reply-to tvees@davincigroep.nl message dated "Mon, 09 Sep 2013 13:18:59 -0000" Date: Mon, 09 Sep 2013 13:28:54 -0400 Message-ID: <22638.1378747734@sss.pgh.pa.us> X-Pg-Spam-Score: -2.8 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org tvees@davincigroep.nl writes: > CREATE OR REPLACE FUNCTION longlevenshtein (string1 character varying > (1000000), string2 character varying (1000000)) RETURNS integer AS $$ > BEGIN > IF (length(coalesce($1, '')) = 0 AND length(coalesce($2, '')) = 0) THEN > RETURN 0; > ELSEIF ($1 IS NULL and $2 IS NOT NULL and length($2) > 0) THEN > RETURN length($2); > ELSEIF ($2 IS NULL and $1 IS NOT NULL and length($1)> 0) THEN > RETURN length($1); > ELSEIF length($1) = 0 AND length(coalesce($2, '')) > 0 THEN > RETURN length(coalesce($2, '')); > ELSEIF length($1) > 0 AND (length($2) = 0 or $2 is null) THEN > RETURN length(coalesce($1, '')); > ELSE > RETURN (Levenshtein(SUBSTRING($1 FROM 1 FOR 254), SUBSTRING($2 FROM 1 > for 254)) + longlevenshtein(coalesce(SUBSTRING($1 FROM 255), ''), > coalesce(SUBSTRING($2 FROM 255), ''))); > END IF; > END; > $$ LANGUAGE plpgsql; > When I invoke this function with > SELECT longlevenshtein(null, 'foobar') > I get a ERROR: stack depth limit exceeded Worksforme. You sure you transcribed the function accurately? Note however that sufficiently long input strings *will* drive this function to stack overrun, if you don't run out of heap memory first (I think the heap consumption will be O(N^2) ...). Consider rewriting it with a loop rather than recursion. regards, tom lane -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs