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 1x1OJp-005A2Y-1t for pgsql-bugs@arkaria.postgresql.org; Tue, 01 Sep 2026 13:14:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1x1OJo-006Ghb-1u for pgsql-bugs@arkaria.postgresql.org; Tue, 01 Sep 2026 13:14:28 +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 1x1B8i-002dy0-2f for pgsql-bugs@lists.postgresql.org; Mon, 31 Aug 2026 23:10:09 +0000 Received: from mahout.postgresql.org ([2001:4800:3e1:1::227]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.98.2) (envelope-from ) id 1x1B8g-00000002HE5-0doq for pgsql-bugs@lists.postgresql.org; Mon, 31 Aug 2026 23:10:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=postgresql.org; s=20171124; h=Message-ID:Date:Reply-To:Cc:From:To:Subject: Content-Transfer-Encoding:MIME-Version:Content-Type:Sender:Content-ID: Content-Description:In-Reply-To:References; bh=fTrSZQFnnRZeblL3aLvUnAADOx2OnufI6QJbFR1/DvU=; b=NvTBA/Aa3pyMcISl4VFfA3FZ/8 Mn1GIJxI2lUT/8cKfSb7Uy9t0w9O1KSZ+XJ8cORrIz5HpKsUqWEfDCeuREDsIt7J6G6FL5LPTtOMM Lj2RQwv13uSNiXFKfB3CVhEFSquG5xyCw/bww/Lv+3cJE+zvrCIGChPeOkSpPSzmIhvtqDGE2wIyk /G6dE9t/JC5QP1GUestQi2wxLT6BzQPyiTAJs+xbgJtsecjeRzb2LMnrWcp26BZLJqJ0JNp2w0cbe fTNr5BcMoErptwpSUGLyPmPVIHalwW+7Kfjo2/ayqkob9M18EOHnGjkO0TmYqUDg7s4UQJAkbMI88 t+perD8g==; Received: from wrigleys.postgresql.org ([2a02:16a8:dc51::60]) by mahout.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1x1B8e-00AH6I-2a for pgsql-bugs@lists.postgresql.org; Mon, 31 Aug 2026 23:10:04 +0000 Received: from localhost ([127.0.0.1] helo=wrigleys.postgresql.org) by wrigleys.postgresql.org with esmtp (Exim 4.98.2) (envelope-from ) id 1x1B8d-00000009sCK-1vBA for pgsql-bugs@lists.postgresql.org; Mon, 31 Aug 2026 23:10:03 +0000 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Subject: BUG #19648: Docs on DISTINCT ON do not describe how expressions are matched to ORDER BY To: pgsql-bugs@lists.postgresql.org From: PG Bug reporting form Cc: jacobtylerwalls@gmail.com Reply-To: jacobtylerwalls@gmail.com, pgsql-bugs@lists.postgresql.org Date: Mon, 31 Aug 2026 23:10:00 +0000 Message-ID: <19648-25e90c26a67ae02e@postgresql.org> X-Auto-Response-Suppress: All Auto-Submitted: auto-generated List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk The following bug has been logged on the website: Bug reference: 19648 Logged by: Jacob Walls Email address: jacobtylerwalls@gmail.com PostgreSQL version: 18.4 Operating system: Linux Description: =20 Hi, The SELECT page states: "The DISTINCT ON expression(s) must match the leftmost ORDER BY expression(s)." However, the notion of "matching" is quite specific: it appears to also matter which alias is used in the ordering, as shown by its position in the select list: ``` CREATE TABLE t (a int); SELECT DISTINCT ON (a) a AS x, a AS y FROM t ORDER BY y; ``` Gives: ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions We hit this error in the Django ORM by generating SQL like: ``` CREATE TABLE t (a int, b int); SELECT DISTINCT ON (a, a, b) a, a, b FROM t ORDER BY a, a, b; -- ok SELECT DISTINCT ON (a, a, b) a, a, b FROM t ORDER BY 1, 2, 3; -- ERROR SELECT DISTINCT ON (a) a, a as x FROM t ORDER BY x; -- ERROR ``` As part of fixing this in Django, we'd like to know the rule we should be coding against. Is it accurate to say that DISTINCT ON expressions are matched to select-list entries, and that two entries holding the same value count separately? If so, would it be worth stating anything around this in the docs? Thanks, Jacob