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 1sp8CW-00Gqw4-Gf for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 15:27:13 +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 1sp8CV-005Gga-Vo for pgsql-general@arkaria.postgresql.org; Fri, 13 Sep 2024 15:27:11 +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 1sp8CV-005GgO-Jp for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 15:27:11 +0000 Received: from mail-lf1-f50.google.com ([209.85.167.50]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sp8CP-0010ea-47 for pgsql-general@lists.postgresql.org; Fri, 13 Sep 2024 15:27:10 +0000 Received: by mail-lf1-f50.google.com with SMTP id 2adb3069b0e04-5365cf5de24so2961296e87.1 for ; Fri, 13 Sep 2024 08:27:06 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726241225; x=1726846025; h=content-transfer-encoding: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=ha7/JZOCnT2zzmvMiq170FsXE8+Nwe/IYIdhZ4WCSUU=; b=XQqGpXowZAyDBOMkXXnlZ5YuPIYUj5qXuSQXntq4z/kscwBzmeak442ZhAM6zj7JPc UARBdi4Ql+DrLpoCzQip88hR0gx0mIODXUjiaMV2HaJc9+gn/hMe5s9AsWDVhfBV8vLr wP5POwc1lYql9OyW0FDwANnXVeFtmXao/Y2iZJNcoeeMzX0lykt3m2OuNZjCIT7C6IrO bqEJi5KnM8ZloHM8C/NI426SMZxaDPpQ4DPoCBnDoxa70eVgRymxJGKi0t47Hnm0rLdP NXWKoa3CVWSUP0RFZoYZS39aab7Ckh5LAsUF7EdtE9g8c9vzmU7XxddNJzE1TX6/Gorv K2Ww== X-Gm-Message-State: AOJu0YyILbeuxHkiCz7CYo2ltD5UBI5VxLE5d+5cZhKulP0WOMmSTU8W TdKJqyfrIB6c4r5E3ChMmT46xSFrz6RaxRw5jd0BjonHgGbhTrFUtrLhhQg5XGzJ9I7vyTwJIgp 7kJIRqQFxYZbSwn1QftTfjRSBnig5F/XL+vUYnw== X-Google-Smtp-Source: AGHT+IF60JPQ7GLWPBO6sp8uVzs0aBhFhsCMT2znAK736AcfT7Bztw+hBTklKmtfMjDPsQ1tNkIwJu1NAOsWexTeqJM= X-Received: by 2002:a05:6512:224c:b0:535:6892:3be3 with SMTP id 2adb3069b0e04-53678fe63c0mr4824984e87.41.1726241224526; Fri, 13 Sep 2024 08:27:04 -0700 (PDT) MIME-Version: 1.0 References: <88e52d11-6185-473d-9eb2-673b62cb2426@gmx.net> In-Reply-To: <88e52d11-6185-473d-9eb2-673b62cb2426@gmx.net> From: Willow Chargin Date: Fri, 13 Sep 2024 08:26:53 -0700 Message-ID: Subject: Re: Functionally dependent columns in SELECT DISTINCT To: shammat@gmx.net Cc: pgsql-general@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, Sep 12, 2024 at 11:13=E2=80=AFPM wrote: > > 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 (Wind= ows, i7-1260P) Ordering by items.id changes the answer, though. In the example I gave, items.id and items.create_time happened to be in the same order, but that needn't hold. In reality I really do want the ID columns of the *most recent* items. You can see the difference if you build the test dataset a bit differently: INSERT INTO items(id, create_time) SELECT i, now() - make_interval(secs =3D> random() * 1e6) FROM generate_series(1, 1000000) s(i); We want the returned create_times to be all recent, and the IDs now should look roughly random.