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 1hEf0K-0003XV-9P for pgsql-general@arkaria.postgresql.org; Thu, 11 Apr 2019 19:04:57 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hEf0I-0001fK-VB for pgsql-general@arkaria.postgresql.org; Thu, 11 Apr 2019 19:04:54 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hEf0I-0001fD-AN for pgsql-general@lists.postgresql.org; Thu, 11 Apr 2019 19:04:54 +0000 Received: from smtp62.i.mail.ru ([217.69.128.42]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hEf0D-0001HJ-7k for pgsql-general@postgresql.org; Thu, 11 Apr 2019 19:04:53 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=mail.ru; s=mail2; h=Content-Type:In-Reply-To:MIME-Version:Date:Message-ID:From:References:Cc:To:Subject; bh=9vrw8OzAIr3SGenKN2Psn4D92Te0zgyL/BNfveYokqQ=; b=WnZLGx6MWaSCyWmHJIbt2sHob31ZFGAMSoVRYQhTUq0dgpjfOmrp8k1XHEFlQ0PdDXkqGbioXRPZdc1SI8+A/sdw8cRDgdaAjgfoUp/2ywadrhWuHmydQrBP2JD77JC48geydHLjI6cizhjTPZrQR3HEqHA42EtL/0eMh3V0qps=; Received: by smtp62.i.mail.ru with esmtpa (envelope-from ) id 1hEf08-0006vl-O8; Thu, 11 Apr 2019 22:04:45 +0300 Subject: Re: When do vacuumed pages/tuples become available for reuse? To: Jeff Janes Cc: Tom Lane , pgsql-general General References: <9cd73fcb-511d-ede9-6789-209b7d4eb193@mail.ru> <8560.1554990486@sss.pgh.pa.us> <0522481c-4cf6-1928-8780-9bfa2d7c6aca@mail.ru> <9914.1554992415@sss.pgh.pa.us> <17dafb0f-5ab9-390e-96b0-2280d654f790@mail.ru> <44a10837-648b-7cc9-d42a-146bc0c83574@mail.ru> From: rihad Message-ID: <8aa21502-aed5-2114-dbb0-e2e03b7557eb@mail.ru> Date: Thu, 11 Apr 2019 23:04:40 +0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.8.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/alternative; boundary="------------E7B682C5942A6810E48F1CED" Content-Language: en-US Authentication-Results: smtp62.i.mail.ru; auth=pass smtp.auth=rihad@mail.ru smtp.mailfrom=rihad@mail.ru X-77F55803: 689590B63E0A4B015A78504BD2AC29416CDE1282D82D8196C176B12F88987E7C5F7D1904020C53D5881E914539857AD5 X-7FA49CB5: 0D63561A33F958A5020451F116A49C99C4CE84AD5E9286384E72B524D5E0A8548941B15DA834481FA18204E546F3947CEDCF5861DED71B2F389733CBF5DBD5E9C8A9BA7A39EFB7666BA297DBC24807EA117882F44604297287769387670735209ECD01F8117BC8BEA471835C12D1D977C4224003CC8364767815B9869FA544D8D32BA5DBAC0009BE9E8FC8737B5C2249CDE59DDCCE53AF2B3AA81AA40904B5D9CF19DD082D7633A0BCA8734010DFDAB0D81D268191BDAD3D78DA827A17800CE76888F6138A55F47ECD04E86FAF290E2D26E476CCD9869EB175ECD9A6C639B01B78DA827A17800CE79B078A03FA2968D47C71BAD0A6DBEEA075ECD9A6C639B01B4E70A05D1297E1BBC6867C52282FAC8519DC0BE04022C72727F269C8F02392CD5571747095F342E88FB05168BE4CE3AF X-Mailru-Sender: B19AB4DAF1EA2FB518D2771EFF7BC99AF833AC3BE0730249179708B298940D5A1186C5DEF162E43D108F17C94788D7333DDE9B364B0DF2891F953FB4A677E77772D6B4FCE48DF648AE208404248635DF X-Mras: OK List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------E7B682C5942A6810E48F1CED Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit On 04/11/2019 10:13 PM, Jeff Janes wrote: > On Thu, Apr 11, 2019 at 12:18 PM rihad > wrote: > > On 04/11/2019 08:09 PM, Jeff Janes wrote: >> On Thu, Apr 11, 2019 at 11:44 AM rihad > > wrote: >> >> >> Since we dump&restore production DB daily into staging >> environment, the difference in size (as reported by psql's >> \l+) is 11GB in a freshly restored DB as opposed to 70GB in >> production. >> >> >> Yeah, that seems like a problem.  Do you have long lived >> transactions/snapshots that are preventing vacuuming from >> removing dead tuples? You can run a manual "vacuum verbose" and >> see how many dead but nonremovable tuples there were, or >> set log_autovacuum_min_duration to some non-negative value less >> than the autovac takes, and do the same. >> > vacuum frees tuples just fine. It's just that by the time each run > finishes many more accumulate due to table update activity, ad > nauseum. So this unused space constantly grows. Here's a sample > autovacuum run: > > 2019-04-11 19:39:44.450841500 [] LOG:  automatic vacuum of table > "foo.public.bar": index scans: 1 > 2019-04-11 19:39:44.450843500   pages: 0 removed, 472095 remain, 4 > skipped due to pins, 39075 skipped frozen > 2019-04-11 19:39:44.450844500   tuples: 19150 removed, 2725811 > remain, 465 are dead but not yet removable > 2019-04-11 19:39:44.450845500   buffer usage: 62407557 hits, > 6984769 misses, 116409 dirtied > 2019-04-11 19:39:44.450846500   avg read rate: 16.263 MB/s, avg > write rate: 0.271 MB/s > 2019-04-11 19:39:44.450847500   system usage: CPU 59.05s/115.26u > sec elapsed 3355.28 sec > > > This data doesn't seem to support either one of our theories.  "Dead > but not yet removable" is low.  But "removed" also seems pretty low.  > Is 19,150 really the number of updates you think occur over the course > of an hour which causes the problem you are seeing?  Updates that > happened during one vacuum should be cleanly caught by the next one, > so you should only see a steady state of bloat, not unbounded increase. > > But your buffer usage being 132 time the number of pages in the table > suggests it is your indexes, not your table, which are bloated. > How many indexes do you have, and of what type?  Index pages can only > get reused when they become completely empty, or when a new indexed > value fits into (or near) the key-space that that page already > covers.  So if the key space for new tuples is constantly migrating > around and your pages never become absolutely empty, you can get > unbounded bloat in the indexes. Way to many indexes. I'm going to have a hard time convincing our programmers to get rid of any of them ) > > Can you compare the sizes object by object between the live and the > stage, taking care not to include index (or toast) size into the size > of their parent table? You're right, it's mostly indexes that are bloated. Staging: # select pg_size_pretty(pg_relation_size('foo'));  pg_size_pretty ----------------  2924 MB (1 row) # select pg_size_pretty(pg_indexes_size('foo'));  pg_size_pretty ----------------  1958 MB (1 row) Prod: # select pg_size_pretty(pg_relation_size('foo'));  pg_size_pretty ----------------  3688 MB (1 row) # select pg_size_pretty(pg_indexes_size('foo'));  pg_size_pretty ----------------  60 GB (1 row) > >> >> Also, what does pg_freespace >> (https://www.postgresql.org/docs/current/pgfreespacemap.html) >> show about the available of space in the table? How >> about pgstattuple >> (https://www.postgresql.org/docs/current/pgstattuple.html) > Thanks, I'll try those. But as I said freshly restored DB is only > 11GB in size, not 70 (only public schema is used). > > > Yeah, but we need to know **why** that extra 59GB  is not being > reused, not simply the fact that it isn't being reused.  If it isn't > listed as free in the freespace map, then PostgreSQL might not know > how to find it in order to reuse it, for example. But now that I think > it is the indexes, not the table, that is bloated I would chase that > part down first.  No point checking the freespace of the table proper > if the problem is with the indexes. > Cheers, > > Jeff --------------E7B682C5942A6810E48F1CED Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 04/11/2019 10:13 PM, Jeff Janes wrote:
On Thu, Apr 11, 2019 at 12:18 PM rihad <rihad@mail.ru> wrote:
On 04/11/2019 08:09 PM, Jeff Janes wrote:
On Thu, Apr 11, 2019 at 11:44 AM rihad <rihad@mail.ru> wrote:

Since we dump&restore production DB daily into staging environment, the difference in size (as reported by psql's \l+) is 11GB in a freshly restored DB as opposed to 70GB in production.


Yeah, that seems like a problem.  Do you have long lived transactions/snapshots that are preventing vacuuming from removing dead tuples?  You can run a manual "vacuum verbose" and see how many dead but nonremovable tuples there were, or set log_autovacuum_min_duration to some non-negative value less than the autovac takes, and do the same.

vacuum frees tuples just fine. It's just that by the time each run finishes many more accumulate due to table update activity, ad nauseum. So this unused space constantly grows. Here's a sample autovacuum run:

2019-04-11 19:39:44.450841500 [] LOG:  automatic vacuum of table "foo.public.bar": index scans: 1
2019-04-11 19:39:44.450843500   pages: 0 removed, 472095 remain, 4 skipped due to pins, 39075 skipped frozen
2019-04-11 19:39:44.450844500   tuples: 19150 removed, 2725811 remain, 465 are dead but not yet removable
2019-04-11 19:39:44.450845500   buffer usage: 62407557 hits, 6984769 misses, 116409 dirtied
2019-04-11 19:39:44.450846500   avg read rate: 16.263 MB/s, avg write rate: 0.271 MB/s
2019-04-11 19:39:44.450847500   system usage: CPU 59.05s/115.26u sec elapsed 3355.28 sec

This data doesn't seem to support either one of our theories.  "Dead but not yet removable" is low.  But "removed" also seems pretty low.  Is 19,150 really the number of updates you think occur over the course of an hour which causes the problem you are seeing?  Updates that happened during one vacuum should be cleanly caught by the next one, so you should only see a steady state of bloat, not unbounded increase.

But your buffer usage being 132 time the number of pages in the table suggests it is your indexes, not your table, which are bloated.
 
How many indexes do you have, and of what type?  Index pages can only get reused when they become completely empty, or when a new indexed value fits into (or near) the key-space that that page already covers.  So if the key space for new tuples is constantly migrating around and your pages never become absolutely empty, you can get unbounded bloat in the indexes.

Way to many indexes. I'm going to have a hard time convincing our programmers to get rid of any of them )


Can you compare the sizes object by object between the live and the stage, taking care not to include index (or toast) size into the size of their parent table?

You're right, it's mostly indexes that are bloated.

Staging:
# select pg_size_pretty(pg_relation_size('foo'));
 pg_size_pretty
----------------
 2924 MB
(1 row)

# select pg_size_pretty(pg_indexes_size('foo'));
 pg_size_pretty
----------------
 1958 MB
(1 row)

Prod:

# select pg_size_pretty(pg_relation_size('foo'));
 pg_size_pretty
----------------
 3688 MB
(1 row)

# select pg_size_pretty(pg_indexes_size('foo'));
 pg_size_pretty
----------------
 60 GB
(1 row)





Also, what does pg_freespace (https://www.postgresql.org/docs/current/pgfreespacemap.html) show about the available of space in the table?  How about pgstattuple (https://www.postgresql.org/docs/current/pgstattuple.html)
Thanks, I'll try those. But as I said freshly restored DB is only 11GB in size, not 70 (only public schema is used).

Yeah, but we need to know **why** that extra 59GB  is not being reused, not simply the fact that it isn't being reused.  If it isn't listed as free in the freespace map, then PostgreSQL might not know how to find it in order to reuse it, for example. But now that I think it is the indexes, not the table, that is bloated I would chase that part down first.  No point checking the freespace of the table proper if the problem is with the indexes.
 
Cheers,

Jeff


--------------E7B682C5942A6810E48F1CED--