Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rvFGM-00AioY-Ob for pgsql-general@arkaria.postgresql.org; Fri, 12 Apr 2024 11:40:11 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rvFGK-00DFip-Lr for pgsql-general@arkaria.postgresql.org; Fri, 12 Apr 2024 11:40:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rvFGK-00DFih-BP for pgsql-general@lists.postgresql.org; Fri, 12 Apr 2024 11:40:08 +0000 Received: from mail-lf1-x132.google.com ([2a00:1450:4864:20::132]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rvFGH-002V4T-Ot for pgsql-general@lists.postgresql.org; Fri, 12 Apr 2024 11:40:06 +0000 Received: by mail-lf1-x132.google.com with SMTP id 2adb3069b0e04-516c403c2e4so790515e87.0 for ; Fri, 12 Apr 2024 04:40:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712922004; x=1713526804; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=Gt17wtTWetbTne8MQ1Ad0mrQ86bXT6GSPavzaKPL3LY=; b=WP/k4mWjWJdgWi3BmRnSn5NEC02o/KFREV0WIU48VNx/sbIFVf9hgOXzevoS0Vbevd z9ikSoSpoP3yHpCvJqtFFaKxUGBAKHnop2NJyidc7BkbFPVyqsbdAkLhyW5wkET63QUo Wzh0oTh/13iAz9cZu3IPLLw1A1LS2TyqJMtL8G2Ive6lBcixsw7IoQisdoXaTAhtphRV 0JyrfqNPu7P5DSBvMxgDPz+MiitGBroZHDbPUQqGOJiSB40EfcAQADS24HdklvEM7mro hn8u/bTDRlI/vRI4+Eg0QriQYPUO6RmBoZKHcoaaMcABpTT7v7yl8cpdRCpJ4X+eCU9I 9YBA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712922004; x=1713526804; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Gt17wtTWetbTne8MQ1Ad0mrQ86bXT6GSPavzaKPL3LY=; b=mx66b5rQJ5idi/ZATXVdRb3jpb26ZMfAVlGvCXsCPoRm1YkB3SZsbstLEC/G44Kqyn C6blxsOJMenvQ+pmibPyCkL2k9/o+FUHnytHjBcpyxHSkqaKDRus3cUMR97yckjCFZ1A LM79d5OrGIfAhXjR4rJANqfVdu+1c3SPPB5Snet6MSVxxpje37pTfW2YdR7DKnP6mlXZ HpWnu0uFI8LmuNRBpoyZVx+kd3ME5z1nzQ4qw1RhUws0wgH8salgWsEQeQafWudSvIuY 6724W9s09xzvQhLOAqW8c957niu7PkwyFPfXTCG6zdBR8bq27iBAAkaGA8oKaZbc0seq nwIw== X-Gm-Message-State: AOJu0YwHxUFZ9Vb0v2Ukno4+QX0PXdpQuFpGJYlMXKehq/EaGbI7dWjw BoG84B/jqFD/Nf77cYTt6/xAgZNTAfr1om/WK0izIpMEL6idaLTtMxO0LS9y5PFTKQf4ZNjY0Ls t86zTx52gWYnNs1EdFbbvh6hhhPIQmfkD X-Google-Smtp-Source: AGHT+IGctOdDg2oEStPal3GrwjI3uJoe/nJycWoyoJ6pV7ADH+guer36k7HhYb2cBXvHxpnRF04xA3pNH+J8Z9eOfU8= X-Received: by 2002:ac2:5186:0:b0:516:d281:c678 with SMTP id u6-20020ac25186000000b00516d281c678mr623788lfi.17.1712922003942; Fri, 12 Apr 2024 04:40:03 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Fri, 12 Apr 2024 23:39:52 +1200 Message-ID: Subject: Re: effect on planner of turning a subquery to a table, sql function returning table To: Thierry Henrio Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, 12 Apr 2024 at 23:27, Thierry Henrio wrote: > JIT: > Functions: 36 > Options: Inlining true, Optimization true, Expressions true, Deforming true > Timing: Generation 1.949 ms, Inlining 28.891 ms, Optimization 207.481 ms, Emission 134.907 ms, Total 373.228 ms > Execution Time: 429.037 ms It looks very much like the majority of the extra time is being spent doing JIT compilation. This triggers for plan A but not plan B. You can see from: > GroupAggregate (cost=401037.82..503755.82 rows=1467400 width=124) (actual time=416.851..426.534 rows=4670 loops=1) that the top-level row estimates are off. This makes the estimated cost higher than it actually is. The planner opts to have tuple deforming and expression evaluation JIT compiled to try to speed up the plan thinking it's worthwhile. It's not in this case. You can switch JIT off to try without with: SET jit=0; You might want to consider editing postgresql.conf and raising the jit_above_cost, jit_inline_above_cost and jit_optimize_above_cost values to some higher value or disable JIT completely. SELECT pg_reload_conf(); -- to reload the config file afterwards. David