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 1oExxR-0001Zj-FY for pgsql-docs@arkaria.postgresql.org; Fri, 22 Jul 2022 19:05:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oExxQ-0006Tl-AC for pgsql-docs@arkaria.postgresql.org; Fri, 22 Jul 2022 19:05:04 +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 1oExxQ-0006TW-1P for pgsql-docs@lists.postgresql.org; Fri, 22 Jul 2022 19:05:04 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oExxN-0004Aj-2E; Fri, 22 Jul 2022 19:05:03 +0000 Received: from bruce by momjian.us with local (Exim 4.94.2) (envelope-from ) id 1oExxJ-009N35-Jz; Fri, 22 Jul 2022 15:04:57 -0400 Date: Fri, 22 Jul 2022 15:04:57 -0400 From: Bruce Momjian To: Peter Geoghegan Cc: "Jonathan S. Katz" , "David G. Johnston" , Pg Docs Subject: Re: documentation on HOT Message-ID: References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="ldcR1/lqLOM7U91g" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --ldcR1/lqLOM7U91g Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Fri, Jul 22, 2022 at 10:05:04AM -0700, Peter Geoghegan wrote: > On Fri, Jul 22, 2022 at 8:09 AM Jonathan S. Katz wrote: > > Thanks! This is great. Probably the most concise and clear explanation I > > have seen for HOT, which is exactly what we need for the docs :) > > I'm delighted that this is finally happening! > > > I think we should expand on this and explain how adjusting "fillfactor" > > will affect this. I think that may change the final sentence too. > > Definitely -- this is by far the most important reason to tune heap > fill factor, which can make a big difference. There should be a link > from the fill factor docs to the new HOT section, at a minimum. Done in the attached patch, and updated URL: https://momjian.us/tmp/pgsql/storage-hot.html > Another thing: would be good to point out how to monitor the number of > HOT updates vs non-HOT updates using views like pg_stat_user_tables. I added a sentence about that, good idea. > One minor correction: Opportunistic pruning isn't limited to heap-only > tuples -- "HOT pruning" is actually a bit of a misnomer, that somehow > caught on in the Postgres community. Opportunistic pruning can and > will happen even with non-HOT updates -- the only difference on the > heap page level is that 4 byte LP_DEAD line pointers will accumulate > over time, which only VACUUM is able to clean up (we need LP_DEAD > items to stick around until then as tombstones, so that index scans > don't ever get confused). But pruning will still be able to free most > of the space on the heap page, in almost the same way. I see your point. I changed the benefits from pruning to no new page item identifiers. > There is a section about bottom-up index deletion in the docs (in the > B-Tree internals chapter) that already references HOT. It should link > to this new chapter now, I think. Already done in the attached patch, I think. -- Bruce Momjian https://momjian.us EDB https://enterprisedb.com Indecision is a decision. Inaction is an action. Mark Batterson --ldcR1/lqLOM7U91g Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="hot.diff" diff --git a/doc/src/sgml/acronyms.sgml b/doc/src/sgml/acronyms.sgml index 9ed148ab84..2df6559acc 100644 --- a/doc/src/sgml/acronyms.sgml +++ b/doc/src/sgml/acronyms.sgml @@ -299,9 +299,7 @@ HOT - Heap-Only - Tuples + Heap-Only Tuples diff --git a/doc/src/sgml/btree.sgml b/doc/src/sgml/btree.sgml index a9200ee52e..6f608a14bf 100644 --- a/doc/src/sgml/btree.sgml +++ b/doc/src/sgml/btree.sgml @@ -639,7 +639,8 @@ options(relopts local_relopts *) returns accumulate and adversely affect query latency and throughput. This typically occurs with UPDATE-heavy workloads where most individual updates cannot apply the - HOT optimization. Changing the value of only + HOT optimization. + Changing the value of only one column covered by one index during an UPDATE always necessitates a new set of index tuples — one for each and every index on the diff --git a/doc/src/sgml/catalogs.sgml b/doc/src/sgml/catalogs.sgml index a186e35f00..248dbc0e26 100644 --- a/doc/src/sgml/catalogs.sgml +++ b/doc/src/sgml/catalogs.sgml @@ -4381,7 +4381,7 @@ SCRAM-SHA-256$<iteration count>:&l If true, queries must not use the index until the xmin of this pg_index row is below their TransactionXmin - event horizon, because the table may contain broken HOT chains with + event horizon, because the table may contain broken HOT chains with incompatible rows that they can see diff --git a/doc/src/sgml/config.sgml b/doc/src/sgml/config.sgml index e2d728e0c4..e5a84ed76d 100644 --- a/doc/src/sgml/config.sgml +++ b/doc/src/sgml/config.sgml @@ -4482,7 +4482,8 @@ ANY num_sync ( HOT updates + will defer cleanup of dead row versions. The default is zero transactions, meaning that dead row versions can be removed as soon as possible, that is, as soon as they are no longer visible to any open transaction. You may wish to set this to a diff --git a/doc/src/sgml/indexam.sgml b/doc/src/sgml/indexam.sgml index cf359fa9ff..4f83970c85 100644 --- a/doc/src/sgml/indexam.sgml +++ b/doc/src/sgml/indexam.sgml @@ -45,7 +45,8 @@ extant versions of the same logical row; to an index, each tuple is an independent object that needs its own index entry. Thus, an update of a row always creates all-new index entries for the row, even if - the key values did not change. (HOT tuples are an exception to this + the key values did not change. (HOT + tuples are an exception to this statement; but indexes do not deal with those, either.) Index entries for dead tuples are reclaimed (by vacuuming) when the dead tuples themselves are reclaimed. diff --git a/doc/src/sgml/indices.sgml b/doc/src/sgml/indices.sgml index 023157d888..42e1e86c8a 100644 --- a/doc/src/sgml/indices.sgml +++ b/doc/src/sgml/indices.sgml @@ -749,7 +749,7 @@ CREATE INDEX people_names ON people ((first_name || ' ' || last_name)); Index expressions are relatively expensive to maintain, because the derived expression(s) must be computed for each row insertion - and non-HOT update. However, the index expressions are + and non-HOT update. However, the index expressions are not recomputed during an indexed search, since they are already stored in the index. In both examples above, the system sees the query as just WHERE indexedcolumn = 'constant' diff --git a/doc/src/sgml/monitoring.sgml b/doc/src/sgml/monitoring.sgml index 7dbbab6f5c..6408d28c5d 100644 --- a/doc/src/sgml/monitoring.sgml +++ b/doc/src/sgml/monitoring.sgml @@ -4426,7 +4426,7 @@ SELECT pid, wait_event_type, wait_event FROM pg_stat_activity WHERE wait_event i n_tup_upd bigint - Number of rows updated (includes HOT updated rows) + Number of rows updated (includes HOT updated rows) diff --git a/doc/src/sgml/ref/create_table.sgml b/doc/src/sgml/ref/create_table.sgml index 6bbf15ed1a..c14b2010d8 100644 --- a/doc/src/sgml/ref/create_table.sgml +++ b/doc/src/sgml/ref/create_table.sgml @@ -1435,7 +1435,9 @@ WITH ( MODULUS numeric_literal, REM to the indicated percentage; the remaining space on each page is reserved for updating rows on that page. This gives UPDATE a chance to place the updated copy of a row on the same page as the - original, which is more efficient than placing it on a different page. + original, which is more efficient than placing it on a different + page, and makes heap-only tuple + updates more likely. For a table whose entries are never updated, complete packing is the best choice, but in heavily updated tables smaller fillfactors are appropriate. This parameter cannot be set for TOAST tables. diff --git a/doc/src/sgml/storage.sgml b/doc/src/sgml/storage.sgml index f4b9f66589..2e1aa58e4f 100644 --- a/doc/src/sgml/storage.sgml +++ b/doc/src/sgml/storage.sgml @@ -1075,4 +1075,70 @@ data. Empty in ordinary tables. + + + Heap-Only Tuples (<acronym>HOT</acronym>) + + + To allow for high concurrency, PostgreSQL + uses multiversion concurrency + control (MVCC) to store rows. However, + MVCC has some downsides for update queries. + Specifically, updates cause additional rows to be added to tables. + This can also require new index entries for each updated row, and + removal of old versions of rows can be expensive. + + + + To help reduce the overhead of updates, + PostgreSQL has an optimization called + heap-only tuples (HOT). This optimization is + possible when: + + + + + The update does not modify any columns referenced by the table's + indexes, including expression and partial indexes. + + + + + There is sufficient free space on the page containing the old row + for the updated row. + + + + + In such cases, heap-only tuples provide two optimizations: + + + + + New index entries are not needed to represent updated rows. + + + + + Page item identifiers are + not needed for new rows because they are chained to older versions + of rows on the same page. + + + + + + + In summary, heap-only tuple updates can only happen if indexed columns + are not updated. You can increase the chance of sufficient page space + for HOT updates by using non-default table fillfactor settings. + If you don't, HOT updates will still happen because + new rows will naturally migrate to new pages and existing pages with + sufficient free space for new row versions. The system view pg_stat_all_tables + allows monitoring of the occurrence of HOT and non-HOT updates. + + + --ldcR1/lqLOM7U91g--