Received: from malur.postgresql.org ([2a02:16a8:dc51::56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ftUH3-0006VV-J7 for pgsql-bugs@arkaria.postgresql.org; Sat, 25 Aug 2018 08:50:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ftUGz-0006AR-74 for pgsql-bugs@arkaria.postgresql.org; Sat, 25 Aug 2018 08:50:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ftUGz-0006A0-1D for pgsql-bugs@lists.postgresql.org; Sat, 25 Aug 2018 08:50:21 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1ftUGv-0007ou-HM for pgsql-bugs@lists.postgresql.org; Sat, 25 Aug 2018 08:50:20 +0000 Received: from [192.168.127.1] (port=17647 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1ftUGs-0003lQ-EN; Sat, 25 Aug 2018 08:50:14 +0000 Received: from [127.0.0.1] (port=40894 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1ftUGr-000ADO-Lj; Sat, 25 Aug 2018 08:50:13 +0000 From: Andrew Gierth To: pgsql-bugs@lists.postgresql.org, maxim.boguk@gmail.com Subject: Re: BUG #15352: postgresql FDW error "ERROR: ORDER BY position 0 is not in select list" In-Reply-To: <153518420278.1478.14875560810251994661@wrigleys.postgresql.org> (PG Bug reporting form's message of "Sat, 25 Aug 2018 08:03:22 +0000") Message-ID: <87efemzw0v.fsf@news-spur.riddles.org.uk> References: <153518420278.1478.14875560810251994661@wrigleys.postgresql.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sat, 25 Aug 2018 09:50:13 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "PG" == PG Bug reporting form writes: PG> ERROR: ORDER BY position 0 is not in select list PG> CONTEXT: Remote SQL command: EXPLAIN SELECT relid, schemaname, relname, PG> seq_scan, seq_tup_read, idx_scan, idx_tup_fetch, n_tup_ins, n_tup_upd, PG> n_tup_del, n_tup_hot_upd, n_live_tup, n_dead_tup, n_mod_since_analyze, PG> last_vacuum, last_autovacuum, last_analyze, last_autoanalyze, vacuum_count, PG> autovacuum_count, analyze_count, autoanalyze_count FROM PG> pg_catalog.pg_stat_user_tables ORDER BY 0 ASC NULLS LAST So what's happening here is that there's an equivalence class containing members "greatest(pg_catalog.pg_stat_user_tables.idx_scan, public.pg_stat_user_tables.idx_scan)" and "0" (as an integer constant), and somehow a pathkey for this eclass is becoming attached to the query going to the remote for statistics purposes. It seems obviously wrong that a constant pathkey with no actual reference to the foreign table should be being pushed down, so so far I suspect that get_useful_pathkeys_for_relation isn't being selective enough about what is "useful". In this context I find it suspicious that find_em_expr_for_rel will return an expr with no vars as being "for" every rel, since it's just looking for a subset. -- Andrew (irc:RhodiumToad)