{"id":20499515,"url":"https://github.com/chobostar/pgstats-to-clickhouse","last_synced_at":"2025-09-06T06:42:08.287Z","repository":{"id":165448291,"uuid":"297154576","full_name":"chobostar/pgstats-to-clickhouse","owner":"chobostar","description":"postgresql pg_stat_statements and pg_statio_tables metric exporter","archived":false,"fork":false,"pushed_at":"2022-03-23T09:04:53.000Z","size":579,"stargazers_count":7,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-12T21:13:58.985Z","etag":null,"topics":["clickhouse","metrics","monitoring","pg-stat-statements","pg-stat-user-tables","pg-statio-user-tables","postgresql"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/chobostar.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2020-09-20T20:06:41.000Z","updated_at":"2025-06-02T17:47:23.000Z","dependencies_parsed_at":"2024-06-21T17:34:14.471Z","dependency_job_id":"f06eecb0-33dc-4999-98d3-ed31c457319e","html_url":"https://github.com/chobostar/pgstats-to-clickhouse","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/chobostar/pgstats-to-clickhouse","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chobostar%2Fpgstats-to-clickhouse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chobostar%2Fpgstats-to-clickhouse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chobostar%2Fpgstats-to-clickhouse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chobostar%2Fpgstats-to-clickhouse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/chobostar","download_url":"https://codeload.github.com/chobostar/pgstats-to-clickhouse/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/chobostar%2Fpgstats-to-clickhouse/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273868155,"owners_count":25182423,"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-09-06T02:00:13.247Z","response_time":2576,"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":["clickhouse","metrics","monitoring","pg-stat-statements","pg-stat-user-tables","pg-statio-user-tables","postgresql"],"created_at":"2024-11-15T18:17:34.665Z","updated_at":"2025-09-06T06:42:08.238Z","avatar_url":"https://github.com/chobostar.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"pgstat-to-clickhouse\n====================\n\nCollects pg_stat_statements from postgresql and pushes to remote clickhouse.\nAlso optionally can collect (if `STATIO_POSTGRES_DSN` is defined):\n - pg_statio_user_tables\n - pg_stat_user_tables\n\nSee logic in `StatsCollector.Merge`.\n\nFeatures:\n- counts delta (counter -\u003e gauge)\n- skips not changing metrics (not for gauge metrics like n_live_tup, n_dead_tup, relation_size)\n\n#### Example Dashboards\n![pg_stat_statements](examples/img/2e640f2055.png)\n\n![pg_stat_tables](examples/img/62add3afdb.png)\n\nQuick demo:\n```\nmake up\nmake build\n\nenv INTERVAL=\"30s\" \\\n     POSTGRES_DSN=\"postgres://postgres@localhost:5432/postgres?sslmode=disable\" \\\n     CLICKHOUSE_DSN=\"http://localhost:8123/default\" \\\n     STATIO_POSTGRES_DSN=\"postgres://postgres@localhost:5432/postgres?sslmode=disable\" \\\n     ./bin/pgstats-to-clickhouse\n\ndocker-compose -f ./test/docker-compose.yml exec -T postgres psql -U postgres -c \"drop table pg_stat_statements\"\ndocker-compose -f ./test/docker-compose.yml exec -T postgres psql -U postgres -c \"create extension pg_stat_statements\"\ndocker-compose -f ./test/docker-compose.yml exec -T postgres pgbench -U postgres -i\n\ndocker-compose -f ./test/docker-compose.yml exec -T postgres pgbench -U postgres\n...\n# wait 30 sec\n...\ndocker-compose -f ./test/docker-compose.yml exec -T postgres pgbench -U postgres\n```\nopen: http://localhost:3000/d/TzHfPjPnz/pg_top_queries\n\n#### Requirements\n\ntested on\n```\nClickhouse 20.1\nPostgreSQL 11\nGo 1.14\n```\n\n- postgres user should have atleast `pg_monitor` role, otherwise it will fail with error about queryid is NULL\n- postgres user should have connect grants\n\n#### Usage:\n```\nenv INTERVAL=\"60s\" \\\n    POSTGRES_DSN=\"postgres://postgres@localhost:5432/postgres?sslmode=disable\" \\\n    CLICKHOUSE_DSN=\"http://localhost:8123/default\" \\\n    STATIO_POSTGRES_DSN=\"postgres://postgres@localhost:5432/postgres?sslmode=disable\"\n    pgstats-to-clickhouse\n```\n\n#### Params:\n- `INTERVAL` - collect interval in seconds (default: \"30s\", valid units are \"ns\", \"us\" (or \"µs\"), \"ms\", \"s\", \"m\", \"h\")\n- `POSTGRES_DSN` - connection to postgres (default: \"postgres://postgres@localhost:5432/postgres?sslmode=disable\")\n- `STATIO_POSTGRES_DSN` - connection to postgres user's database for tables monitores (default: \"\")\n- `CLICKHOUSE_DSN` - connection to clickhouse (default: \"http://localhost:8123/default\"). As `schema://user:password@host[:port]/database?param1=value1\u0026...\u0026paramN=valueN`\n    - example: `http://user:password@host:8123/clicks?timeout=5s\u0026read_timeout=10s\u0026write_timeout=20s`\n\n#### Dev\n\n```\n$ make test-full\n```\n\n#### Known possible issues\n- hash collision is not handled\n- possible data loss or not honest metrics after `pg_stat_statements_reset()`\n- hangs on during network calls if packets are dropped. Can't be interrapted by SIGINT (solved by connect_timeout)\n- data loss if clickhouse is not accessable\n- can open up to 3 connection in a once, if 3 collectors are used\n\n#### Caveat\n- pg_stat_stamenents file on disk can be too huge and it causes disk swap during reading pg_stat_statements' view. Use small value `pg_stat_statements.max`\n- in case of 1000+ amount of schemas and tables `pgstats-to-clickhouse` daemon may use too much memory, because it holds previous metrics' snapshot. 1 table == 1 entry.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchobostar%2Fpgstats-to-clickhouse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fchobostar%2Fpgstats-to-clickhouse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fchobostar%2Fpgstats-to-clickhouse/lists"}