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 1sk1fY-006rRC-7q for pgsql-general@arkaria.postgresql.org; Fri, 30 Aug 2024 13:28:04 +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 1sk1fV-004OhB-98 for pgsql-general@arkaria.postgresql.org; Fri, 30 Aug 2024 13:28:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sk1fU-004Ofz-RB for pgsql-general@lists.postgresql.org; Fri, 30 Aug 2024 13:28:01 +0000 Received: from mail-lj1-x232.google.com ([2a00:1450:4864:20::232]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sk1fS-002BEl-P1 for pgsql-general@lists.postgresql.org; Fri, 30 Aug 2024 13:28:00 +0000 Received: by mail-lj1-x232.google.com with SMTP id 38308e7fff4ca-2f029e9c9cfso21474581fa.2 for ; Fri, 30 Aug 2024 06:27:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725024477; x=1725629277; darn=lists.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=SUrDqUGsb9zRY+vTFsA15gBGNo1WMB/iHuUG2RlqOWI=; b=eVpJKjCTgOpcCugtTtQiQfBsIvGLCpnZPgMBUfO1FwJKj5UgG7321P7nW9MH7DaBMZ kUFIstIB2yJTVx80OFfDonSWRVJlecJWmZqOWYtAoP5yGbk6xfs/E0xEUeiHq/zeZbmi 7LC+nUAqxtAPG43fp4X3nUomA3w4lHzFscb7tdY/oPVOt7TGPNZ6CBLdMSSzIWnOQ+jv 9snyPa1LSY0JIpVzOH102GeyoUCGPz2laMtGGg3v7+7CuaKjci+xTi/TBFB4MgmswyPk qVeR50kysQ3YEU4E0k0HnfWAhXqhygOYVA0vzDvAoyiEiWXpIHMx2RG1AegVIDf1P4/l LhFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725024477; x=1725629277; 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=SUrDqUGsb9zRY+vTFsA15gBGNo1WMB/iHuUG2RlqOWI=; b=JAkCp/jxvHDuvWu8K1iFL4IuGAn1Jo8L2dYbvYdpCpAvppJoHS035QJOLovY77LQY1 hdLO4Jj1AS1h4BFkN5iP75o0Y6wPFKDuwixRUt7PnvW34pZO8VmOAgBkA13pXXCHWRoF RccC+OI2yB/DiOqskyJzKN7VtG+dXSVCPk5FsnjLUOzae8xtV36817PTx4LAM7xBa0Bx r1/X2mc0N8gkOf+38IiS2QjuH6qJeAepn7m41aim8J6i7ijbRiy2pHVYfvkJnJEwKV/c z2aNdfU4xyQ2qYF64VYLc54PsocGaiFPdpMpLkEhMr4AfdoU0o1RHAQiCyeNBCdSXixR oP5Q== X-Gm-Message-State: AOJu0YwsF4OrGX8z3dfr4kNs4Tt6KOmQ7hcNO71iQKPhuWT/8RPJFGVX +15d8Omf4YYmzwOY0cFWwDn+FXqcKe/y74fkHsl6XRQZj8BBUECvuAE0r/g6nprvLxBssYuYwZg HHnlBy42BteN1KJVD/oZEnAQWPno= X-Google-Smtp-Source: AGHT+IEGGxjwCpRhEoYIbglT5fEDfECBiYkkdg2/V9BLdYMxKwLWPJn0X/Nf0As+3gTaJVigCSXnv9h1sXjDSi6Ty+g= X-Received: by 2002:a05:6512:4022:b0:52c:8075:4f3 with SMTP id 2adb3069b0e04-53546b5ed98mr1836711e87.36.1725024476624; Fri, 30 Aug 2024 06:27:56 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Sat, 31 Aug 2024 01:27:44 +1200 Message-ID: Subject: Re: optimizing a join against a windowed function To: James Brown Cc: pgsql-general@lists.postgresql.org, Gabriel Sinkin 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 Fri, 30 Aug 2024 at 23:36, James Brown wrote: > I have two tables: one named taxpayers which has a goodish number of colu= mns an an integer PK id, and one named insights, which has a taxpayer_id fo= reign key to taxpayers, a year, and (again) a lot of other columns. There's= an index on insights (taxpayer_id, year DESC). I'm executing the following= SQL: > If there's only a single value in the IN clause, the EXPLAIN plan looks g= reat: > However, if there are multiple rows in the IN clause, the optimizer decid= es to execute the subselect against the entire giant table, and it is not g= reat: Unfortunately, you've hit a limitation with the EquivalenceClass code. With the "ON latest_insights.taxpayer_id =3D taxpayers.id WHERE taxpayers.id =3D 650974", the planner is able to deduce that latest_insights.taxpayer_id is also equal to 650974 and push that condition down into the common table expression. With the "ON latest_insights.taxpayer_id =3D taxpayers.id WHERE taxpayers.id IN (?, ?)" query, the EquivalenceClass code doesn't handle this, so the optimisation isn't performed. We likely should improve this someday, but for today, think of it as an unimplemented optimisation rather than a bug. > If I add in a second repetitive WHERE clause, it goes back to being happy= , but that feels a bit like a hack: That's likely your best bet on how to make the planner do what you want, provided you're able to given the query is inside a view. David