|
| 1 | +<!-- doc/src/sgml/sr_plan.sgml --> |
| 2 | + |
| 3 | +<sect1 id="sr-plan" xreflabel="sr-plan"> |
| 4 | + <title>sr_plan</title> |
| 5 | + <indexterm zone="sr-plan"> |
| 6 | + <primary>sr_plan</primary> |
| 7 | + </indexterm> |
| 8 | + <sect2 id="rationale"> |
| 9 | + <title>Rationale</title> |
| 10 | + <para> |
| 11 | + sr_plan is an extension which allows to save query execution plans |
| 12 | + and use these plans for all repetitions of same query, instead of |
| 13 | + optimizing identical query again and again/ |
| 14 | + </para> |
| 15 | + <para> |
| 16 | + sr_plan looks like Oracle Outline system. It can be used to lock |
| 17 | + the execution plan. It is necessary if you do not trust the |
| 18 | + planner or able to form a better plan. |
| 19 | + </para> |
| 20 | + <para> |
| 21 | + Typically, DBA would play with queries interactively, and save |
| 22 | + their plans and then enable use of saved plans for the queries, |
| 23 | + where predictable responce time is essential. |
| 24 | + </para> |
| 25 | + <para> |
| 26 | + Then application which uses these queries would use saved plans. |
| 27 | + </para> |
| 28 | + </sect2> |
| 29 | + <sect2> |
| 30 | + <title>Installation</title> |
| 31 | + <para> |
| 32 | + In your db: |
| 33 | + </para> |
| 34 | + <programlisting > |
| 35 | +CREATE EXTENSION sr_plan; |
| 36 | +</programlisting> |
| 37 | + <para> |
| 38 | + and modify your postgresql.conf: |
| 39 | + </para> |
| 40 | + <programlisting> |
| 41 | +shared_preload_libraries = 'sr_plan.so' |
| 42 | +</programlisting> |
| 43 | + <para> |
| 44 | + It is essential that library is preloaded during server startup, |
| 45 | + because use of saved plans is enabled on per-database basis and |
| 46 | + doesn't require any per-connection actions. |
| 47 | + </para> |
| 48 | + </sect2> |
| 49 | + <sect2> |
| 50 | + <title>Usage</title> |
| 51 | + <para> |
| 52 | + If you want to save the query plan is necessary to set the |
| 53 | + variable: |
| 54 | + </para> |
| 55 | + <programlisting > |
| 56 | +set sr_plan.write_mode = true; |
| 57 | +</programlisting> |
| 58 | + <para> |
| 59 | + Now plans for all subsequent queries will be stored in the table |
| 60 | + sr_plans, until this variable is set to false. Don't forget that |
| 61 | + all queries will be stored including duplicates. Making an example |
| 62 | + query: |
| 63 | + </para> |
| 64 | + <programlisting > |
| 65 | +select query_hash from sr_plans where query_hash=10; |
| 66 | +</programlisting> |
| 67 | + <para> |
| 68 | + Disable saving the query: |
| 69 | + </para> |
| 70 | + <programlisting > |
| 71 | +set sr_plan.write_mode = false; |
| 72 | +</programlisting> |
| 73 | + <para> |
| 74 | + Now verify that your query is saved: |
| 75 | + </para> |
| 76 | + <programlisting > |
| 77 | +select query_hash, enable, valid, query, explain_jsonb_plan(plan) from sr_plans; |
| 78 | + |
| 79 | + query_hash | enable | valid | query | explain_jsonb_plan |
| 80 | +------------+--------+-------+------------------------------------------------------+---------------------------------------------------- |
| 81 | + 1783086253 | f | t | select query_hash from sr_plans where query_hash=10; | Bitmap Heap Scan on sr_plans + |
| 82 | + | | | | Recheck Cond: (query_hash = 10) + |
| 83 | + | | | | -> Bitmap Index Scan on sr_plans_query_hash_idx+ |
| 84 | + | | | | Index Cond: (query_hash = 10) + |
| 85 | + | | | | |
| 86 | +</programlisting> |
| 87 | + <para> |
| 88 | + Note use of <literal>explain_jsonb_plan</> function, that allows you to |
| 89 | + visualize execution plan in the similar way as EXPLAIN command |
| 90 | + does. |
| 91 | + </para> |
| 92 | + <para> |
| 93 | + In the database plans are stored as jsonb. By default, all the |
| 94 | + newly saved plans are disabled, you need enable it manually: |
| 95 | + </para> |
| 96 | + <para> |
| 97 | + To enable use of the saved plan |
| 98 | + </para> |
| 99 | + <programlisting > |
| 100 | +update sr_plans set enable=true where query_hash=1783086253; |
| 101 | +</programlisting> |
| 102 | + <para> |
| 103 | + (1783086253 for example only) After that, the plan for the query |
| 104 | + will be taken from the <literal>sr_plans</> table. |
| 105 | + </para> |
| 106 | + <para> |
| 107 | + In addition sr plan allows you to save a parameterized query plan. |
| 108 | + In this case, we have some constants in the query that, as we |
| 109 | + know, do not affect plan. |
| 110 | + </para> |
| 111 | + <para> |
| 112 | + During plan saving mode we can mark these constants as query |
| 113 | + parameters using a special function <literal>_p (anyelement)</>. For example: |
| 114 | + </para> |
| 115 | + <programlisting > |
| 116 | + |
| 117 | +=>create table test_table (a numeric, b text); |
| 118 | +CREATE TABLE |
| 119 | +=>insert into test_table values (1,'1'),(2,'2'),(3,'3'); |
| 120 | +INSERT 0 3 |
| 121 | +=> set sr_plan.write_mode = true; |
| 122 | +SET |
| 123 | +=> select a,b from test_table where a = _p(1); |
| 124 | + a | b |
| 125 | +---+--- |
| 126 | + 1 | 1 |
| 127 | +(1 row) |
| 128 | + |
| 129 | +=> set sr_plan.write_mode = false; |
| 130 | +SET |
| 131 | +</programlisting> |
| 132 | + <para> |
| 133 | + Now plan for query from our table is saved with parameter. So, if |
| 134 | + we enable saved plan in this table, this plan would be used for |
| 135 | + query with any value for a, as long as this value is wrapped with |
| 136 | + <literal>_p()</> function. |
| 137 | + </para> |
| 138 | + <programlisting > |
| 139 | +=>update sr_plans set enable = true where quesry= |
| 140 | + 'select a,b from test_table where a = _p(1)'; |
| 141 | +UPDATE 1 |
| 142 | +-- These queries would use saved plan |
| 143 | + |
| 144 | +=>select a,b from test_table where a = _p(2); |
| 145 | + a | b |
| 146 | +---+--- |
| 147 | + 2 | 2 |
| 148 | +(1 row) |
| 149 | + |
| 150 | +=>select a,b from test_table where a = _p(3); |
| 151 | + a | b |
| 152 | +---+--- |
| 153 | + 3 | 3 |
| 154 | +(1 row) |
| 155 | + |
| 156 | +-- This query wouldn't use saved plan, because constant is not wrapped |
| 157 | +-- with _p() |
| 158 | + |
| 159 | +=>select a,b from test_table where a = 1; |
| 160 | + a | b |
| 161 | +---+--- |
| 162 | + 1 | 1 |
| 163 | +(1 row) |
| 164 | +</programlisting> |
| 165 | + </sect2> |
| 166 | +</sect1> |