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 1l7EDU-0005NT-Uv for pgsql-general@arkaria.postgresql.org; Wed, 03 Feb 2021 09:12:54 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1l7EDS-0000rb-Bv for pgsql-general@arkaria.postgresql.org; Wed, 03 Feb 2021 09:12:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l7EDR-0000r8-Ly for pgsql-general@lists.postgresql.org; Wed, 03 Feb 2021 09:12:50 +0000 Received: from mail-fr1ger01on0054.outbound.protection.outlook.de ([51.4.72.54] helo=GER01-FRA-obe.outbound.protection.outlook.de) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1l7EDN-00080k-GV for pgsql-general@lists.postgresql.org; Wed, 03 Feb 2021 09:12:48 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=V4R40eCtuOEk101iD6xyHTrJZbCbw/xDeq91nqJnEYoQmEeTHbL71+Br2jLUwt2vlG1+h5VOEbu7XFsp4IKurYhBylnhUONXnYx+PAPw6Q7fKM52o2oHyXVvFQZqqW3RgCsMgHPBCYAucqUjS6LPE4P9D4EU/7dzfbCW4rfA4fD3OxXgL+dCuvuICLDB065RnpYbeDFCfzdJL1ro9vuMWfXdfxudJzl5g55mDJystFnhg9GFnZze1PfIvZ4dCDV1HIwluT1nYPvHNmahyG6q9mcs9ooIpWezLmDaUA4QkbHR0wHU9aANT7RJYp2ED3FqisttOEw2MNj2pozum070Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=foSMOiiKIHj4ceVRJD4GVx0DZ6a1Klany0V5Pw6A4a0=; b=MOynAYxHN5yIYY3cUZtu+Mn/KSWjkmQtrQRrjNTqM2igmg8NBSPbxyiRaT2BZVreZ9hWVptbmPOmFW1sCzMt7yjoZQqond/w1/qbQ0VjZS4EqBseXWSZnyHH3uTBvCE5GleKHmlMRRyit15Ajng2dXGGDV2L4wgwOs8vi42BjGR3W6af9oRR+65X1cl72mxRbkb4bY1WbTCF+3Gc1cAuGiN6Gg8xJS9qxE32zuhntULdd9ezsgxkAV7aK4/tEwepwTfCzXhZ5HZKr0VNPKPdyld+gp6gth5necFiO2T229yixvt5QUu8i0SlqhuE5p/y6TYCU7COGYuMBlt3xPNmRg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=swarm64.com; dmarc=pass action=none header.from=swarm64.com; dkim=pass header.d=swarm64.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=swarm64hive.onmicrosoft.de; s=selector1-swarm64hive-onmicrosoft-de; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=foSMOiiKIHj4ceVRJD4GVx0DZ6a1Klany0V5Pw6A4a0=; b=FNBk4nxALTXtxLKpEH37XqRiRCYqarig2gvmPCQ/3ADE6Sj1MspW/UikK+0b6vfeescS/QjFLUyucXzk4h8eYHPDocisEquyLaUgqfe16YNY3NBr/CtYuVvzJmkYjBw1j14qVGxIU7ubXL+CFGyWXR0IBrZkbSEeZwGuRLvmC2g= Received: from FRXPR01MB0453.DEUPRD01.PROD.OUTLOOK.DE (2a01:4180:c010:1f::20) by FRXPR01MB0775.DEUPRD01.PROD.OUTLOOK.DE (2a01:4180:c010:21::23) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3825.12; Wed, 3 Feb 2021 09:12:41 +0000 Received: from FRXPR01MB0453.DEUPRD01.PROD.OUTLOOK.DE ([fe80::a9d6:1926:9888:6643]) by FRXPR01MB0453.DEUPRD01.PROD.OUTLOOK.DE ([fe80::a9d6:1926:9888:6643%7]) with mapi id 15.20.3825.020; Wed, 3 Feb 2021 09:12:41 +0000 From: Sebastian Dressler To: "Markhof, Ingolf" CC: "pgsql-general@lists.postgresql.org" Subject: Re: Foreign table performance issue / PostgreSQK vs. ORACLE Thread-Topic: Foreign table performance issue / PostgreSQK vs. ORACLE Thread-Index: Adb2PEtpXwRgOxmgTgeXwXdXu2W1VgAuK6IAAJkDKTAALOyKAA== Date: Wed, 3 Feb 2021 09:12:41 +0000 Message-ID: References: <6E566E1E-2A9A-413C-873E-56480146A293@swarm64.com> <1e568ab5fb204e79b6f55620ccbd85c5@MS-FRA-E13EX02.intl.ad.vzwcorp.com> In-Reply-To: <1e568ab5fb204e79b6f55620ccbd85c5@MS-FRA-E13EX02.intl.ad.vzwcorp.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: de.verizon.com; dkim=none (message not signed) header.d=none;de.verizon.com; dmarc=none action=none header.from=swarm64.com; x-originating-ip: [92.116.119.165] x-ms-publictraffictype: Email x-ms-office365-filtering-correlation-id: 99222f33-f187-4bd8-aec9-08d8c823dc09 x-ms-traffictypediagnostic: FRXPR01MB0775: x-microsoft-antispam-prvs: x-ms-oob-tlc-oobclassifiers: OLM:10000; x-ms-exchange-senderadcheck: 1 x-microsoft-antispam: BCL:0; x-microsoft-antispam-message-info: o4o5IkyFFzo0JLM3AEEDiONTqFe1x5feBtfeWRQ38f+wuskHAwbuQcz63zlwJmvsTk+j/BY38iE6zQa6BOqw2At+sslcCNPdW4YNnzVU3GjWlesVR99ugaTeSsBMo6YsHIKBioDVPbQvsd0NBPqQ+viSq4m1CXPe1SRWTT9n8cQ/deth9f7yM96MzsQjmcaDOO8hpj9v+qqwTRFdDM/227nx6s0XV006XDoPI/THhu6Fs0enBCG2LYAiE7zC1DA2plnZaPerz5EYKCeGKmHyjhX+nErShV3m6P0aTxupU2ty3qzBZtZu+zDMECFjnl5/e1+K26mBcLZLcv1DBrRwAjVKl+QEdM/Te3R9KBM2BzQ40HINVss++wyAzYR8J6piU/MwNc70nhTb27ubtGeQQUVtmciA4ov8OYg4r5KLqFPJ7Opgoo24EiYQzkbgXl9Icr62BIcLorUGNT3lyl/kuhfwX2JDlzGrOb0ZHYMiMnpyC49v2yeJn+qjZRf7+Vom2J1Ihim2bmet0e1uNon/H+WMV1edRcaeZFX//PtW8+At4razuXhBrcin+EybVaHf x-forefront-antispam-report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:FRXPR01MB0453.DEUPRD01.PROD.OUTLOOK.DE;PTR:;CAT:NONE;SFS:(376002)(366004)(39830400003)(396003)(136003)(346002)(2616005)(71200400001)(33656002)(5660300002)(83380400001)(8676002)(76116006)(186003)(4326008)(86362001)(66946007)(66476007)(66556008)(64756008)(66446008)(8936002)(478600001)(966005)(2906002)(6916009)(316002)(36756003)(26005)(166002);DIR:OUT;SFP:1101; x-ms-exchange-antispam-messagedata: =?us-ascii?Q?dbmhWHyVsDbRQORfw1A0v6DwYbBqO0C6wXQAILHNZ+QLYJyADqfE21Pr2lIN?= =?us-ascii?Q?b2kg6J/Tgus1SaA+eCkbSaiLSvnVGDtNdwe0KAKg8Y8if8/nyguEb5RsDN4b?= =?us-ascii?Q?YI7L/9LjSgU2f8aYwsQWRAj1n6aRN6fJU7L+6JExmwO6f9ffpO4gJCupoBrQ?= =?us-ascii?Q?P9pUP04A/5ngro7UFUit5fB04e28NpHvaAL8gpiwPtw81CbeihlPEzJUvzSa?= =?us-ascii?Q?cp1jP6z6errmmBg124rl2v+9XXxjHpP7Yvo8PCCKm5diPpPGiu7L0Eq7PlOi?= =?us-ascii?Q?ju4fpVtSG8QhCctYzzyMwvkZ3whu4m1d//63wYjeCgiS7aEHxf01pVgs7ejy?= =?us-ascii?Q?0gt/hvcPSIQS8w3HGoV+uBPhzkZE9ZNpji3/1ZVIONmyfQM9UzBEUR8lBFAN?= =?us-ascii?Q?xjhMCr6vafVFXXqI2A91nHKomYewur/L6iH4rZkeRi/Pg+kpDpIRSIkOig0w?= =?us-ascii?Q?dfRfOgH5A7pmkEDk+hBmROOsTanTlg4gNCQ1FOice4+Y1MFKFsbGYancjoEc?= =?us-ascii?Q?b2i/tUqCdellxo4oq4N+HUy2dzqueXP96mm37n8Ydmkwe5jyXQJELgwQar6K?= =?us-ascii?Q?+al0tmRdtDZJW3s6N/hrhv0/m5jAmKlBVW2rMAJNMEUQzQiASMDgSvHX8H9d?= =?us-ascii?Q?NUJadEKbqwxzZobw3cB3U5hVD+40EqH5bfiL9mldTYmFJlVmRG5v+kfOoQ5G?= =?us-ascii?Q?tv+yClgW5T7ZRxsGNJJjmzoqEwozMSn/a8TlTEzOQSmongqZM7LjjpSKYB2N?= =?us-ascii?Q?V+7P30FDbtoACubUPptz1btfIch56DfFa6FaJa8S6JWXVZuug3YkKW/XKcZu?= =?us-ascii?Q?GTtKk1vbGCEOLK5/p6pXgQDZ0yb/ZV/OFY1T/jwZ+TiCWBcqsix81d3iI3+N?= =?us-ascii?Q?f3NCJSUdM1SJ4J9t3gd+QxdM/Is1x7lKblF1kYmQ9g9gRfDgasqE8cu2Ynzq?= =?us-ascii?Q?cqeElzlJdNyNeyltpmWmdCipFrUGuPtpfKLNwSq5vJOHNuKnve/aYVswbhYj?= =?us-ascii?Q?bNuTg0SJOdKxuoSNgjBl8E3elZ7+V1vchY2vCL9JmQwTE8/k+3kMDhjCuA4+?= =?us-ascii?Q?tyB8WcyIZihzIEGJl5WsEuQsCKHl1A=3D=3D?= x-ms-exchange-transport-forked: True Content-Type: multipart/alternative; boundary="_000_CFC62B0FE4DE4E038ADE66148C989144swarm64com_" MIME-Version: 1.0 X-OriginatorOrg: swarm64.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-AuthSource: FRXPR01MB0453.DEUPRD01.PROD.OUTLOOK.DE X-MS-Exchange-CrossTenant-Network-Message-Id: 99222f33-f187-4bd8-aec9-08d8c823dc09 X-MS-Exchange-CrossTenant-originalarrivaltime: 03 Feb 2021 09:12:41.2958 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 6a3a733d-0e05-4774-8f9e-4fc6a12face6 X-MS-Exchange-CrossTenant-mailboxtype: HOSTED X-MS-Exchange-CrossTenant-userprincipalname: NjWBB/ZBEE+um/QaDYm8ck2ccdTEI0MhP8QrRRyfTTrok+Q/iSAMw60y+wGhTsf6Phx6Zz59A64eXNgYiK7y+g== X-MS-Exchange-Transport-CrossTenantHeadersStamped: FRXPR01MB0775 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --_000_CFC62B0FE4DE4E038ADE66148C989144swarm64com_ Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Hi Ingolf, On 2. Feb 2021, at 13:05, Markhof, Ingolf > wrote: Hi! My PostgreSQL version is 11.8. The query I am running is referring to a number of foreign tables. The firs= t one (table1) has to IDs, let's say ID_A and ID_B. While ID_A is unique, I= D_B is not. In my case, I am pulling formation for a value of IB_B for whic= h about 800 rows (with unique ID_A) exist. I found: While select * from my_view where id_b=3D'some value'; seemingly runs "forever" (I cancelled execution after a few hours), the fol= lowing completes in about 1 hr: select * from my_view where ia_a in ( select id_a from table1 where id_b=3D'some value' ); So, I tried smaller chunks of ID_a and found the execution time is non-line= ar with respect to number of IDs. For e.g. 50 ID_A's, it was completed in a= bout 12 sec. [...] I then found the option fetch_size, e.g. ALTER SERVER some_server OPTIONS (= fetch_size '50000'). A chunk of 50 now executes in 2 seconds (instead of 12= before). Thanks for the additional info. I tried to replicate this, you can find the= GitHub Gist at [1], happy to hear your feedback about it. What I can see from the execution plans in my example is, that the postgres= _fdw pushes down that part Remote SQL: SELECT id_a FROM public.a WHERE ((id_b =3D 1)) part to the remote. On the remote DB this query can result in either an ind= ex-only scan, an index scan or a full table scan. Which method is chosen de= pends on table size and indexes. Given the nature of postgres_fdw this will= be done in any case with a sequential query. For this part I would claim: - If the planner expects few rows, it will choose an index-lookup which is = a good thing because it effectively reduces the amount of data that needs t= o be queried. This would make it fast, given this is a sequential scan. - If the planner expects many rows, it might choose a sequential scan which= can be slow depending on the overall size of the table and likely whether = it is cached or not. So, I found the "size" of the query has a serious impact to the execution t= ime. I don't really understand why execution 16*50 takes 16*2 secs only, bu= t executing 1*800 takes about 3000 seconds... The mentioned fetch_size parameter has a positive effect, because one can g= rab many more rows and return them at the same time. Worst case (and this i= s just pure assumption), on each new fetch, the query might be re-executed = and thus runtime becomes much more. Further up in the plan, I see Remote SQL: SELECT id_a, id_b FROM public.a which is the "SELECT * FROM my_view" part. Meaning, here it will definitely= do a full table scan on remote since it cannot push down the IN condition.= I don't really see right now why this query at all is slower than your ori= ginal form. In my experiment it is not, but maybe I am doing something wron= g in the schema. One thought would be however, that the full table scan on the remote is mor= e efficient than pushing down the filter and thus it returns faster. To really figure out more, I would suggest to increase the logging level on= your remote server in order to see which queries are really executed. Even= better to maybe use auto_explain to fetch plans and see whether these clai= ms apply. Best, Sebastian -- [1]: https://gist.github.com/sdressler/9a93d66b7052dc75ec45c0a4bf5c61de Sebastian Dressler, Solution Architect, Swarm64 +49 30 994 0496 72 | sebastian@swarm64.com --_000_CFC62B0FE4DE4E038ADE66148C989144swarm64com_ Content-Type: text/html; charset="us-ascii" Content-ID: <1752F085F53E3E4EA35FE2B709195BB3@DEUPRD01.PROD.OUTLOOK.DE> Content-Transfer-Encoding: quoted-printable Hi Ingolf,

