F.3. auto_explain
Модуль auto_explain
предоставляет возможность автоматического протоколирования планов выполнения медленных операторов, что позволяет обойтись без выполнения EXPLAIN вручную. Это особенно полезно для выявления неоптимизированных запросов в больших приложениях.
Этот модуль не предоставляет функций, доступных из SQL. Чтобы использовать его, просто загрузите его в процесс сервера. Это можно сделать в отдельном сеансе:
LOAD 'auto_explain';
(Для этого нужно быть суперпользователем.) Более типична конфигурация, когда он загружается в некоторые или все сеансы в результате включения auto_explain
в переменную session_preload_libraries или в shared_preload_libraries в файле postgresql.conf
. Загрузив этот модуль, вы можете отслеживать исключительно медленные запросы, вне зависимости от того, когда они происходят. Конечно, это имеет свою цену.
F.3.1. Параметры конфигурации
Есть несколько параметров конфигурации, которые управляют поведением auto_explain
. Заметьте, что поведение по умолчанию сводится к бездействию, так что необходимо установить как минимум переменную auto_explain.log_min_duration
, если вы хотите получить какие-либо результаты.
auto_explain.log_min_duration
(integer
)Переменная
auto_explain.log_min_duration
задаёт время выполнения оператора, в миллисекундах, при превышении которого план оператора будет протоколироваться. Если это значение равно 0, протоколироваться будут планы всех операторов. При значении -1 (по умолчанию) протоколирование планов полностью отключается. Например, если вы установите значение250ms
, протоколироваться будут все запросы, выполняющиеся 250 мс и дольше. Изменить этот параметр могут только суперпользователи.auto_explain.log_analyze
(boolean
)При включении параметра
auto_explain.log_analyze
в протокол будет записываться вывод командыEXPLAIN ANALYZE
, а не простойEXPLAIN
. По умолчанию этот параметр отключён. Изменить его могут только суперпользователи.Примечание
Когда этот параметр включён, замер времени на уровне узлов плана производится для всех операторов, даже если они выполняются недостаточно долго для протоколирования. Это может оказать крайне негативное влияние на производительность. Отключение
auto_explain.log_timing
исключает это влияние, но при этом собирается меньше информации.auto_explain.log_buffers
(boolean
)Параметр
auto_explain.log_buffers
определяет, будет ли при протоколировании плана выполнения выводиться статистика об использовании буферов; он равносилен указаниюBUFFERS
командыEXPLAIN
. Этот параметр действует, только если включён параметрauto_explain.log_analyze
. По умолчанию этот параметр отключён. Изменить его могут только суперпользователи.auto_explain.log_timing
(boolean
)Параметр
auto_explain.log_timing
определяет, будет ли при протоколировании плана выполнения выводиться длительность на уровне узлов: он равнозначен указаниюTIMING
командыEXPLAIN
. Издержки от постоянного чтения системных часов могут значительно замедлить запросы в некоторых системах, так что может иметь смысл отключать этот параметр, когда нужно знать только знать количество строк, но не точную длительность каждого узла. Этот параметр действует, только если включёнauto_explain.log_analyze
. По умолчанию этот параметр отключён. Изменить его могут только суперпользователи.auto_explain.log_triggers
(boolean
)При включении параметра
auto_explain.log_triggers
в протокол будет записываться статистика выполнения триггеров. Этот параметр действует, только если включён параметрauto_explain.log_analyze
. По умолчанию этот параметр отключён. Изменить его могут только суперпользователи.auto_explain.log_verbose
(boolean
)Параметр
auto_explain.log_verbose
определяет, будут ли при протоколировании плана выполнения выводиться подробные сведения; он равнозначен указаниюVERBOSE
командыEXPLAIN
. По умолчанию этот параметр отключён. Изменить его могут только суперпользователи.auto_explain.log_format
(enum
)Параметр
auto_explain.log_format
выбирает формат вывода дляEXPLAIN
. Он может принимать значениеtext
,xml
,json
иyaml
. Значение по умолчанию — text. Изменить этот параметр могут только суперпользователи.auto_explain.log_nested_statements
(boolean
)При включении параметра
auto_explain.log_nested_statements
протоколированию могут подлежать и вложенные операторы (операторы, выполняемые внутри функции). Когда он отключён, протоколируются планы запросов только верхнего уровня. Изменить этот параметр могут только суперпользователи.auto_explain.sample_rate
(real
)Параметр
auto_explain.sample_rate
задаёт для auto_explain процент операторов, которые будут отслеживаться в каждом сеансе. Значение по умолчанию — 1, то есть отслеживаются все запросы. Вложенные операторы отслеживаются совместно — либо все, либо никакой из них. Изменить этот параметр могут только суперпользователи.
В обычной ситуации эти параметры устанавливаются в postgresql.conf
, хотя суперпользователи могут изменить их «на лету» в рамках своих сеансов. Типичное их использование может выглядеть так:
# postgresql.conf session_preload_libraries = 'auto_explain' auto_explain.log_min_duration = '3s'
F.3.2. Пример
postgres=# LOAD 'auto_explain'; postgres=# SET auto_explain.log_min_duration = 0; postgres=# SET auto_explain.log_analyze = true; postgres=# SELECT count(*) FROM pg_class, pg_index WHERE oid = indrelid AND indisunique;
В результате этих команд может быть получен такой вывод:
LOG: duration: 3.651 ms plan: Query Text: SELECT count(*) FROM pg_class, pg_index WHERE oid = indrelid AND indisunique; Aggregate (cost=16.79..16.80 rows=1 width=0) (actual time=3.626..3.627 rows=1 loops=1) -> Hash Join (cost=4.17..16.55 rows=92 width=0) (actual time=3.349..3.594 rows=92 loops=1) Hash Cond: (pg_class.oid = pg_index.indrelid) -> Seq Scan on pg_class (cost=0.00..9.55 rows=255 width=4) (actual time=0.016..0.140 rows=255 loops=1) -> Hash (cost=3.02..3.02 rows=92 width=4) (actual time=3.238..3.238 rows=92 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 4kB -> Seq Scan on pg_index (cost=0.00..3.02 rows=92 width=4) (actual time=0.008..3.187 rows=92 loops=1) Filter: indisunique
9.19. Array Functions and Operators #
Table 9.54 shows the specialized operators available for array types. In addition to those, the usual comparison operators shown in Table 9.1 are available for arrays. The comparison operators compare the array contents element-by-element, using the default B-tree comparison function for the element data type, and sort based on the first difference. In multidimensional arrays the elements are visited in row-major order (last subscript varies most rapidly). If the contents of two arrays are equal but the dimensionality is different, the first difference in the dimensionality information determines the sort order.
Table 9.54. Array Operators
Operator Description Example(s) |
---|
Does the first array contain the second, that is, does each element appearing in the second array equal some element of the first array? (Duplicates are not treated specially, thus
|
Is the first array contained by the second?
|
Do the arrays overlap, that is, have any elements in common?
|
Concatenates the two arrays. Concatenating a null or empty array is a no-op; otherwise the arrays must have the same number of dimensions (as illustrated by the first example) or differ in number of dimensions by one (as illustrated by the second). If the arrays are not of identical element types, they will be coerced to a common type (see Section 10.5).
|
Concatenates an element onto the front of an array (which must be empty or one-dimensional).
|
Concatenates an element onto the end of an array (which must be empty or one-dimensional).
|
See Section 8.15 for more details about array operator behavior. See Section 11.2 for more details about which operators support indexed operations.
Table 9.55 shows the functions available for use with array types. See Section 8.15 for more information and examples of the use of these functions.
Table 9.55. Array Functions
Function Description Example(s) |
---|
Appends an element to the end of an array (same as the
|
Concatenates two arrays (same as the
|
Returns a text representation of the array's dimensions.
|
Returns an array filled with copies of the given value, having dimensions of the lengths specified by the second argument. The optional third argument supplies lower-bound values for each dimension (which default to all
|
Returns the length of the requested array dimension. (Produces NULL instead of 0 for empty or missing array dimensions.)
|
Returns the lower bound of the requested array dimension.
|
Returns the number of dimensions of the array.
|
Returns the subscript of the first occurrence of the second argument in the array, or
|
Returns an array of the subscripts of all occurrences of the second argument in the array given as first argument. The array must be one-dimensional. Comparisons are done using
|
Prepends an element to the beginning of an array (same as the
|
Removes all elements equal to the given value from the array. The array must be one-dimensional. Comparisons are done using
|
Replaces each array element equal to the second argument with the third argument.
|
Returns an array of
|
Randomly shuffles the first dimension of the array.
|
Converts each array element to its text representation, and concatenates those separated by the
|
Returns the upper bound of the requested array dimension.
|
Returns the total number of elements in the array, or 0 if the array is empty.
|
Trims an array by removing the last
|
Expands an array into a set of rows. The array's elements are read out in storage order.
1 2
foo bar baz quux |
Expands multiple arrays (possibly of different data types) into a set of rows. If the arrays are not all the same length then the shorter ones are padded with
a | b ---+----- 1 | foo 2 | bar | baz |
See also Section 9.21 about the aggregate function array_agg
for use with arrays.