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 1sozYl-00FSIu-0U for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 06:13:36 +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 1sozYk-00Clzx-Jl for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 06:13:34 +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.94.2) (envelope-from ) id 1sozYk-00Clzp-6i for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 06:13:34 +0000 Received: from mout.gmx.net ([212.227.17.21]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sozYd-000wB6-AQ for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 06:13:33 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.net; s=s31663417; t=1726208006; x=1726812806; i=shammat@gmx.net; bh=/6U9Dwij/uR77i9T23Vr3e+jEfvxZYmU2GgnprUR7vI=; h=X-UI-Sender-Class:Message-ID:Date:MIME-Version:Subject:To: References:From:In-Reply-To:Content-Type: Content-Transfer-Encoding:cc:content-transfer-encoding: content-type:date:from:message-id:mime-version:reply-to:subject: to; b=bqEQo3W7L8nW47wBu9JczdG7IA8XVSWPQenT0i6nrH3KV3VF3Uz4ps6Vq4hbzvjK K8ZTh9rYpE0fhjs7RBbo2eikTG3XcbkkH00WaT8W2xuGb/6fU9Mgi5iStzI1QmTAr Pm1l8+zHSaxODK6WVu/yDIxRwwII0G6VJn8Dx1ZbkDVAGfwoc+j50zsCRPSAE6zCu 8GvcWzuIUEaorU5kMu4VQE32LZ89kwjjnyvrvpKBKzA2l/rY+h1e7PyQFGOfTFS/W bSqinlqPTtIhEh8Td7vdFZyV+4Kuz+humTFeme7L5ZOu0AKIHS6yMYiV7HQHvY2t9 q9rOLY529sXjCuZrEA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [10.1.34.23] ([185.40.248.10]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MdNY8-1sFeo21FQ0-00ZPVU for ; Fri, 13 Sep 2024 08:13:26 +0200 Message-ID: <88e52d11-6185-473d-9eb2-673b62cb2426@gmx.net> Date: Fri, 13 Sep 2024 08:13:25 +0200 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: Functionally dependent columns in SELECT DISTINCT To: pgsql-general@lists.postgresql.org References: Content-Language: de-DE From: shammat@gmx.net In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Provags-ID: V03:K1:BAZgcQV2xyjyh37O/9CSRhiXS9zTkU2BznkH1qImYPVZVVKW7Py Xo42uMFRbCArweP1cQeapM/icLagOtdDlDW8ED/4GG9o+oGGg3nPM7f4LXtc0rQ3sqKAvKT R2VRwSljoNnpC92vvQB3vpgTVsWledIGkSNJKQea2pyfn7dFqGMudr6pTCsVgkI0lbTZaOw 88xrRN/3Ef2G+EVzt+O5Q== X-Spam-Flag: NO UI-OutboundReport: notjunk:1;M01:P0:RM6tdvqsa0A=;qlcqcpYfcyYMYOsBgHRuD6UuMyR OqO6sQ6G1HUU60hMip1qrOwjBgaPv6ygUi97vk87ZquPC2kFJAYJIObA4gRmUfN6QTcdqUpBt ZrNFhHvCAagP/1NIWiaZU49pTdXAHTyhMIwhpXFZf4rlmgQ/+rIq5cnXxklEMQN7U0T9WXRCQ ean4dqJAKe8ekStPFfuhrsUucd7VAg+RzjaUaPpgax2dedovdEsuqYDE0+pAa7/6DBWhLRS0y 1qRji3irNgSkQHmrgro7W8PSkaJyCTBi8I/kCfp0tyT0JnZu7/NMUIC68gxO04bBXA/6DI3Ei Fm1tMXiGHZYRPOkWeHdzw+mrxzWX4/myzm0+WMKR0U2JJ5UEZMS3Vy3lRqQ4c76eUvLe2zvF5 0Cn3QInG8KPJWf4vZ+bTUauILUSQwROdkQg5/l5lwWWTq85DVSeERRmtAsN9qYZiNkpripder usqP30K1z6RkTZbe84eFYm6e2whvSw181FUEcA6GGIs63cJYv6rnFVwObGOhpukLHBsJeMzLj V6+aHKA4ipumHiHlz+9LfCayEI0bsEUEMjzkwr7EG+CpjSUf33ZGI2nP3nYCtqPSxV3Di+Q6x MjK1GXJ9OnbeoNP4a7RVsOL8LAIipIc7JQTnvDSXYA38JAMFxMAjkWNP22O7grzpH+PGFyC5G WFqLbQnducORkD33455xF9IkKmsmxJLwKQzl0qu0gEhTphCnYLj9jRDpp5mUgsJxV4k+qwdBE J/Hi1zdjpxkb5OD6QSMyq6JRa8NygQTNALHpOLsQ46b/QI3zXOxatePIrw2fX/hm6O2EHxPmT jk86u36/IDwjLutZpg3M6hCA== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Willow Chargin schrieb am 13.09.2024 um 07:20: > Hello! Postgres lets us omit columns from a GROUP BY clause if they are > functionally dependent on a grouped key, which is a nice quality-of-life > feature. I'm wondering if a similar relaxation could be permitted for > the SELECT DISTINCT list? > > I have a query where I want to find the most recent few items from a > table that match some complex condition, where the condition involves > joining other tables. Here's an example, with two approaches: What about using DISTINCT ON () ? SELECT DISTINCT ON (items.id) items.* FROM items JOIN parts ON items.id =3D parts.item_id WHERE part_id % 3 =3D 0 ORDER BY items.id,items.create_time DESC LIMIT 5; This gives me this plan: https://explain.depesz.com/s/QHr6 on 16.2 (Windo= ws, i7-1260P)