Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n5PEq-0000fF-Uq for pgsql-performance@arkaria.postgresql.org; Thu, 06 Jan 2022 09:39:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n5PEp-0003tn-TI for pgsql-performance@arkaria.postgresql.org; Thu, 06 Jan 2022 09:39:15 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n5PEp-0003te-JN for pgsql-performance@lists.postgresql.org; Thu, 06 Jan 2022 09:39:15 +0000 Received: from mail-pj1-x102b.google.com ([2607:f8b0:4864:20::102b]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1n5PEm-0002CB-UP for pgsql-performance@postgresql.org; Thu, 06 Jan 2022 09:39:15 +0000 Received: by mail-pj1-x102b.google.com with SMTP id l16-20020a17090a409000b001b2e9628c9cso2563950pjg.4 for ; Thu, 06 Jan 2022 01:39:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=HSSozjiO/qRTGChlVjDFsxnYccau/jGFTe2wMGmfYGk=; b=nqPk/QY9JdCNq/9Mb3zHkL0YKHHmnJwnV5Grlz2irgtn8gpjdxw2tVLCdZkX3pYNWa Wfm1R9v3UBazMeGzB7UXrVEALX/0O7Zo4hJeI2cpfVvrF9FHLxt0IoyRrK0gmD0A/BfQ /FyHX0/sjpDsZCENFAe/fR6fAFOH/fbRU9JCVHsYznG9Y97M+gxuKQaDztR0HP1gNBRh 6DCEljDCoCb63MBMJ2nxrYy8+5u5AR1tVRNwexLyg1WEi8MLbArVUvsE7aXZ0vwZQjcm Dm04Hzpum+ggIl73w7gyLJPWZGFLzkuT01B/glJRW1dvschaHm6A/ZmNJl44YMxvAjTF RFww== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=HSSozjiO/qRTGChlVjDFsxnYccau/jGFTe2wMGmfYGk=; b=1MqbfnYrax394sVaX0FDcRRfDiE226FcMDujgMVRTqQxGca5+bzi0yz2LdLElUGRrl rAyvK/7a9jwkGDi9DZDcrTXDzXugTs6SN6+e4weWZIupJx8L7R278MK5BqtxbGOV2VzO alfwGafbbjS5k1japcTKSi/i8Ez8B6CGWPElMCOGAzJ3aiajffCuHVFpzYxnjEtfR81D gOMxJUTkwPNiU5PNaemVZO49zqMNgH52xDRwm5DaAG68hltun8YDPFpHHKWmrjpK+aNu by4xbv7fy7R7y+RaLKJhRq7zIRxm/JOmZzuQeB9hDReWGkTFufiOOVZaK+gBLFqzkllC TeqA== X-Gm-Message-State: AOAM530UWmTOZEAfx24vXp/qh6swygDA4MGVeWCFU7DKPEtqrPPl909y l6fKNyhFXPLkakTw6JqV1HY= X-Google-Smtp-Source: ABdhPJyH9tXfZ8906zog0TuwwMt9X4EsaJNTjhVzQ0Ho/hHoKYJWALLc+VMBFh6cAxwa6o/mmQH8pQ== X-Received: by 2002:a17:902:e751:b0:148:fb86:410a with SMTP id p17-20020a170902e75100b00148fb86410amr57798611plf.96.1641461951026; Thu, 06 Jan 2022 01:39:11 -0800 (PST) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id b4sm4844062pjh.44.2022.01.06.01.39.09 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 06 Jan 2022 01:39:10 -0800 (PST) Date: Thu, 06 Jan 2022 18:39:08 +0900 (JST) Message-Id: <20220106.183908.758244498024205160.horikyota.ntt@gmail.com> To: vijaykumarjain.github@gmail.com Cc: AviW@gilat.com, pgsql-performance@postgresql.org Subject: Re: Same query 10000x More Time From: Kyotaro Horiguchi In-Reply-To: References: User-Agent: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Text/Plain; charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk At Thu, 6 Jan 2022 13:50:55 +0530, Vijaykumar Jain wrote in = > On Thu, 6 Jan 2022 at 13:13, Avi Weinberg wrote: > = > > Hi > > > > > > > > I have postgres_fdw table called tbl_link. The source table is 2.5= GB in > > size with 122 lines (some lines has 70MB bytea column, but not the = ones I > > select in the example) > > > > I noticed that when I put the specific ids in the list "where id in= > > (140,144,148)" it works fast (few ms), but when I put the same list= as > > select "where id in (select 140 as id union select 144 union selec= t 148)" > > it takes 50 seconds. This select union is just for the example, I > > obviously have a different select (which by itself takes few ms but= cause > > the whole insert query to take 10000x more time) > > > > > > > > Why is that? How can I still use regular select and still get reas= onable > > response time? > > > > > > > > Thanks > > > > > > > = > couple of things: > PostgreSQL: Documentation: 14: F.35. postgres_fdw > > when you s= et > your foreign server what are your > use_remote_estimate > fetch_size > params for the foreign server. > = > you need to know there are certain restrictions on what gets pushed d= own to > the remote server > i generally use postgres/postgres_fdw.sql at master =B7 postgres/post= gres > (github.com) > > as > a reference > if you predicates are not pushed down, it will bring all the rows fro= m the > foreign server to your local server (and fetch_size value and network= io > will add to delay) > and given you used select * , it will be a lot of io, so maybe restri= ct > only to columns needed after being filtered would help. > = > = > you can try by running > explain (verbose,analyze) query and then also enabling log_statement= =3D > 'all' / log_min_duration_statement =3D 0 > on the foreign server to see the actual plan for the foreign scan. > = > That might help in trouble shooting. > = > = > as always, i have little production exposure. If i am wrong, i can be= > corrected. In this specific case, the FAST query doesn't contain a join and its predicate can be pushed down to remote. On the other hand the SLOW one contains a join. The planner considers remote join only when the both hands of a join are on the same foreign server. Tthis is not the case since the inner subquery is not even a foreign scan. The planner doesn't consider the possibility that a subquery is executable anywhere. As the result, the local inevitably draw all rows from remote table to join with the result of the subquery on-local, which should be quite slow. It could be improved, but I don't think we are going to consider that case because the SLOW query seems like a kind of bad query, which can be improved by rewriting to the FAST one. regards. -- = Kyotaro Horiguchi NTT Open Source Software Center