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.96) (envelope-from ) id 1w7YSn-005REV-2X for pgsql-bugs@arkaria.postgresql.org; Tue, 31 Mar 2026 12:44:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w7YSm-00ADKW-0a for pgsql-bugs@arkaria.postgresql.org; Tue, 31 Mar 2026 12:44:56 +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.96) (envelope-from ) id 1w7YSl-00ADKI-32 for pgsql-bugs@lists.postgresql.org; Tue, 31 Mar 2026 12:44:56 +0000 Received: from mail-wr1-x42f.google.com ([2a00:1450:4864:20::42f]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w7YSk-00000001y8T-2gBd for pgsql-bugs@lists.postgresql.org; Tue, 31 Mar 2026 12:44:55 +0000 Received: by mail-wr1-x42f.google.com with SMTP id ffacd0b85a97d-439d8dc4ae4so5562737f8f.2 for ; Tue, 31 Mar 2026 05:44:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774961093; cv=none; d=google.com; s=arc-20240605; b=RwOuvN2wVq9LFN5TGnwzZs5A+H0CKHPmqCdqdWgYnQphxBo7mW1CXyfd1Bg++pRnd8 pzG92+FoDQxRGXI2z22RepKG4H6Nko5uqI3mVmIqZ/gc1A3U3wu+WxK/8dXgK5u7xMJC yxT3DHFkPj56E2tFZnqPZwaAhZo2m57nJiIdqtENTp+S5sN+s9aAdTr1qFVNL2vpWdoK vh0WJAn5oLfAbw1ejNTtiR1vdt2WbeinP04hm/PQHwE7DGZmXA2psv6Bai/QO6mNwMRY /qAk8/cq04lgEWzDFtVnNeXh4alzezgKJ3nOudH7rLIVC5RvuWbnEQDyDmG6qUVAAtu/ AN7Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=YqRJp1+M9GGr3zpwt7zidTVc+Alzc2eo4/bB/JZ4DE0=; fh=GCVeVXfCE+1nANcFaZ2WhfUO+/pSqtvdM28TlWYPq1w=; b=ZW4IVgwwlDRuCfeKMKc6OWDwFsdR7BETRQVP3I+0rxWbhHiuYjrKbcdw3YRHZxtA+S KTYDWYN5WWYcA3Vr9jtBh+5nbMWZmTdo/RniBALlEnGMhC39bX1ltXoB3BYgT4q4fDjv LDkWQr8TNLhliuMtT8JOpUigJ2Zk0vB2Uv0y0++nGoHsnQLx2SeTR+xer6MGjZBFUDvz UTua9WDOBaQRloN+dOonKpnVX51cgE+8FuVJOWnkQf1xA2OrMUZLps+t0NHhWTZ0NFqg pLzcNsSZYFoElkX5XDxrjD7TgBTQmuQz7V7kGmOTb0g6MTeHAMOC8kcuGZQeL09jzsXY 3/pg==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774961093; x=1775565893; 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=YqRJp1+M9GGr3zpwt7zidTVc+Alzc2eo4/bB/JZ4DE0=; b=R7qrYfkPSutE2rQ/MTOrcbphPL1JWMwebOaMpgmJq7Aue4RFQUMCTEStYdt1zDLe3a 3QfTPp1GwmVvGoPZJSmUlmlfasDkEFfThRxwWYuWYGR4dpWxM+x9L3Kr7n3/5dT5a0fD mQ6KpmARNxmclSqTjQZOuZqwlxatTsFWCoWppoXyY1qbJIa6h5yVvfw/XI4E6QL7RV63 XHWXHOYSEBBGP+JReGYb635n2/JNNZxA3mqA2+6k01xGn7pg3WHdSw+hwM7q64DAg9QK KvmDjhJjDRcSUAc7I52CbUnE+jFvKmxU5RJx/c+UeEV08aTnAHLNVjGnePGS7Z1kIRvS faYw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774961093; x=1775565893; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=YqRJp1+M9GGr3zpwt7zidTVc+Alzc2eo4/bB/JZ4DE0=; b=A1mALbaIbMIxwxTMr54wYSSsHp7UB3X22BLIz7ijKXFIZlPwSawYV93EjlltuGxP8B uzPyrNxmp7J1U+16aiMk9UO3Mplxiib8xWDzVJbzSL95YqrGcC/5HQRK4vDitt41/xpb 2Hh0rvkMnK8ZyV1PlOYPnEtrU5CBYF6IOiVb/iH5yi7had0xZFQdjw3kHMQacfCoi9PG p1YADZRwKiNGzF1iw2woIHKzVoGvM/71JlV/bSy0jmKvEH1dMwkKdPjoh2acen+xI9KD ReahqeBn+xjZUVpVYmvUmsOx8CGmOq9oZhpEL5xRlS2fqfO3C54JW/XDYaCcImkmbpEH 3wOA== X-Gm-Message-State: AOJu0YyfZhrw3QV1Zzofcl5i3OSvi9Qt8CbEGjQWFL/+oIR7YK6Y9twh r/tmb3rJI+z6rhsIwYkTUODtMIeohG2luBBEWxuYqTlNcgPln+F+uvNRXBaKx9InJv2aq9LUV1B WNyFg41CU840s+5Jar4BZozN8OXjufgI= X-Gm-Gg: ATEYQzz7GJRAFywNTlzFEx0DgYg6Gh60n6tbcxdndpzoakWrCmB88rNJ/tE3NNLxFMG FZylTk8GCpm2Psi51cCHl6sjaRmTsKFuHvXR1crD7Z0EhmXoFp6uoH3CS2OWq/2LOYEpS5jaXMS UOV2sbJZ/O0HO6tP+HAJ2aeS0cU6Ng2VbrklU4mrh2kogxlbaIB7jtMVHmRXeHa/F/j3brdzk6F oO77KoTVdmOcnpGASh7OfTL7ijNCV/Nisl/7U6DiEJoiS7sBMyAxdcDJqFj3TK7OJY0Wlx+lhCD mJJLpPcE0Dwud0tfzaviFAvwPyg5b7ftUKqI+UlkLRnF7ysQ46vC6idwEhM1gG07Hr5XZa+iXw= = X-Received: by 2002:a05:6000:310c:b0:43b:3b80:6776 with SMTP id ffacd0b85a97d-43b9e9ee8f2mr29895371f8f.30.1774961093022; Tue, 31 Mar 2026 05:44:53 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: David Rowley Date: Wed, 1 Apr 2026 01:44:41 +1300 X-Gm-Features: AQROBzCACTlsJlhPUKgyuM-xt730wivqQXCwzFE_E6X6Y5lCSn0Gj-fNpSYjDEA Message-ID: Subject: Re: Surprising behavior with pushing predicates down into a view To: Justin Christensen Cc: pgsql-bugs@lists.postgresql.org 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 Tue, 31 Mar 2026 at 22:45, Justin Christensen wrote: > When I query this view using a simple constant ticker like 'AAPL' the que= ry plan shows that it correctly filters the set of tickers before applying = the return calculations, joining, and then calculating beta. I've included = the EXPLAIN output after the query in this gist. > > https://gist.github.com/JustinChristensen/1cad256ea61ad30aa39d42aebc4557b= 9#file-literal-filter-sql > > When I query this view using a subquery to find the tickers to filter on = it instead tries to execute the view and calculate the beta for all of the = tickers in the table before filtering: In short, this isn't a bug. When planning subqueries, which is effectively how a VIEW will be planned unless it passed is_simple_subquery()'s tests, we only consider pushing down "base" quals into that subquery. When you do ticker =3D 'AAPL', that's a base qual, and that can be pushed down because ticker is in the WINDOW's PARTITION BY clause, but ticker IN (SELECT ticker FROM metrics ORDER BY random() LIMIT 50) is converted into a SEMI join much earlier in planning, so that isn't a base qual anymore. Even if we didn't do that tranformation in convert_ANY_sublink_to_join(), we'd still fail to push down the base qual into the subquery as qual_is_pushdown_safe() doesn't accept base quals with subplans. You might be better off changing the view to a table returning function which accepts a ticker parameter and calling that function once for each ticker you need. David