{"id":20491687,"url":"https://github.com/dalibo/pg_query_settings","last_synced_at":"2025-04-13T17:00:23.640Z","repository":{"id":153581533,"uuid":"626910523","full_name":"dalibo/pg_query_settings","owner":"dalibo","description":"Module that dynamically set queries parameters based on their queryid","archived":false,"fork":false,"pushed_at":"2024-09-06T08:56:06.000Z","size":148,"stargazers_count":4,"open_issues_count":2,"forks_count":2,"subscribers_count":8,"default_branch":"main","last_synced_at":"2025-03-27T07:51:32.816Z","etag":null,"topics":[],"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/dalibo.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-12T12:04:46.000Z","updated_at":"2024-10-03T08:56:41.000Z","dependencies_parsed_at":null,"dependency_job_id":"7afb0ebe-f0bb-4b99-8036-7ed24b80d0a3","html_url":"https://github.com/dalibo/pg_query_settings","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalibo%2Fpg_query_settings","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalibo%2Fpg_query_settings/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalibo%2Fpg_query_settings/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dalibo%2Fpg_query_settings/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dalibo","download_url":"https://codeload.github.com/dalibo/pg_query_settings/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248750078,"owners_count":21155685,"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","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":[],"created_at":"2024-11-15T17:25:33.903Z","updated_at":"2025-04-13T17:00:23.595Z","avatar_url":"https://github.com/dalibo.png","language":"C","funding_links":[],"categories":[],"sub_categories":[],"readme":"README\n======\n\n`pg_query_settings` is a module that dynamically set queries' parameters based\non their queryid.\n\nThe original idea was to configure a specific value of the `work_mem` parameter\nfor a specific query. Currently, all query parameters may have a customizable\nvalue.\n\nRequirements\n------------\n\nTo compile these tools, you will need the PostgreSQL 14+ header files, and the\n`pg_config` tool. The header files and the tool are usually available in a -dev\npackage.\n\nCompilation\n-----------\n\nYou only have to do:\n\n```\n$ make\n$ make install\n```\n\nUsage\n-----\n\nThe extension must be created first:\n\n```sql\nCREATE EXTENSION pg_query_settings;\n```\n\nThe library must be loaded either with `LOAD`:\n\n```sql\nLOAD 'pg_query_settings';\n```\n\nor with the usual parameters (`shared_preload_libraries` for example).\n\nFor each query executed, the library will read the `pgqs_config` table searching\nfor the queryid of the query. For each `pgqs_config` line with this queryid,\nthe second column indicates the parameter name and the third column indicates\nthe parameter value.\n\nThe `pgqs_config` table may be populated with standard DML queries (`INSERT`,\n`UPDATE`, `DELETE`).\n\nThe execution of the library function's code is automatic once it is loaded.\n\nThe extension can be disabled with the `pg_query_settings.enabled` parameter.\n\nMore informations on pg_query_settings\n--------------------------------------\n\nLoad the library :\n\n```\npostgres=# LOAD 'pg_query_settings';\nLOAD\n```\n\nCreate the extension:\n\n```\npostgres=# CREATE EXTENSION pg_query_settings;\nCREATE EXTENSION\n```\n\nCreate a user table and add some rows:\n\n```\npostgres=# CREATE TABLE toto (c1 integer, c2 text);\nCREATE TABLE\npostgres=# INSERT INTO toto SELECT i, 'Ligne '||i FROM generate_series(1, 10000000) i;\nINSERT 0 10000000\n```\n\nWe run a query that uses a sort :\n\n```\npostgres=# EXPLAIN (COSTS OFF, ANALYZE, SETTINGS, VERBOSE) SELECT * FROM toto ORDER BY c2;\n┌────────────────────────────────────────────────────────────────────────────────────┐\n│                                     QUERY PLAN                                     │\n├────────────────────────────────────────────────────────────────────────────────────┤\n│ Sort (actual time=28686.665..41124.312 rows=10000000 loops=1)                      │\n│   Output: c1, c2                                                                   │\n│   Sort Key: toto.c2                                                                │\n│   Sort Method: external merge  Disk: 272920kB                                      │\n│   -\u003e  Seq Scan on public.toto (actual time=0.029..12849.197 rows=10000000 loops=1) │\n│         Output: c1, c2                                                             │\n│ Settings: random_page_cost = '1.1'                                                 │\n│ Query Identifier: 2507635424379213761                                              │\n│ Planning Time: 0.125 ms                                                            │\n│ Execution Time: 52424.680 ms                                                       │\n└────────────────────────────────────────────────────────────────────────────────────┘\n(10 rows)\n```\n\nThis query sorts rows on disk because `work_mem` is not high enough.\n\nWe increase `work_mem` for this session:\n\n```\npostgres=# SET work_mem TO '1GB';\nSET\npostgres=# EXPLAIN (COSTS OFF, ANALYZE, SETTINGS, VERBOSE) SELECT * FROM toto ORDER BY c2;\n┌────────────────────────────────────────────────────────────────────────────────────┐\n│                                     QUERY PLAN                                     │\n├────────────────────────────────────────────────────────────────────────────────────┤\n│ Sort (actual time=31700.626..43454.941 rows=10000000 loops=1)                      │\n│   Output: c1, c2                                                                   │\n│   Sort Key: toto.c2                                                                │\n│   Sort Method: quicksort  Memory: 1020995kB                                        │\n│   -\u003e  Seq Scan on public.toto (actual time=0.026..13009.674 rows=10000000 loops=1) │\n│         Output: c1, c2                                                             │\n│ Settings: random_page_cost = '1.1', work_mem = '1GB'                               │\n│ Query Identifier: 2507635424379213761                                              │\n│ Planning Time: 0.376 ms                                                            │\n│ Execution Time: 55004.212 ms                                                       │\n└────────────────────────────────────────────────────────────────────────────────────┘\n(10 rows)\n```\n\nNow, it uses this memory to sort in memory even if the duration is roughly the same.\n\nWe go back to the default configuration (4 MB):\n\n```\npostgres=# RESET work_mem;\nRESET\n```\n\nWe insert the configuration to apply in the `pgqs_config` table (we get the\nquery identifier from the `EXPLAIN output`):\n\n```\npostgres=# INSERT INTO pgqs_config VALUES (2507635424379213761, 'work_mem', '1000000000');\nINSERT 0 1\n```\n\nWe execute the query :\n\n```\npostgres=# EXPLAIN (COSTS OFF, ANALYZE, SETTINGS, VERBOSE) SELECT * FROM toto ORDER BY c2;\nWARNING:  queryid is '2507635424379213761'\nWARNING:  value is 1000000000\n┌────────────────────────────────────────────────────────────────────────────────────┐\n│                                     QUERY PLAN                                     │\n├────────────────────────────────────────────────────────────────────────────────────┤\n│ Sort (actual time=31387.960..43111.072 rows=10000000 loops=1)                      │\n│   Output: c1, c2                                                                   │\n│   Sort Key: toto.c2                                                                │\n│   Sort Method: quicksort  Memory: 1171342kB                                        │\n│   -\u003e  Seq Scan on public.toto (actual time=0.025..12888.501 rows=10000000 loops=1) │\n│         Output: c1, c2                                                             │\n│ Settings: random_page_cost = '1.1', work_mem = '1000000000kB'                      │\n│ Query Identifier: 2507635424379213761                                              │\n│ Planning Time: 0.392 ms                                                            │\n│ Execution Time: 54615.882 ms                                                       │\n└────────────────────────────────────────────────────────────────────────────────────┘\n(10 rows)\n```\n\nAnd this time, the specific configuration is applied.\n\nCaveats\n--------\n\n`pg_query_settings` doesn't work well with **prepared queries**. These are\nconsidered as not supported yet. This will be fixed in future releases.\n\nEnabling this extension might have a performance impact on any workloads\nwith a high number of fast queries, as it slighlty increases the time taken\nto compute the query plan. The more entries in the table, the greater the\nimpact. This will be improved in future releases.\n\nIt is possible to disable it globally by setting the parameter\n`pg_query_settings.enabled` to `false` in the main configuration file,\nand to enable it only for a specific database or user. For example:\n\n```\nALTER ROLE olap_user SET pg_query_settings.enabled = true;\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdalibo%2Fpg_query_settings","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdalibo%2Fpg_query_settings","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdalibo%2Fpg_query_settings/lists"}