{"id":27747257,"url":"https://github.com/woodprogrammer/postgresql-connection-manager","last_synced_at":"2025-04-28T20:10:02.358Z","repository":{"id":288781247,"uuid":"968564996","full_name":"WoodProgrammer/postgresql-connection-manager","owner":"WoodProgrammer","description":"This is project to manage postgresql connections via cgroup V2","archived":false,"fork":false,"pushed_at":"2025-04-28T15:45:00.000Z","size":348,"stargazers_count":9,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-28T20:09:57.234Z","etag":null,"topics":["cgroups","devops","pg","postgresql","sre"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/WoodProgrammer.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,"zenodo":null}},"created_at":"2025-04-18T10:05:49.000Z","updated_at":"2025-04-28T17:49:15.000Z","dependencies_parsed_at":"2025-04-19T18:38:07.654Z","dependency_job_id":"8e114345-d8a8-4c1e-913b-e642eba85eff","html_url":"https://github.com/WoodProgrammer/postgresql-connection-manager","commit_stats":null,"previous_names":["woodprogrammer/postgresql-connection-manager"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WoodProgrammer%2Fpostgresql-connection-manager","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WoodProgrammer%2Fpostgresql-connection-manager/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WoodProgrammer%2Fpostgresql-connection-manager/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/WoodProgrammer%2Fpostgresql-connection-manager/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/WoodProgrammer","download_url":"https://codeload.github.com/WoodProgrammer/postgresql-connection-manager/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251380918,"owners_count":21580338,"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":["cgroups","devops","pg","postgresql","sre"],"created_at":"2025-04-28T20:10:01.831Z","updated_at":"2025-04-28T20:10:02.343Z","avatar_url":"https://github.com/WoodProgrammer.png","language":"Go","readme":"# pg-connection-manager\n\n**A tool to isolate PostgreSQL connections using Linux cgroups.** \n\nThis project allows system administrators to detect specific PostgreSQL queries, extract their associated PIDs, and move those processes into dedicated cgroups for fine-grained CPU and memory control.\n\n---\n\n## 🚀 Features\n\n- Detects active PostgreSQL connections based on query filters\n- Creates new cgroups (cgroups v2 supported)\n- Moves PostgreSQL backend PIDs into specified cgroups\n- Enables CPU and memory resource throttling\n- Provides an HTTP API for automation and control\n- AuthenticationMiddleWare enabled (v0.0.3)\n- Prometheus Metrics exposed (v0.0.4)\n\n---\n\n## 🔧 Installation\n\n```bash\ngit clone https://github.com/WoodProgrammer/pg-connection-manager.git\ncd pg-connection-manager\ngo build -o pg-cgroup-manager\n\nexport PG_CONNECTION_HANDLER_PORT=9001\nexport PG_CONNECTION_AUTH_TOKEN=enc_S1UP3RS3CR3T_4UTH_TOK3n\n./pg-connection-manager\n\n```\nPlease specify the port that you would like to expose.\n\n\u003chr\u003e\n\n# 📡 API Endpoints\n\n## 1. Get PIDs of Queries\nReturns the PIDs of PostgreSQL backend processes that match a given query pattern.\n\n```sh\nGET /v1/get-pid-of-queries\n```\n\nBasically you can gather PIDs by this endpoint and please specify your query to find out;\n\n### Payload\n\n```sh\ncurl http://localhost:8080/v1/get-pid-of-queries \\\n--include \\\n--header \"Authorization: Bearer enc_S1UP3RS3CR3T_4UTH_TOK3n\" \\\n--request \"GET\" \\\n--data '{\"query\": \"SELECT pid, usename, application_name, state FROM pg_stat_activity;\",\"port\": \"5432\", \"password\":\"CVVVVV\", \"username\": \"postgres\", \"sslmode\": \"disable\"}'\n```\n\n### Sample Response;\n\n```json\n[{\n    \"application_name\":\"\",\n        \"pid\":12416,\n        \"state\":null,\n        \"usename\":null\n    },\n    ...\n```\n\nThen you can create a cgroupV2 and move your postgresql process under the CgroupV2 in specified resource limitations.\n\n\n\u003chr\u003e\u003c/hr\u003e\n\n## 2. Create CgroupsV2\n\nCreates a new cgroup under the default cgroups v2 hierarchy.\n\n```sh\nPOST /v1/create-cgroups\n```\n\n### Payload\n\nThere are only three allowed resource groups for now;\n\n* Cpu cycles;\n* Cpu period per cycle;\n* Memory.max \n\n\nTo calcuate better values please check the documentation.\n\n```sh\ncurl http://localhost:8080/v1/create-cgroups \\\n    --include \\\n    --header \"Authorization: Bearer enc_S1UP3RS3CR3T_4UTH_TOK3n\" \\\n    --request \"POST\" \\\n    --data '{\"name\": \"pg-new-cgroup\",\"period\":1000, \"cycle\": 1000, \"memory\": 536870912}'\n```\n\nIt basically create cgroup please check the /sys/fs/cgroup directory then you receive 200 OK response\n\n\n## 3. Move PID to Cgroup\nMoves the picked PIDs as you can see on previous payloads (payload-1) into the given cgroup.\n\n\n```sh\nPOST /v1/move-pid-to-cgroups\n```\n\n### Payload\n\n```sh\ncurl http://localhost:8080/v1/move-pid-to-cgroups \\\n    --include \\\n    --header \"Authorization: Bearer enc_S1UP3RS3CR3T_4UTH_TOK3n\" \\\n    --request \"POST\" \\\n    --data '{\"pid\": \"7323\",\"name\": \"pg-long-running-group\"}'\n```\n\nThen you can basically check the cgroups.procs file of the given cgroup then your postgresql process will runs in given Cgroup.\n\n\n\u003chr\u003e\n\n### Metrics\n\nFor metrics you have to adjust your prometheus configs like this;\n\n```yaml\nscrape_configs:\n  - job_name: \"pg_cgroup_manager\"\n    metrics_path: \"/v1/metrics\"   # change if your exporter exposes metrics on a different path\n    scheme: \"http\"            # or \"http\" depending on your exporter\n    static_configs:\n      - targets:\n        - \"localhost:8080\"  # your exporter IP:port or domain\n\n    authorization:\n      type: Bearer\n      credentials: enc_S1UP3RS3CR3T_4UTH_TOK3n\n```\n\n## Dashboard\n\nThis project also have very nice dashboard to show up Postgresql connections by groupsV2;\n\n\u003cimg src=\"./img/dashboard.png\"\u003e\u003c/img\u003e\n\n## 🧪 Use Cases\nIsolate and throttle heavy or suspicious queries\n\nEnforce resource limits on multi-tenant PostgreSQL instances\n\nPerform controlled performance experiments under constrained resources\n\nIntegrate with observability tools for resource-aware database tuning (FUTURE)\n\n\u003chr\u003e\n\n## 🔐 Security Notes\nThis tool requires root privileges to interact with the cgroup subsystem.\n\nEnsure the API is protected in production environments (e.g., behind a firewall or with token-based authentication).\n\n\u003chr\u003e\n\n## 📌 Future Improvements\n* Add support for deleting or listing existing cgroups\n\n\u003chr\u003e\n\n## 🧡 Contributions\n\nContributions, ideas, and improvements are welcome. If you’re interested in making this tool better, feel free to open an issue or a pull request!\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwoodprogrammer%2Fpostgresql-connection-manager","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwoodprogrammer%2Fpostgresql-connection-manager","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwoodprogrammer%2Fpostgresql-connection-manager/lists"}