auto_explain 模組提供了一種自動記錄慢速語句執行計劃的方法,毌須手動執行 EXPLAIN。這對於在大型應用程序中追踪未最佳化的查詢特別有用。
該模組不提供 SQL 可存取的功能。要使用它,只需將其載入到伺服器中即可。您也可以將其載入到單個連線之中:
(您必須是超級使用者才能這樣做。)更典型的用法是透過在 postgresql.conf 中的 session_preload_libraries 或 shared_preload_libraries 中包含 auto_explain 將其預先載入到部分或全部連線中。然後,無論何時發生,您都可以追踪意外緩慢的查詢。當然,會有一些系統代價。
有幾個組態參數可以控制 auto_explain 的行為。請注意,預設行為是什麼都不做,因此如果需要任何結果,必須至少設定 auto_explain.log_min_duration。
auto_explain.log_min_duration
(integer
)
auto_explain.log_min_duration 是記錄語句計劃的最小語句執行時間(以毫秒為單位)。將此設定為零會記錄所有計劃。減號(預設值)停用計劃的記錄。例如,如果將其設定為 250ms,則將記錄執行 250ms 或更長時間的所有語句。只有超級使用者才能變更此設定。
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 控制是否在記錄執行計劃時輸出緩衝區使用情況統計訊息;它相當於 EXPLAIN 的 BUFFERS 選項。除非啟用了 auto_explain.log_analyze,否則此參數無效。預鉆水情況下,此參數處於停用狀態。只有超級使用者才能變更改此設定。
auto_explain.log_timing
(boolean
)
auto_explain.log_timing 控制在記錄執行計劃時是否輸出每個節點的計時訊息;它相當於 EXPLAIN 的 TIMING 選項。重複讀取系統時鐘的成本會在某些系統上明顯減慢查詢速度,因此當只需要實際資料列計數而非精確時間計時,將此參數設定為關閉可能很有用。除非啟用了 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 控制是否在記錄執行計劃時輸出詳細訊息;它相當於 EXPLAIN 的 VERBOSE 選項。預設情況下,此參數處於停用狀態。只有超級使用者才能變更此設定。
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,表示 EXPLAIN 所有查詢。在巢狀語句的情況下,要就全部都要解釋,要就都不解釋。只有超級使用者才能變更此設定。
在一般的用法中,這些參數在 postgresql.conf 中設定,儘管超級使用者可以在他們自己的連線中即時更改它們。典型用法可能是:
這可能會產生如下的日誌輸出:
Takahiro Itagaki <
itagaki.takahiro@oss.ntt.co.jp
>
Table of Contents
F.2.2. Usingamcheck
effectively
F.3.1. Configuration Parameters
F.4.1. Configuration Parameters
F.5.3. Operator Class Interface
F.9.3. String Comparison Behavior
— opens a persistent connection to a remote database
— opens a persistent connection to a remote database, insecurely
— closes a persistent connection to a remote database
— executes a query in a remote database
— executes a command in a remote database
— opens a cursor in a remote database
— returns rows from an open cursor in a remote database
— closes a cursor in a remote database
— returns the names of all open named dblink connections
— gets last error message on the named connection
— sends an async query to a remote database
— checks if connection is busy with an async query
— retrieve async notifications on a connection
— gets an async query result
— cancels any active query on the named connection
— returns the positions and field names of a relation's primary key fields
— builds an INSERT statement using a local tuple, replacing the primary key field values with alternative supplied values
— builds a DELETE statement using supplied values for primary key field values
— builds an UPDATE statement using a local tuple, replacing the primary key field values with alternative supplied values
F.14.1. Cube-based Earth Distances
F.14.2. Point-based Earth Distances
F.17.1.hstore
External Representation
F.17.2.hstore
Operators and Functions
F.19.1.intarray
Functions and Operators
F.20.3. Functions and Operators
F.22.2. Operators and Functions
F.26.1. General Hashing Functions
F.26.2. Password Hashing Functions
F.26.3. PGP Encryption Functions
F.26.4. Raw Encryption Functions
F.30.1. Thepg_stat_statements
View
F.30.3. Configuration Parameters
F.32.1. Trigram (or Trigraph) Concepts
F.32.2. Functions and Operators
F.32.5. Text Search Integration
F.34.1. FDW Options of postgres_fdw
F.34.3. Transaction Management
F.34.4. Remote Query Optimization
F.34.5. Remote Query Execution Environment
F.34.6. Cross-Version Compatibility
F.37.1. refint — Functions for Implementing Referential Integrity
F.37.2. timetravel — Functions for Implementing Time Travel
F.37.3. autoinc — Functions for Autoincrementing Fields
F.37.4. insert_username — Functions for Tracking Who Changed a Table
F.37.5. moddatetime — Functions for Tracking Last Modification Time
F.46.2. Description of Functions
This appendix and the next one contain information regarding the modules that can be found in thecontrib
directory of thePostgreSQLdistribution. These include porting tools, analysis utilities, and plug-in features that are not part of the core PostgreSQL system, mainly because they address a limited audience or are too experimental to be part of the main source tree. This does not preclude their usefulness.
This appendix covers extensions and other server plug-in modules found incontrib
.Appendix Gcovers utility programs.
When building from the source distribution, these components are not built automatically, unless you build the "world" target (seeStep 2). You can build and install all of them by running:
in thecontrib
directory of a configured source tree; or to build and install just one selected module, do the same in that module's subdirectory. Many of the modules have regression tests, which can be executed by running:
before installation or
once you have aPostgreSQLserver running.
If you are using a pre-packaged version ofPostgreSQL, these modules are typically made available as a separate subpackage, such aspostgresql-contrib
.
Many modules supply new user-defined functions, operators, or types. To make use of one of these modules, after you have installed the code you need to register the new SQL objects in the database system. InPostgreSQL9.1 and later, this is done by executing aCREATE EXTENSIONcommand. In a fresh database, you can simply do
This command must be run by a database superuser. This registers the new SQL objects in the current database only, so you need to run this command in each database that you want the module's facilities to be available in. Alternatively, run it in databasetemplate1
so that the extension will be copied into subsequently-created databases by default.
Many modules allow you to install their objects in a schema of your choice. To do that, addSCHEMA
_schema_name
_to theCREATE EXTENSION
command. By default, the objects will be placed in your current creation target schema, typicallypublic
.
If your database was brought forward by dump and reload from a pre-9.1 version ofPostgreSQL, and you had been using the pre-9.1 version of the module in it, you should instead do
This will update the pre-9.1 objects of the module into a proper_extension_object. Future updates to the module will be managed byALTER EXTENSION. For more information about extension updates, seeSection 37.15.
Note, however, that some of these modules are not“extensions”in this sense, but are loaded into the server in some other way, for instance by way ofshared_preload_libraries. See the documentation of each module for details.
— opens a persistent connection to a remote database
— opens a persistent connection to a remote database, insecurely
— closes a persistent connection to a remote database
— executes a query in a remote database
— executes a command in a remote database
— opens a cursor in a remote database
— returns rows from an open cursor in a remote database
— closes a cursor in a remote database
— returns the names of all open named dblink connections
— gets last error message on the named connection
— sends an async query to a remote database
— checks if connection is busy with an async query
— retrieve async notifications on a connection
— gets an async query result
— cancels any active query on the named connection
— returns the positions and field names of a relation's primary key fields
— builds an INSERT statement using a local tuple, replacing the primary key field values with alternative supplied values
— builds a DELETE statement using supplied values for primary key field values
— builds an UPDATE statement using a local tuple, replacing the primary key field values with alternative supplied values
dblink
is a module that supports connections to otherPostgreSQLdatabases from within a database session.
See alsopostgres_fdw, which provides roughly the same functionality using a more modern and standards-compliant infrastructure.
The pg_visibility
module provides a means for examining the visibility map (VM) and page-level visibility information of a table. It also provides functions to check the integrity of a visibility map and to force it to be rebuilt.
Three different bits are used to store information about page-level visibility. The all-visible bit in the visibility map indicates that every tuple in the corresponding page of the relation is visible to every current and future transaction. The all-frozen bit in the visibility map indicates that every tuple in the page is frozen; that is, no future vacuum will need to modify the page until such time as a tuple is inserted, updated, deleted, or locked on that page. The page header's PD_ALL_VISIBLE
bit has the same meaning as the all-visible bit in the visibility map, but is stored within the data page itself rather than in a separate data structure. These two bits will normally agree, but the page's all-visible bit can sometimes be set while the visibility map bit is clear after a crash recovery. The reported values can also disagree because of a change that occurs after pg_visibility
examines the visibility map and before it examines the data page. Any event that causes data corruption can also cause these bits to disagree.
Functions that display information about PD_ALL_VISIBLE
bits are much more costly than those that only consult the visibility map, because they must read the relation's data blocks rather than only the (much smaller) visibility map. Functions that check the relation's data blocks are similarly expensive.
pg_visibility_map(relation regclass, blkno bigint, all_visible OUT boolean, all_frozen OUT boolean) returns record
Returns the all-visible and all-frozen bits in the visibility map for the given block of the given relation.
pg_visibility(relation regclass, blkno bigint, all_visible OUT boolean, all_frozen OUT boolean, pd_all_visible OUT boolean) returns record
Returns the all-visible and all-frozen bits in the visibility map for the given block of the given relation, plus the PD_ALL_VISIBLE
bit of that block.
pg_visibility_map(relation regclass, blkno OUT bigint, all_visible OUT boolean, all_frozen OUT boolean) returns setof record
Returns the all-visible and all-frozen bits in the visibility map for each block of the given relation.
pg_visibility(relation regclass, blkno OUT bigint, all_visible OUT boolean, all_frozen OUT boolean, pd_all_visible OUT boolean) returns setof record
Returns the all-visible and all-frozen bits in the visibility map for each block of the given relation, plus the PD_ALL_VISIBLE
bit of each block.
pg_visibility_map_summary(relation regclass, all_visible OUT bigint, all_frozen OUT bigint) returns record
Returns the number of all-visible pages and the number of all-frozen pages in the relation according to the visibility map.
pg_check_frozen(relation regclass, t_ctid OUT tid) returns setof tid
Returns the TIDs of non-frozen tuples stored in pages marked all-frozen in the visibility map. If this function returns a non-empty set of TIDs, the visibility map is corrupt.
pg_check_visible(relation regclass, t_ctid OUT tid) returns setof tid
Returns the TIDs of non-all-visible tuples stored in pages marked all-visible in the visibility map. If this function returns a non-empty set of TIDs, the visibility map is corrupt.
pg_truncate_visibility_map(relation regclass) returns void
Truncates the visibility map for the given relation. This function is useful if you believe that the visibility map for the relation is corrupt and wish to force rebuilding it. The firstVACUUM
executed on the given relation after this function is executed will scan every page in the relation and rebuild the visibility map. (Until that is done, queries will treat the visibility map as containing all zeroes.)
By default, these functions are executable only by superusers and members of the pg_stat_scan_tables
role, with the exception of pg_truncate_visibility_map(relation regclass)
which can only be executed by superusers.
Robert Haas <
>
dblink — executes a query in a remote database
dblink
executes a query (usually aSELECT
, but it can be any SQL statement that returns rows) in a remote database.
When twotext
arguments are given, the first one is first looked up as a persistent connection's name; if found, the command is executed on that connection. If not found, the first argument is treated as a connection info string as fordblink_connect
, and the indicated connection is made just for the duration of this command.
connname
Name of the connection to use; omit this parameter to use the unnamed connection.
connstr
A connection info string, as previously described fordblink_connect
.
sql
The SQL query that you wish to execute in the remote database, for exampleselect * from foo
.
fail_on_error
If true (the default when omitted) then an error thrown on the remote side of the connection causes an error to also be thrown locally. If false, the remote error is locally reported as a NOTICE, and the function returns no rows.
The function returns the row(s) produced by the query. Sincedblink
can be used with any query, it is declared to returnrecord
, rather than specifying any particular set of columns. This means that you must specify the expected set of columns in the calling query — otherwisePostgreSQLwould not know what to expect. Here is an example:
The“alias”part of theFROM
clause must specify the column names and types that the function will return. (Specifying column names in an alias is actually standard SQL syntax, but specifying column types is aPostgreSQLextension.) This allows the system to understand what*
should expand to, and whatproname
in theWHERE
clause refers to, in advance of trying to execute the function. At run time, an error will be thrown if the actual query result from the remote database does not have the same number of columns shown in theFROM
clause. The column names need not match, however, anddblink
does not insist on exact type matches either. It will succeed so long as the returned data strings are valid input for the column type declared in theFROM
clause.
A convenient way to usedblink
with predetermined queries is to create a view. This allows the column type information to be buried in the view, instead of having to spell it out in every query. For example,