Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIUpK-0008I2-Ic for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Jan 2023 13:19:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pIUpJ-0003cN-HG for pgsql-hackers@arkaria.postgresql.org; Thu, 19 Jan 2023 13:19:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pIUpJ-0003cD-8J for pgsql-hackers@lists.postgresql.org; Thu, 19 Jan 2023 13:19:33 +0000 Received: from mail-oa1-x2e.google.com ([2001:4860:4864:20::2e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pIUpH-0004UF-5D for pgsql-hackers@lists.postgresql.org; Thu, 19 Jan 2023 13:19:32 +0000 Received: by mail-oa1-x2e.google.com with SMTP id 586e51a60fabf-15f83e0fc63so2421719fac.11 for ; Thu, 19 Jan 2023 05:19:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=TT+at6gZ+iOxJpeUUJMI9//6ILT782XtFrcuOWmZfJ0=; b=TSiv2WEYBe8h6pVtaKqnCJ22XWGQ61SmX4oaDDTnufL5rMPnNMDO0PdfeXrEI5uhfV JRH17O/SS70taFQH+agihzscGdy2PXzdeGOWJHxs5FyjUtNnmWDZ2zw4I/JzCTvBdgT0 CMf2+RhSJVN8UIDlSemY1f5BCXvYLnzn2Ac8cKxgi2wwNGnOgQdHcw58PPD3Q8+KJQ2T CTGBRptPp9l2zXDUhcScdJhlYF0nL2iWyFYCTMnsXfsDHuyZ5cElwW8K/hPhmqGxYzIi EEjZPFj3E3syg1FqlLIRO8gcR74lT5eQ2pA4wq/9rru7kYIcWYfYKkhy8vwexGUgJEmR grVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=TT+at6gZ+iOxJpeUUJMI9//6ILT782XtFrcuOWmZfJ0=; b=id/TjKT4tOAJXWEngHzkDm1zY08DpgbWKHs6m4C742RWInxZqs9i1v35Q1e+xl04Pg 9NK+p+AD8ohLPrf3t0TtrPa6guwfnKaVfn/EwtJ41ZOa7igfFq69QPSoj0DGfdagJoc8 vDucYHzqLmx4v+BBWg9vOh80WCW3vWbwjAiMHFmur9AOJPMPuWR/Qfbb6mA+tzBD2XP1 iCryGBEFGwrKFZ8fVWwUlmE4ApPPU3ygtWKXig1RNgO1VHRWg+DGdkKqWTUETZ9ukuqB lzJOud9o/MaDg8eW3aHIr+J8SosrnCbZLLGy4QDUhUMRWEBvPUtgtjN1TUdCXlT04KHj Sdxw== X-Gm-Message-State: AFqh2kqBFCPOtyiCxKz5wLB4VnB5NkrZGHu+xciHnZxtUkL+F+iFpwDd oIZgSg2HBfh3+Eq38Pn43VLI+FCpIBCFDIYRNmc= X-Google-Smtp-Source: AMrXdXvJUrNrW6e76DilbJ0cMekV4wMDv5qe8EMuiRrkl2O8IVFSGDGjUhxj4KAIopmaxE8ED4RdedS6qxFlDVijNoE= X-Received: by 2002:a05:6870:9728:b0:148:5512:23c0 with SMTP id n40-20020a056870972800b00148551223c0mr1091926oaq.265.1674134369057; Thu, 19 Jan 2023 05:19:29 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Fri, 20 Jan 2023 02:19:11 +1300 Message-ID: Subject: Re: [PATCH] Teach planner to further optimize sort in distinct To: Ankit Kumar Pandey Cc: pghackers 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, 18 Jan 2023 at 08:27, Ankit Kumar Pandey wrote: > There is bit confusion in wording here: > > "returns a List of pathkeys > which are in keys1 but not in keys2 and NIL if keys2 has a pathkey > that does not exist as a pathkey in keys1." > > You mean extract common keys without ordering right? I think you should write a function like: bool pathkeys_count_contained_in_unordered(List *keys1, List *keys2, List **reorderedkeys, int *n_common) which works very similarly to pathkeys_count_contained_in, but populates *reorderedkeys so it contains all of the keys in keys1, but put the matching ones in the same order as they are in keys2. If you find a keys2 that does not exist in keys1 then just add the additional unmatched keys1 keys to *reorderedkeys. Set *n_common to the number of common keys excluding any that come after a key2 key that does not exist as a key1 key. You can just switch to using that function in create_final_distinct_paths(). You'll need to consider if the query is a DISTINCT ON query and not try the unordered version of the function in that case. I also just noticed that in build_index_paths() we'll leave the index path's pathkeys empty if we deem the pathkeys as useless. I'm not sure what the repercussions of setting those to the return value of build_index_pathkeys() if useful_pathkeys is otherwise empty. It's possible that truncate_useless_pathkeys() needs to be modified to check if the pathkeys might be useful for DISTINCT, but now that I see we don't populate the IndexPath's pathkeys when we deem them not useful makes me wonder if this entire patch is a good idea. When I thought about it I assumed that we always set IndexPath's pathkeys to whatever (if any) sort order that the index provides. David