pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tomas Vondra <tomas.vondra@enterprisedb.com>
To: PostgreSQL Hackers <pgsql-hackers@lists.postgresql.org>
Subject: minor bug in sort_inner_and_outer()
Date: Fri, 14 Jan 2022 01:48:33 +0100
Message-ID: <bf3a6ea1-a7d8-7211-0669-189d5c169374@enterprisedb.com> (raw)

Hi,

While looking at sort_inner_and_outer() I was rather confused what is
stored in all_pathkeys, because the code does this:

  List *all_pathkeys;

  ...

  all_pathkeys = select_outer_pathkeys_for_merge(root,
                                             extra->mergeclause_list,
                                             joinrel);

  foreach(l, all_pathkeys)
  {
      List	   *front_pathkey = (List *) lfirst(l);
      ...

      /* Make a pathkey list with this guy first */
      if (l != list_head(all_pathkeys))
          outerkeys = lcons(front_pathkey,
                            ...);
      else
          ...

which seems to suggest all_pathkeys is a list of lists, because why else
would front_pathkey be a (List *). But that doesn't seem to be the case,
front_pathkey is actually a PathKey, not a List, as demonstrated by gdb:

(gdb) p *front_pathkey
$2 = {type = T_PathKey, length = 0, ...}

Maybe it's some clever list-fu that I can't comprehend, but I guess it's
a bug present since ~2004. It's benign because we only ever pass the
front_pathkey to lcons() which does not really care.


regards

-- 
Tomas Vondra
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company

Attachments:

  [text/x-patch] sort-inner-and-outer-fix.patch (492B, ../bf3a6ea1-a7d8-7211-0669-189d5c169374@enterprisedb.com/2-sort-inner-and-outer-fix.patch)
  download | inline diff:
diff --git a/src/backend/optimizer/path/joinpath.c b/src/backend/optimizer/path/joinpath.c
index f96fc9fd282..81e04b6ac9d 100644
--- a/src/backend/optimizer/path/joinpath.c
+++ b/src/backend/optimizer/path/joinpath.c
@@ -1258,7 +1258,7 @@ sort_inner_and_outer(PlannerInfo *root,
 
 	foreach(l, all_pathkeys)
 	{
-		List	   *front_pathkey = (List *) lfirst(l);
+		PathKey	   *front_pathkey = lfirst_node(PathKey, l);
 		List	   *cur_mergeclauses;
 		List	   *outerkeys;
 		List	   *innerkeys;


Message-ID: <bf3a6ea1-a7d8-7211-0669-189d5c169374@enterprisedb.com>
Permalink:  ../bf3a6ea1-a7d8-7211-0669-189d5c169374@enterprisedb.com/
Also on:    postgresql.org/message-id/bf3a6ea1-a7d8-7211-0669-189d5c169374@enterprisedb.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: pgsql-hackers@postgresql.org
  Cc: tomas.vondra@enterprisedb.com, pgsql-hackers@lists.postgresql.org
  Subject: Re: minor bug in sort_inner_and_outer()
  In-Reply-To: <bf3a6ea1-a7d8-7211-0669-189d5c169374@enterprisedb.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox