{"id":19458498,"url":"https://github.com/postgrespro/sr_plan","last_synced_at":"2025-08-11T05:08:23.905Z","repository":{"id":51003784,"uuid":"48793977","full_name":"postgrespro/sr_plan","owner":"postgrespro","description":"Save and restore query plans in PostgreSQL","archived":false,"fork":false,"pushed_at":"2022-08-01T16:48:09.000Z","size":155,"stargazers_count":63,"open_issues_count":3,"forks_count":11,"subscribers_count":33,"default_branch":"master","last_synced_at":"2025-08-04T13:33:07.804Z","etag":null,"topics":["extension","outline","planning","postgres","postgresql"],"latest_commit_sha":null,"homepage":null,"language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/postgrespro.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-12-30T09:45:26.000Z","updated_at":"2025-06-23T01:44:03.000Z","dependencies_parsed_at":"2022-09-25T00:51:32.773Z","dependency_job_id":null,"html_url":"https://github.com/postgrespro/sr_plan","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/postgrespro/sr_plan","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fsr_plan","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fsr_plan/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fsr_plan/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fsr_plan/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/postgrespro","download_url":"https://codeload.github.com/postgrespro/sr_plan/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/postgrespro%2Fsr_plan/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269832882,"owners_count":24482330,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["extension","outline","planning","postgres","postgresql"],"created_at":"2024-11-10T17:27:21.563Z","updated_at":"2025-08-11T05:08:23.865Z","avatar_url":"https://github.com/postgrespro.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/postgrespro/sr_plan.svg?branch=master)](https://travis-ci.org/postgrespro/sr_plan)\n[![GitHub license](https://img.shields.io/badge/license-PostgreSQL-blue.svg)](https://raw.githubusercontent.com/postgrespro/sr_plan/master/LICENSE)\n\n\n# Save and restore query plans in PostgreSQL\n\n## Rationale\n\nsr_plan looks like Oracle Outline system. It can be used to lock the execution plan. It is necessary if you do not trust the planner or able to form a better plan.\n\n## Build and install\n\n```bash\nmake USE_PGXS=1\nmake USE_PGXS=1 install\n```\n\nand modify your postgres config:\n```\nshared_preload_libraries = 'sr_plan'\n```\n\n## Usage\n\nInstall the extension in your database:\n\n```SQL\nCREATE EXTENSION sr_plan;\n```\nIf you want to save the query plan is necessary to set the variable:\n\n```SQL\nset sr_plan.write_mode = true;\n```\n\nNow plans for all subsequent queries will be stored in the table sr_plans.\nDon't forget that all queries will be stored including duplicates.\n\nMake an example query:\n```SQL\nselect query_hash from sr_plans where query_hash=10;\n```\n\nDisable saving the plan for the query:\n```SQL\nset sr_plan.write_mode = false;\n```\n\nEnable it:\n\n```SQL\nupdate sr_plans set enable=true;\n```\n\nAfter that, the plan for the query will be taken from the sr_plans.\n\nIn addition sr plan allows you to save a parameterized query plan.\nIn this case, we have some constants in the query are not essential.\nFor the parameters we use a special function _p (anyelement) example:\n\n```SQL\nselect query_hash from sr_plans where query_hash=1000+_p(10);\n```\n\nIf we keep the plan for the query and enable it to be used also for the following queries:\n\n```SQL\nselect query_hash from sr_plans where query_hash=1000+_p(11);\nselect query_hash from sr_plans where query_hash=1000+_p(-5);\n```\n\n## EXPLAIN for saved plans\n\nIt is possible to see saved plans by using `show_plan` function. It requires\nknowing query hash which could be fetched from `sr_plans` table.\n\nExamples:\n\nShow enabled plan for query hash:\n\n```SQL\nSELECT show_plan(1);\n                  show_plan                   \n----------------------------------------------\n (\"Seq Scan on public.explain_test\")\n (\"  Output: test_attr1, test_attr2\")\n (\"  Filter: (explain_test.test_attr1 = 10)\")\n(3 rows)\n```\n\nGet second saved plan by using `index` parameter (ignores `enable` attribute):\n\n```SQL\nSELECT show_plan(1, index := 2);\n                  show_plan                   \n----------------------------------------------\n (\"Seq Scan on public.explain_test\")\n (\"  Output: test_attr1, test_attr2\")\n (\"  Filter: (explain_test.test_attr1 = 10)\")\n(3 rows)\n```\n\nUse another output format (supported formats are `json`, `text`, `xml`, `yaml`):\n\n```SQL\nSELECT show_plan(1, format := 'json');\n                      show_plan                       \n------------------------------------------------------\n (\"[                                                 +\n   {                                                 +\n     \"\"Plan\"\": {                                     +\n       \"\"Node Type\"\": \"\"Seq Scan\"\",                  +\n       \"\"Parallel Aware\"\": false,                    +\n       \"\"Relation Name\"\": \"\"explain_test\"\",          +\n       \"\"Schema\"\": \"\"public\"\",                       +\n       \"\"Alias\"\": \"\"explain_test\"\",                  +\n       \"\"Output\"\": [\"\"test_attr1\"\", \"\"test_attr2\"\"], +\n       \"\"Filter\"\": \"\"(explain_test.test_attr1 = 10)\"\"+\n     }                                               +\n   }                                                 +\n ]\")\n(1 row)\n```\n\n## `pg_stat_statements` integration\n\n`sr_plans` table contains `query_id` columns which could be used to make\njoins with `pg_stat_statements` tables and views.\n\nNote: in `shared_preload_libraries` list `pg_stat_statements` should be\nspecified after `sr_plan`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpostgrespro%2Fsr_plan","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fpostgrespro%2Fsr_plan","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fpostgrespro%2Fsr_plan/lists"}