The <filename>pg_stat_statements</filename> module provides a means for
tracking planning and execution statistics of all SQL statements executed by
a server.
pg_stat_statementsモジュールは、サーバで実行されたすべてのSQL文のプラン生成時と実行時の統計情報を記録する手段を提供します。
The module must be loaded by adding <literal>pg_stat_statements</literal> to
<xref linkend="guc-shared-preload-libraries"/> in
<filename>postgresql.conf</filename>, because it requires additional shared memory.
This means that a server restart is needed to add or remove the module.
In addition, query identifier calculation must be enabled in order for the
module to be active, which is done automatically if <xref linkend="guc-compute-query-id"/>
is set to <literal>auto</literal> or <literal>on</literal>, or any third-party
module that calculates query identifiers is loaded.
このモジュールは追加の共有メモリを必要とするため、postgresql.confのshared_preload_librariesにpg_stat_statementsを追加してモジュールをロードしなければなりません。
このことは、このモジュールを追加もしくは削除するには、サーバを再起動する必要があるということを意味しています。
さらに、モジュールをアクティブにするには、問い合わせIDの計算を有効にする必要があります。これはcompute_query_idパラメータがautoかonに設定されているか、問い合わせIDを計算するサードパーティのモジュールがロードされている場合に自動的に実行されます。
When <filename>pg_stat_statements</filename> is active, it tracks
statistics across all databases of the server. To access and manipulate
these statistics, the module provides views
<structname>pg_stat_statements</structname> and
<structname>pg_stat_statements_info</structname>,
and the utility functions <function>pg_stat_statements_reset</function> and
<function>pg_stat_statements</function>. These are not available globally but
can be enabled for a specific database with
<command>CREATE EXTENSION pg_stat_statements</command>.
pg_stat_statementsはアクティブになると、サーバのデータベース全体に渡って統計情報を記録します。
この統計情報にアクセスしたり操作したりするために、このモジュールはビューpg_stat_statementsとpg_stat_statements_infoとユーティリティ関数pg_stat_statements_reset、pg_stat_statementsを提供します。
これらは大域的に利用可能ではなく、CREATE EXTENSION pg_stat_statementsで特定のデータベースで可能になります。
pg_stat_statements ビュー #
The statistics gathered by the module are made available via a
view named <structname>pg_stat_statements</structname>. This view
contains one row for each distinct combination of database ID, user
ID, query ID and whether it's a top-level statement or not (up to
the maximum number of distinct statements that the module can track).
The columns of the view are shown in
<xref linkend="pgstatstatements-columns"/>.
このモジュールによって収集された統計情報は、pg_stat_statementsというビューを通して利用することができます。
このビューは、1行に対して、それぞれ個々のデータベースID、ユーザID、問い合わせID、および最上位レベルの文かどうかの組み合わせを含んでいます(モジュールが記録できるSQL文の最大数まで)。
ビューの列は、表 F.22に示す通りです。
表F.22 pg_stat_statementsの列
Column Type 列 型 Description 説明 |
|---|
OID of user who executed the statement SQL文を実行したユーザのOID |
OID of database in which the statement was executed SQL文が実行されたデータベースのOID |
True if the query was executed as a top-level statement
(always true if <varname>pg_stat_statements.track</varname> is set to
<literal>top</literal>)
問い合わせが最上位レベルのSQL文として実行された時は真
( |
Hash code to identify identical normalized queries. 同一の正規化された問い合わせを識別するためのハッシュコード |
Text of a representative statement 代表的なSQL文の文字列 |
Number of times the statement was planned
(if <varname>pg_stat_statements.track_planning</varname> is enabled,
otherwise zero)
SQL文がプラン生成された回数
( |
Total time spent planning the statement, in milliseconds
(if <varname>pg_stat_statements.track_planning</varname> is enabled,
otherwise zero)
SQL文のプラン生成に費やした総時間(ミリ秒単位)
( |
Minimum time spent planning the statement, in milliseconds.
This field will be zero if <varname>pg_stat_statements.track_planning</varname>
is disabled, or if the counter has been reset using the
<function>pg_stat_statements_reset</function> function with the
<structfield>minmax_only</structfield> parameter set to <literal>true</literal>
and never been planned since.
SQL文のプラン生成に費やした最小時間(ミリ秒単位)
このフィールドは、 |
Maximum time spent planning the statement, in milliseconds.
This field will be zero if <varname>pg_stat_statements.track_planning</varname>
is disabled, or if the counter has been reset using the
<function>pg_stat_statements_reset</function> function with the
<structfield>minmax_only</structfield> parameter set to <literal>true</literal>
and never been planned since.
SQL文のプラン生成に費やした最大時間(ミリ秒単位)
このフィールドは、 |
Mean time spent planning the statement, in milliseconds
(if <varname>pg_stat_statements.track_planning</varname> is enabled,
otherwise zero)
SQL文のプラン生成に費やした平均時間(ミリ秒単位)
( |
Population standard deviation of time spent planning the statement,
in milliseconds
(if <varname>pg_stat_statements.track_planning</varname> is enabled,
otherwise zero)
SQL文のプラン生成に費やした時間の母標準偏差(ミリ秒単位)
( |
Number of times the statement was executed SQL文が実行された回数 |
Total time spent executing the statement, in milliseconds SQL文の実行に費やした総時間(ミリ秒単位) |
Minimum time spent executing the statement, in milliseconds,
this field will be zero until this statement
is executed first time after reset performed by the
<function>pg_stat_statements_reset</function> function with the
<structfield>minmax_only</structfield> parameter set to <literal>true</literal>
SQL文の実行に費やした最小時間(ミリ秒単位)
このフィールドは、 |
Maximum time spent executing the statement, in milliseconds,
this field will be zero until this statement
is executed first time after reset performed by the
<function>pg_stat_statements_reset</function> function with the
<structfield>minmax_only</structfield> parameter set to <literal>true</literal>
SQL文の実行に費やした最大時間(ミリ秒単位)
このフィールドは、 |
Mean time spent executing the statement, in milliseconds SQL文の実行に費やした平均時間(ミリ秒単位) |
Population standard deviation of time spent executing the statement, in milliseconds SQL文の実行に費やした時間の母標準偏差(ミリ秒単位) |
Total number of rows retrieved or affected by the statement SQL文によって取得された、あるいは影響を受けた行の総数 |
Total number of shared block cache hits by the statement SQL文によってヒットした共有ブロックキャッシュの総数 |
Total number of shared blocks read by the statement SQL文によって読み込まれた共有ブロックの総数 |
Total number of shared blocks dirtied by the statement SQL文によってダーティ状態となった共有ブロックの総数 |
Total number of shared blocks written by the statement SQL文によって書き込まれた共有ブロックの総数 |
Total number of local block cache hits by the statement SQL文によってヒットしたローカルブロックキャッシュの総数 |
Total number of local blocks read by the statement SQL文によって読み込まれたローカルブロックの総数 |
Total number of local blocks dirtied by the statement SQL文によってダーティ状態となったローカルブロックの総数 |
Total number of local blocks written by the statement SQL文によって書き込まれたローカルブロックの総数 |
Total number of temp blocks read by the statement SQL文によって読み込まれた一時ブロックの総数 |
Total number of temp blocks written by the statement SQL文によって書き込まれた一時ブロックの総数 |
Total time the statement spent reading shared blocks, in milliseconds (if <xref linkend="guc-track-io-timing"/> is enabled, otherwise zero) SQL文が共有ブロックの読み取りに費やした総時間(ミリ秒単位) (track_io_timingが有効な場合。無効であればゼロ) |
Total time the statement spent writing shared blocks, in milliseconds (if <xref linkend="guc-track-io-timing"/> is enabled, otherwise zero) SQL文が共有ブロックの書き出しに費やした総時間(ミリ秒単位) (track_io_timingが有効な場合。無効であればゼロ) |
Total time the statement spent reading local blocks, in milliseconds (if <xref linkend="guc-track-io-timing"/> is enabled, otherwise zero) SQL文がローカルブロックの読み取りに費やした総時間(ミリ秒単位) (track_io_timingが有効な場合。無効であればゼロ) |
Total time the statement spent writing local blocks, in milliseconds (if <xref linkend="guc-track-io-timing"/> is enabled, otherwise zero) SQL文がローカルブロックの書き出しに費やした総時間(ミリ秒単位) (track_io_timingが有効な場合。無効であればゼロ) |
Total time the statement spent reading temporary file blocks, in milliseconds (if <xref linkend="guc-track-io-timing"/> is enabled, otherwise zero) SQL文が一時ファイルブロックの読み取りに費やした総時間(ミリ秒単位) (track_io_timingが有効な場合。無効であればゼロ) |
Total time the statement spent writing temporary file blocks, in milliseconds (if <xref linkend="guc-track-io-timing"/> is enabled, otherwise zero) SQL文が一時ファイルブロックの書き出しに費やした総時間(ミリ秒単位) (track_io_timingが有効な場合。無効であればゼロ) |
Total number of WAL records generated by the statement SQL文により生成されたWALレコードの総数 |
Total number of WAL full page images generated by the statement SQL文により生成されたWALフルページイメージの総数 |
Total amount of WAL generated by the statement in bytes SQL文により生成されたバイト単位のWAL総量 |
Number of times the WAL buffers became full 《機械翻訳》WALバッファが一杯になった回数です。 |
Total number of functions JIT-compiled by the statement SQL文がJITコンパイルされた関数の総数 |
Total time spent by the statement on generating JIT code, in milliseconds SQL文のJITコードの生成に費やした総時間(ミリ秒単位) |
Number of times functions have been inlined 関数がインライン化された回数 |
Total time spent by the statement on inlining functions, in milliseconds SQL文が関数のインライン化に費やした総時間(ミリ秒単位) |
Number of times the statement has been optimized SQL文が最適化された回数 |
Total time spent by the statement on optimizing, in milliseconds SQL文の最適化に費やした総時間(ミリ秒単位) |
Number of times code has been emitted コードが出力された回数 |
Total time spent by the statement on emitting code, in milliseconds SQL文のコードを出力するのに費やした総時間(ミリ秒単位) |
Total number of tuple deform functions JIT-compiled by the statement SQL文がJITコンパイルされたタプルデフォーム関数の総数 |
Total time spent by the statement on JIT-compiling tuple deform functions, in milliseconds SQL文がJITコンパイルによる関数のタプルデフォームに費やした総時間(ミリ秒単位) |
Number of parallel workers planned to be launched 《機械翻訳》起動が計画されているパラレルワーカーの数。 |
Number of parallel workers actually launched 《機械翻訳》実際に起動されたパラレルワーカーの数。 |
Time at which statistics gathering started for this statement SQL文の統計情報収集が開始された時刻 |
Time at which min/max statistics gathering started for this
statement (fields <structfield>min_plan_time</structfield>,
<structfield>max_plan_time</structfield>,
<structfield>min_exec_time</structfield> and
<structfield>max_exec_time</structfield>)
SQL文の最小/最大の統計情報収集が開始された時刻( |
For security reasons, only superusers and roles with privileges of the
<literal>pg_read_all_stats</literal> role are allowed to see the SQL text and
<structfield>queryid</structfield> of queries executed by other users.
Other users can see the statistics, however, if the view has been installed
in their database.
セキュリティ上の理由から、スーパーユーザとpg_read_all_statsロールの権限を持つメンバだけが、他のユーザによって実行されたSQLテキストや問い合わせのqueryidを見ることができます。
ただし、ユーザのデータベースにビューがインストールされている場合、統計情報については他のユーザから見ることができます。
Plannable queries (that is, <command>SELECT</command>, <command>INSERT</command>,
<command>UPDATE</command>, <command>DELETE</command>, and <command>MERGE</command>)
and utility commands are combined into a single
<structname>pg_stat_statements</structname> entry whenever they have identical query
structures according to an internal hash calculation. Typically, two
queries will be considered the same for this purpose if they are
semantically equivalent except for the values of literal constants
appearing in the query.
計画作成が可能な問い合わせ(つまりSELECT、INSERT、UPDATE、DELETE、 MERGE)とユーティリティコマンドは、内部のハッシュ計算に従った、同一の問い合わせ構造を持つ限り、1つのpg_stat_statements項目に組み合わせられます。
典型的には、2つの問い合わせは、問い合わせの中に現れるリテラル定数の値以外、意味的に等価である場合、この目的では同一とみなされます。
The following details about constant replacement and
<structfield>queryid</structfield> only apply when <xref
linkend="guc-compute-query-id"/> is enabled. If you use an external
module instead to compute <structfield>queryid</structfield>, you
should refer to its documentation for details.
定数置換とqueryidに関する以下の詳細は、compute_query_idが有効な場合のみ適用されます。
queryidの計算に外部モジュールを使用する場合、詳細はその外部モジュールのドキュメントを参照する必要があります。
When a constant's value has been ignored for purposes of matching the query
to other queries, the constant is replaced by a parameter symbol, such
as <literal>$1</literal>, in the <structname>pg_stat_statements</structname>
display.
The rest of the query text is that of the first query that had the
particular <structfield>queryid</structfield> hash value associated with the
<structname>pg_stat_statements</structname> entry.
他の問い合わせと合致させるために定数値が無視された場合、pg_stat_statementsの表示の中で定数は$1のようなパラメータ記号に置換されます。
問い合わせの残りのテキストは、pg_stat_statements項目に関連付いた特定のqueryidハッシュ値を持つ、1つ目の問い合わせのテキストです。
Queries on which normalization can be applied may be observed with constant
values in <structname>pg_stat_statements</structname>, especially when there
is a high rate of entry deallocations. To reduce the likelihood of this
happening, consider increasing <varname>pg_stat_statements.max</varname>.
The <structname>pg_stat_statements_info</structname> view, discussed below
in <xref linkend="pgstatstatements-pg-stat-statements-info"/>,
provides statistics about entry deallocations.
正規化が適用できる問い合わせは、pg_stat_statements内で一定の値で観察されるでしょう。 特に、エントリの割り当て解除率が高い場合にそうです。
このような事態の発生を減らすためには、pg_stat_statements.maxを増やすことを検討してください。
pg_stat_statements_infoビューは、後述のF.32.2で説明しますが、エントリの割り当て解除に関する統計情報を提供します。
In some cases, queries with visibly different texts might get merged into a
single <structname>pg_stat_statements</structname> entry; as explained above,
this is expected to happen for semantically equivalent queries.
In addition, if the only difference between queries is the number of elements
in a list of constants, the list will get squashed down to a single element but shown
with a commented-out list indicator:
《マッチ度[50.717703]》一部の状況では、見た目上異なるテキストを持つ問い合わせが1つのpg_stat_statements項目にまとめられることがあります。
通常これは意味的に等しい問い合わせでのみ発生しますが、関連がない問い合わせが1つの項目にまとめられるハッシュ競合の可能性がわずかながら存在します。
(しかしこれは別のユーザまたは別のデータベースに属する問い合わせでは発生することはあり得ません。)
《機械翻訳》場合によっては、明らかに異なるテキストを持つ問い合わせが単一のpg_stat_statementsエントリ;上で説明したように、これは意味的に等価な問い合わせに対して発生することが予想されます。
さらに、問い合わせ間の唯一の差が定数のリストの要素数である場合、リストは単一の要素に圧縮されますが、リスト指示子:はコメントアウトされて表示されます。
ダウン
=# SELECT pg_stat_statements_reset(); =# SELECT * FROM test WHERE a IN (1, 2, 3, 4, 5, 6, 7); =# SELECT * FROM test WHERE a IN (1, 2, 3, 4, 5, 6, 7, 8); =# SELECT query, calls FROM pg_stat_statements WHERE query LIKE 'SELECT%'; -[ RECORD 1 ]------------------------------ query | SELECT * FROM test WHERE a IN ($1 /*, ... */) calls | 2
In addition to these cases, there is a small chance of hash collisions causing unrelated queries to be merged into one entry. (This cannot happen for queries belonging to different users or databases, however.) 《機械翻訳》これらの場合に加えて、ハッシュの衝突により無関係な問い合わせが1つのエントリにマージされる可能性がわずかにあります(しかし、これは異なるユーザやデータベースに属する問い合わせでは起こり得ません)。
Since the <structfield>queryid</structfield> hash value is computed on the
post-parse-analysis representation of the queries, the opposite is
also possible: queries with identical texts might appear as
separate entries, if they have different meanings as a result of
factors such as different <varname>search_path</varname> settings.
queryidハッシュ値は問い合わせの解析後の表現に対して計算されますので、search_pathの設定が異なる等の要因の結果として異なる意味を持つ場合、同じテキストを持つ問い合わせが別の項目として現れるという、反対もまたあり得ます。
Consumers of <structname>pg_stat_statements</structname> may wish to use
<structfield>queryid</structfield> (perhaps in combination with
<structfield>dbid</structfield> and <structfield>userid</structfield>) as a more stable
and reliable identifier for each entry than its query text.
However, it is important to understand that there are only limited
guarantees around the stability of the <structfield>queryid</structfield> hash
value. Since the identifier is derived from the
post-parse-analysis tree, its value is a function of, among other
things, the internal object identifiers appearing in this representation.
This has some counterintuitive implications. For example,
<filename>pg_stat_statements</filename> will consider two apparently-identical
queries to be distinct, if they reference for example a function that was
dropped and recreated between the executions of the two queries.
Conversely, if a table is dropped and recreated between the
executions of queries, two apparently-identical queries may be
considered the same. However, if the alias for a table is different
for otherwise-similar queries, these queries will be considered
distinct.
The hashing process is also sensitive to differences in
machine architecture and other facets of the platform.
Furthermore, it is not safe to assume that <structfield>queryid</structfield>
will be stable across major versions of <productname>PostgreSQL</productname>.
《マッチ度[77.014716]》pg_stat_statementsの消費者は、問い合わせテキストよりもより安定で信頼できる各項目への識別子として(おそらくdbidやuseridと組み合わせて)queryidを使いたいかもしれません。
しかし、queryidハッシュ値の安定性には限定された保証しかないのを理解することは重要です。
識別子は解析後の木から得られますので、その値は、とりわけ、この表現に現れる内部オブジェクト識別子の関数です。
これは少々直観に反する結果です。
例えば、pg_stat_statementsは見たところは同一の問い合わせを、それらが2つの問い合わせの実行の間に削除され再作成されたテーブルを参照しているのであれば、別個のものとみなします。
ハッシュ処理はプラットフォームのマシンアーキテクチャやその他の面の違いにも敏感です。
その上、PostgreSQLのメジャーバージョンをまたがってqueryidが安定であるとみなすのは安全ではありません。
《機械翻訳》pg_stat_statementsのコンシューマは、queryidをおそらくdbidやuseridと組み合わせて各安定(stable)の識別子よりもエントリに対してより信頼できる問い合わせテキストとして使用したいと思うかもしれません。
しかし、queryidハッシュの値の安定に関しては限られた保証しかないことを理解することが重要です。
識別子は解析解析後のツリーから派生するので、その値は他のものと共に、この表現形式に現れる内部オブジェクト識別子の関数になります。
これは直感に反する意味合いを持ちます。
例にとってpg_stat_statementsは、例のリファレンスが2つの問い合わせの実行の間に削除され再作成された関数である場合、見かけ上同一の2つの問い合わせは異なるものとみなします。
逆に、問い合わせの実行の間にテーブルが削除され再作成された場合、見かけ上同一の2つの問い合わせは同じものとみなされる可能性があります。
しかし、他の点では類似している問い合わせに対してテーブルの別名が異なる場合、これらの問い合わせは異なるものとみなされます。
ハッシュのプロセスはプラットフォームのマシンアーキテクチャや他の面の違いにも敏感です。
さらに、PostgreSQLのメジャーバージョン間でqueryidが安定(stable)になると仮定するのはセーフではありません。
Two servers participating in replication based on physical WAL replay can
be expected to have identical <structfield>queryid</structfield> values for
the same query. However, logical replication schemes do not promise to
keep replicas identical in all relevant details, so
<structfield>queryid</structfield> will not be a useful identifier for
accumulating costs across a set of logical replicas.
If in doubt, direct testing is recommended.
物理WAL再生に基づくレプリケーションに参加する2つのサーバでは、同じ問い合わせに対して同一のqueryid値を持つことが期待できます。
しかし、論理レプリケーションの仕組みは、レプリカが対応する詳細すべてで同一であることを約束しません。そのため、論理レプリカの集まりで増えるコストを識別するのにqueryidは有用な識別子ではないでしょう。
疑わしければ、直接テストすることを薦めます。
Generally, it can be assumed that <structfield>queryid</structfield> values
are stable between minor version releases of <productname>PostgreSQL</productname>,
providing that instances are running on the same machine architecture and
the catalog metadata details match. Compatibility will only be broken
between minor versions as a last resort.
一般に、queryid値は、インスタンスが同じマシンアーキテクチャで動作し、カタログメタデータの詳細が一致する場合、PostgreSQLのマイナーバージョンリリース間で安定していると仮定できます。
マイナーバージョン間で互換性が破壊されるのは、最後の手段としてのみでしょう。
The parameter symbols used to replace constants in
representative query texts start from the next number after the
highest <literal>$</literal><replaceable>n</replaceable> parameter in the original query
text, or <literal>$1</literal> if there was none. It's worth noting that in
some cases there may be hidden parameter symbols that affect this
numbering. For example, <application>PL/pgSQL</application> uses hidden parameter
symbols to insert values of function local variables into queries, so that
a <application>PL/pgSQL</application> statement like <literal>SELECT i + 1 INTO j</literal>
would have representative text like <literal>SELECT i + $2</literal>.
代表的な問い合わせテキストの定数を置き換えるのに使われたパラメータ記号は、元の問い合わせテキストの最も大きな$nパラメータの次の数字から、もしそれがなければ$1から始まります。
ある場合には、この番号付けに影響する隠れたパラメータ記号があるかもしれないことに言及しておく価値はあります。
例えば、PL/pgSQLは関数の局所変数の値を問い合わせに挿入するために隠れたパラメータ記号を使います。そのため、SELECT i + 1 INTO jのようなPL/pgSQL文はSELECT i + $2のような代表的なテキストになります。
The representative query texts are kept in an external disk file, and do
not consume shared memory. Therefore, even very lengthy query texts can
be stored successfully. However, if many long query texts are
accumulated, the external file might grow unmanageably large. As a
recovery method if that happens, <filename>pg_stat_statements</filename> may
choose to discard the query texts, whereupon all existing entries in
the <structname>pg_stat_statements</structname> view will show
null <structfield>query</structfield> fields, though the statistics associated with
each <structfield>queryid</structfield> are preserved. If this happens, consider
reducing <varname>pg_stat_statements.max</varname> to prevent
recurrences.
代表的な問い合わせテキストは外部ディスクファイルに保持され、共有メモリを消費しません。
そのため、非常に長い問い合わせテキストであっても保存に成功します。
しかし、数多くの長い問い合わせテキストが蓄積されると、外部ファイルは手に負えないくらい大きくなるかもしれません。
もしそのようなことが起きれば、回復手法として、pg_stat_statementsは問い合わせテキストを破棄することを選ぶでしょう。その結果、各queryidに関連する統計は保存されるものの、pg_stat_statementsビュー内に存在するエントリはすべてqueryフィールドがヌルになります。
もしこのようなことが起きたら、再発防止のためpg_stat_statements.maxを減らすことを検討してください。
<structfield>plans</structfield> and <structfield>calls</structfield> aren't
always expected to match because planning and execution statistics are
updated at their respective end phase, and only for successful operations.
For example, if a statement is successfully planned but fails during
the execution phase, only its planning statistics will be updated.
If planning is skipped because a cached plan is used, only its execution
statistics will be updated.
プラン生成時と実行時の統計情報はそれぞれの終了フェーズで更新され、操作に成功した場合にのみ更新されるため、plansとcallsは常に一致するとは限りません。
例えば、SQL文のプラン生成に成功しても実行フェーズの間に失敗した場合、そのプラン生成時の統計情報のみが更新されます。
キャッシュされたプランが使用されプラン生成がスキップされた場合、実行時の統計情報のみが更新されます。
pg_stat_statements_infoビュー #
The statistics of the <filename>pg_stat_statements</filename> module
itself are tracked and made available via a view named
<structname>pg_stat_statements_info</structname>. This view contains
only a single row. The columns of the view are shown in
<xref linkend="pgstatstatementsinfo-columns"/>.
pg_stat_statementsモジュール自体の統計は記録され、pg_stat_statements_infoというビューを通して利用することができます。
このビューは、1行のみが含まれます。
ビューの列は、表 F.23に示す通りです。
表F.23 pg_stat_statements_infoの列
Column Type 列 型 Description 説明 |
|---|
Total number of times <structname>pg_stat_statements</structname>
entries about the least-executed statements were deallocated
because more distinct statements than
<varname>pg_stat_statements.max</varname> were observed
|
Time at which all statistics in the
<structname>pg_stat_statements</structname> view were last reset.
|
pg_stat_statements_reset(userid Oid, dbid Oid, queryid
bigint, minmax_only boolean) returns timestamp with time zone
<function>pg_stat_statements_reset</function> discards statistics
gathered so far by <filename>pg_stat_statements</filename> corresponding
to the specified <structfield>userid</structfield>, <structfield>dbid</structfield>
and <structfield>queryid</structfield>. If any of the parameters are not
specified, the default value <literal>0</literal>(invalid) is used for
each of them and the statistics that match with other parameters will be
reset. If no parameter is specified or all the specified parameters are
<literal>0</literal>(invalid), it will discard all statistics.
If all statistics in the <filename>pg_stat_statements</filename>
view are discarded, it will also reset the statistics in the
<structname>pg_stat_statements_info</structname> view.
When <structfield>minmax_only</structfield> is <literal>true</literal> only the
values of minimum and maximum planning and execution time will be reset (i.e.
<structfield>min_plan_time</structfield>, <structfield>max_plan_time</structfield>,
<structfield>min_exec_time</structfield> and <structfield>max_exec_time</structfield>
fields). The default value for <structfield>minmax_only</structfield> parameter is
<literal>false</literal>. Time of last min/max reset performed is shown in
<structfield>minmax_stats_since</structfield> field of the
<structname>pg_stat_statements</structname> view.
This function returns the time of a reset. This time is saved to
<structfield>stats_reset</structfield> field of
<structname>pg_stat_statements_info</structname> view or to
<structfield>minmax_stats_since</structfield> field of the
<structname>pg_stat_statements</structname> view if the corresponding reset was
actually performed.
By default, this function can only be executed by superusers.
Access may be granted to others using <command>GRANT</command>.
pg_stat_statements_resetは指定されたuserid、dbid、queryidに対応するpg_stat_statementsによってこれまでに収集したすべての統計情報を削除します。
いずれかのパラメータを指定しないのであれば、デフォルト値0(無効)を使ってください。他のパラメータに一致する統計情報がリセットされます。
どのパラメータも指定しない、または、すべての指定されたパラメータが0(無効)ならば、すべての統計情報を削除します。
pg_stat_statementsビューのすべての統計情報が破棄された場合、pg_stat_statements_infoビューの統計情報もリセットされます。
minmax_onlyがtrueの場合、最小および最大のプラン生成・実行時間の値のみリセットされます。(min_plan_time、max_plan_time、min_exec_time、max_exec_time フィールド)。
minmax_onlyパラメータのデフォルト値はfalseです。
最後に実行された最小/最大のリセット時刻は、pg_stat_statementsビューのminmax_stats_sinceフィールドに表示されます。
この関数は、リセット時刻を返します。
対応するリセットが実際に実行された場合、この時刻はpg_stat_statements_infoビューのminmax_stats_sinceフィールド、またはpg_stat_statementsビューのminmax_stats_sinceフィールドに保存されます。
デフォルトでは、この関数はスーパーユーザのみ実行できます。
GRANTを使ってアクセス権を他のユーザに付与できます。
pg_stat_statements(showtext boolean) returns setof record
The <structname>pg_stat_statements</structname> view is defined in
terms of a function also named <function>pg_stat_statements</function>.
It is possible for clients to call
the <function>pg_stat_statements</function> function directly, and by
specifying <literal>showtext := false</literal> have query text be
omitted (that is, the <literal>OUT</literal> argument that corresponds
to the view's <structfield>query</structfield> column will return nulls). This
feature is intended to support external tools that might wish to avoid
the overhead of repeatedly retrieving query texts of indeterminate
length. Such tools can instead cache the first query text observed
for each entry themselves, since that is
all <filename>pg_stat_statements</filename> itself does, and then retrieve
query texts only as needed. Since the server stores query texts in a
file, this approach may reduce physical I/O for repeated examination
of the <structname>pg_stat_statements</structname> data.
pg_stat_statementsビューは同じくpg_stat_statementsという名前の関数の項で定義されています。
クライアントがpg_stat_statements関数を直接呼び出し、showtext := falseと指定することで問い合わせテキストを省略することが可能です(すなわち、ビューのquery列に対応するOUT引数はNULLを返します)。
この機能は不定長の問い合わせテキストを繰り返し取得するオーバーヘッドを避けたいと考える外部のツールをサポートすることを意図したものです。
そのようなツールは代わりに、それがpg_stat_statements自身が行なっていることのすべてですので、各項目で最初に観察された問い合わせテキストをキャッシュし、必要とされる問い合わせテキストのみを取得できます。
サーバは問い合わせテキストをファイルに格納しますので、この方法はpg_stat_statementsデータの繰り返しの検査に対する物理I/Oを減らすでしょう。
pg_stat_statements.max (integer)
<varname>pg_stat_statements.max</varname> is the maximum number of
statements tracked by the module (i.e., the maximum number of rows
in the <structname>pg_stat_statements</structname> view). If more distinct
statements than that are observed, information about the least-executed
statements is discarded. The number of times such information was
discarded can be seen in the
<structname>pg_stat_statements_info</structname> view.
The default value is 5000.
This parameter can only be set at server start.
pg_stat_statements.maxは、このモジュールによって記録されるSQL文の最大数(すなわち、pg_stat_statementsビューにおける行の最大数)です。
これを超えて異なるSQL文を検出した場合は、最も実行回数の低いSQL文の情報が捨てられます。
このような情報が破棄された回数は、pg_stat_statements_infoビューで確認できます。
デフォルトは5000です。
このパラメータはサーバの起動時にのみ指定できます。
pg_stat_statements.track (enum)
<varname>pg_stat_statements.track</varname> controls which statements
are counted by the module.
Specify <literal>top</literal> to track top-level statements (those issued
directly by clients), <literal>all</literal> to also track nested statements
(such as statements invoked within functions), or <literal>none</literal> to
disable statement statistics collection.
The default value is <literal>top</literal>.
Only superusers can change this setting.
pg_stat_statements.trackは、どのSQL文をモジュールによって計測するかを制御します。
topを指定した場合は(直接クライアントによって発行された)最上層のSQL文を記録します。
allは(関数の中から呼び出された文などの)入れ子になった文も記録します。
noneは文に関する統計情報収集を無効にします。
デフォルトはtopです。
この設定はスーパーユーザだけが変更できます。
pg_stat_statements.track_utility (boolean)
<varname>pg_stat_statements.track_utility</varname> controls whether
utility commands are tracked by the module. Utility commands are
all those other than <command>SELECT</command>, <command>INSERT</command>,
<command>UPDATE</command>, <command>DELETE</command>, and <command>MERGE</command>.
The default value is <literal>on</literal>.
Only superusers can change this setting.
pg_stat_statements.track_utilityは、このモジュールがユーティリティコマンドを記録するかどうかを指定します。
ユーティリティコマンドとは、 SELECT、INSERT、UPDATE、DELETE、およびMERGE以外のすべてです。
デフォルトはonです。
この設定はスーパーユーザのみが変更できます。
pg_stat_statements.track_planning (boolean)
<varname>pg_stat_statements.track_planning</varname> controls whether
planning operations and duration are tracked by the module.
Enabling this parameter may incur a noticeable performance penalty,
especially when statements with identical query structure are executed
by many concurrent connections which compete to update a small number of
<structname>pg_stat_statements</structname> entries.
The default value is <literal>off</literal>.
Only superusers can change this setting.
pg_stat_statements.track_planningは、このモジュールがプラン生成の操作と時間を記録するかどうかを指定します。
このパラメータを有効にすると、特に同一の問い合わせ構造を持つSQL文が、少数のpg_stat_statementsエントリ更新のために競合する多数の同時接続によって実行される場合にパフォーマンスが著しく低下する可能性があります。
デフォルトはoffです。
この設定はスーパーユーザのみが変更できます。
pg_stat_statements.save (boolean)
<varname>pg_stat_statements.save</varname> specifies whether to
save statement statistics across server shutdowns.
If it is <literal>off</literal> then statistics are not saved at
shutdown nor reloaded at server start.
The default value is <literal>on</literal>.
This parameter can only be set in the <filename>postgresql.conf</filename>
file or on the server command line.
pg_stat_statements.saveは、サーバを終了させる際に文の統計情報を保存するかどうかを指定します。
offの場合、統計情報は終了時に保存されず、サーバ開始時に再読み込みもされません。
デフォルト値はonです。
このパラメータはpostgresql.confファイル、またはサーバコマンドラインでのみ設定できます。
The module requires additional shared memory proportional to
<varname>pg_stat_statements.max</varname>. Note that this
memory is consumed whenever the module is loaded, even if
<varname>pg_stat_statements.track</varname> is set to <literal>none</literal>.
このモジュールは、pg_stat_statements.maxに比例する追加の共有メモリを必要とします。
pg_stat_statements.trackにnoneが設定されていても、モジュールがロードされている限り常にこのメモリが消費されることに注意してください。
These parameters must be set in <filename>postgresql.conf</filename>.
Typical usage might be:
これらのパラメータはpostgresql.confの中で設定しなければなりません。
典型的な使用方法は以下のようになります。
# postgresql.conf shared_preload_libraries = 'pg_stat_statements' compute_query_id = on pg_stat_statements.max = 10000 pg_stat_statements.track = all
bench=# SELECT pg_stat_statements_reset();
$ pgbench -i bench
$ pgbench -c10 -t300 bench
bench=# \x
bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2
calls | 3000
total_exec_time | 25565.855387
rows | 3000
hit_percent | 100.0000000000000000
-[ RECORD 2 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2
calls | 3000
total_exec_time | 20756.669379
rows | 3000
hit_percent | 100.0000000000000000
-[ RECORD 3 ]---+--------------------------------------------------------------------
query | copy pgbench_accounts from stdin
calls | 1
total_exec_time | 291.865911
rows | 100000
hit_percent | 100.0000000000000000
-[ RECORD 4 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2
calls | 3000
total_exec_time | 271.232977
rows | 3000
hit_percent | 98.8454011741682975
-[ RECORD 5 ]---+--------------------------------------------------------------------
query | alter table pgbench_accounts add primary key (aid)
calls | 1
total_exec_time | 160.588563
rows | 0
hit_percent | 100.0000000000000000
bench=# SELECT pg_stat_statements_reset(0,0,s.queryid) FROM pg_stat_statements AS s
WHERE s.query = 'UPDATE pgbench_branches SET bbalance = bbalance + $1 WHERE bid = $2';
bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_tellers SET tbalance = tbalance + $1 WHERE tid = $2
calls | 3000
total_exec_time | 20756.669379
rows | 3000
hit_percent | 100.0000000000000000
-[ RECORD 2 ]---+--------------------------------------------------------------------
query | copy pgbench_accounts from stdin
calls | 1
total_exec_time | 291.865911
rows | 100000
hit_percent | 100.0000000000000000
-[ RECORD 3 ]---+--------------------------------------------------------------------
query | UPDATE pgbench_accounts SET abalance = abalance + $1 WHERE aid = $2
calls | 3000
total_exec_time | 271.232977
rows | 3000
hit_percent | 98.8454011741682975
-[ RECORD 4 ]---+--------------------------------------------------------------------
query | alter table pgbench_accounts add primary key (aid)
calls | 1
total_exec_time | 160.588563
rows | 0
hit_percent | 100.0000000000000000
-[ RECORD 5 ]---+--------------------------------------------------------------------
query | vacuum analyze pgbench_accounts
calls | 1
total_exec_time | 136.448116
rows | 0
hit_percent | 99.9201915403032721
bench=# SELECT pg_stat_statements_reset(0,0,0);
bench=# SELECT query, calls, total_exec_time, rows, 100.0 * shared_blks_hit /
nullif(shared_blks_hit + shared_blks_read, 0) AS hit_percent
FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT 5;
-[ RECORD 1 ]---+-----------------------------------------------------------------------------
query | SELECT pg_stat_statements_reset(0,0,0)
calls | 1
total_exec_time | 0.189497
rows | 1
hit_percent |
-[ RECORD 2 ]---+-----------------------------------------------------------------------------
query | SELECT query, calls, total_exec_time, rows, $1 * shared_blks_hit / +
| nullif(shared_blks_hit + shared_blks_read, $2) AS hit_percent+
| FROM pg_stat_statements ORDER BY total_exec_time DESC LIMIT $3
calls | 0
total_exec_time | 0
rows | 0
hit_percent |
Takahiro Itagaki <email>itagaki.takahiro@oss.ntt.co.jp</email>.
Query normalization added by Peter Geoghegan <email>peter@2ndquadrant.com</email>.
板垣 貴裕 <itagaki.takahiro@oss.ntt.co.jp>。
Peter Geoghegan <peter@2ndquadrant.com>により問い合わせの正規化が追加されました。