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 1lD6FO-0006uf-Qi for pgsql-general@arkaria.postgresql.org; Fri, 19 Feb 2021 13:55:06 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lD6FN-0002vJ-QD for pgsql-general@arkaria.postgresql.org; Fri, 19 Feb 2021 13:55:05 +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 1lD6FN-0002vC-Ig for pgsql-general@lists.postgresql.org; Fri, 19 Feb 2021 13:55:05 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lD6FJ-0001FU-As for pgsql-general@lists.postgresql.org; Fri, 19 Feb 2021 13:55:04 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 9A95E44F3; Fri, 19 Feb 2021 14:54:59 +0100 (CET) Date: Fri, 19 Feb 2021 14:54:59 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Slow index creation Message-ID: <20210219135459.GB10464@hjp.at> Mail-Followup-To: pgsql-general@lists.postgresql.org References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="vGgW1X5XWziG23Ko" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --vGgW1X5XWziG23Ko Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2021-02-16 19:30:23 +0100, Paul van der Linden wrote: > I have 2 functions: > > CREATE FUNCTION foo(a text, b text, c text) RETURNS text AS > $func$ > DECLARE > =A0 =A0 retVal text; > BEGIN > =A0 =A0 SELECT > =A0 =A0 =A0 CASE > =A0 =A0 =A0 =A0 ... snip long list containing various tests on a,b and c > =A0 =A0 =A0 END INTO retval; > =A0 =A0 RETURN retVal; > END > $func$ LANGUAGE PLPGSQL IMMUTABLE; >=20 > CREATE FUNCTION bar(r text, geom geometry) RETURNS int AS > $func$ > DECLARE > =A0 =A0 retVal int; > BEGIN > =A0 =A0 SELECT > =A0 =A0 =A0 CASE > =A0 =A0 =A0 =A0 WHEN r=3D'r1' AND st_area(geom)>100 THEN 1 > =A0 =A0 =A0 =A0 ... snip long list containing various tests on r and st_a= rea(geom) > =A0 =A0 =A0 END INTO retval; > =A0 =A0 RETURN retVal; > END > $func$ LANGUAGE PLPGSQL IMMUTABLE; >=20 > and a large table t (100M+ records) with columns a, b, c and geom running= on PG > 11, on spinning disks with 64GB memory and 28 cores. >=20 > When I create a simple geom index with CREATE INDEX ON t USING gist(geom)= it > finishes in about an hour, but when I create a partial index using these 2 > functions > CREATE INDEX ON t USING gist(geom) WHERE bar(foo(a,b,c),geom)<12 it takes= over > 20 hours... It has to call these functions for each of those 100M+ lines. So that's about 0.7 ms per line. Is that to be expected for what those functions do? I don't know. As depesz wrote, use smaller test case, like 1000 or 10000 rows. You can test the speed of those functions in isolation. Compare select a, b, c from t; select foo(a, b, c) from t; select bar(foo(a, b, c), geom) from t; That tells you how much time is spent in foo and bar. If the sum is close to those 0.7 ms, you know that you need to make those functions faster. I think SQL doesn't short-circuit, so in your function bar, that st_area(geom) in the first WHEN clause will always be called, regardless of the value of r. You can either call that once (as depesz suggested) or you can avoid calling it by nesting the cases: case when r =3D 'r1' then case when st_area(geom) > 100 then 1 else 2 end when r =3D 'r2' then .... end You can then go one step furthe: case when r like 'r_' then -- handle r1 ... r9 when r like 'r1_' then -- handle r10 ... r19 when r like 'r2_' then -- handle r20 ... r29 end That reduces the average number of comparisons from 50+ to 8. Depesz measured an overhead of 0.016ms per log message. That's low enough that you can probably afford a few messages, even if each function invocation only takes a few milliseconds. So definitely try that if you need to know where your functions spend their time. hp --=20 _ | Peter J. Holzer | Story must make more sense than reality. |_|_) | | | | | hjp@hjp.at | -- Charles Stross, "Creative writing __/ | http://www.hjp.at/ | challenge!" --vGgW1X5XWziG23Ko Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAmAvwy8ACgkQ8g5IURL+ KF34AhAArxKa8f4CBLmt6ob9HGoZYoQ1rFwSB5nQoWBFHqmqrzsM0DwOHcCtG1rx 7zXIqUz/T4yhiN7CYpiDQPd0yIEmU7eg+n1YGchjlMUBK1Kl/jqzylzYsC3jAHEQ 06OmpN/ogf7gZljlxOU1dY10isJ32GRAPx1i/NBHojW5U1FcIOw3sK9uNaZ7Eu5j han/CF4B2HIPiPvrN3ME1WOySoi8arRMRFhAF9xApIFQP4lDVBJdzgk5h/yqBo3H VDPt5zW5WGitxEvMsOXypjOVGFZw0lcTfJhLRkuHwns1YB/j8QKgwzmBVy4BaDaU QdmGzeehPZMUSioxtWkNVqS98eqLKl5212bq7oCH8qI+sjPDNg2sogvpLEWdQ8Q5 EPeMeuf6wsl73ArdJeInnPdzB8ue9bA51HCm+cCCzS0c/aXUODcJO9iaCUYrgT8g KOgEH1PoZ0PbpSTsjxMGxKfF1uE35ob1TP/Mlz9k4+M4q31oSu5a9CXQzivhE02B 9Tc0gYwI4fXN5tn27MvC7dNmG0e4CYfUyYOMVyh94nhF41cegejATSIep54XUgHn vVIuGhV7c3DnBg3NTnfkPWyH5E8cW8SeJkMXFF/XMq/943r3t+BVHnPL2vypnq97 gbL5dcNW7Tx+SvxnMtpv1vzOBPgFg0b80PlKMmdc4n1bIJYYE8M= =SwaT -----END PGP SIGNATURE----- --vGgW1X5XWziG23Ko--