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 1lCPyJ-0005o9-As for pgsql-general@arkaria.postgresql.org; Wed, 17 Feb 2021 16:46:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lCPyH-0002rF-0l for pgsql-general@arkaria.postgresql.org; Wed, 17 Feb 2021 16:46:37 +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 1lCPyG-0002nY-Lf for pgsql-general@lists.postgresql.org; Wed, 17 Feb 2021 16:46:36 +0000 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lCPyE-0005il-BG for pgsql-general@lists.postgresql.org; Wed, 17 Feb 2021 16:46:36 +0000 Received: by mail-oi1-x233.google.com with SMTP id j5so3388589oie.1 for ; Wed, 17 Feb 2021 08:46:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=BP+/vepuuSWeDOYlthHi5IgyectdWxrhbcLLwurBAR4=; b=VXLqeK/OizJiWlHqJ2bB8qHbEZhXYgpTR/tylwfzRMvPyO7h4/0PfHoJKPaKc7cGCI JHwJtFKcWwvr4nr71XVBjveFaXOksnv0cbr45nsQ/hJY6wiOkOnlWNF4lC9McCIwOey8 TryU5At8h5AKD3eoMkafrNEGnoShpFVWnujVWQvX/vtVogWwE37MoXBw6zvquwzfmhcM /GcrJ98omwE0PP5p6UUNYe98YWI7ygSIlBS1L/Rn7HcCOUrqSCn5rZlUnZLcW+WKAzUh kMTqUeeeNw0Ezxg4hyIB/oQJVGgxNH4FOuoyDB0aPdTdJ0o32WLF8NgB/0NCrO9wYkEo RvJQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=BP+/vepuuSWeDOYlthHi5IgyectdWxrhbcLLwurBAR4=; b=XrC66SwJr3mGpk+zOSg6B4gfMnGfQGrQtTA5SP9aJhk/53fvDKhM7BxPWs4v9Dmjx0 QoDAL8RftLaJ3PvJA3V8Ary9Q2yMdizeb/Bsugye+T4iywHwb9Rl1qJrRuoOhpTPpYAo j5Pph/vnWQbz584hniHwYK89+cLcqBH78Gj5+JpBCCt/Xleo4kgjx+XkRwyMLUCrj7Yt fq/ea0NNWfs8K1QirmvqChs0ZVPZ/PDnPG/06Mi7BIr4lMqXIkagJD2n2LePo+EE6zIg TLvJF2+WWFoAcauoHMLfHci+oFJRMg6HteMYSc7eUc0DG8Ze0ifetblkLAv5JpZ0DDFX y2WA== X-Gm-Message-State: AOAM532FrbTqAUBA7qWiLIIdjp9LNqiOf5KTUwjPxyq7lQvYFsdwBY5s cOKqdERn2Qz4i1qUWFVU9zMQrXNcYW/2EQ== X-Google-Smtp-Source: ABdhPJyhg8KQX8Yx5k5HlpSGuHNGFP1fK2dHi6u/4BF+gDg9KU9yU0aJ90bM46LX/yVcjb/23ZdKVg== X-Received: by 2002:aca:a9ce:: with SMTP id s197mr6022067oie.37.1613580391085; Wed, 17 Feb 2021 08:46:31 -0800 (PST) Received: from [192.168.88.10] (ip174-67-141-204.no.no.cox.net. [174.67.141.204]) by smtp.googlemail.com with ESMTPSA id y4sm483993oou.44.2021.02.17.08.46.29 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 17 Feb 2021 08:46:30 -0800 (PST) Subject: Re: Slow index creation To: pgsql-general@lists.postgresql.org References: From: Ron Message-ID: <589427a8-ef2c-7b3e-b2cc-71168235b276@gmail.com> Date: Wed, 17 Feb 2021 10:46:29 -0600 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------8B08B92DDA0F5DE01B8BFBF0" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------8B08B92DDA0F5DE01B8BFBF0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 2/16/21 12:30 PM, Paul van der Linden wrote: > Hi, > > I have 2 functions: > CREATE FUNCTION foo(a text, b text, c text) RETURNS text AS > $func$ > DECLARE >     retVal text; > BEGIN >     SELECT >       CASE >         WHEN a='v1' AND b='b1' THEN 'r1' >         WHEN a='v1' THEN 'r2' >         ... snip long list containing various tests on a,b and c >         WHEN a='v50' THEN 'r50' >       END INTO retval; >     RETURN retVal; > END > $func$ LANGUAGE PLPGSQL IMMUTABLE; > > CREATE FUNCTION bar(r text, geom geometry) RETURNS int AS > $func$ > DECLARE >     retVal int; > BEGIN >     SELECT >       CASE >         WHEN r='r1' AND st_area(geom)>100 THEN 1 >         WHEN r='r1' THEN 2 >         ... snip long list containing various tests on r and st_area(geom) >         WHEN r='r50' THEN 25 >       END INTO retval; >     RETURN retVal; > END > $func$ LANGUAGE PLPGSQL IMMUTABLE; > > 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. > > 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... > > Is that because I'm using functions in the WHERE clause, or because my > CASE lists are quite long, or both? How long does SELECT a, b, c, foo(a, b, c) from blarg; take? Ditto SELECT a, b, c, foo(a, b, c), bar(foo(a, b, c)) from blarg; Ditto SELECT a, b, c, foo(a, b, c) from blarg where bar(foo(a, b, c)) < 12; That'll narrow the problem. > Is there any way to speed up that index creation? Is upgrading to a newer > postgres a viable option (so the JITTER can speed up the functions)? > > Paul -- Angular momentum makes the world go 'round. --------------8B08B92DDA0F5DE01B8BFBF0 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit On 2/16/21 12:30 PM, Paul van der Linden wrote:
Hi,

I have 2 functions:
CREATE FUNCTION foo(a text, b text, c text) RETURNS text AS
$func$
DECLARE
    retVal text;
BEGIN
    SELECT
      CASE
        WHEN a='v1' AND b='b1' THEN 'r1'
        WHEN a='v1' THEN 'r2'
        ... snip long list containing various tests on a,b and c
        WHEN a='v50' THEN 'r50'
      END INTO retval;
    RETURN retVal;
END
$func$ LANGUAGE PLPGSQL IMMUTABLE;

CREATE FUNCTION bar(r text, geom geometry) RETURNS int AS
$func$
DECLARE
    retVal int;
BEGIN
    SELECT
      CASE
        WHEN r='r1' AND st_area(geom)>100 THEN 1
        WHEN r='r1' THEN 2
        ... snip long list containing various tests on r and st_area(geom)
        WHEN r='r50' THEN 25
      END INTO retval;
    RETURN retVal;
END
$func$ LANGUAGE PLPGSQL IMMUTABLE;

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.

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...

Is that because I'm using functions in the WHERE clause, or because my CASE lists are quite long, or both?

How long does SELECT a, b, c, foo(a, b, c) from blarg; take?
Ditto SELECT a, b, c, foo(a, b, c), bar(foo(a, b, c)) from blarg;
Ditto SELECT a, b, c, foo(a, b, c) from blarg where bar(foo(a, b, c)) < 12;

That'll narrow the problem.

Is there any way to speed up that index creation? Is upgrading to a newer postgres a viable option (so the JITTER can speed up the functions)?

Paul

--
Angular momentum makes the world go 'round.
--------------8B08B92DDA0F5DE01B8BFBF0--