Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGqkD-0001Tl-I1 for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 22:42:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mGqkC-0000nb-Hb for pgsql-general@arkaria.postgresql.org; Thu, 19 Aug 2021 22:42:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGqkC-0000nI-8N for pgsql-general@lists.postgresql.org; Thu, 19 Aug 2021 22:42:40 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mGqkA-0006uz-3o for pgsql-general@postgresql.org; Thu, 19 Aug 2021 22:42:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 17JMgZAh1809529; Thu, 19 Aug 2021 18:42:35 -0400 From: Tom Lane To: "Markhof, Ingolf" cc: pgsql-general@postgresql.org Subject: Re: Regexp_replace bug / does not terminate on long strings In-reply-to: References: Comments: In-reply-to "Markhof, Ingolf" message dated "Thu, 19 Aug 2021 13:48:20 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1809527.1629412955.1@sss.pgh.pa.us> Date: Thu, 19 Aug 2021 18:42:35 -0400 Message-ID: <1809528.1629412955@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Markhof, Ingolf" writes: > BRIEF: > regexp_replace(source,pattern,replacement,flags) needs very (!) long to > complete or does not complete at all (?!) for big input strings (a few k > characters). (Oracle SQL completes the same in a few ms) Regexps containing backrefs are inherently hard --- every engine has strengths and weaknesses. I doubt it'd be hard to find cases where our engine is orders of magnitude faster than Oracle's; but you've hit on a case where the opposite is true. The core of the problem is that it's hard to tell how much of the string could be matched by the (,\1)* subpattern. In principle, *all* of the remaining string could be, if it were N repetitions of the initial word. Or it could be N-1 repetitions followed by one other word, and so on. The difficulty is that since our engine guarantees to find the longest feasible match, it tries these options from longest to shortest. Usually the actual match (if any) will be pretty short, so that you have O(N) wasted work per word, making the runtime at least O(N^2). I think your best bet is to not try to eliminate multiple duplicates at a time. Get rid of one dup at a time, say by str := regexp_replace(str, '([^,]+)(,\1)?($|,)', '\1\3', 'g'); and repeat till the string doesn't get any shorter. I did come across a performance bug [1] while poking at this, but alas fixing it doesn't move the needle very much for this example. regards, tom lane [1] https://www.postgresql.org/message-id/1808998.1629412269%40sss.pgh.pa.us