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 1mKXnA-0007Jv-E5 for pgsql-performance@arkaria.postgresql.org; Mon, 30 Aug 2021 03:17:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mKXn7-0006Ph-Lo for pgsql-performance@arkaria.postgresql.org; Mon, 30 Aug 2021 03:16:57 +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 1mKXn7-0006PZ-9J for pgsql-performance@lists.postgresql.org; Mon, 30 Aug 2021 03:16:57 +0000 Received: from mail-io1-xd29.google.com ([2607:f8b0:4864:20::d29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1mKXn2-0003NV-2O for pgsql-performance@postgresql.org; Mon, 30 Aug 2021 03:16:56 +0000 Received: by mail-io1-xd29.google.com with SMTP id b200so18038066iof.13 for ; Sun, 29 Aug 2021 20:16:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telsasoft-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:content-transfer-encoding:in-reply-to :user-agent; bh=0J/UiXPC6x/tsvucS27MwFJUsgaBuErMbaZ9MXztgus=; b=IaoGJcFAdYsCFj+Kek9dYJBA/YAzkcakF4zBMpjnK121zHQTUpfpLwAnfqOtmLqC/m NyW4ZfDGzXMMFzRP9xkxVyxYg8l/XF0TQ5QDEGSWERvfjGbFDFVftbrF4XLpseBlZL2U V+xT83MFULm3i9uMUl+6B5t91+R3vhuTyBKnbi8zO2DpjZ2sSUwXI+IEv1uBytBqrcZw tM59J6z1e03xZEc2fglcdYD6+z+CRFV78G5DwUxSkbLs1fp6YheWlRdaIjSfp2kfmTIQ TbHt4cLPSy17lIZKSqrFXYoAj2vfqaDiwrLPUn2SaSNvtOaDw7O4DKXv9U0i/1wxhKWa i+pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:content-transfer-encoding :in-reply-to:user-agent; bh=0J/UiXPC6x/tsvucS27MwFJUsgaBuErMbaZ9MXztgus=; b=h62hiGxCndiJKoaRAJz+MH2KPD0QJWsQJS0HRpp8vCPDpCIR98NXVd1ijRU09yvAYM RAtE7lpm1JiWelapQcoO4P1mC3Q7CVk5KWWBZRwhjrGdXGvvaqqmmKcHe+BnG++CxvyA sKKZag+NwgA4xKUdxmm7Whmly70yBTW4HxWGpLOSidsjwue+Earua5gwaDmiGi3roh9T 6q8cR8GJRQbc8e9Jakxehwho/w1Es8CjyPsgzWPsnETOGl+Ja7AfA7dxb1jOCcRc4SS+ tc551ykMRtEzbxJoXMYr0cYlRKtHCU6vk+2God1cz60oj4u2lzdOh6X3vbnYrmsK4mI4 BKbA== X-Gm-Message-State: AOAM532oqHzKTgeT0g2iVk6v7GDQmOZhrX2bqVpGB5EhBx9UbzZWUzEN jyVlSFa6y9zmQKhWhdbTR6nQsA== X-Google-Smtp-Source: ABdhPJyB0DL1tChc+piW1Zkno2kVcyc+ZHE8VejuMKRdbDX8CCncXC88lf723/D6FBQsbG/ZXzjMwQ== X-Received: by 2002:a5d:9ac1:: with SMTP id x1mr16953716ion.191.1630293410346; Sun, 29 Aug 2021 20:16:50 -0700 (PDT) Received: from pryzbyj.telsasoft (charmander.telsasoft.com. [50.244.222.1]) by smtp.gmail.com with ESMTPSA id k14sm7586618ili.19.2021.08.29.20.16.49 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Sun, 29 Aug 2021 20:16:50 -0700 (PDT) Received: by pryzbyj.telsasoft (Postfix, from userid 1000) id F2AA9800C52; Sun, 29 Aug 2021 22:16:48 -0500 (CDT) Date: Sun, 29 Aug 2021 22:16:48 -0500 From: Justin Pryzby To: Pavel Stehule Cc: ldh@laurent-hasson.com, Tom Lane , Ranier Vilela , Andrew Dunstan , pgsql-performance@postgresql.org Subject: Re: Big Performance drop of Exceptions in UDFs between V11.2 and 13.4 (workarounds) Message-ID: <20210830031648.GU26465@telsasoft.com> References: <1050917.1630180243@sss.pgh.pa.us> <1124170.1630244105@sss.pgh.pa.us> <1177685.1630253948@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: User-Agent: Mutt/1.9.4 (2018-02-28) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, Aug 30, 2021 at 04:43:23AM +0200, Pavel Stehule wrote: > po 30. 8. 2021 v 2:44 odesílatel ldh@laurent-hasson.com napsal: > > At this point, I am not sure how to proceed except to rethink that > > toFloat() function and many other places where we use exceptions. We get > > such dirty data that I need a "safe" way to convert a string to float > > without throwing an exception. BTW, I tried other combinations in case > > there may have been some weird interactions with the ::REAL conversion > > operator, but nothing made any change. Could you recommend another approach > > off the top of your head? I could use regexes for testing etc... Or maybe > > there is another option like a no-throw conversion that's built in or in > > some extension that you may know of? Like the "SAFE." Prefix in BigQuery. > > CREATE OR REPLACE FUNCTION safe_to_double_precision(t text) > RETURNS double precision AS $$ > BEGIN > IF $1 SIMILAR TO '[+-]?([0-9]*[.])?[0-9]+' THEN > RETURN $1::double precision; > ELSE > RETURN NULL; > END IF; > END; > $$ LANGUAGE plpgsql IMMUTABLE STRICT; This tries to use a regex to determine if something is a "Number" or not. Which has all the issues enumerated in painful detail by long answers on stack overflow, and other wiki/blog/forums. Rather than trying to define Numbers using regex, I'd try to avoid only the most frequent exceptions and get 90% of the performance back. I don't know what your data looks like, but you might try things like this: IF $1 IS NULL THEN RETURN $2 ELSE IF $1 ~ '^$' THEN RETURN $2 ELSE IF $1 ~ '[[:alpha:]]{2}' THEN RETURN $2 ELSE IF $1 !~ '[[:digit:]]' THEN RETURN $2 BEGIN RETURN $1::float; EXCEPTION WHEN OTHERS THEN RETURN $2; END; You can check the stackoverflow page for ideas as to what kind of thing to reject, but it may depend mostly on your data (what is the most common string? The most common exceptional string?). I think it's possible that could even be *faster* than the original, since it avoids the exception block for values which are for sure going to cause an exception anyway. It might be that using alternation (|) is faster (if less readable) than using a handful of IF branches. -- Justin