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.96) (envelope-from ) id 1vVtEG-009Sxu-0K for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Dec 2025 15:14:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vVtEF-00Dapy-0D for pgsql-bugs@arkaria.postgresql.org; Wed, 17 Dec 2025 15:14:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vVtEE-00Dapn-2R for pgsql-bugs@lists.postgresql.org; Wed, 17 Dec 2025 15:14:15 +0000 Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vVtED-001FRv-0w for pgsql-bugs@postgresql.org; Wed, 17 Dec 2025 15:14:15 +0000 Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-649820b4b3aso4234736a12.3 for ; Wed, 17 Dec 2025 07:14:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec.at; s=google; t=1765984453; x=1766589253; darn=postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:from:to:cc:subject:date :message-id:reply-to; bh=JgkqoLgVBYTcYWuvUh+l9zamxrOv9ejhMA3JQvSnVO8=; b=cgzVNdxizZhvhlbTxp2nfDmispoZ4y0nYyJ6Cg5fgNnO4YyPmc8Y1tJMYbkGWOUbGg 3wxv5EV5R+C5l+7aX/cMUIqAAWjPUrRZfB90/rDMD0YbWcqLOwqBaqG6eqnOxDurSYrL X+vrHtZyWlGRMre3kPGmYfh4JFFwfgPPqtsd2d2Jjo4DmXE+QpbYV0zS9TxikhihVmhF Vcdr4I6OLb4Ryawrdruh3bVJcWqs9m1NgswKuYGMz+o0qFqbXqNsGQjOYMUTvZA+3a+W 6zPWaTsTFZ8f7/2fBVQETRWpVU2ZvDa6diUqD5Z27niPYnhsXJC9QHqv8hYcDKE9ly5z mL0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1765984453; x=1766589253; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:to:from:subject:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=JgkqoLgVBYTcYWuvUh+l9zamxrOv9ejhMA3JQvSnVO8=; b=ERKE2f1cwJ/qIQXh1Qeu7WKqEzMrjg/KxRZOEIT1WlZVCVayc6Bjc22ZdZ3YOvxJKI TnixfzO6Uwnc95Mh88IqT4ccMaaIazbA1niRQi4l1Qz5nxBZYIx2rxE5ygIk8NWXjZjF nkG1sUYPwH2+a8Tii4BS1AEaEllojXI5gamzo5k7Sp/CNU6OwWQX+EUpZNpNMh+lxEUX XPXeLMdspCQxLAW3cTPu1To6IKeYoa9VMi5UICLvp03eEIrTbzR/AYwgz0NF6xiLzKFR Mcb6DE0ThiVXnVJvA/ZG3PBqCrAq6yt2mkRsXnlHRGhFsbRIF7mRqUvbZ83BAx9bxgai Jf6Q== X-Forwarded-Encrypted: i=1; AJvYcCWPg/0a5BDCRMXy4Z1qUc+iVbPqG8fzuFdJsE3vww32Qfj6CBh+nnnSMAeLn9aQZJOpoHC1RDH2/ufA@postgresql.org X-Gm-Message-State: AOJu0YyiEVM+nb8WUa/lQSJP6RUKXwQfS0JtNbHLCyf9pJB+kYpSqYbZ eMxNdNNVWIpV4SPx2HUO/aVXbbvmsazJtGSV3OX7BbxMtJyCsSqZc/Q/fXFR2r9oycA= X-Gm-Gg: AY/fxX7nnJdUrolF57MuGFH4Ll7vZj90vQdKD1oIbsomCNH4b19UOr6NKifgOVRLzDJ 8jvYa/UWxOWPprs/iMZKuKNX+8TaRc2B50jRR7tyvwbwbyNlWD1hNKtDpPrgN+igWFGmRi8DmYd sNazdrZxcH2c17BZ5boFUFXbu+dwBoavE15fFVLH7g/2CU8vxStgZdUfJKGHDTLzQXSL+dXeW8b x7tlUTOvxxyUV8EI4iUvjYcT73taGhuN9QoamW4+xxxdEEjvs6VvhyQqEUg982z1tXvZzveUTGA Tw5Szwv9ATEXR5qZWoByBTWPPnLU31hjjEdO9d+Vcu8J+vRogdhVBcWOx49ok7xmf3S/N0CA4Qq vuWXV4ZTh27rTkzu2h8eq3PyPDBYmKzrXeyPf3p82U0nnnFBRPVeIFBMMplnsDp6ca20agRy76Q qP/ZcrlJUqJ9svPpSdyAfGVK7sH2sP51ogCkLX7/4T X-Google-Smtp-Source: AGHT+IFFyusxsYYmqtvNy1wc8xDV3bVKWrMrdb29wiWZ4b0BSZ29OO+L/smQKoB7QL85UylsyNWdVw== X-Received: by 2002:a17:907:7e90:b0:b76:d880:e70b with SMTP id a640c23a62f3a-b7d23b23dfcmr1993282566b.55.1765984452560; Wed, 17 Dec 2025 07:14:12 -0800 (PST) Received: from laurenz.albe-K4N0CV00F97414D ([88.116.133.170]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-b7cfa51b864sm1931363366b.37.2025.12.17.07.14.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 17 Dec 2025 07:14:12 -0800 (PST) Message-ID: <90df511ca8ff12c52e05b7ed3500b1233ef93def.camel@cybertec.at> Subject: Re: Possible performance issues: DISTINCT ON + ORDER BY and JIT with aggregation From: Laurenz Albe To: Jhonathan Cruz , pgsql-bugs@postgresql.org Date: Wed, 17 Dec 2025 16:14:11 +0100 In-Reply-To: References: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.56.2 (3.56.2-2.fc42) MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Tue, 2025-12-16 at 21:25 -0300, Jhonathan Cruz wrote: > I would like to report two query execution behaviors that may indicate > performance issues or regressions. I am not certain whether these are > known or expected behaviors, so I would appreciate guidance. >=20 > ---------------------------------------------------------------------- > 1) DISTINCT ON combined with ORDER BY > ---------------------------------------------------------------------- >=20 > I observed cases where queries using DISTINCT ON together with ORDER BY > produce execution plans with explicit sorting steps, even when the orderi= ng > requirements are clearly defined. >=20 > Query: >=20 > EXPLAIN (ANALYZE, BUFFERS) > SELECT DISTINCT ON (grp) > =C2=A0 =C2=A0 =C2=A0 =C2=A0grp, id, created_at > FROM t > ORDER BY grp, created_at DESC; >=20 > Example EXPLAIN ANALYZE output: >=20 > Unique =C2=A0(cost=3D45231.12..47731.12 rows=3D100 width=3D24) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 (actual time=3D182.413..198.721 rows=3D100 lo= ops=3D1) > =C2=A0 Buffers: shared hit=3D12845 > =C2=A0 -> =C2=A0Sort =C2=A0(cost=3D45231.12..46481.12 rows=3D500000 width= =3D24) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (actual time=3D182.410..191.256= rows=3D500000 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Sort Key: grp, created_at DESC > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Sort Method: quicksort =C2=A0Memory: 51200kB > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Buffers: shared hit=3D12845 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 -> =C2=A0Seq Scan on t =C2=A0(cost=3D0.00..11= 231.00 rows=3D500000 width=3D24) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (actual time=3D0.012..38= .117 rows=3D500000 loops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Buffers: shared hit=3D12= 845 > Planning Time: 0.213 ms > Execution Time: 202.981 ms >=20 > Question: > - Is this planner behavior expected for DISTINCT ON queries, or could > =C2=A0 this case potentially avoid a full sort? That is normal and as expected. If you need all result rows, you have to sort all the rows to find the unique results. If you put a LIMIT clause on the query, PostgreSQL could perform a top-N sort. DISTINCT is easy to write, but sometimes the most expensive part of a query. I don't think that can be avoided. > ---------------------------------------------------------------------- > 2) Possible JIT performance regression with aggregation and light filteri= ng > ---------------------------------------------------------------------- >=20 > On PostgreSQL 16.x, I observed that queries performing aggregation with > light filtering may run slower with JIT enabled compared to JIT disabled. >=20 > Query: >=20 > SET jit =3D on; >=20 > EXPLAIN (ANALYZE, BUFFERS) > SELECT grp, count(*) > FROM t > WHERE id > 100 > GROUP BY grp; >=20 > Example EXPLAIN ANALYZE output (JIT enabled): >=20 > HashAggregate =C2=A0(cost=3D12431.22..12441.22 rows=3D100 width=3D12) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(actual time=3D156= .234..156.941 rows=3D100 loops=3D1) > =C2=A0 Group Key: grp > =C2=A0 Buffers: shared hit=3D12483 > =C2=A0 -> =C2=A0Seq Scan on t =C2=A0(cost=3D0.00..11231.00 rows=3D499900 = width=3D4) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 (actual time=3D0.014..52.731 rows=3D499900 lo= ops=3D1) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Filter: (id > 100) > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Rows Removed by Filter: 100 > =C2=A0 =C2=A0 =C2=A0 =C2=A0 Buffers: shared hit=3D12483 > JIT: > =C2=A0 Functions: 5 > =C2=A0 Options: Inlining true, Optimization true, Expressions true, Defor= ming true > =C2=A0 Timing: Generation 2.312 ms, Inlining 18.942 ms, > =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 Optimization 94.117 ms, Emission 22.88= 4 ms, Total 138.255 ms > Planning Time: 0.184 ms > Execution Time: 159.214 ms Now this is more like a bug. JIT still has its problems. I think not the least of the problems is that PostgreSQL cannot predict how long the compilation will take. At the risk of drawing fire, I have now come to recommend turning JIT off unless you have an analytical workload. Yours, Laurenz Albe