On 2. Feb 2021, at 13:05, Markhof, Ingolf <ingolf.markhof@de.verizon.co= m> wrote:

Hi!

My PostgreSQL version is 11.8. 

The query I am running is referring to a number of foreign tables. The first o= ne (table1) has to IDs, let's say ID_A and ID_B. While ID_A is unique, ID_B= is not. In my case, I am pulling formation for a value of IB_B for which a= bout 800 rows (with unique ID_A) exist. I found:

While

= select * from my_view where id_b=3D'some value';

seemingly runs "forever" (I cancelled execution after a few hours), the fo= llowing completes in about 1 hr:

= select * from my_view where ia_a in (
       select id_a from table1 where id_b=3D'some value'
    );

So, I tried smaller chunks of ID_a and found the execution time is non-linear = with respect to number of IDs. For e.g. 50 ID_A's, it was completed in abou= t 12 sec.

[...]

I then found the option fetch_size, e.g. ALTER SERVER some_server OPTIONS (f= etch_size '50000'). A chunk of 50 now executes in 2 seconds (instead of 12 = before).

Thanks for the additional info. I tried to replicate this, you can fin= d the GitHub Gist at [1], happy to hear your feedback about it.

What I can see from the execution plans in my example is, that the pos= tgres_fdw pushes down that part

    Remote SQL: SELECT id_a FROM public.a WHERE ((id_b =3D 1= ))

