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 1mHAFm-0002cO-0R for pgsql-general@arkaria.postgresql.org; Fri, 20 Aug 2021 19:32:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mHAFk-0006Kh-W0 for pgsql-general@arkaria.postgresql.org; Fri, 20 Aug 2021 19:32:32 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mHAFk-0006KW-MZ for pgsql-general@lists.postgresql.org; Fri, 20 Aug 2021 19:32:32 +0000 Received: from mail-pg1-x533.google.com ([2607:f8b0:4864:20::533]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mHAFh-0000s8-QD for pgsql-general@postgresql.org; Fri, 20 Aug 2021 19:32:32 +0000 Received: by mail-pg1-x533.google.com with SMTP id t1so10194471pgv.3 for ; Fri, 20 Aug 2021 12:32:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb.com; s=google; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=g+mckAFcdxAfl5irX6e5fJfpL9LSu4QlfItM6jX6Lig=; b=F2Io2eZqM5Ru1fvb2GtgYIGApi7Hyq2BW+V8RTj2l1WJ2DyN3oHF8iWAdRpyiiKFJf 3icgOD4IwkIHa2Zdx0eKZ8PZua6VLucaxaDVptD20sA+Uu3lC+lP5aBzBya4TdeADuDm FyUEEmACUTeteIeKNmhs7t8RyAbchHvfWrddJ1fnwvxCsgBJmGbPzcyMTRWpqqhL95OS TdXmNwa/I5i8kJWTNyu+wBaXSQlQB0XxNb60nh88ibX69dEcwgjWU2FS9xIxy0cWwpqy 9KVClL574WVKB7eX4daTbR7whesHU9EPl9/o2k2y+SoIXAaDcRjG4s9rYcgYLTXsTCwQ aogg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=g+mckAFcdxAfl5irX6e5fJfpL9LSu4QlfItM6jX6Lig=; b=BCQcA0Fi33/dqE9YjXPJTxPrjOT2e4izjXXhOLlv7l7AnuGIL0pPlkfIUtHdxRvl7D f3rc34U2T2LJMwBbahphf8ygb+PwlDqPppLLvp6s2DJeo6DBtJWOj6AmkjfDapBu0Un0 939zdS020CggVUBh3islNbwZnadazMSR7xRyZXNcbAR+HW9L2H6HSOC4Tuj3oATdZ3EH dsVon8Vvo4Z0L3EbsIXAz1/Cgd3C0PGVjTH4oys6WjA02ScREpbYghg2lygRbdxwZTMs whGNK0qmXSjGMjIYGJofXtP4Fyp008wUgrofuS9/7sLBl943wlO+dqdJjrpAa3FVREKN sAqg== X-Gm-Message-State: AOAM531kGoMu4/gwiD+J+TVVmcAYzz29CiACnHQNjWqJdIszv6TGa74k kMsQHs9bhGTug1xL30Ls4A8IeQRUGbO9lIN/o258tKw0qK9KVf7J0AURF4haH55u9d3iDdtBhtc 40Hqg63y+VTb2Bn5BF7C+ekwzYCnuZVjZXmn8ZQCJFjqymyH3sl+XxAISVKBT9MnsILvNTohCq+ lNXi5X7Xk0Q4BHvZ6IJV1FhjwWV5eb3iA8AjaX X-Google-Smtp-Source: ABdhPJx/N0aQ2Mg7k+768eMyhc+vYfMFIewL2WvB8HVqtM6GKRXjW02F8jBr/Rcb2Qu1RhBnwOXN8Q== X-Received: by 2002:a63:ed47:: with SMTP id m7mr20094683pgk.194.1629487947710; Fri, 20 Aug 2021 12:32:27 -0700 (PDT) Received: from laptop280-ma-us.hitronhub.home (24-113-193-150.wavecable.com. [24.113.193.150]) by smtp.gmail.com with ESMTPSA id u16sm9002021pgh.53.2021.08.20.12.32.27 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 20 Aug 2021 12:32:27 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Subject: Re: [E] Regexp_replace bug / does not terminate on long strings From: Mark Dilger In-Reply-To: <1917678.1629478364@sss.pgh.pa.us> Date: Fri, 20 Aug 2021 12:32:26 -0700 Cc: "Markhof, Ingolf" , pgsql-general@postgresql.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <1809528.1629412955@sss.pgh.pa.us> <1917678.1629478364@sss.pgh.pa.us> To: Tom Lane X-Mailer: Apple Mail (2.3608.120.23.2.7) X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk > On Aug 20, 2021, at 9:52 AM, Tom Lane wrote: >=20 > "a*" is easy. "(a*)\1" is less easy --- if you let the a* consume the > whole string, you will not get a match, even though one is possible. > In general, backrefs create a mess in what would otherwise be a pretty > straightforward concept :-(. The following queries take radically different time to run: \timing select regexp_replace( repeat('someone,one,one,one,one,one,one,', 60), '(?<=3D^|,)([^,]+)(?:,\1)+(?=3D$|,)', '\1', -- replacement 'g' -- apply globally (all matches) ); Time: 16476.529 ms (00:16.477) select regexp_replace( repeat('someone,one,one,one,one,one,one,', 60), '(?<=3D^|,)([^,]+)(?:,\1){5}(?=3D$|,)', '\1', -- replacement 'g' -- apply globally (all matches) ); Time: 1.452 ms The only difference in the patterns is the + vs. the {5}. It looks to = me like the first pattern should greedily match five ",one" matches and = be forced to stop since ",someone" doesn't match, and the second pattern = should grab the five ",one" matches it was told to grab and not try to = grab the ",someone", but other than that, they should be performing the = same work. I don't see why the performance should be so different. =E2=80=94 Mark Dilger EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company