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 1lCRs1-0001Lw-5y for pgsql-general@arkaria.postgresql.org; Wed, 17 Feb 2021 18:48:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lCRs0-0007IY-40 for pgsql-general@arkaria.postgresql.org; Wed, 17 Feb 2021 18:48:16 +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 1lCRrz-0007Gn-R2 for pgsql-general@lists.postgresql.org; Wed, 17 Feb 2021 18:48:15 +0000 Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lCRrv-0005Pm-JZ for pgsql-general@postgresql.org; Wed, 17 Feb 2021 18:48:14 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=depesz.com; s=20170201; h=In-Reply-To:Content-Transfer-Encoding:Content-Type: MIME-Version:References:Reply-To:Message-ID:Subject:Cc:To:Sender:From:Date: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=QSKZwg73cZ9Gjj8NXEKIzuymG7FynuUlQXvvhuz1sI0=; b=BfXfIm/6udu5kpmKsZweVKi9j8 vtBBvIOCRfHJeF3cF/HF/Q6rxREJy9n7r2P3TrKodlQu4krRkLF1Q1qT+Jlllxif+5Co8V4VXhKOn NnK3O6ow0K0czpIqRvILdHFObKfVG8kmpkj+LjgIdDCyUBI7f06DTWeB4Nab4ArflCUE=; Received: from lana.depesz.com ([88.198.49.178] helo=depesz.com) by depesz.com with esmtpa (Exim 4.92) (envelope-from ) id 1lCRrs-0007s3-Ru; Wed, 17 Feb 2021 19:48:08 +0100 Date: Wed, 17 Feb 2021 19:48:08 +0100 From: hubert depesz lubaczewski Sender: depesz@depesz.com To: Paul van der Linden Cc: pgsql-general Subject: Re: Slow index creation Message-ID: <20210217184808.GA14032@depesz.com> Reply-To: depesz@depesz.com References: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 On Tue, Feb 16, 2021 at 07:30:23PM +0100, 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; If this function was converted to SQL function it could be faster, as it could be inlined. > 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; First thing that I notice is that it seems (from the code and comment) that you run st_area(geom) multiple times. If that's really the case - why don't you cache it in some variable? declare v_area float := st_area( geom ); begin ... and then use v_area instead of st_area(geom) depesz