Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fn2W7-0004hQ-8c for pgsql-general@arkaria.postgresql.org; Tue, 07 Aug 2018 13:59:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1fn2W5-00082e-S8 for pgsql-general@arkaria.postgresql.org; Tue, 07 Aug 2018 13:59:17 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fn2TL-0004O0-Cp for pgsql-general@lists.postgresql.org; Tue, 07 Aug 2018 13:56:29 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1fn2TH-000609-85 for pgsql-general@postgresql.org; Tue, 07 Aug 2018 13:56:25 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 42E3021A29; Tue, 7 Aug 2018 09:56:22 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Tue, 07 Aug 2018 09:56:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; bh=+bOhbLAP6rT0LUmcYKiQMh/qAc9yA Ue/aqXP9e1e9m8=; b=IOD83BkpKnt3Kzi8/HCD3GSIxR0epguZvEpOcNHXs8eWe kwcxm8XJmBDSAkUmWuT1St1k0xXH7KPNYcXoOX8A0UYR/pVKV9Lhif4l79qsRKoX pb/U3/pHTof8TzqpVJHyyVy5LO2Nc4iGiYJe93ZPjBBGRAEvQKCuxMb2bQJ5rB2Y URCu0BHnQIMddFkkhqwAv8lNXeo//17s0GlX67MqkKPaVhZhQc0WnUOTeqatHVlP EHL+fZckYllzgUaU45QXU/3ZkOPEp+S1dxx4nwQbbRfs2WO6fzq/uflVVQa1nsRa lm9+6pi4Qn3PLqliNtNEB3Nrgf30FlYeGZhF/ciXQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm3; bh=+bOhbL AP6rT0LUmcYKiQMh/qAc9yAUe/aqXP9e1e9m8=; b=GaZRIs40qYPSvB9IBPbUkR ZfeLtpjfhO01N2S5RUMgmcmQ/WbPGiho5fCtX6QtdARSuU6k4Ugtaq4JRk8/zu6V qHSLSeG+Rz+h+N6WyPkUOH+1bBW1+HdUsi4dINUo2YxX0IeCqJTi5cHseX6N0NMr u9Bt8lBFPewhtKsavKrHoDO/WbVScmTj9he0pJA8epXWlBCj6EjmHXV5mb7IhvBO fjR3rwuw+5x/j1buXTQ6nM0Mzs+9FTki+VIML9Rpn/K48r6qOKnBKrsjqyDY6OB5 8QmLYS8XoqPUgqlF2dQ2Ea+q9x/YqWBBEzrWGDn2KcY2RCz+f61/RMs6bfZZxIfA == X-ME-Proxy: X-ME-Sender: Received: from [192.168.1.3] (unknown [71.212.134.181]) by mail.messagingengine.com (Postfix) with ESMTPA id C1DB310261; Tue, 7 Aug 2018 09:56:20 -0400 (EDT) Subject: Re: Immutable function WAY slower than Stable function? To: Ken Tanzer , laurenz.albe@cybertec.at Cc: Tom Lane , PG-General Mailing List References: <17961.1533597095@sss.pgh.pa.us> <1533621599.2438.4.camel@cybertec.at> From: Adrian Klaver Message-ID: <7d690a98-0be6-69d8-c91f-3af2ced942d1@aklaver.com> Date: Tue, 7 Aug 2018 06:56:19 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 08/07/2018 12:38 AM, Ken Tanzer wrote: > > > On Tue, Aug 7, 2018 at 12:05 AM Ken Tanzer > wrote: > Whoops, scratch that previous explain and query.  I accidentally left in > a hard-coded client_id from earlier testing.  The correct query is: > > EXPLAIN (VERBOSE,ANALYZE,BUFFERS) SELECT client_id, > CASE WHEN > (SELECT program_type_code FROM reg_spc WHERE target_date() BETWEEN reg_spc_date AND COALESCE(reg_spc_date_end,target_date()) AND client_id=tro.client_id LIMIT 1) > ILIKE 'SSP%' THEN > > COALESCE((SELECT staff_id FROM staff_employment_current WHERE staff_position_code='COORD_PROP' AND agency_project_code='SSP' LIMIT 1),(SELECT staff_id FROM staff_employment_current WHERE staff_position_code='MGRPROJ' AND agency_project_code='SSP' LIMIT 1)) > ELSE > > (SELECT staff_inspector FROM tbl_residence_own ro LEFT JOIN l_housing_project USING (housing_project_code) WHERE client_id=tro.client_id AND target_date() BETWEEN residence_date AND COALESCE(residence_date_end,target_date()) AND NOT ro.is_deleted LIMIT 1) > END > FROM tbl_residence_own tro; > > The corrected explain output is attached, and the actual timing was: > >  Planning time: 2.741 ms >  Execution time: 2538.277 ms > > Sorry for the confusion!  It's been a long day, and filled with many 20+ > second waits for queries to finish. :) Hmm, whatever it is looks to be tied to si_* being IMMUTABLE. Just a thought but have you tried(NOTE: DEFAULT value): CREATE OR REPLACE FUNCTION staff_inspector_stable( client INTEGER, asof DATE DEFAULT target_date()) RETURNS INTEGER AS $$ SELECT CASE WHEN (SELECT program_type_code FROM reg_spc WHERE asof BETWEEN reg_spc_date AND COALESCE(reg_spc_date_end,asof) AND client_id=client LIMIT 1) ILIKE 'SSP%' THEN --- SSP answer COALESCE((SELECT staff_id FROM staff_employment_current WHERE staff_position_code='COORD_PROP' AND agency_project_code='SSP' LIMIT 1),(SELECT staff_id FROM staff_employment_current WHERE staff_position_code='MGRPROJ' AND agency_project_code='SSP' LIMIT 1)) ELSE ---SPC answer (SELECT staff_inspector FROM tbl_residence_own ro LEFT JOIN l_housing_project USING (housing_project_code) WHERE client_id=client AND asof BETWEEN residence_date AND COALESCE(residence_date_end,asof) AND NOT ro.is_deleted LIMIT 1) END --LIMIT 1 $$ LANGUAGE SQL STABLE; > > Cheers, > Ken > > -- Adrian Klaver adrian.klaver@aklaver.com