Repository navigation
Conversation
Utility statements share many things with regular queries, but thus far there was no utility-statement equivalent of QueryDesc to hold common fields that matter for execution. Instead this information was passed down via an ever-growing list of arguments to ProcessUtility / ProcessUtility_hook. To enable a future change that will introduce another field, and to reduce future breakage of the hook API, introduce a dedicated utility statement descriptor (UtilityStmtDesc), and make it the single argument passed to ProcessUtility. Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Discussion:
This replaces different repeated code blocks that read pgBufferUsage / pgWalUsage, and may have also been running a timer to measure activity, with the Instrumentation struct and associated helpers. Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Discussion: https://postgr.es/m/CAP53PkzdBK8VJ1fS4AZ481LgMN8f9mJiC39ZRHqkFUSYq6KWmg%40mail.gmail.com
…ith INSTR_* macros This encapsulates the ownership of these globals better, and will allow a subsequent refactoring. Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Andres Freund <andres@anarazel.de> Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com> Discussion: https://postgr.es/m/CAP53PkzZ3UotnRrrnXWAv%3DF4avRq9MQ8zU%2BbxoN9tpovEu6fGQ%40mail.gmail.com#fc7140e8af21e07a90a09d7e76b300c4
This adds regression tests that cover some of the expected behaviour around the buffer statistics reported in EXPLAIN ANALYZE, specifically how they behave in parallel query, nested function calls and abort situations. Testing this is challenging because there can be different sources of buffer activity, so we rely on temporary tables where we can to prove that activity was captured and not lost. This supports a future commit that will rework some of the instrumentation logic that could cause areas covered by these tests to fail. Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Discussion: https://postgr.es/m/CAP53PkzdBK8VJ1fS4AZ481LgMN8f9mJiC39ZRHqkFUSYq6KWmg%40mail.gmail.com
Previously, in order to determine the buffer/WAL usage of a given code section, we utilized continuously incrementing global counters that get updated when the actual activity (e.g. shared block read) occurred, and then calculated a diff when the code section ended. This resulted in a bottleneck for executor node instrumentation specifically, with profiles showing BufferUsageAccumDiff in EXPLAIN (ANALYZE, BUFFERS) runs. Instead, introduce a stack-based mechanism, where the actual activity writes into the current stack entry. In the case of executor nodes, this means that each node gets its own stack entry that is pushed at InstrStartNode, and popped at InstrStopNode. Stack entries are zero initialized (avoiding the diff mechanism) and get added to their parent entry when they are finalized, i.e. no more modifications can occur. Per-node counters are thereby exclusive of child nodes: they only reflect activity that happened while the node itself was the current stack entry, which includes any statements the node ran (e.g. through a function). At ExecutorFinish, each node is added directly to the query-level entry rather than rolled up through its parents, which keeps the per-node counters identical for the leader and for parallel workers, whose per-node stats are copied to the leader through shared memory and kept apart from the leader's own. EXPLAIN ANALYZE computes the inclusive per-node numbers it displays, combining leader and workers, by summing each subtree at display time. To correctly handle abort situations, any use of instrumentation stacks must involve either a top-level QueryInstrumentation struct, and its associated InstrQueryStart/InstrQueryStop helpers (which use resource owners to handle aborts), or the Instrumentation struct itself with dedicated PG_TRY/PG_FINALLY calls that ensure the stack is in a consistent state after an abort. In tests, the stack-based instrumentation mechanism reduces the overhead of EXPLAIN (ANALYZE, BUFFERS ON, TIMING OFF) for a large COUNT(*) query from about 35% to about 20% on top of the actual runtime. Callers of the plain Instrumentation API (InstrAlloc/InstrInitOptions with InstrStart/InstrStop) keep measuring by diffing the global pgBufferUsage/pgWalUsage counters for now. The diff-mode handling lives only in InstrStart/InstrStop themselves: node instrumentation never measures by diffing, so InstrStartNode/InstrStopNode go through common helpers that handle just the timer and the stack, keeping the diff-mode branches out of the per-tuple ExecProcNodeInstr path (which would otherwise regress EXPLAIN (ANALYZE, TIMING OFF, BUFFERS OFF) slightly relative to master). Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com> (in an earlier version) Reviewed-by: Heikki Linnakangas <hlinnaka@iki.fi> (in an earlier version) Discussion: https://postgr.es/m/CAP53PkxrmpECzVFpeeEEHDGe6u625s%2BYkmVv5-gw3L_NDSfbiA%40mail.gmail.com#cb583a08e8e096aa1f093bb178906173
The stack-based instrumentation left the plain Instrumentation API (InstrAlloc/InstrInitOptions with InstrStart/InstrStop) measuring by diffing the global pgBufferUsage/pgWalUsage counters, and kept pgBufferUsage updated alongside the stack for that purpose. Convert the remaining users of that API to the stack: VACUUM and ANALYZE logging as well as the planning-time measurement of EXPLAIN and EXECUTE use QueryInstrumentation, which handles aborts through the resource owner, while pg_stat_statements wraps its planner and utility statement timing in PG_TRY/PG_FINALLY and the EXPLAIN (SERIALIZE) destination receiver registers its entry as a child of the query instrumentation. With that, Instrumentation entries always track WAL/buffer usage through the stack, so remove the diff mode along with the global pgBufferUsage and BufferUsageAccumDiff, and update comments that referred to it. Extensions that read pgBufferUsage directly should use InstrStart and InstrStop around the code section they want to measure instead. The related global pgWalUsage is kept for now due to its use in pgstat to track aggregate WAL activity and heap_page_prune_and_freeze for measuring FPIs. Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Discussion: https://postgr.es/m/CAP53PkzdBK8VJ1fS4AZ481LgMN8f9mJiC39ZRHqkFUSYq6KWmg%40mail.gmail.com
For most queries, the bulk of the overhead of EXPLAIN ANALYZE happens in ExecProcNodeInstr when starting/stopping instrumentation for that node. Previously each ExecProcNodeInstr would check which instrumentation options are active in the InstrStartNode/InstrStopNode calls, and do the corresponding work (timers, instrumentation stack, etc.). These conditionals being checked for each tuple being emitted add up, and cause a non-optimal set of instructions to be generated by the compiler. Because we already have a mechanism to specify a function pointer when instrumentation is enabled, we can instead create specialized functions that are tailored to the instrumentation options enabled, and avoid conditionals on subsequent ExecProcNodeInstr calls. In a stress test with a large COUNT(*) that does many ExecProcNode calls, this roughly halves the overhead of EXPLAIN (ANALYZE, TIMING OFF, BUFFERS OFF), from ~13% on top of actual runtime to ~6%. When using BUFFERS ON the same query goes from ~20% to ~15% overhead. Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com> Discussion: https://postgr.es/m/CAP53PkzdBK8VJ1fS4AZ481LgMN8f9mJiC39ZRHqkFUSYq6KWmg%40mail.gmail.com
This sets up a separate instrumentation stack that is used while an Index Scan fetches heap tuples, or an Index Only Scan checks the visibility map and fetches heap tuples for pages that are are not all-visible. EXPLAIN ANALYZE will now show "Table Buffers" that represent such activity. The activity is also included in regular "Buffers" together with index activity and that of any child nodes. Author: Lukas Fittl <lukas@fittl.com> Suggested-by: Andres Freund <andres@anarazel.de> (in an earlier version) Reviewed-by: Zsolt Parragi <zsolt.parragi@percona.com> (in an earlier version) Reviewed-by: Tomas Vondra <tomas@vondra.me> (in an earlier version) Discussion: https://postgr.es/m/CAP53PkzdBK8VJ1fS4AZ481LgMN8f9mJiC39ZRHqkFUSYq6KWmg%40mail.gmail.com
Statements that run while a query executes, e.g. through SPI from a function or a trigger, or a utility command run from PL/pgSQL, used to be accounted to the plan node that ran them. A Function Scan calling a function that scans a large table would show that table's buffers as its own, with no way to tell what the node itself did. Instead, keep such nested statements out of the plan nodes, and have EXPLAIN ANALYZE list them after the plan in a "Nested Statements" section, numbered, with their text, call count, time and buffer/WAL usage, and the origin they were run from: a plan node of the main statement (marked with a matching "Node ID" in the plan), a trigger, and/or another nested statement. Statements nested at any depth are listed, including the statements a utility command runs itself, such as the query of CREATE TABLE AS. Executions of the same statement from the same origin are aggregated. To support this, every instrumentation entry carries a kind. A query entry no longer accumulates to the plan node or trigger entry that is current when it is finalized, but to the nearest enclosing query or plain entry, and query entries are always on the stack while started so that the enclosing statement can be found. A statement with per-node instrumentation tracks its nested statements: each gets a query entry even if nobody asked for one, and is recorded in the enclosing entry when finalized, which also covers statements that fail. Utility commands need a query entry of their own for that. Introduce UtilityStmtDesc->instr, the utility counterpart of QueryDesc's query_instr: a consumer can provide an entry set up with InstrQueryInitLocal(), and standard_ProcessUtility brackets the command with it, or sets one up itself for a command nested in a statement that tracks nested statements. Such an entry lives on the C stack and owns no memory, since a command may end the transaction and with it any resource owner or transaction-lifetime memory. The origin of a nested statement comes from a new current-origin pointer maintained by the instrumented ExecProcNode wrappers and the trigger start/stop functions and scoped to the running statement by InstrQueryStart/InstrQueryStop. This is kept separate from the instrumentation stack, which plan nodes only join when buffer/WAL usage is wanted, so origins are known whatever options were requested, at the cost of one load and two stores per instrumented node call. Author: Lukas Fittl <lukas@fittl.com>
…on totals Each parallel worker tracks its own buffer/WAL activity, reports it through shared memory when it finishes, and the leader then imported it into its own instrumentation stack. From there it propagated into the leader's session totals (instr_top) and, for WAL, into pgWalUsage. This double counts worker activity once the session totals are reported to the cumulative statistics system, since each worker reports its own activity there as well: parallel workers already double reported their WAL in pg_stat_wal for utility statements like parallel vacuum, and for queries that cause pruning or update hint bits in a parallel scan. To fix, never accumulate worker activity into the leader's own counters. Instead, QueryInstrumentation gets a separate worker part, which the leader imports worker usage into: the executor into the query entry of the statement that launched the workers, parallel index builds and parallel vacuum into the innermost query entry on the stack. When a query entry is finalized, its worker part is passed on to the enclosing query entry only, never to a plain entry or to instr_top, so the session totals only ever describe the process' own activity. Consumers that want the complete picture, such as pg_stat_statements, EXPLAIN, and VACUUM and ANALYZE's verbose output, add the two up with InstrTotalUsage(). Statement-level instrumentation for utility statements, which pg_stat_statements previously measured with a plain entry around the whole command, needs to be a query entry for this to work. Introduce UtilityStmtDesc->instr: a consumer provides an entry set up with InstrQueryInitLocal(), and standard_ProcessUtility brackets the command with it. The consumer owns the memory, since a command may end the transaction and with it any resource owner or transaction-lifetime memory. The usage of workers outside of plan nodes, e.g. at startup, was attributed to the Gather node through the stack before. Keep that for EXPLAIN by remembering it on the Gather and Gather Merge nodes. Author: Lukas Fittl <lukas@fittl.com>
This is intended for testing instrumentation related logic as it pertains to the top level stack that is maintained as a running total. There is currently no in-core user that utilizes the top-level values in this manner, and especially during abort situations this helps ensure we don't lose activity due to incorrect handling of unfinalized node stacks.
pg_stat_database's blk_read_time and blk_write_time were accumulated in the backend-local counters pgStatBlockReadTime and pgStatBlockWriteTime, which pgstat_count_io_op_time() incremented alongside the equivalent fields of the current instrumentation stack entry. With the stack-based instrumentation the session-level totals are available in instr_top, so the separate counters are redundant. Remove the counters and the pgstat_count_buffer_read_time() and pgstat_count_buffer_write_time() macros, and instead have pgstat_update_dbstats() compute the increase of the shared and local block read/write times in instr_top since the previous report. This covers the same activity as before: relation and temp relation reads, writes and extends, but not temporary file I/O or WAL I/O. instr_top is only updated when an instrumentation stack entry is finalized, but pgstat_report_stat() is only called between transactions, when a backend is idle or at process exit. The stack is empty at the first two, but a FATAL error exits without unwinding the C stack and leaves entries behind, so finalize anything still on the stack before the final report. Parallel workers report their own activity themselves, like before, which requires each worker's instr_top to be complete. The switch to stack-based instrumentation left a gap, where per-node instrumentation does not accumulate into the query entry on the worker, to avoid double-counting in the leader import. Resolve by having the worker accumulate its nodes directly to its instr_top instead. The leader's instr_top never includes what it imported from workers, so each process' totals can be reported as they are. Since the amounts are now computed as a difference of the session totals, these totals must never be reset. Author: Lukas Fittl <lukas@fittl.com> Reviewed-by: Discussion: https://postgr.es/m/CAP53PkzdBK8VJ1fS4AZ481LgMN8f9mJiC39ZRHqkFUSYq6KWmg%40mail.gmail.com
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
No description provided.