Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eaDIC-0004z4-Pe for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jan 2018 04:19:40 +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 1eaDIB-000472-4a for pgsql-hackers@arkaria.postgresql.org; Sat, 13 Jan 2018 04:19:39 +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 1eaDIA-00046m-Qa for pgsql-hackers@lists.postgresql.org; Sat, 13 Jan 2018 04:19:38 +0000 Received: from mail-wr0-x236.google.com ([2a00:1450:400c:c0c::236]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1eaDI5-0006K5-8B for pgsql-hackers@postgresql.org; Sat, 13 Jan 2018 04:19:37 +0000 Received: by mail-wr0-x236.google.com with SMTP id 36so6985349wrh.1 for ; Fri, 12 Jan 2018 20:19:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=cE0QASeLwXF34stmGMJqCLFYSCLCzo2WQ5JKCUnvzJ0=; b=lUX5yNERvVCtFEWBIsG8Wsz5AM/9Qwvke0HncpAQEE2RnOsmthJwtc//k30rgF1PVo 4CS3y6aIvTW2QViJ5bqrFisj1Ek9U2cqyr+jG5wO1u2oXJfHsYA8/QiMllidMfoU2jUH +GFTWGDKL4ubakTAzRPGZcUlX2DWgekFtxwn754piAXSyrWsTq8bkDtesVXbFJPFuIII SfJnU5KlUzLLPFi9VBusX6MyVq1VR8Lz5afSuChzrftoAeWsqUutPfyxU638JTlJX6iP wH4qeAUTDLKXqeB8BCNVSAUd5cpNepZYVylkn9DfDDKryNobtanlY4NC9AyLE+zxG7nn 1D1g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=cE0QASeLwXF34stmGMJqCLFYSCLCzo2WQ5JKCUnvzJ0=; b=cyvNit1MAp+1I97m9eCDn6shxSrxgRTZ34tT+Q+XnnjFkw2QIIlkvCWDFq4VyGLsDh VFZLLmqPxJ8VqQSnXb4BQU/73FNzo6kzSxvyiHBAUV64W9KjCwhjlPGJyrKOePisXM9s AGtdb/tW9Rr7jUEaAwiOF4/vEu0Zx1WzQUHwcBVBo9tEWQLVsDkKzX8zA59w36v0sJ1h TXc8ayEWgZxeFd/Nq4lJ3JsNxUTp7iDXUxOPHb0WbtL6t+RhbGnT/8gLI2ABKUcdGJxa 2Zp7+rdpuUpV2JrCO0mgNKWw8SIJoX7wMzhplpKOfRdUSsPmlUUoNM5uEuyABr4a9+Tm xJPA== X-Gm-Message-State: AKwxytfa7tX9+2TwlDSqxhyHcNo2AlIcWrGI/KmIgTtlUCJ9ih0hE67s Tvjn+TYsa8gbYYFrsVK5AVnOaeSuw2Vb82w/QL+W3I5vpgICyCqveN8oRloRMiFWfczJHgnk4rj 48E74iRwuQ8aaBrs5cJ0DqwYLkfU+tYBkkYztcOxRdD7jF0y8wpM/zmRiIxTqWZfQCWuk9xhSa5 G85j0Z9k2D X-Google-Smtp-Source: ACJfBov0WNdfi0Y9V8Oqp81LQTKjyYCQnaQseeJDfli3IBQORkgpzM45tOGF6dCZEYQdqMn8nvDFIw== X-Received: by 10.223.167.145 with SMTP id j17mr6302344wrc.217.1515817170066; Fri, 12 Jan 2018 20:19:30 -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 x81sm12380588wmd.14.2018.01.12.20.19.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 12 Jan 2018 20:19:28 -0800 (PST) Subject: Re: PATCH: logical_work_mem and logical streaming of large in-progress transactions To: Peter Eisentraut , Greg Stark Cc: PostgreSQL Hackers References: <688b0b7f-2f6c-d827-c27b-216a8e3ea700@2ndquadrant.com> <5d00346a-b565-7fde-c6da-35f39c72936a@2ndquadrant.com> From: Tomas Vondra Message-ID: Date: Sat, 13 Jan 2018 05:19:27 +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: <5d00346a-b565-7fde-c6da-35f39c72936a@2ndquadrant.com> 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/12/2018 05:35 PM, Peter Eisentraut wrote: > On 1/11/18 18:23, Greg Stark wrote: >> AIUI spilling to disk doesn't affect absorbing future updates, we >> would just keep accumulating them in memory right? We won't need to >> unspill until it comes time to commit. > > Once a transaction has been serialized, future updates keep accumulating > in memory, until perhaps it gets serialized again. But then at commit > time, if a transaction has been partially serialized at all, all the > remaining changes are also serialized before the whole thing is read > back in (see reorderbuffer.c line 855). > > So one optimization would be to specially keep track of all transactions > that have been serialized already and pick those first for further > serialization, because it will be done eventually anyway. > > But this is only a secondary optimization, because it doesn't help in > the extreme cases that either no (or few) transactions have been > serialized or all (or most) transactions have been serialized. > >> The real aim should be to try to pick the transaction that will be >> committed furthest in the future. That gives you the most memory to >> use for live transactions for the longest time and could let you >> process the maximum amount of transactions without spilling them. So >> either the oldest transaction (in the expectation that it's been open >> a while and appears to be a long-lived batch job that will stay open >> for a long time) or the youngest transaction (in the expectation that >> all transactions are more or less equally long-lived) might make >> sense. > > Yes, that makes sense. We'd still need to keep a separate ordered list > of transactions somewhere, but that might be easier if we just order > them in the order we see them. > Wouldn't the 'toplevel_by_lsn' be suitable for this? Subtransactions don't really commit independently, but as part of the toplevel xact. And that list is ordered by LSN, which is pretty much exactly the order in which we see the transactions. I feel somewhat uncomfortable about evicting oldest (or youngest) transactions for based on some assumed correlation with the commit order. I'm pretty sure that will bite us badly for some workloads. Another somewhat non-intuitive detail is that because ReorderBuffer switched to Generation allocator for changes (which usually represent 99% of the memory used during decoding), it does not reuse memory the way AllocSet does. Actually, it does not reuse memory at all, aiming to eventually give the memory back to libc (which AllocSet can't do). Because of this evicting the youngest transactions seems like a quite bad idea, because those chunks will not be reused and there may be other chunks on the blocks, preventing their release. Yeah, complicated stuff. -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services