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 1sEUvi-001lGj-T7 for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Jun 2024 14:14:28 +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 1sEUvi-00HB9L-Mb for pgsql-hackers@arkaria.postgresql.org; Tue, 04 Jun 2024 14:14:26 +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 1sEUvi-00HB9D-7Y for pgsql-hackers@lists.postgresql.org; Tue, 04 Jun 2024 14:14:26 +0000 Received: from mail-ej1-x62f.google.com ([2a00:1450:4864:20::62f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sEUve-002JEx-27 for pgsql-hackers@lists.postgresql.org; Tue, 04 Jun 2024 14:14:25 +0000 Received: by mail-ej1-x62f.google.com with SMTP id a640c23a62f3a-a66e9eac48fso128703766b.2 for ; Tue, 04 Jun 2024 07:14:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1717510461; x=1718115261; darn=lists.postgresql.org; h=message-id:date:content-transfer-encoding:content-id:mime-version :comments:references:in-reply-to:subject:cc:to:from:from:to:cc :subject:date:message-id:reply-to; bh=cnf4JfC6XCatNYgH2smpeuoUqEuWfQkX2hXYTITWIaY=; b=p2yFXWIK87ZbOzpHKhBql9nVt4EqVWQMUCLWgkBDve/bOA9lrsQwTz6fV0xe74gijn 3MA2KiBbR4MU08TDz4sRKlBRbegJ/7q/ibkZ7SO4tUOcD3GgsNM660EqkTz/1m6WJ5rb CizUSPB6cFfIYUtu7zeAR1Zbc63Y3D2fwfLGmhfxoHL0a6HSd2T97GNqHVpxpUETDrHY ihHK1hPu6c56H6jRpZaAtrf1vxOubnK16GRDSrpzBgvOmz+oi5s8NnDCCUc3tIRFhG7z dZmYQnanyJWiYLiS3xEFAt8nLeFfZDG9OL07EhikD9gLIDBU/hrVL6sGBrvBBL8Cfrnv 0PvQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1717510461; x=1718115261; h=message-id:date:content-transfer-encoding:content-id:mime-version :comments:references:in-reply-to:subject:cc:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cnf4JfC6XCatNYgH2smpeuoUqEuWfQkX2hXYTITWIaY=; b=lZJscsom4Od6PljIW4OFGBKsEHfD+nq8/JB/xad0Y6RNLIwmpy34arZymnBEavhekb s/Oq46UvG1Sv3aeNWQY9htURnMkPwCgPDaJbAbR4xZnqTVn5wi+4jn+VKfa9UCx2t2ws InGSUkOse8+MNsaHPKcuAeknwEtz4hHFk5uHwfcxD+Zu6yDHspWzI/8Zyz4pJY/ZSX94 S7//oHmn0+LEJyeKuQDl4epnHBNa7lxrxnKaK9tr/5olkEUd428p7+hhbrLHIiXECk+h mvTpv1gx/STN4JU9jwrndaYNipoUZB2mxTNxkolQU8X4yplK0oISwmqogknFhoTWsIIQ 0yhw== X-Gm-Message-State: AOJu0YwWwKo7ltKdlaWzML2wpQYSWCqKHUEVW25Tlb0FGZg0XbSJaAsV MvdvpDD/zTS7b+BNLsbIaE9satyk+v6QFCRBDmoiohl+x3YRJsLN7i7jA40jid8= X-Google-Smtp-Source: AGHT+IFeL1E7SC9Y0JW7nPVghG23knnT3MYaRi+ch9wArVlU6CUiTx7WhFU16At3D09oIMmBkwM+0Q== X-Received: by 2002:a17:906:2658:b0:a68:eade:17af with SMTP id a640c23a62f3a-a68eade17dfmr483114966b.25.1717510460922; Tue, 04 Jun 2024 07:14:20 -0700 (PDT) Received: from antos (109-81-174-135.rct.o2.cz. [109.81.174.135]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a68a408ef12sm528122466b.183.2024.06.04.07.14.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Jun 2024 07:14:20 -0700 (PDT) From: Antonin Houska To: Andy Fan cc: PostgreSQL Hackers , David Rowley , Tom Lane , Ashutosh Bapat , jian he Subject: Re: UniqueKey v2 In-reply-to: <87h6f1kqr6.fsf@163.com> References: <7mlamswjp81p.fsf@e18c07352.et15sqa> <871qczm9oc.fsf@163.com> <7971.1713526758@antos> <875xvrnzga.fsf@163.com> <3739.1715594150@antos> <87h6f1kqr6.fsf@163.com> Comments: In-reply-to Andy Fan message dated "Tue, 14 May 2024 11:15:58 +0800." X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.2.50 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <21672.1717510459.1@antos> Content-Transfer-Encoding: quoted-printable Date: Tue, 04 Jun 2024 16:14:19 +0200 Message-ID: <21674.1717510459@antos> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Andy Fan wrote: > Antonin Houska writes: > = > >> Could you make the reason clearer for adding 'List *opfamily_lists;' > >> into UniqueKey? You said "This is needed to create ECs in the parent > >> query if the upper relation represents a subquery." but I didn't get = the > >> it. Since we need to maintain the UniqueKey in the many places, I'd l= ike > >> to keep it as simple as possbile. Of course, anything essentical shou= ld > >> be added for sure. = > > > > If unique keys are generated for a subquery output, they also need to = be > > created for the corresponding relation in the upper query ("sub" in th= e > > following example): > = > OK. > > > > select * from tab1 left join (select * from tab2) sub; > > > > However, to create an unique key for "sub", you need an EC for each ex= pression > > of the key. > = > OK. > > And to create an EC, you in turn need the list of operator > > families. > = > I'm thinking if we need to "create" any EC. Can you find out a user case > where the outer EC is missed and the UniqueKey is still interesting? I > don't have an example now. = > = > convert_subquery_pathkeys has a similar sistuation and has the following > codes: > = > outer_ec =3D > get_eclass_for_sort_expr(root, > (Expr *) outer_var, > sub_eclass->ec_opfamilies, > sub_member->em_datatype, > sub_eclass->ec_collation, > 0, > rel->relids, > NULL, > false); > = > /* > * If we don't find a matching EC, sub-pathkey isn't > * interesting to the outer query > */ > if (outer_ec) > best_pathkey =3D > make_canonical_pathkey(root, > outer_ec, > sub_pathkey->pk_opfamily, > sub_pathkey->pk_strategy, > sub_pathkey->pk_nulls_first); > } I think that convert_subquery_pathkeys() just does not try that hard to achieve its goal. The example where it's important to create the EC in the outer query is wh= at I added to the subselect.sql regression test in the 0004- diff in [1]: create table tabx as select * from generate_series(1,100) idx; create table taby as select * from generate_series(1,100) idy; create unique index on taby using btree (idy); create view view_barrier with (security_barrier=3Dtrue) as select * from t= aby; analyze tabx, taby; explain (costs off, verbose on) select * from tabx x left join view_barrie= r y on idy =3D idx; If you modify find_ec_position_matching_expr() to return -1 instead of creating the EC, you will get this plan Hash Left Join Output: x.idx, taby.idy Hash Cond: (x.idx =3D taby.idy) -> Seq Scan on public.tabx x Output: x.idx -> Hash Output: taby.idy -> Seq Scan on public.taby Output: taby.idy instead of this Hash Left Join Output: x.idx, taby.idy Inner Unique: true Hash Cond: (x.idx =3D taby.idy) -> Seq Scan on public.tabx x Output: x.idx -> Hash Output: taby.idy -> Seq Scan on public.taby Output: taby.idy > >> > * uniquekey_useful_for_merging() > >> > > >> > How does uniqueness relate to merge join? In README.uniquekey you= seem to > >> > point out that a single row is always sorted, but I don't think t= his > >> > function is related to that fact. (Instead, I'd expect that pathk= eys are > >> > added to all paths for a single-row relation, but I'm not sure yo= u do that > >> > in the current version of the patch.) > >> = > >> The merging is for "mergejoinable join clauses", see function > >> eclass_useful_for_merging. Usually I think it as operator "t1.a =3D t= 2.a"; > > > > My question is: why is the uniqueness important specifically to merge = join? I > > understand that join evaluation can be more efficient if we know that = one > > input relation is unique (i.e. we only scan that relation until we fin= d the > > first match), but this is not specific to merge join. > = > So the answer is the "merging" in uniquekey_useful_for_merging() has > nothing with merge join. = I don't understand. The function comment does mention merge join: /* * uniquekey_useful_for_merging * Check if the uniquekey is useful for mergejoins above the given relatio= n. * * similar with pathkeys_useful_for_merging. */ static bool uniquekey_useful_for_merging(PlannerInfo *root, UniqueKey * ukey, RelOptIn= fo *rel) [1] https://www.postgresql.org/message-id/7971.1713526758%40antos -- = Antonin Houska Web: https://www.cybertec-postgresql.com