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 1lnRwz-0002X3-EY for pgsql-hackers@arkaria.postgresql.org; Sun, 30 May 2021 20:22:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lnRww-000734-Q0 for pgsql-hackers@arkaria.postgresql.org; Sun, 30 May 2021 20:22:18 +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 1lnRww-00072s-II for pgsql-hackers@lists.postgresql.org; Sun, 30 May 2021 20:22:18 +0000 Received: from mail-ej1-x633.google.com ([2a00:1450:4864:20::633]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lnRws-0000F6-JW for pgsql-hackers@postgresql.org; Sun, 30 May 2021 20:22:17 +0000 Received: by mail-ej1-x633.google.com with SMTP id h24so9510862ejy.2 for ; Sun, 30 May 2021 13:22:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=enterprisedb-com.20150623.gappssmtp.com; s=20150623; h=to:from:subject:message-id:date:user-agent:mime-version :content-language:content-transfer-encoding; bh=YmFhgxhzawKMgc9RYu9zwVe7Q+bWhdCDbJn+CFvU2Po=; b=rOPmTqqLjVEmpjCQaCFSfedJVsF55pXLrGb2CAfIE4y570ew4Ezi3av7+SkIwC/NZJ h/svIdZQtKHCOyiirGXGvp4bIv41u5AQwGVGTcMd41e4TxOWk93LiLz3SLbWuk8HGkp9 Fx1j21NN7XM+NcYHq9D2ZwjzU097vSw5ipZSG3IsYyKB2JG4QaxdCQGbzTYhdWvnxJPZ 9JlgrsZLKFxgGJ0YEYiGyyY8BeoxBosCiswaUeyRxcRgVBkfb/MsOPYgOvOtjcBJzVq3 hQ9sNis3tBS9JSl2gbfiuv+Fd9HesRF4C43UnwRBWSMufSZZm4/0ulgAPM/Q4U5Wze05 N16w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:to:from:subject:message-id:date:user-agent :mime-version:content-language:content-transfer-encoding; bh=YmFhgxhzawKMgc9RYu9zwVe7Q+bWhdCDbJn+CFvU2Po=; b=mbzFHthB9s9qg1H6Y6mPYOCymEMdg1J8HosvKhSKusC3Zf2habAH8uLO03KNKn2l91 HQs/gQri4xpkjbQtZ1splZHnCmucody6kpVU8ZKKnq/fI2ktqKUAloXlT+hE35ZRiZeV 3f9thCUkxJY/XsNxFzA5O8OKDE36n08RUFQnB6pk31l5wukmM5zhb/DzRvp2N4Kev4Zq pvajHJdOAUoOa8nh9LpP6JvFm6VCDTwU4gvDoXNvSmj2iId3BrIPtMfjslcV2/X7ugl0 0lZruF/Wihph9sQJ3D5hp9dfD3WRwqMXZBU7wstkAWvb+X7Mi9vX6g75emJg5cwSbMsl 2yOQ== X-Gm-Message-State: AOAM530S+73z870sRuBbIHo3FOufxQWE3MTOZ8HvIIX44mUCBSzyF2G0 bN0vMRvZtFdfMNjAc+XLrP+duZcCuw05oYlow6qu/bb7PZnnhqRbInV7U8xupZl5qa0b8kfZKR0 nWUuWWHm+m6/yJn3e3xNMK9wbzc7HzaiakIfqYzNNh+9XlGWdImzRthf7Ts0uOuAL1CaKY3OZCO B/2Pjgxds+5fEquKDw1iEMu1An+qlIuHwYPz7IWelH4uHrTf4OGT0wiy9G9eJ4pVviIBmD0b4Of rM6nyBehUn7TO/lKARWas2pe+fH/yzRCikqjA== X-Google-Smtp-Source: ABdhPJyUcNlRq7uvMLscpDP2rUuB+YlP5shCVFtSY0ggG+azMld/qXksQAUCanNJXV00TOFLvGn/cA== X-Received: by 2002:a17:906:e10b:: with SMTP id gj11mr19085083ejb.202.1622406132778; Sun, 30 May 2021 13:22:12 -0700 (PDT) Received: from [10.137.0.19] (ip-86-49-254-183.net.upcbroadband.cz. [86.49.254.183]) by smtp.gmail.com with ESMTPSA id v7sm2908779edx.38.2021.05.30.13.22.12 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 30 May 2021 13:22:12 -0700 (PDT) To: PostgreSQL-development From: Tomas Vondra Subject: postgres_fdw batching vs. (re)creating the tuple slots Message-ID: Date: Sun, 30 May 2021 22:22:10 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.1 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit X-CLOUD-SEC-AV-Info: enterprisedb,google_mail,monitor X-CLOUD-SEC-AV-Sent: true X-Gm-Spam: 0 X-Gm-Phishy: 0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, while looking at the other thread related to postgres_fdw batching [1] and testing with very large batches, I noticed this disappointing behavior when inserting 1M rows (just integers, nothing fancy): no batching: 64782 ms 100 rows: 2118 ms 32767 rows: 41115 ms Pretty nice improvement when batching 100 rows, but then it all goes wrong for some reason. The problem is pretty obvious from a perf profile: --100.00%--ExecModifyTable | --99.70%--ExecInsert | |--50.87%--MakeSingleTupleTableSlot | | | --50.85%--MakeTupleTableSlot | | | --50.70%--IncrTupleDescRefCount | | | --50.69%--ResourceOwnerRememberTupleDesc | | | --50.69%--ResourceArrayAdd | |--48.18%--ExecBatchInsert | | | --47.92%--ExecDropSingleTupleTableSlot | | | |--47.17%--DecrTupleDescRefCount | | | | | --47.15%--ResourceOwnerForgetTupleDesc | | | | | --47.14%--ResourceArrayRemove | | | --0.53%--ExecClearTuple | --0.60%--ExecCopySlot There are two problems at play, here. Firstly, the way it's coded now the slots are pretty much re-created for each batch. So with 1M rows and batches of 32k rows, that's ~30x drop/create. That seems a bit wasteful, and it shouldn't be too difficult to keep the slots across batches. (We can't initialize all the slots in advance, because we don't know how many will be needed, but we don't have to release them between batches.) The other problem is that ResourceArrayAdd/Remove seem to behave a bit poorly with very many elements - I'm not sure if it's O(N^2) or worse, but growing the array and linear searches seem to be a bit expensive. I'll take a look at fixing the first point, but I'm not entirely sure how much will that improve the situation. regards [1] https://postgr.es/m/OS0PR01MB571603973C0AC2874AD6BF2594299%40OS0PR01MB5716.jpnprd01.prod.outlook.com -- Tomas Vondra EnterpriseDB: http://www.enterprisedb.com The Enterprise PostgreSQL Company