Skip to content

7 Contrib Modules

Changes to contrib extensions and the modules covered by the source document.
Beta 1Source pages: PDF 105-119Original PDF on GitHub

PDF page 105

This section describes new features related to the Contrib module.

3.5.1 Auto_explain

The following parameters have been added to the auto_explain module.

Parameter log_extension_options

The auto_explain.log_extension_options parameter has been added. This parameter specifies the string to be passed to the EXPLAIN statement in additional modules such as pg_overexplain.

Example 125 Specifying the log_extension_options option
postgres=# LOAD 'auto_explain' ;
LOAD
postgres=# LOAD 'pg_overexplain' ;
LOAD
postgres=# SET auto_explain.log_extension_options = 'DEBUG' ;
SET
Parameter log_io

The parameter auto_explain.log_io has been added. The default value is “off”. If this parameter is set to “on”, I/O statistics for executed SQL statements will be logged. This feature is effective only when the parameter auto_explain.log_analyze is set to “on”. [61c36a3]

Parameter log_extension_options

This includes additional options from external modules such as the pg_overexplain module and the pg_plan_adice module in the log.

PDF page 106

Example 126 Specifying the log_io option
postgres=# SET auto_explain.log_analyze = on ;
SET
postgres=# SET auto_explain.log_io = on ;
SET
postgres=# SELECT COUNT(*) FROM data1 ;
  count
----------
 10000000