part to the remote. On the remote DB this query can result in either a= n index-only scan, an index scan or a full table scan. Which method is chos= en depends on table size and indexes. Given the nature of postgres_fdw this= will be done in any case with a sequential query. For this part I would claim:

- If the planner expects few rows, it will choose an index-lookup whic= h is a good thing because it effectively reduces the amount of data that ne= eds to be queried. This would make it fast, given this is a sequential scan= .
- If the planner expects many rows, it might choose a sequential scan = which can be slow depending on the overall size of the table and likely whe= ther it is cached or not.

So, I found the &= quot;size" of the query has a serious impact to the execution time. I = don't really understand why execution 16*50 takes 16*2 secs only, but executing 1*800 takes about 3000 seconds...

The mentioned fetch_size parameter has a positive effect, because one = can grab many more rows and return them at the same time. Worst case (and t= his is just pure assumption), on each new fetch, the query might be re-exec= uted and thus runtime becomes much more.

Further up in the plan, I see

    Remote SQL: SELECT id_a, id_b FROM public.a

which is the "SELECT * FROM my_view" part. Meaning, here it = will definitely do a full table scan on remote since it cannot push down th= e IN condition. I don't really see right now why this query at all is slowe= r than your original form. In my experiment it is not, but maybe I am doing something wrong in the schema.

One thought would be however, that the full table scan on the remote i= s more efficient than pushing down the filter and thus it returns faster.

To really figure out more, I would suggest to increase the logging lev= el on your remote server in order to see which queries are really executed.= Even better to maybe use auto_explain to fetch plans and see whether these= claims apply.

Best,
Sebastian


Sebastian Dressler, Solution Architect, Swarm64
+49 30 994 0496 72 | sebastian@swarm64.com 

--_000_CFC62B0FE4DE4E038ADE66148C989144swarm64com_--