Skip to content

PostgreSQL

Koality checks tables in PostgreSQL through DuckDB's core postgres extension, which attaches a Postgres database as a DuckDB catalog. It uses koality's default backend, and CI runs every check type against Postgres 17 (the contract suite against emulated services), including writing results to it.

Configuration

name: orders-dqm
extensions:
  - postgres              # installed and loaded before database_setup (`koality setup` installs it ahead of a run)
database_setup: |
  CREATE SECRET shop_db (
      TYPE postgres,
      HOST 'db.example.com',
      PORT 5432,
      DATABASE 'shop',
      USER 'koality',
      PASSWORD '${PG_PASSWORD}'
  );
  ATTACH '' AS shop (TYPE postgres, SECRET shop_db);
database_accessor: shop   # the name the database is attached as
defaults:
  result_table: dqm.results
  filters:
    partition_date:
      column: order_date
      value: yesterday
      type: date
check_bundles:
  - name: orders
    defaults:
      table: sales.orders # schema.table; a table in the `public` schema can be named without it
    checks:
      - check_type: NullRatioCheck
        check_column: amount
        upper_threshold: 0.01
      - check_type: CountCheck
        check_column: "*"
        lower_threshold: 1

Pass the password as a variable, so it isn't stored in the configuration; koality masks variable values in its messages:

koality validate --config_path checks.yaml --connect -dsv PG_PASSWORD="$PG_PASSWORD"
koality run --config_path checks.yaml -dsv PG_PASSWORD="$PG_PASSWORD"

Instead of a secret, ATTACH also takes a connection string (ATTACH 'host=db.example.com dbname=shop user=koality' AS shop (TYPE postgres)), and the extension reads the usual PGHOST, PGPASSWORD, … environment variables, as libpq does.

Results

With result_table set, koality creates the table on the first run (CREATE TABLE IF NOT EXISTS) and appends a row per check. The table name is relative to database_accessor, as the checks' tables are; its schema (dqm above) must exist. The columns get Postgres types:

Column Type
DATE date
METRIC_NAME, TABLE, the identifier column, COLUMN, RESULT character varying
VALUE, LOWER_THRESHOLD, UPPER_THRESHOLD double precision (an unset threshold is -Infinity or Infinity)
INSERT_TIMESTAMP timestamp without time zone (UTC)

The column names are upper case, so quote them in Postgres (SELECT "VALUE" FROM dqm.results).

The user needs SELECT on the checked tables and, for results, CREATE on the result schema and INSERT on the result table: every run sends CREATE TABLE IF NOT EXISTS, which Postgres refuses without CREATE on the schema even when the table exists. To check without writing, leave result_table out and attach the database read-only: ATTACH '' AS shop (TYPE postgres, SECRET shop_db, READ_ONLY).

What Postgres runs

Koality reads each table once per run, with one query for all checks on it (see koality explain), and DuckDB pushes that query's columns and filters down to Postgres: only the columns the checks use, and only the rows their filters select, leave the database. For the configuration above, run on 2024-01-06 (so yesterday is 2024-01-05), Postgres reads the rows with a statement like this (its exact form depends on the DuckDB version and the column types):

COPY (SELECT "order_date", "amount" FROM "sales"."orders" WHERE "order_date" = '2024-01-05') TO STDOUT (FORMAT "binary");

This is DuckDB's default (pg_enable_filter_pushdown; DuckDB 2.0 also pushes whole queries into Postgres where it can, pg_enable_remote_pushdown). To see the statements sent to Postgres, add SET pg_debug_show_queries = true; to database_setup. Other settings of the extension, e.g. its connection pool, can be set there too.