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 1juiyJ-0006VO-I0 for pgsql-general@arkaria.postgresql.org; Sun, 12 Jul 2020 20:53:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1juiyH-0003LM-GO for pgsql-general@arkaria.postgresql.org; Sun, 12 Jul 2020 20:53:13 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1juiyH-0003LE-0C for pgsql-general@lists.postgresql.org; Sun, 12 Jul 2020 20:53:13 +0000 Received: from mxh01aws49.mxout.inova.com.br ([34.198.242.49] helo=mxhero01.a.inova.com.br) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1juiyA-0003ID-NP for pgsql-general@lists.postgresql.org; Sun, 12 Jul 2020 20:53:10 +0000 Received: from MXHERO01.A.INOVA.COM.BR (localhost [127.0.0.1]) by mxhero01.a.inova.com.br (Postfix) with ESMTP id E1B8F4CC1F for ; Sun, 12 Jul 2020 20:53:04 +0000 (UTC) Received: from mxhero01.a.inova.com.br (localhost [127.0.0.1]) by mxhero01.a.inova.com.br (Postfix) with ESMTP; Sun, 12 Jul 2020 20:53:04 +0000 (UTC) Received: from mxhero02.a.inova.com.br (ip-10-0-4-48.ec2.internal [10.0.4.48]) by mxhero01.a.inova.com.br (Postfix) with ESMTPS id C810E4CC1F; Sun, 12 Jul 2020 20:53:04 +0000 (UTC) Received: from corpmoutpolicyd01.a.inova.com.br (puppetmaster.a.inova.com.br [10.0.1.88]) by mxhero02.a.inova.com.br (Postfix) with ESMTP id B1C5618E21A; Sun, 12 Jul 2020 20:53:04 +0000 (UTC) Received: from corpmoutpolicyd01.a.inova.com.br (localhost [127.0.0.1]) by corpmoutpolicyd01.a.inova.com.br (Postfix) with ESMTP id A44842203BF; Sun, 12 Jul 2020 17:53:01 -0300 (BRT) Received: from corpmout01.a.inova.com.br (ip-10-0-1-125.ec2.internal [10.0.1.125]) by corpmoutpolicyd01.a.inova.com.br (Postfix) with ESMTP id 94D80AAF4F; Sun, 12 Jul 2020 17:53:01 -0300 (BRT) Received: from corpmbox02.a.inova.com.br (ip-10-0-4-158.ec2.internal [10.0.4.158]) by corpmout01.a.inova.com.br (Postfix) with ESMTP; Sun, 12 Jul 2020 17:53:01 -0300 (-03) Date: Sun, 12 Jul 2020 17:53:01 -0300 (BRT) From: luis.roberto@siscobra.com.br To: David Rowley Cc: =?utf-8?Q?Fabr=C3=ADzio?= de Royes Mello , pgsql-general Message-ID: <601802839.11789890.1594587181511.JavaMail.zimbra@siscobra.com.br> In-Reply-To: References: <2028783297.11609965.1594488024498.JavaMail.zimbra@siscobra.com.br> <1436239160.11622485.1594493964776.JavaMail.zimbra@siscobra.com.br> Subject: Re: Join optimization MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [10.0.1.247] X-Mailer: Zimbra 8.8.15_GA_3918 (ZimbraWebClient - GC83 (Win)/8.8.15_GA_3895) Thread-Topic: Join optimization Thread-Index: xO4oIEpKuSd2VycOCx7LpXwvgf3P7A== X-CMAE-Score: 0 X-CMAE-Analysis: v=2.2 cv=H8qr+6Qi c=1 sm=1 tr=0 cx=a_idp_d a=aVKa9x/uM93/m3V8yChZ6A==:117 a=FKkrIqjQGGEA:10 a=_uFG4AKbl_AA:10 a=IkcTkHD0fZMA:10 a=_RQrkK6FrEwA:10 a=h2mZ7ym4DuMA:10 a=pGLkceISAAAA:8 a=m19YD-nRAAAA:8 a=VSGBPb8XAAAA:8 a=epTmVMiNAAAA:8 a=W2qJBIc1AAAA:8 a=K5APW_Fw9j9W_LY6UjwA:9 a=tSZQd9kk1rtmxNb8:21 a=m7l8T_OzXTzguZkE:21 a=QEXdDO2ut3YA:10 a=FnJ2BL13HOe6lA8RQuFj:22 a=r-vnRQDpo0Mhf4Iua1vZ:22 a=ndEWmUVY6Yapc0oHF_P4:22 a=f3LCGml2-gjdAfozEohe:22 x-mxHero-Origin-Ip: /127.0.0.1:49364 X-mxHero-AttachmentBlock: rule=48;result=false List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ----- Mensagem original ----- De: "David Rowley" Para: "luis.roberto" Cc: "Fabr=C3=ADzio de Royes Mello" , "pgsql-genera= l" Enviadas: Domingo, 12 de julho de 2020 5:29:08 Assunto: Re: Join optimization On Sun, 12 Jul 2020 at 06:59, wrote: > > I'm sorry for the bad example. > > Here is another, with some data on PG: https://dbfiddle.uk/?rdbms=3Dpost= gres_13&fiddle=3Dccfd1c4fa291e74a6db9db1772e2b5ac and Oracle: https://dbf= iddle.uk/?rdbms=3Doracle_18&fiddle=3D21a98f499065ad4e2c35ff4bd1487e14. I believe what you're talking about is join removals. It appears as though Oracle is able to remove the inner join to the users table as the join only serves to check the user record exists. No columns are being selected. The record must exist due to the foreign key referencing users. PostgreSQL currently can only remove left joins. Likely what you could do here is just change your inner join into a left join. If you're happy enough that the user record will always exist then that should allow it to work. The reason PostgreSQL does not currently support inner join is that by default, foreign key constraints are only triggered at the end of the query, (or if deferred, at the end of the transaction). WIth PostgreSQL, it's possible for a SELECT query to see a violated foreign key constraint. This can happen if your SELECT query calls a function which updates a referenced record. The cascade of the foreign key won't occur until the end of the statement, so the select may stumble upon a violated foreign key. Here's a quick example of this case: drop table t1,t2; create table t1 (a int primary key); create table t2 (a int references t1 on update cascade); insert into t1 values(1),(2); insert into t2 values(2),(2); create or replace function update_t1 (p_a int) returns int as $$ begin update t1 set a =3D a + 1 where a =3D p_a; return p_a; end; $$ language plpgsql volatile; -- in theory, this should never return any rows as we're asking for all rows that -- don't exist in the referenced table. However, we do get a result since the function -- call updates t1 setting the row with a=3D2 to a=3D3. The cascade to t2 does not occur -- until the end of the statement. select update_t1(a+1),t1.a from t1 where not exists(select 1 from t2 where t1.a=3Dt2.a); update_t1 | a -----------+--- 2 | 1 (1 row) If you're happy that you don't have any functions like that which could momentarily cause the foreign key to appear violated, then there shouldn't be any harm in changing the INNER JOIN on users to a LEFT JOIN. PostgreSQL will be able to remove the join in that case. David ----- Thanks for the reply David! I understand it better now.