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 1ueLAK-00811o-4H for pgsql-general@arkaria.postgresql.org; Tue, 22 Jul 2025 22:08:52 +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 1ueLAI-00EnED-F0 for pgsql-general@arkaria.postgresql.org; Tue, 22 Jul 2025 22:08:50 +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 1ueLAI-00EnE5-4Y for pgsql-general@lists.postgresql.org; Tue, 22 Jul 2025 22:08:50 +0000 Received: from mail-lj1-x229.google.com ([2a00:1450:4864:20::229]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1ueLAG-000H2x-28 for pgsql-general@postgresql.org; Tue, 22 Jul 2025 22:08:49 +0000 Received: by mail-lj1-x229.google.com with SMTP id 38308e7fff4ca-32b8134ef6aso60341381fa.0 for ; Tue, 22 Jul 2025 15:08:48 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1753222125; x=1753826925; darn=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=TLi0CCfjZ5JTZ2Ex857bevMMwFqp63T5VRVcXGzBSdA=; b=glA801jKv04QsygZMBH2PEQabkbm4RLwg0zgZGuTT/TvLdDOb2SbGIIgzKLRMJm8a6 FevIhPq8BKvhD0Gmu9ycl8TjDUZJOyYczX1I8/g/3tWhxoZBFzFvoAQXUliTbnaLcFRQ bJDiffEyFJBECj+gRrCMxHQXrPfpsHsnGwLdRjcZqxCFFhUlvT1vnGrh8jvtKmEkvt45 tMR8dcWpsj12+hBlmJzYaynMSTot2/Bj96Z9equUy2vVwhNvs9kKj32rG9CjempIF3Q0 okYAGsZ5taEoosQ9Kt2e5z8uPqVjj3bFj4iuHNlha4PKlXPA8lTVedreXy89EbwUnnaD sE4w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1753222125; x=1753826925; 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=TLi0CCfjZ5JTZ2Ex857bevMMwFqp63T5VRVcXGzBSdA=; b=ja08txO17OxvROogdRhy57bH6WZyPmv5z/8N0JCLiuFqxf8pTUUbxjqPusX5fH0BFY 8ocxDNFx849szx/9mzzBNoyPrJjY7KhOZDCMJeVdvDk3Ee62NEY4vs7ywf25VjMklXLJ bKazYh6+3UiGRAPxxdceKREgsPQrLthYj0UGvI6+U4MfhG8kyzxZgNM6YrhFJAOk8O3a Jqw9DeRe6r/6FVFDPzOITqmUDvAb32d0f+LnLxEY4KMojYa/0lf5gLu5/9nCGM/j/+8X yFdHciIUhsbuzQzxx8oL31L8QFsLOUkbtnSKwTu6WUSFpLaMwqFhIrskGkJtwTVMb+LU +43Q== X-Gm-Message-State: AOJu0YyvKhwlEvZMgJGxcI+ncQ4gzUPr6ixmrq+YN7LPYConxsVFKHO9 cB2waSLpqdhSPLnUiGJaRTwf+A24zdZJRqwR1eZtcr3YcIDpWMYEblmNlNxJQlNT7POU+P/0uLY KgkO9bA27QEkZpeMvj+66tiQZsAyrLIyD7Xa0 X-Gm-Gg: ASbGnctSXnNCEIhAUpuf9jVJVSJlTZ0kSpB0veMbeKXw5ZQD0GM54p4VaLn+O31Xxrn TC+kMg5Ap9uAXWRvbVfOFxClztcR9sGovAP5VHLfWvb2DPdQ8K0dzaNYO6izlEPuZyj+yRRO73K 4ZJ3PZFH9LDaPkvF80zpNw1DCGaWG0s/McLDtH505ITefHQVssfIgjPB497EdfnanKSZV/wvxNV mUm7ZjnxEchYEjKZZ0I0wW5pLKBod6y8kqhG+dKLQ== X-Google-Smtp-Source: AGHT+IFPxAWu6o1Uhypa8Z5F4Q7ycfrbruM8PeVEFACQjT8oijTMeieSCQnO0lUHiM1/+yEXJNuhw1tKzChpg7L8DBA= X-Received: by 2002:a2e:b895:0:b0:32b:755e:6cc8 with SMTP id 38308e7fff4ca-330dfdf4067mr1036181fa.31.1753222124997; Tue, 22 Jul 2025 15:08:44 -0700 (PDT) MIME-Version: 1.0 References: <20250722151735.toridqqnb3krhimp@hjp.at> In-Reply-To: <20250722151735.toridqqnb3krhimp@hjp.at> From: David Rowley Date: Wed, 23 Jul 2025 10:08:31 +1200 X-Gm-Features: Ac12FXz4gzBPXcjh22cYuLUs167em8ppRn1siqI1OoLIsNQabkNWDixyX_1eQ3Y Message-ID: Subject: Re: Wrapping a select in another select makes it slower To: "Peter J. Holzer" Cc: pgsql-general@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 Wed, 23 Jul 2025 at 03:18, Peter J. Holzer wrote: > > PostgreSQL version 17.5 on Ubuntu 24.04 (PGDG build). > -> Merge Left Join (cost=4613.25..7180.30 rows=8357 width=136) (actual time=222.037..292242.701 rows=40460 loops=1) > Merge Cond: (ns.nspname = s.schemaname) > Join Filter: ((s.tablename = tbl.relname) AND (s.attname = att.attname)) > Rows Removed by Join Filter: 1215045276 > -> Sort (cost=4476.84..4477.18 rows=139 width=200) (actual time=156.864..178628.030 rows=1215036357 loops=1) > Sort Key: s.schemaname > Sort Method: external sort Disk: 6480kB > -> Subquery Scan on s (cost=2189.59..4471.89 rows=139 width=200) (actual time=81.840..131.044 rows=30921 loops=1) > The plans are clearly different, with the slow one claiming to sort 1.2 > billion rows (but using only 6.5MB of disk space) despite the node below > it only returning 30921 rows. 1215036357 is almost but not quite > 30921*40460, but that nested loop is a sibling of the sort, so it > shouldn't be included, right? This is coming from the "mark and restore" due to the Merge Join that executes that sort on its inner side "rewinding" the inner side back to check the matches for the next outer row. This is happening because the Merge Join on the slow query only contains 1 of the 3 columns you're joining on. Notice the Sort's subnode only returned 30921 rows. > And why are the plans different at all? Computing a few extra values per > row shouldn't change the cost of the query delivering the rows, IMHO. > But then the costs are very similar, so maybe it's just some random > variation. That's a good question. What happens if you SET enable_incremental_sort=0; and run the slow query again? I've not managed to recreate this locally, so far. I tried on 17.5 with hash joins disabled to get the merge join plan but my merge join includes all 3 clauses rather than your 1 clause. I'm unsure why your planner doesn't include all 3. To eliminate 1 moving part, is it still slow if you make the outer query just SELECT * FROM the subquery rather than have the additional calculations in the select list? (the planner won't pullup the subquery due to the GROUP BY, it'll only be eliminated during setrefs.c, which is well after the planner will choose the merge join clauses) David