(1 row)
postgres=# \! tail -6 data/log/postgresql.log
LOG:  duration: 341.638 ms  plan:
        Query Text: SELECT COUNT(*) FROM data1;
        Aggregate  (cost=179055.00..179055.01 rows=1 width=8) (actual time=…
          ->  Seq Scan on data1  (cost=0.00..154055.00 rows=10000000 width=…
                Prefetch: avg=46.37 max=91 capacity=94
                I/O: count=2397 waits=11 size=15.89 in-progress=4.27

3.5.2 Basic_archive

The system no longer checks for the existence of the directory specified by the basic_archive.archive_directory parameter at startup. This change will be backported to PostgreSQL 15 and later. [cc774c5]

3.5.3 File_fdw

The “header” option, which specifies the number of header records in the data file, has been added. [26cb14a]

PDF page 107

Example 127 Reading the CSV file with a two-line header
postgres=# \! cat /tmp/multi1.csv
HEADER1-1,HEADER1-2
HEADER2-1,HEADER2-2
100,data1
200,data2
postgres=# CREATE EXTENSION file_fdw ;
CREATE EXTENSION
postgres=# CREATE SERVER filesvr1 FOREIGN DATA WRAPPER file_fdw ;
CREATE SERVER
postgres=# CREATE FOREIGN TABLE multi1(col1 INT, col2 VARCHAR(10)) SERVER
filesvr1 OPTIONS (filename '/tmp/multi1.csv', HEADER '2', format 'csv') ;
CREATE FOREIGN TABLE
postgres=# SELECT * FROM multi1 ;
 col1 | col2
------+-------
  100 | data1
  200 | data2
(2 rows)

3.5.4 Injection_points

The following new features have been added to the injection_points module.

Function injection_points_attach

The injection_points_attach function has been updated to include additional arguments. [16a2f70]

Function injection_points_list

The injection_points_list function, which returns a list of injection points, has been added. [4eca711]

Removed Functions

The following functions related to statistics information have been removed. [d52c24b]

PDF page 108

  • injection_points_stats_numcalls

  • injection_points_stats_drop

  • injection_points_stats_fixed

3.5.5 Pg_buffercache

The following new features have been implemented in the pg_buffercache module.

Add Views

The following views have been added. [4b203d4]

View nameDescription
pg_buffercache_os_pagesMapping between the buffer pool and OS pages
pg_buffercache_numaMapping of the buffer pool and NUMA nodes
Table 37 Added Views
Add Functions

The following functions have been added. [9ccc049, 4b203d4]

Function nameDescription
pg_buffercache_os_pagesProvides a mapping between the buffer pool and OS pages
pg_buffercache_mark_dirtySets the specified buffer to the dirty state
pg_buffercache_mark_dirty_relationSets the buffers for the specified relation in the buffer pool to the dirty state
Table 38 Added Functions

pg_buffercache_mark_dirty_all Sets all buffers in the buffer pool to the dirty state

Example 128 Executing of the pg_buffercache_mark_dirty_all function
postgres=# SELECT * FROM pg_buffercache_mark_dirty_all() ;
-[ RECORD 1 ]---------+------
buffers_dirtied       | 5527
buffers_already_dirty | 10857
buffers_skipped       | 0

PDF page 109

3.5.6 Pg_plan_advice

The new Contrib module pg_plan_advice has been added to PostgreSQL 19. This module provides features for changing execution plans and verifying the modified execution plans. [5883ff3, 6455e55, 26255a3]

Basic Usage

To use this module, follow the steps below. 1. Specify pg_plan_advice in the LOAD statement or in the shared_preload_libraries parameter. 2. Verify the execution plan using an EXPLAIN statement with the PLAN_ADICE option. 3. Information for changing the execution plan is output in the “Generated Plan Advice:” section. 4. Specify information regarding changes to the execution plan in the pg_plan_advice.advice parameter. 5. The execution plan will be changed. In the following example, the join order of tables data1 and data2 is changed to verify that the execution plan changes.

PDF page 110

Example 129 Verifying the Method for Modifying the Execution Plan
postgres=# LOAD 'pg_plan_advice' ;
LOAD
postgres=# EXPLAIN (COSTS OFF, PLAN_ADVICE) SELECT * FROM data1 AS d1
INNER JOIN data2 AS d2 ON d1.col1 = d2.col1 WHERE d1.col1 < 1000 ;
                       QUERY PLAN
---------------------------------------------------------
 Nested Loop
   ->  Index Scan using data1_pkey on data1 d1
         Index Cond: (col1 < 1000)
   ->  Index Scan using data2_pkey on data2 d2
         Index Cond: (col1 = d1.col1)
 Generated Plan Advice:
   JOIN_ORDER(d1 d2)
   NESTED_LOOP_PLAIN(d2)
   INDEX_SCAN(d1 public.data1_pkey d2 public.data2_pkey)
   NO_GATHER(d1 d2)
(10 rows)

PDF page 111

Example 130 Configure Execution Plan Changes
postgres=# SET pg_plan_advice.advice = 'JOIN_ORDER(d2 d1)' ;
SET
postgres=# EXPLAIN (COSTS OFF) SELECT * FROM data1 AS d1 INNER JOIN data2 AS
d2 ON d1.col1 = d2.col1 WHERE d1.col1 < 1000 ;
                        QUERY PLAN
-----------------------------------------------------------
 Gather
   Workers Planned: 2
   ->  Hash Join
         Hash Cond: (d2.col1 = d1.col1)
         ->  Parallel Seq Scan on data2 d2
         ->  Hash
               ->  Index Scan using data1_pkey on data1 d1
                     Index Cond: (col1 < 1000)
 Supplied Plan Advice:
   JOIN_ORDER(d2 d1) /* matched */
(10 rows)
Specify the changes to the execution plan in the pg_plan_advice.advice parameter. The major settings
are listed below.
Setting valueDescription
SEQ_SCAN(target)Use the sequential scan (Seq Scan)
INDEX_SCAN(target)Use the specified index
JOIN_ORDER(item item…)Specify the join order
HASH_JOIN(target)Execute the hash join
NESTED_LOOP_PLAN(target)Execute the nested loop join
GATHER(item)Specify the parallel query
NO_GATHER(item)Do not use a parallel query
DO_NOT_SCAN(item)Stop scanning the specified relation
Table 39 Typical &quot;advice&quot; parameter settings

PDF page 112

Parameters

The pg_plan_advice module provides the following custom parameters.

Parameter nameDescriptionDefault value
adviceAdvice on changing the execution plan''
always_explain_supplied_adviceAlways include output indicating which child has had its execution plan changed in the results of the EXPLAIN statementon
always_store_advice_detailsWhether to output feedback for prepared statementsoff
feedback_warningsWhether to issue a warning if the advice was not successfully appliedoff
trace_maskWhether to output debug tracesoff
Table 40 追加されたパラメーター

If the always_explain_supplied_advice parameter is set to “on” (the default value), the EXPLAIN statement will always output information about any changes to the execution plan (under the heading “Supplied Plan Advice:”). If this parameter is set to “off”, specifying the PLAN_ADVICE option in the EXPLAIN statement is required to output information about changes to the execution plan.

3.5.7 Pg_stash_advice

The new Contrib module pg_stash_advice has been added to PostgreSQL 19. The pg_stash_advice module applies execution plan advice to query IDs. By default, the generated advice is stored in memory and is not persisted. To persist the advice stash data, set the pg_stash_advice.persist parameter to “true”. The persisted data is created as the file pg_stash_advice.tsv in the database cluster. [e8ec19a, c10edb1]

Function nameDescriptionNote
pg_create_advice_stashCreating the Advice Stash
pg_drop_advice_stashDropping the Advice Stash
pg_set_stashed_adviceSetting advice for a query
pg_get_advice_stashesList of Advice Stashes
pg_get_advice_stash_contentsViewing the contents of the Advice Stash
Table 41 Added Functions

PDF page 113

The following parameters are defined in this module.

Parameter nameDescriptionDefault value
pg_stash_advice.persistWhether to persist advicefalse
pg_stash_advice.stash_nameName of the active advice stash''
pg_stash_advice.persist_intervalPersistence check interval30s
Table 42 Added Parameters
Preparation

Set the shared_preload_libraries parameter to pg_stash_advice. Set the compute_query_id parameter to “auto” (default value) or “on”. Next, enable the module using the CREATE EXTENSION statement.

Example 131 Preparing pg_stash_advice module
postgres=# SHOW shared_preload_libraries ;
 shared_preload_libraries
--------------------------
 pg_stash_advice
(1 row)
postgres=# SHOW compute_query_id ;
 compute_query_id
------------------
 on
(1 row)
postgres=# CREATE EXTENSION pg_stash_advice ;
CREATE EXTENSION
Create the advice stash. Specify the name for the pg_create_advice_stash function, and set the name
of the advice just created to the pg_advice_stash.stash_name parameter.

PDF page 114

Example 132 Creating and Configuring a Advice Stash
postgres=# SELECT pg_create_advice_stash('stash1') ;
pg_create_advice_stash
------------------------
(1 row)
postgres=# SET pg_stash_advice.stash_name = 'stash1' ;
SET
Identify the query ID and execution plan of the query for which the execution plan needs to be
changed.
Example 133 Checking the Query ID
postgres=# EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM data1 AS d1 INNER
 JOIN data2 AS d2 ON d1.col1 = d2.col1 WHERE d1.col1 = 10000 ;
                      QUERY PLAN
------------------------------------------------------
 Nested Loop
   Output: d1.col1, d1.col2, d2.col1, d2.col2
   ->  Index Scan using data1_pkey on public.data1 d1
         Output: d1.col1, d1.col2
         Index Cond: (d1.col1 = 10000)
   ->  Index Scan using data2_pkey on public.data2 d2
         Output: d2.col1, d2.col2
         Index Cond: (d2.col1 = 10000)
 Query Identifier: 1524396781679940627
(9 rows)
Specify the advice to change the execution plan for the query ID. In the following example, the join
order is changed. For instructions on changing the execution plan, refer to the pg_plan_advice Contrib
module.

PDF page 115

Example 134 Specify advice
postgres=#  SELECT pg_set_stashed_advice('stash1', 1524396781679940627,
'JOIN_ORDER(d2 d1)') ;
 pg_set_stashed_advice
-----------------------
(1 row)
Check the execution plan. The "Supplied Plan Advice" entry is output, indicating that advice for
changing the execution plan has been specified.
Example 135 Check the Execution Plan
postgres=# EXPLAIN (VERBOSE, COSTS OFF) SELECT * FROM data1 AS d1 INNER JOIN
data2 AS d2 ON d1.col1 = d2.col1 WHERE d1.col1 = 10000 ;
                      QUERY PLAN
------------------------------------------------------
 Nested Loop
   Output: d1.col1, d1.col2, d2.col1, d2.col2
   ->  Index Scan using data2_pkey on public.data2 d2
         Output: d2.col1, d2.col2
         Index Cond: (d2.col1 = 10000)
   ->  Index Scan using data1_pkey on public.data1 d1
         Output: d1.col1, d1.col2
         Index Cond: (d1.col1 = 10000)
 Query Identifier: 1524396781679940627
 Supplied Plan Advice:
   JOIN_ORDER(d2 d1) /* matched */
(11 rows)

3.5.8 Pg_stat_statements

The following new features have been added to the pg_stat_statements module.

PDF page 116

Changes in the pg_stat_statements view

The following columns have been added to the pg_stat_statements view. [3357471]

Column nameData typeDescription
generic_plan_callsbigintNumber of executions of the generic execution plan
custom_plan_callsbigintNumber of executions of the custom execution plan
Table 43 Added Columns
Example 136 Number of Execution Plans Executed
postgres=> SELECT query, generic_plan_calls, custom_plan_calls FROM
pg_stat_statements WHERE query LIKE 'PREPARE%' ;
-[ RECORD 1 ]------+-----------------------------------
query              | PREPARE prepare1(int) AS SELECT $1
generic_plan_calls | 4
custom_plan_calls  | 4
Constancy of FETCH Statements

The number of rows specified in the FETCH statement is now treated as the constant and assigned the single query ID. [bee23ea]

PDF page 117

Example 137 Constancy of FETCH Statements
postgres=> BEGIN ;
BEGIN
postgres=*> DECLARE c1 CURSOR FOR SELECT * FROM data1 ;
DECLARE CURSOR
postgres=*> FETCH 1 FROM c1 ;
 c1 |  c2
----+-------
  1 | data1
(1 row)
postgres=*> FETCH 2 FROM c1 ;
 c1 |  c2
----+-------
  2 | data2
  3 | data3
(2 rows)
postgres=*> SELECT query, calls FROM pg_stat_statements WHERE query LIKE
'FETCH%' ;
      query       | calls
------------------+-------
 FETCH $1 FROM c1 |     2
(1 row)

3.5.9 Postgres_fdw

The following features have been added to the postgres_fdw module.

Option restore_stats

The Foreign Data Wrapper now supports importing remote statistics via the callback. The postgres_fdw module implements this callback, and the restore_stats option can be specified for both the FOREIGN SERVER and the FOREIGN TABLE. [28972b6]

PDF page 118

Example 138 Specifying the restore_stats option
postgres=> CREATE FOREIGN TABLE data1(col1 INT, col2 VARCHAR(10)) SERVER dbsvr1
OPTIONS (restore_stats 'true') ;
CREATE FOREIGN TABLE
postgres=> ANALYZE (VERBOSE) data1 ;
INFO:  importing statistics for foreign table "public.data1"
INFO:  finished importing statistics for foreign table "public.data1"
ANALYZE
Pushdown of ARRAY Operations

When using a generic plan, the ANY clause in the PREPARE statement can now be pushed down. [62c3b4c]

Example 139 SQL statement to execute
postgres=> SET plan_cache_mode = force_generic_plan ;
SET
postgres=> PREPARE array_stmt(VARCHAR[]) AS SELECT * FROM data1 WHERE
col2 = ANY($1) ;
PREPARE
postgres=> EXPLAIN (VERBOSE, COSTS OFF) EXECUTE
array_stmt(ARRAY['data1', 'data2']) ;
Example 140 PostgreSQL 18 Execution Plan
postgres=> EXPLAIN (VERBOSE, COSTS OFF) EXECUTE
array_stmt(ARRAY['data1', 'data2']) ;
                     QUERY PLAN
-----------------------------------------------------
 Foreign Scan on public.data1
   Output: col1, col2
   Filter: ((data1.col2)::text = ANY (($1)::text[]))
   Remote SQL: SELECT col1, col2 FROM public.data1
(4 rows)

PDF page 119

Example 141 PostgreSQL 19 Execution Plan
postgres=> EXPLAIN (VERBOSE, COSTS OFF) EXECUTE
array_stmt(ARRAY['data1', 'data2']) ;
                                            QUERY PLAN
----------------------------------------------------------------------------…
 Foreign Scan on public.data1
   Output: col1, col2
   Remote SQL: SELECT col1, col2 FROM public.data1 WHERE ((col2 = ANY
($1::character varying[])))
(3 rows)
Transaction State Propagation

The status of local READ ONLY and DEFERRABLE transactions is now pushed down to remote connections. [de28140]

3.5.10 Miscellaneous

In addition to what was described above, the following test extension modules have been added to the src/test/modules directory. [31280d9, e0e4c13]

Module nameDescriptionNote
test_binaryheaptest_bitmapsettest_checksumsBinary heap testing Bitmapset testing Checksum behavior testing
test_cplusplusextC++ header compilation testing
test_custom_statsFixed- and variable-size custom statistics testing
test_custom_typesCustom type testing
test_int128128-bit int type testing
test_lwlock_tranchestest_plan_advicetest_saslpreptest_shmemLWLock Tranche testing pg_plan_advice module testing SASLprep implementation testing Shared memory testing
Table 44 Added extension modules