Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eaD0u-0004AP-NZ for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jan 2018 04:01:48 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eaD0t-0000he-LH for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jan 2018 04:01:47 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1eaD0t-0000hU-3v for pgsql-hackers@lists.postgresql.org; Sat, 13 Jan 2018 04:01:47 +0000 Received: from mail-wm0-x230.google.com ([2a00:1450:400c:c09::230]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eaD0p-00060P-PA for pgsql-hackers@postgresql.org; Sat, 13 Jan 2018 04:01:45 +0000 Received: by mail-wm0-x230.google.com with SMTP id f140so15162492wmd.2 for ; Fri, 12 Jan 2018 20:01:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language:content-transfer-encoding; bh=3GR0ki//gOFFfYbw77GGOSLgCLSEpzp5UtdUuGTvPSE=; b=SZlMrD9UZ917uxS44JNMwa6TVt7Dw7GGA2HAkAhXArZ9Lbx7iqshsRQaGVhlaxNrvU +17e5WHfIaEhbMD+WU2aHGDW9aviIYAU+Haqc25CLp4WhUY77s1NhJgNpQeTyvRHfusj T7F6tGd7gB8P/UBHd6PtUC0aN2KAa2pNcrQkNLKOJgDOR/Is3q5tUOlgQNFODQ9uwmOA U1+LdJSzXTJ1cgeXE7dTOiYOzANUCc/O3if5jTC04NGpofXwocnFZN63qx4uo8PW5Is4 6suaMVPXjEacIHoweXxoTM++Jgfsz6C98Ftjc7A3R/R/FQ/Q8S13+Y1nAltm+MAvtN4m gSKQ== 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-language :content-transfer-encoding; bh=3GR0ki//gOFFfYbw77GGOSLgCLSEpzp5UtdUuGTvPSE=; b=qs/MfKkiO+t0zqjXWJ4pqTr9aYW6mYBkJ4V5V/O3wJ6jDjN0LSvhCHltkR3L3UMwJL agI4ziqlbCPQqROIdJxNgB7ODdM/b0myqvzQbEDa+SdxaJ8bXTt5wmql8mM5MVEHWMT+ Pm2EzXX5wdWACgJSdhM8PWhIWS2TXEsPpkbMjrC1XvEMsQml6MHznFd/G+aNuk1uV0+U 7aR2nxufrayj1arIFgSTGFXSf63/01ast4q4wdzcSINZbJLJ6IQw08Fzwy45sIke5LTh hSGQ3EgX0kb5mu2+NQXWqVfo7S+RPrvfXaU8D94kO/RriemoG4T9Uo0E4chh5V2ih6cC rLDA== X-Gm-Message-State: AKwxytfgXgXQtgHxETnSSDnT3qakH7w0/9uP8ZNuO93JBDau6UfmmYsm +Ti4lS5ufc342Pmn5aiPMgomQIc9/loo6EJDoku3Igtu2NdITh8aI0ILy0adoJEuFlc6Rk6DXdJ Z7aMRIDV9DftOF3iOEQ5YH0YjVa/hbXcO7BujugmhbCMI9VdYF1OEn8uKJ9zg7xFiNE/NrYD8lk LCQXPRmT9v X-Google-Smtp-Source: ACJfBouI1zSRtlWvkc/EkWTJG9XvqtjMCif5X2Fy6x5C94C4P/maX8mVJ/YpfoAKVdBa+aFB/EtNzw== X-Received: by 10.28.113.21 with SMTP id m21mr5509565wmc.29.1515816101568; Fri, 12 Jan 2018 20:01:41 -0800 (PST) Received: from [10.137.2.19] (ip-78-102-97-226.net.upcbroadband.cz. [78.102.97.226]) by smtp.gmail.com with ESMTPSA id o22sm4485575wra.51.2018.01.12.20.01.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 20:01:40 -0800 (PST) Subject: Re: PATCH: logical_work_mem and logical streaming of large in-progress transactions To: Peter Eisentraut , PostgreSQL Hackers References: <688b0b7f-2f6c-d827-c27b-216a8e3ea700@2ndquadrant.com> From: Tomas Vondra Message-ID: <4de1faeb-5d1a-ffb7-6148-8237d6147e01@2ndquadrant.com> Date: Sat, 13 Jan 2018 05:01:38 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.4.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 01/11/2018 08:41 PM, Peter Eisentraut wrote: > On 12/22/17 23:57, Tomas Vondra wrote: >> PART 1: adding logical_work_mem memory limit (0001) >> --------------------------------------------------- >> >> Currently, limiting the amount of memory consumed by logical decoding is >> tricky (or you might say impossible) for several reasons: > > I would like to see some more discussion on this, but I think not a lot > of people understand the details, so I'll try to write up an explanation > here. This code is also somewhat new to me, so please correct me if > there are inaccuracies, while keeping in mind that I'm trying to simplify. > > ... snip ... Thanks for a comprehensive summary of the patch! > > "XXX With many subtransactions this might be quite slow, because we'll > have to walk through all of them. There are some options how we could > improve that: (a) maintain some secondary structure with transactions > sorted by amount of changes, (b) not looking for the entirely largest > transaction, but e.g. for transaction using at least some fraction of > the memory limit, and (c) evicting multiple transactions at once, e.g. > to free a given portion of the memory limit (e.g. 50%)." > > (a) would create more overhead for the case where everything fits into > memory, so it seems unattractive. Some combination of (b) and (c) seems > useful, but we'd have to come up with something concrete. > Yeah, when writing that comment I was worried that (a) might get rather expensive. I was thinking about maintaining a dlist of transactions sorted by size (ReorderBuffer now only has a hash table), so that we could evict transactions from the beginning of the list. But while that speeds up the choice of transactions to evict, the added cost is rather high, particularly when most transactions are roughly of the same size. Because in that case we probably have to move the nodes around in the list quite often. So it seems wiser to just walk the list once when looking for a victim. What I'm thinking about instead is tracking just some approximated version of this - it does not really matter whether we evict the really largest transaction or one that is a couple of kilobytes smaller. What we care about is an answer to this question: Is there some very large transaction that we could evict to free a lot of memory, or are all transactions fairly small? So perhaps we can define some "size classes" and track to which of them each transaction belongs. For example, we could split the memory limit into 100 buckets, each representing a 1% size increment. A transaction would not switch the class very often, and it would be trivial to pick the largest transaction. When all the transactions are squashed in the smallest classes, we may switch to some alternative strategy. Not sure. In any case, I don't really know how expensive the selection actually is, and if it's an issue. I'll do some measurements. regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services