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 1vEdnX-00EoGY-RX for pgsql-performance@arkaria.postgresql.org; Fri, 31 Oct 2025 01:19:23 +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 1vEdnW-00BRMF-2H for pgsql-performance@arkaria.postgresql.org; Fri, 31 Oct 2025 01:19:21 +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 1vEdnV-00BRM7-O7 for pgsql-performance@lists.postgresql.org; Fri, 31 Oct 2025 01:19:20 +0000 Received: from mail-lj1-x231.google.com ([2a00:1450:4864:20::231]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1vEdnQ-005ABh-2u for pgsql-performance@postgresql.org; Fri, 31 Oct 2025 01:19:20 +0000 Received: by mail-lj1-x231.google.com with SMTP id 38308e7fff4ca-378f010bf18so27492211fa.1 for ; Thu, 30 Oct 2025 18:19:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1761873555; x=1762478355; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=yYMnE0QCIo9qvyG+tjoyXjLdVoe8tmmmJ6IXF5v6lGo=; b=OzyUtv5zE1nCDDmbLBobZ84V2ZjW+YaSdImiUN+8dmU1wAYEF+eJGVEqag3bHj0YPV O0uOzsZJXaj4Y2VojohHtvw7n10SGFqjGFLbxn2vwrj7lzdD257/jvb/TLhGijZ4EKr6 AzyiAYjcexErZCJKCaRqZqC8/ZnaiIndNbiDBr+ImP01kIM+Lw25xOGPuohoYMDuLOIY j8lxwogoxf8g+kGH2SU4DgzTmtYdhO+SsjVEnvHzcDkSEJVJ0DFcO9MGq6IrftlXDnHA pg9/9eMOePksg+LVcoZk/uTk5NohXLQd/MUlUYEaK/QuJUWruubgyOb7Cehkvne9Mmwd SiFw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1761873555; x=1762478355; h=content-transfer-encoding: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=yYMnE0QCIo9qvyG+tjoyXjLdVoe8tmmmJ6IXF5v6lGo=; b=g4WWCmJdvAhaolVjHNLjc8Gios1og7wV5V3L+gGQIIMGnPvxm1cDvbaG5KtkeWsewu wgTsmTsiIoNvTlMiumsVEkkrtr39HXYTaoPQT/aWbBpxm3ETx5QQ5mzhMruqRWazGdTL +BHd+jDNaxAlZWzsXgxTAB7cae7LKkMlGZfT+huMVPif3e9snXaUl9Yp0gREjZLLOHYT 7EVKxpaqhufSDHuF9TSUqkxtWEqIG6FzSk828zQmd/Df40sEJ1YH06dLZ/hrrUi1ysP0 ebcbcJNE005ICBmv45QACiWGBtysXwncoU+umZPRjPW73KX8r80h4ZCQ9NtyhFlCKG0v r6lg== X-Forwarded-Encrypted: i=1; AJvYcCVwzJH0nc5qZoecdnUSWbUOFasAGXvNUrLoDJeIY2o61ckCxRot9wY0CNFY4uy0C5sTf5KElXS3TcfyHeX+eOmerA==@postgresql.org X-Gm-Message-State: AOJu0Yz9TAlxGZZp8kWM0fMmJ0lgBOgnP2OpaZ+ZNeOVJD4t0yupezH0 JwAClk6hLxOKmULqUiVPuOwPrllCUTGW46Pen+6cvhmHAL5XUpLzyqZbBEPHaJu6icRA3J25L/N St932NmYcOUnHYaMvvPswHuyxBNooe+I= X-Gm-Gg: ASbGncvGDwuHrcQcvWBqmKxfQg/Tzr8/VO90x1T4vjRyMHkVl55oTSxBUIVf9CVI7hZ x1f01G08xxYkPP4arKVv/LKABSgS20KqcypHzmBviF4CxXg+nAlHWvTorFBP3VXcs7r58OTn9Ny BYjVmhmAMWkuvltpIkK9tZbTYfNXGzAQ0Mhkk6D40BZTiZSnUODva6voULC33PqegR/pxmeHukA VTAX+spWhidfHMk1OaG6aBngW87+TNs3Bx9x+xITUwgFOXSyTGKbz2Nrn5CBpdjqxeyJwWMIvG5 xc6jdczLHNifrE8mZ1uzoPFh4LrOWYfs28eWcfwd/mDPN4PM0OhaWbfK0F6LVg== X-Google-Smtp-Source: AGHT+IE1q/TprzW1GPCuITv4LvtlVkmlC6MvOW+ae77tlfomVeh9nBxFIQOw4c3KU1pYisgOz+B07qSp2leAwArxR8A= X-Received: by 2002:a05:6512:3ca2:b0:58b:75:8fc6 with SMTP id 2adb3069b0e04-59416d8ce02mr1631001e87.19.1761873554957; Thu, 30 Oct 2025 18:19:14 -0700 (PDT) MIME-Version: 1.0 References: <3148f6f9-2429-4ee0-9b1e-860aa1578d4f@vondra.me> In-Reply-To: From: David Rowley Date: Fri, 31 Oct 2025 14:19:03 +1300 X-Gm-Features: AWmQ_blxamXIQyFj25O3Ye4o4QDllCSHuj2COKRJrEnBS0qpLqtaAHdKycH_lls Message-ID: Subject: Re: GEQO plans much slower than standard join plans To: Carlo Sganzerla Cc: Tomas Vondra , pgsql-performance@postgresql.org, Rafael Almeida , Leandro Noman Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 30 Oct 2025 at 03:57, Carlo Sganzerla wrote: > Yes, I also found that counterintuitive. By turning geqo =3D off (join/fr= om_collapse_limit were 14) and looking at some metrics we obtained from pg_= stat_statements, we found that planning times of the affected queries (the = ones which would make use of GEQO) mostly increased (but not by much), whic= h is already the documented behavior. However, one particular query with th= e same structure like the one on treejoin-14-dims.sql (attached to the firs= t email) had a _decrease_ on planning and execution times, so geqo =3D off = for this query yielded faster plans than geqo =3D on. This counterintuitive= behavior is the reason that I created and shared the script that tried to = reproduce it. I'm not sure whether you've run it or not, but on every machi= ne I have run so far I had a 10 fold increase on TPS and a 10 fold decrease= on planning times if I turn geqo =3D off. > > I guess what I'm trying to show is that, in very particular cases, GEQO m= ay harm planning times, that's why I felt a little disclaimer could be usef= ul. I'm not sure what else I can add here. I think what's happening here is that in the query as you've written it, there isn't much flexibility in the how the tables are joined. There are 14 EquivalanceClasses in the 14 table case. "f" cannot be joined to "c13" without "c12", and joining "c12" requires "c11" and so on. For queries that join all on the same column, there'd be fewer EquivalanceClasses and more flexibility in the join order. Doing: \set id random(1,10000) set join_collapse_limit =3D 14; set geqo =3D off; explain (summary on) select * from f join c0 on (c0.id =3D f.id) join c1 on (c1.id =3D c0.id) join c2 on (c1.id =3D c0.id) join c3 on (c3.id =3D c2.id) join c4 on (c4.id =3D c3.id) join c5 on (c5.id =3D c4.id) join c6 on (c6.id =3D c5.id) join c7 on (c7.id =3D c6.id) join c8 on (c8.id =3D c7.id) join c9 on (c9.id =3D c8.id) join c10 on (c10.id =3D c9.id) join c11 on (c11.id =3D c10.id) join c12 on (c12.id =3D c11.id) join c13 on (c13.id =3D c12.id) where f.id =3D :id; Is much slower to plan as it opens up the possibilities for join order as there's a single EquivalanceClass with 14 members and any combination is possible. I've not studied the GEQO code nearly as much as the standard join search code, but what it seems to do is try random combinations. For the case you demonstrated in [1], it discovers a large number of pairs that cannot be joined as there's no join condition between them. I suspect that's the issue here. The standard search manages to just incrementally build up towards the final join incrementally as it's processing the tables in the same order as they appear in the query and it never hits the case of not finding a join condition between the pairs it tries. Maybe there's some way to make GEQO more efficient by precalculating which pairs might be good ones to try. That likely could be done fairly efficiently now that we have RelOptInfo.eclass_indexes, or at least it would help identify ones that there's no point in trying. Maybe someone wants to study this more than I have and see what might be possible. I've not quite got the motivation, at least not today. David [1] https://www.postgresql.org/message-id/attachment/183634/treejoin-14-dim= s.sql