Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGao3-0004cx-Vl for pgsql-admin@arkaria.postgresql.org; Wed, 17 Apr 2019 03:00:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hGao1-0006tO-3c for pgsql-admin@arkaria.postgresql.org; Wed, 17 Apr 2019 03:00:13 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGao0-0006sc-Nv for pgsql-admin@lists.postgresql.org; Wed, 17 Apr 2019 03:00:12 +0000 Received: from mail-ot1-x344.google.com ([2607:f8b0:4864:20::344]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hGanw-00084H-Mm for pgsql-admin@lists.postgresql.org; Wed, 17 Apr 2019 03:00:10 +0000 Received: by mail-ot1-x344.google.com with SMTP id o74so19449785ota.3 for ; Tue, 16 Apr 2019 20:00:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-transfer-encoding:content-language; bh=jEN/RtlLbo/RjLc5PUkmgMQU5Jwx9/ANIFt+1WVg418=; b=GQ/l+b51n7tur2b5N0Ohc3AQ39rkjw9oWE8E8oaVm2vZFnbJVMg0CUGiK8oO9BwTqR fX1+fv1KofHd/1SJXFU/x8YKpgytOUdfHaWSH3Np0NN+zlcw79RbNneaPSWzMzfbkCP7 2WcIb4WbBYDPkKxbUaJyxn4RThWHY5hn2i0hzcotVh9uUwZ35svv8S/NhXvwbf7qSVsl 5Isvv1ci5JK6I2i69qkFAkPrzicnYBHzxDA0dnfhQbkeGP67p66dD8x0kZmu/mHaRRFJ dFhooXf2wpxdRb2+PLy9jcr8qWuZ9QNJGEyGy/8O9kUXVRyMG7QjOjgvmcj5nDnGG3p+ LHIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-transfer-encoding :content-language; bh=jEN/RtlLbo/RjLc5PUkmgMQU5Jwx9/ANIFt+1WVg418=; b=omcMeX6g0RoDTnqFX9gc0l+VCzg4zFuZgVqD0lq4gBRSZbW6BMpJqYwiu9kDJNZ3HV vcbVxEMzIvpF8ocvVF37jYOY1geS/3E+6sb4svxxdAAii+n1PYrIe+G3yqGzZW9BjMRz UdM0LnF+J6tqb5ntQei9EEmMSS71KdJbvYObyySvHHSHcFL556DZPLclysxpTbVjRRQo ifKy0iYU/o82eiBWA2f5pZ6Jv69yCanvSudK9C6TbibnHiENK08wdRltncYjbzAS1PPO e1PTLRn3MKMR5ebgynOYxJ1qgQflSshzJSAGZh7R6Gr0jUG9maKLLYIK1bAvnQjd+pfE HB/A== X-Gm-Message-State: APjAAAWdsoRf5zE3BeskTMaj7V5eZNF1/FW84mPPPQHPT+F7JSwSd22Y OnM2zcIX1m06JjZ46BEUsLWCz5Cd2TA= X-Google-Smtp-Source: APXvYqzZ63Gcyqb7TQP2jDnmfbtaPr8Y6e5xQLhEoLcYt+YzKx4aW9EykJaCQfWsLMkND2aHDZUrpg== X-Received: by 2002:a05:6830:2144:: with SMTP id r4mr53294816otd.250.1555470005447; Tue, 16 Apr 2019 20:00:05 -0700 (PDT) Received: from [192.168.1.10] (ip70-171-116-89.no.no.cox.net. [70.171.116.89]) by smtp.googlemail.com with ESMTPSA id v201sm18650673oia.39.2019.04.16.20.00.04 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 16 Apr 2019 20:00:04 -0700 (PDT) Subject: Re: Performance killed with FDW when using CAST. To: pgsql-admin@lists.postgresql.org References: <3e9c9857-f95e-a5b4-151a-6634a589a9e7@2ndquadrant.com> From: Ron Message-ID: Date: Tue, 16 Apr 2019 22:00:03 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.6.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 4/16/19 9:40 PM, Jorge Torralba wrote: > Thanks for taking the time to look. > > Both servers are on .... > >    version > ---------------------------------------------------------------------------------------------------------------------------------- >  PostgreSQL 11.2 (Debian 11.2-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled > by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit > > I have reduced the query to select only two columns for simplicity with > the same results. > > The table contain about 50 million rows > > On the server hosting the table ..... Hiding private data. > > alertsdb_recent_events=#  select id, attributes -> 'account_incident_id' > from recent_events where account_id = 1 AND (attributes -> > 'account_incident_id')::integer = 2617116 limit 5; >                   id                  | ?column? > --------------------------------------+---------- >  ***** | 2617116 >  ***** | 2617116 >  ***** | 2617116 >  ***** | 2617116 >  ***** | 2617116 > (5 rows) > > Time: 82.868 ms > > > # explain  select id, attributes -> 'account_incident_id' from > recent_events where account_id = 1 AND (attributes -> > 'account_incident_id')::integer = 2617116 limit 5; >      QUERY PLAN > ----------------------------------------------------------------------------------------------------------------------------------------- >  Limit  (cost=0.56..3.77 rows=5 width=48) >    ->  Index Scan using > recent_event_account_id_attributes_account_incident_id_idx on > recent_events  (cost=0.56..56.31 rows=87 width=48) >          Index Cond: ((account_id = 1) AND (((attributes -> > 'account_incident_id'::text))::integer = 2617116)) > (3 rows) > > Time: 71.907 ms > > # explain  analyze select id, attributes -> 'account_incident_id' from > recent_events where account_id = 1 AND (attributes -> > 'account_incident_id')::integer = 2617116 limit 5; >                           QUERY PLAN > ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- >  Limit  (cost=0.56..3.77 rows=5 width=48) (actual time=0.019..0.025 rows=5 > loops=1) >    ->  Index Scan using > recent_event_account_id_attributes_account_incident_id_idx on > recent_events  (cost=0.56..56.31 rows=87 width=48) (actual > time=0.018..0.023 rows=5 loops=1) >          Index Cond: ((account_id = 1) AND (((attributes -> > 'account_incident_id'::text))::integer = 2617116)) >  Planning Time: 0.124 ms >  Execution Time: 0.038 ms > (5 rows) > > Time: 80.782 ms > > > On the server that communicates with the FDW server ..... > > # explain  select id, attributes -> 'account_incident_id' from > recent_events where account_id = 1 AND (attributes -> > 'account_incident_id')::integer = 2617116 limit 5; >                                      QUERY PLAN > ------------------------------------------------------------------------------------- >  Limit  (cost=100.00..426.27 rows=5 width=48) >    ->  Foreign Scan on recent_events (cost=100.00..6663659.61 rows=102117 > width=48) >          Filter: (((attributes -> 'account_incident_id'::text))::integer = > 2617116) > (3 rows) > > Time: 85.276 ms > > > # explain  analyze select id, attributes -> 'account_incident_id' from > recent_events where account_id = 1 AND (attributes -> > 'account_incident_id')::integer = 2617116 limit 5; >     QUERY PLAN > --------------------------------------------------------------------------------------------------------------------------------------- >  Limit  (cost=100.00..426.27 rows=5 width=48) (actual > time=21242.087..26387.257 rows=5 loops=1) >    ->  Foreign Scan on recent_events (cost=100.00..6663703.90 rows=102117 > width=48) (actual time=21242.086..26387.252 rows=5 loops=1) >          Filter: (((attributes -> 'account_incident_id'::text))::integer = > 2617116) >          Rows Removed by Filter: 724249 >  Planning Time: 1.164 ms >  Execution Time: 26387.851 ms > (6 rows) > > Time: 26528.113 ms (00:26.528) > > The query killer is the ... > > AND (attributes -> 'account_incident_id')::integer = 2617116 > > Run the query this way ... > > select id, attributes -> 'account_incident_id' from recent_events where > account_id = 1  limit 5; > > and the results is only 10ms slower than on the hosting server directly > which is what we are expecting.  It's like the casting of the hstore > column is just not playing nice. Casting the left side of a predicate is not recommended.  What if you cast 2617166 to be the same type as attributes -> 'account_incident_id'? -- Angular momentum makes the world go 'round.