Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1eobZP-0000DU-FV for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Feb 2018 21:04:56 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eobZO-0004LJ-Bh for pgsql-hackers@arkaria.postgresql.org; Wed, 21 Feb 2018 21:04:54 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eobZN-0004L8-V1 for pgsql-hackers@lists.postgresql.org; Wed, 21 Feb 2018 21:04:54 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1eobZJ-000465-6F for pgsql-hackers@postgresql.org; Wed, 21 Feb 2018 21:04:52 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id 2FE3A21C1F47 for ; Thu, 22 Feb 2018 00:04:46 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=x tagged_above=-99 required=4 WHITELISTED tests=[] autolearn=unavailable Received: from [192.168.1.63] (unknown [46.39.228.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id 0B7C821C1CB3 for ; Thu, 22 Feb 2018 00:04:46 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1519247086; bh=NjdhFTAeNHNZUX0qAV5fDRN5qP0pXPD7fHPFM4YV3OE=; h=To:From:Subject:Date; b=F+n6sRaJ/pw1BmlzezZRdH+tMSZp4EGOlsPhy2V4zZLaPYc2WZQzNv73uCcz+od5F 6S/doUglWx1yyZuZvQO1sPTv5nXJP74sHg2DGLFM9u8TkcQvos0CmqwKD7J/d9GUmQ 8OI2SzsGPdt5xOBGL+tfQXmch/zy0dxCrkZwtV0s= To: PostgreSQL-development From: Alexander Kuzmenkov Subject: ERROR: left and right pathkeys do not match in mergejoin Message-ID: <5dad9160-4632-0e47-e120-8e2082000c01@postgrespro.ru> Date: Thu, 22 Feb 2018 00:04:45 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hi hackers, I found a bug related to the planning of merge joins. The steps to reproduce are as follows: ``` CREATE TABLE J1_TBL (     i integer,     j integer,     t text ); CREATE TABLE J2_TBL (     i integer,     k integer ); set enable_hashjoin to off; explain select * from j1_tbl full join (select * from j2_tbl order by j2_tbl.i desc, j2_tbl.k) j2_tbl on j1_tbl.i = j2_tbl.i and j1_tbl.i = j2_tbl.k; ERROR:  left and right pathkeys do not match in mergejoin ``` It can be reproduced on the latest 9.6, 10 and devel versions. Basically, j2_tbl is used as the outer relation in merge join. We try to use the existing order of j2_tbl, that is, 'i2 desc' and 'k asc'. As a result, there is no order for i1 that would allow it to be merge-joined to both i2 and k. The corresponding code path begins in `generate_mergejoin_pathkeys()`, when it is first called by `match_unsorted_outer()`. `find_mergeclauses_for_pathkeys()` selects both mergeclauses for the given outer pathkeys, and then `make_inner_pathkeys_for_merge()` generates inner pathkey for the first mergeclause. Inner pathkey for the second mergeclause is skipped as redundant. It looks suspicious already, because this new pathkey has different direction and is more like conflicting than redundant. Finally, `create_mergejoin_plan()` detects that the inner pathkey doesn't match the second mergeclause and throws the error. I found this while working on the inequality merge join patch. I don't know yet how to fix this, so any help is welcome. -- Alexander Kuzmenkov Postgres Professional: http://www.postgrespro.com The Russian Postgres Company