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 1ifQvD-0004xL-KG for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Dec 2019 16:02:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1ifQvC-0001BO-Am for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Dec 2019 16:02:34 +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 1ifQvC-0001BF-1G for pgsql-hackers@lists.postgresql.org; Thu, 12 Dec 2019 16:02:34 +0000 Received: from mail-pf1-x444.google.com ([2607:f8b0:4864:20::444]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1ifQv9-0007GO-J5 for pgsql-hackers@postgresql.org; Thu, 12 Dec 2019 16:02:33 +0000 Received: by mail-pf1-x444.google.com with SMTP id s18so973057pfd.8 for ; Thu, 12 Dec 2019 08:02:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=4QjsaWZEqLCJbz3hChhqE2IeDUW7wKTkc8TJaLY9fWs=; b=IqCwMAJmZtrD6uwFMlOwnci+nGWARMU7cN1j8IjaNP9WSy7nMUAhzbe2W3fwaAvuQn DRztjhc6zHBqm2BZEheOIIjkZLSLrsQyZJj3zErITzMn6o7RpU+stJ7owYUoEsYw5RbY v68p+vZsOk9oTJ0bTWepA8U+k5MTdAi0USL1MevWV3JlZmY05seDokStB7VDcbdMuN3r TW8qa3aTUK2225D7ICCb7Mpxmo1Z92JO+xxqZ0IeBdO6ldyCiS5l8q8KepEAIs/XbJgX tkf38gI2v+HHd4rOZTJO4GP0i19pUX+dmDx6bCI2b1OTC/oFA9rljp+BJ04NKM5m4lwZ y4Ew== 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=4QjsaWZEqLCJbz3hChhqE2IeDUW7wKTkc8TJaLY9fWs=; b=mZGBPjJlCejuRBs0vLAQ8k7AslOrCSyKatvHtYahfTEir8ueel7OcDOk0EmIcNdO0U znSq9TJLRAN2lNf+HzVDTXsLrr8YKgxtVvnC08N6jvsU0sPTC2oktQx4nEI/3I5q+oxf EtwYNg56cwoXo0uUE9dWGANJOcCwrJMLsy3NmUQEZMy2IZIZL6DdBA7RldyQwaa0GO1C 8jkv810wvQ2yJOOMM0mdS/ZIa4K1gz5MCLD2UqcnrHnnf9jWAj4FDTcPHXVA2sQjNYWE hRCly0YIjaJcOz8Mq08+K8OnEkPlT7LC/WwrNqAImUsQVb5fzg/X5I0glCNA2QdmYezS RFOw== X-Gm-Message-State: APjAAAWtrkg6sJscDOxso2TDJ4iXqGTD4LSGuk4uYWuPxfUyDF1cm8A6 EkkVlGD7hltk3iETXa/s8CU= X-Google-Smtp-Source: APXvYqy7uFbxu7ufMSkfmNC2qfs1+VeICnUgnpcjoz9CFqFX+7bVStnJ8+YAXAG2wewzkGM4m11UQA== X-Received: by 2002:a62:7696:: with SMTP id r144mr10532070pfc.177.1576166548905; Thu, 12 Dec 2019 08:02:28 -0800 (PST) Received: from [192.168.0.17] (24-113-193-150.wavecable.com. [24.113.193.150]) by smtp.gmail.com with ESMTPSA id i4sm6437114pjd.19.2019.12.12.08.02.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 12 Dec 2019 08:02:28 -0800 (PST) Subject: Re: Make autovacuum sort tables in descending order of xid_age To: David Fetter Cc: PostgreSQL Development , Christophe Pettus References: <20191129180138.GD8731@fetter.org> <20191129222152.GE8731@fetter.org> <2488acea-328b-1328-d437-8c156bc8f7a4@gmail.com> <20191130222314.GF8731@fetter.org> From: Mark Dilger Message-ID: <877ac074-e95d-bdee-09e7-c51f0ee0fde2@gmail.com> Date: Thu, 12 Dec 2019 08:02:25 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: <20191130222314.GF8731@fetter.org> Content-Type: text/plain; charset=windows-1252; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/30/19 2:23 PM, David Fetter wrote: > On Sat, Nov 30, 2019 at 10:04:07AM -0800, Mark Dilger wrote: >> On 11/29/19 2:21 PM, David Fetter wrote: >>> On Fri, Nov 29, 2019 at 07:01:39PM +0100, David Fetter wrote: >>>> Folks, >>>> >>>> Per a suggestion Christophe made, please find attached a patch to >>>> $Subject: >>>> >>>> Apart from carefully fudging with pg_resetwal, and short of running in >>>> production for a few weeks, what would be some good ways to test this? >>> >>> Per discussion on IRC with Sehrope Sarkuni, please find attached a >>> patch with one fewer bug, this one in the repalloc() calls. >> >> Hello David, >> >> Here are my initial thoughts. >> >> Although you appear to be tackling the problem of vacuuming tables >> with older Xids first *per database*, > > Yes, that's what's come up for me in production, but lately, > production has consisted of a single active DB maxing out hardware. I > can see how in other situations--multi-tenant, especially--it would > make more sense to sort the DBs first. I notice you don't address that in your latest patch. Do you have any thoughts on whether that needs to be handled in this patch? Should tackling that problem be left for later? >> have you considered changing the logic in building and sorting the >> database list in get_database_list and rebuild_database_list? I'm >> just curious what your thoughts might be on this subject. > > I hadn't, but now that you mention it, it seems like a reasonable > thing to try. > >> As far as sorting the list of tables in an array and then copying >> that array into a linked list, I think there is no need. The >> copying of table_ages into table_oids is followed immediately by >> >> foreach(cell, table_oids) >> >> and then table_oids seems not to serve any further purpose. Perhaps >> you can just iterate over table_ages directly and avoid the extra >> copying. > > I hadn't looked toward any optimizations in this section, given that > the vacuums in question can take hours or days, but I can see how that > would make the code cleaner, so please find that change attached. That looks better, thanks! >> I have not tested this change, but I may do so later today or perhaps >> on Monday. The code compiles cleanly and passes all regression tests, but I don't think those tests really cover what you are changing. Have you been using any test framework for this? I wonder if you might add information about table size, table changes, and bloat to your RelFrozenXidAge struct and modify rfxa_comparator to use a heuristic to cost the (age, size, bloat, changed) grouping and sort on that cost, such that really large bloated tables with old xids might get vacuumed before smaller, less bloated tables that have even older xids. Sorting the tables based purely on xid_age seems to ignore other factors that are worth considering. I do not have a formula for how those four factors should be weighted in the heuristic, but you are implicitly assigning three of them a weight of zero in your current patch. relation_needs_vacanalyze currently checks the reltuples, n_dead_tuples and changes_since_analyze along with vac_scale_factor and anl_scale_factor for the relation, but only returns booleans dovacuum, doanalyze, and wraparound. If you pass your RelFrozenXidAge struct (perhaps renamed) into relation_needs_vacanalyze, it could store those values for the relation so that you don't need to look it up again when sorting. -- Mark Dilger