Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 45 additions & 19 deletions Readme.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,33 @@
`analyzer` is capable of giving index recommendations after going through your
postgres logs. It works with all languages, ORMs and query builders!
# Query Doctor Analyzer

PostgreSQL index recommendation tool that analyzes your queries and suggests optimal indexes.

## Features

- Works with any language, ORM, or query builder
- Automatic index recommendations based on real query execution
- Cost-based analysis using PostgreSQL EXPLAIN plans
- Optional production statistics sync for accurate cost estimates
- Support for TimescaleDB hypertables and materialized views
- Non-destructive (all analysis done in a local copy of the schema)

## Modes

The analyzer can run in two modes:

### Server Mode

Run as an HTTP server for real-time query analysis during development.

```bash
docker run --pull always -t -p 2345:2345 ghcr.io/query-doctor/analyzer
```

The server runs on port `2345` and can be connected to from [app.querydoctor.com](https://app.querydoctor.com).

### CI Mode

Run as a GitHub Action to automatically analyze queries from your test suite and post index recommendations as PR comments.

There are a couple assumptions about your CI pipeline we make for this to work.

Expand Down Expand Up @@ -27,22 +55,20 @@ log_directory='/var/log/postgresql' # or any path you like
log_filename='postgres.log' # or any name you like
```

### Optional
#### Optional

You have a production database you can pull statistics from (using a query given
by us)

---
by us).

# Steps for setup
## CI Mode Setup

Currently we only support GitHub actions but it would not be difficult to add
support for other CI platforms like azure pipelines.
Currently we only support GitHub Actions but it would not be difficult to add
support for other CI platforms like Azure Pipelines.

## Github Actions
### GitHub Actions

`ubuntu` runners in github already ships with postgres as part of the default
image, so we try to leverage that. Because github workflows does not support
`ubuntu` runners in GitHub already ships with postgres as part of the default
image, so we try to leverage that. Because GitHub workflows does not support
specifying arguments to services https://github.com/actions/runner/pull/1152, we
can’t run postgres as a container. And trying to run postgres in docker directly
causes network problems because it seems `--network=host` is also not supported
Expand Down Expand Up @@ -85,7 +111,7 @@ you can change `sudo -u postgres createuser -s -d -r -w me` to create a new user
with a name of your choosing and `sudo -u postgres createdb testing` to create a
db with a different name.

1. Run your migrations and seed scripts. This is just an example showing that
2. Run your migrations and seed scripts. This is just an example showing that
the migrations should target the postgres instance that was set up with the
previous command

Expand All @@ -101,9 +127,9 @@ jobs:
POSTGRES_URL: postgres://me@localhost/testing
```

1. Run your test suite against the same database. You can do this with any tool
3. Run your test suite against the same database. You can do this with any tool
and use any query builder or ORM you like.
2. Run the analyzer. `GITHUB_TOKEN` is needed to post a comment to your PR
4. Run the analyzer. `GITHUB_TOKEN` is needed to post a comment to your PR
reviewing the indexes found in your database.

```yaml
Expand All @@ -127,7 +153,7 @@ jobs:
POSTGRES_URL: postgres://me@localhost/testing
```

1. Add `pull-request: write` permissions to your job to allow
5. Add `pull-request: write` permissions to your job to allow

```yaml
jobs:
Expand All @@ -139,7 +165,7 @@ jobs:
...
```

## Statistics synchronization
## Statistics Synchronization

To make sure that we can most accurately emulate your production database, we
need access to its stats.
Expand Down Expand Up @@ -245,13 +271,13 @@ Note: The function is defined with `SECURITY DEFINER` so that it can be called
either manually, or automatically by the analyzer if you set up stats pull
integration.

#### Regular PSQL
### Regular PSQL

```shell
psql -d <yourdb> -At -F "" -c "select _qd_dump_stats(false)" > stats.json
```

#### Postgres in Kubernetes
### Postgres in Kubernetes

This example uses cloudnative-pg, but it can apply to any pod that has access to
psql as superuser.
Expand Down