|
| 1 | +:sectnums: |
| 2 | +:sectnumlevels: 5 |
| 3 | + |
| 4 | += pg_show_plans |
| 5 | + |
| 6 | +== Overview |
| 7 | + |
| 8 | +pg_show_plans is a PostgreSQL extension that shows the query plans of all currently running SQL statements. Query plans can be displayed in several formats, including `TEXT`, `JSON`, and `YAML`. |
| 9 | + |
| 10 | +NOTE: This extension creates a hash table within shared memory. The hash table is not resizable — no new plans can be added once it has been filled up. |
| 11 | + |
| 12 | +== Installation |
| 13 | + |
| 14 | +[TIP] |
| 15 | +The source installation environment is Ubuntu 24.04 (x86_64), with IvorySQL 5 or above installed at `/usr/ivory-5`. |
| 16 | + |
| 17 | +=== Install from Source |
| 18 | + |
| 19 | +Make sure `pg_config` is available on your PATH. |
| 20 | + |
| 21 | +[source,bash] |
| 22 | +---- |
| 23 | +git clone https://github.com/cybertec-postgresql/pg_show_plans.git |
| 24 | +cd pg_show_plans |
| 25 | +
|
| 26 | +make PG_CONFIG=/usr/ivory-5/bin/pg_config |
| 27 | +make PG_CONFIG=/usr/ivory-5/bin/pg_config install |
| 28 | +---- |
| 29 | + |
| 30 | +=== Configure shared_preload_libraries |
| 31 | + |
| 32 | +Add `pg_show_plans` to the `shared_preload_libraries` parameter in `ivorysql.conf`: |
| 33 | + |
| 34 | +[source,conf] |
| 35 | +---- |
| 36 | +shared_preload_libraries = 'liboracle_parser, ivorysql_ora, pg_show_plans' |
| 37 | +---- |
| 38 | + |
| 39 | +Restart the IvorySQL instance to apply the change: |
| 40 | + |
| 41 | +[source,bash] |
| 42 | +---- |
| 43 | +pg_ctl restart -D data |
| 44 | +---- |
| 45 | + |
| 46 | +=== Create the Extension |
| 47 | + |
| 48 | +After the server has restarted, connect to the target database and create the extension: |
| 49 | + |
| 50 | +[source,sql] |
| 51 | +---- |
| 52 | +CREATE EXTENSION pg_show_plans; |
| 53 | +---- |
| 54 | + |
| 55 | +== Usage |
| 56 | + |
| 57 | +=== View Current Query Plans |
| 58 | + |
| 59 | +Use the `pg_show_plans` view to inspect the execution plans of all currently running SQL statements: |
| 60 | + |
| 61 | +[source,sql] |
| 62 | +---- |
| 63 | +SELECT * FROM pg_show_plans; |
| 64 | +---- |
| 65 | + |
| 66 | +[source,text] |
| 67 | +---- |
| 68 | + pid | level | userid | dbid | plan |
| 69 | +-------+-------+--------+-------+----------------------------------------------------------------------- |
| 70 | + 11473 | 0 | 10 | 16384 | Function Scan on pg_show_plans (cost=0.00..10.00 rows=1000 width=56) |
| 71 | + 11504 | 0 | 10 | 16384 | Function Scan on print_item (cost=0.25..10.25 rows=1000 width=524) |
| 72 | + 11504 | 1 | 10 | 16384 | Result (cost=0.00..0.01 rows=1 width=4) |
| 73 | +(3 rows) |
| 74 | +---- |
| 75 | + |
| 76 | +=== View Query Plans with SQL Text |
| 77 | + |
| 78 | +Use the `pg_show_plans_q` view to see query plans together with the corresponding SQL statement. This view joins `pg_show_plans` with `pg_stat_activity`: |
| 79 | + |
| 80 | +[source,sql] |
| 81 | +---- |
| 82 | +SELECT * FROM pg_show_plans_q; |
| 83 | +---- |
| 84 | + |
| 85 | +[source,text] |
| 86 | +---- |
| 87 | +-[ RECORD 1 ]------------------------------------------------------------------------------------ |
| 88 | +pid | 11473 |
| 89 | +level | 0 |
| 90 | +plan | Sort (cost=72.08..74.58 rows=1000 width=80) |
| 91 | + | Sort Key: pg_show_plans.pid, pg_show_plans.level |
| 92 | + | -> Hash Left Join (cost=2.25..22.25 rows=1000 width=80) |
| 93 | + | Hash Cond: (pg_show_plans.pid = s.pid) |
| 94 | + | Join Filter: (pg_show_plans.level = 0) |
| 95 | + | -> Function Scan on pg_show_plans (cost=0.00..10.00 rows=1000 width=48) |
| 96 | + | -> Hash (cost=1.00..1.00 rows=100 width=44) |
| 97 | + | -> Function Scan on pg_stat_get_activity s (cost=0.00..1.00 rows=100 width=44) |
| 98 | +query | SELECT p.pid, p.level, p.plan, a.query FROM pg_show_plans p |
| 99 | + | LEFT JOIN pg_stat_activity a |
| 100 | + | ON p.pid = a.pid AND p.level = 0 ORDER BY p.pid, p.level; |
| 101 | +-[ RECORD 2 ]------------------------------------------------------------------------------------ |
| 102 | +pid | 11517 |
| 103 | +level | 0 |
| 104 | +plan | Function Scan on print_item (cost=0.25..10.25 rows=1000 width=524) |
| 105 | +query | SELECT * FROM print_item(1,20); |
| 106 | +-[ RECORD 3 ]------------------------------------------------------------------------------------ |
| 107 | +pid | 11517 |
| 108 | +level | 1 |
| 109 | +plan | Result (cost=0.00..0.01 rows=1 width=4) |
| 110 | +query | |
| 111 | +---- |
0 commit comments