# PostgreSQL load test, driven by the `loadr-plugin-postgres` native protocol
# plugin.
#
# PostgreSQL support is NOT built into loadr core — it is a runtime-loadable
# plugin that ships the heavy `sqlx` Rust driver on its own (with ONLY the
# `postgres` feature, so it carries no `rsa` advisory). The plugin declares the
# `postgres://`/`postgresql://` URL schemes, so once it is installed a request
# to one of those URLs routes straight to it; no explicit `protocol:` needed.
#
# Each request runs one configured query as the "request"; loadr records query
# latency in `postgres_req_duration`, the number of rows returned (SELECT) or
# affected (INSERT/UPDATE/DELETE) in `postgres_rows`, and any database error
# fails the request (status 1 = ok, status 0 = DB/transport error). Positional
# parameters are bound safely by the driver with `$1, $2, ...` placeholders.
#
# Build + install the plugin, then run:
#   cargo build -p loadr-plugin-postgres --release
#   mkdir -p dist && cp plugins/loadr-plugin-postgres/plugin.toml dist/ \
#     && cp target/release/libloadr_plugin_postgres.so dist/
#   loadr plugin install dist
#   loadr run examples/27-postgres.yaml
#
# Or point the plan's `plugins:` entry at the built artifact directly (below).
name: postgres
description: Parameterised SELECT/INSERT against PostgreSQL via loadr-plugin-postgres

plugins:
  # Resolve `postgres` by name from the plugins dir (after `loadr plugin install`).
  # To run straight from a build tree instead, set:
  #   path: target/release/libloadr_plugin_postgres.so
  - name: postgres

scenarios:
  # Read-mostly traffic with an occasional write.
  postgres_reads:
    executor: constant-vus
    vus: 10
    duration: 30s
    flow:
      - request:
          name: pg list products
          url: postgres://loadr:loadr@db.example.com:5432/loadr
          sql:
            query: SELECT id, name, price FROM products WHERE price < $1 ORDER BY price
            params: ["50"]
          checks:
            - { type: status, equals: 1 }                 # 1 = ok, 0 = DB error
            - { type: duration, name: query is fast, max: 250ms }
      - request:
          name: pg lookup by id
          url: postgres://loadr:loadr@db.example.com:5432/loadr
          sql:
            query: SELECT name FROM products WHERE id = $1
            params: ["${vu}"]
          checks:
            - { type: status, equals: 1 }
      - request:
          name: pg insert order
          url: postgres://loadr:loadr@db.example.com:5432/loadr
          sql:
            query: INSERT INTO products (name, price, stock) VALUES ($1, $2, $3)
            params: ["vu-${vu}-item", "1.23", "5"]
          assert:
            - { type: status, equals: 1 }

thresholds:
  checks: [ "rate>0.99" ]
  postgres_req_duration: [ "p(95)<300ms" ]
  postgres_reqs: [ "count>0" ]
