|
1 |
| -#sr_plan |
| 1 | +#Save and restore query plans in PostgreSQL |
| 2 | + |
| 3 | +##Build |
| 4 | + |
| 5 | +Dependencies: >= Python 3.2, Mako, pycparser |
| 6 | +If you only have a Python you can use the virtual environment: |
| 7 | +```bash |
| 8 | +virtualenv env |
| 9 | +source ./env/bin/activate |
| 10 | +pip install -r ./requirements.txt |
| 11 | +``` |
| 12 | + |
| 13 | +Then you need to generate C code and compiled it: |
| 14 | +```bash |
| 15 | +make USE_PGXS=1 genparser |
| 16 | +make USE_PGXS=1 |
| 17 | +make USE_PGXS=1 install |
| 18 | +``` |
| 19 | + |
| 20 | +and modify your postgres config: |
| 21 | +``` |
| 22 | +shared_preload_libraries = 'sr_plan.so' |
| 23 | +``` |
| 24 | + |
| 25 | +##Usage |
| 26 | +In your db: |
| 27 | +```SQL |
| 28 | +CREATE EXTENSION sr_plan; |
| 29 | +``` |
| 30 | +If you want to save the query plan is necessary to set the variable: |
| 31 | +```SQL |
| 32 | +setsr_plan.write_mode= true; |
| 33 | +``` |
| 34 | +Now plans for all subsequent requests will be stored in the table sr_plans. It must be remembered that all requests will be maintained including duplicates. |
| 35 | +Making an example query: |
| 36 | +```SQL |
| 37 | +select query_hashfrom sr_planswhere query_hash=10; |
| 38 | +``` |
| 39 | +disable saving the query: |
| 40 | +```SQL |
| 41 | +setsr_plan.write_mode= false; |
| 42 | +``` |
| 43 | +Now verify that your query is saved: |
| 44 | +```SQL |
| 45 | +select query_hash, enable, query, explain_jsonb_plan(plan)from sr_plans; |
| 46 | +``` |
| 47 | +explain_jsonb_plan function allows you to display explain execute the plan of which lies in jsonb. By default, all the plans are off, you need enable it: |
| 48 | +```SQL |
| 49 | +update sr_plansset enable=truewhere query_hash=812619660; |
| 50 | +``` |
| 51 | +(812619660 for example only) |
| 52 | +After that, the plan for the query will be taken from the sr_plans. |