Skip to content

feat(birdbench): add PostgreSQL dialect - #6063

Merged
dkmisra merged 15 commits into
NVIDIA-NeMo:mainfrom
abelatnvidia:abel/birdpostgresql
Nov 18, 2026
Merged

dkmisra merged 15 commits into
NVIDIA-NeMo:mainfrom
abelatnvidia:abel/birdpostgresql

Conversation

@abelatnvidia

@abelatnvidia abelatnvidia commented Nov 12, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Adds --dialect postgres to the BIRD benchmark, next to sqlite and spark from #4898. It grades the way Spark does: the model's query runs on PostgreSQL, the gold SQL runs untouched on SQLite, and the two result sets are compared.

Please review prompts/default_postgres.yaml, the new config fields in bird_sql/app.py, the PostgreSQL half of eval_utils.py, and the last commit. Skip the tests and postgres_gold_check.py.

  • Server (dialect: postgres): setup_bird_postgres.py loads each BIRD database into its own schema, typed like the Spark loader, with text in the "C" collation so sorting matches SQLite. It runs ANALYZE after each load. Each grade is one prepared statement in a read-only transaction with a server-side timeout. A connection or server failure is masked as a verifier error, not scored against the model. New config: postgres_dsn (a read-only role) and an optional postgres_admin_dsn (loads missing databases at startup).
  • Pooling: each server process keeps a pool of max_concurrency connections. On 8 cores the pooled grader does 642–664 grades per second, compared to 67.5 when it opens a connection per grade. SQLite and Spark are unchanged.
  • prepare and eval.sh: --dialect postgres renders schema-qualified, double-quoted names. The prompt is the Spark prompt with the engine changed and one sentence: write every name double-quoted, exactly as shown, because PostgreSQL folds unquoted names to lower case.
  • Dependencies: psycopg[binary] and psycopg_pool are locked dependencies of bird_sql, like pyspark, and load only for dialect: postgres. The birdbench prepare project adds psycopg[binary].
  • Last commit, lower-cased columns (04737b78c): the loader lower-cases column names; tables keep BIRD's names. Unquoted column names then work as they do in SQLite. One model's saved dev predictions go from 32.99% to 45.96% with no change to its SQL, and the gold check is unchanged. The trade-off: BIRD's evidence spells some columns in mixed case, which can lead a model to quote a name that no longer exists (61 of 1,534 dev questions). I propose keeping it. Drop the commit to keep BIRD's column names.

Validation

  • resources_servers/bird_sql/tests against a local PostgreSQL 16, Spark tests included: 115 passed (114 without the last commit). Without a server, the PostgreSQL tests skip.
  • benchmarks/birdbench/tests: 77 passed.
  • postgres_gold_check.py on dev: 1,401 of 1,534 gold queries (91.3%) match after sqlglot translation and three mechanical fixes.
  • eval.sh --dialect postgres on all of dev, with a stub model that answers with those translations: 1,401 of 1,534. It differs from the gold check only on two questions whose gold uses LIMIT 1 over tied rows.
  • pre-commit on the changed files and uv lock --check for both projects: clean.

Rollouts

The stub-model run above covers the full eval.sh path without model calls. Earlier full dev runs with real models used a previous version of this grader and aren't repeated here.

Compatibility and benchmark impact

None for sqlite and spark: defaults are unchanged. PostgreSQL's max_connections must exceed max_concurrency times the number of server processes. Two existing tests used postgres as their unsupported-dialect example; they now use mysql.

Checklist

  • I have read the contributing guidelines.
  • The change is focused; unrelated "drive-by" edits are tracked as separate issues/PRs.
  • Tests added or updated and pass locally, or N/A for docs-only / non-code changes (so CI unit/server checks pass when applicable).
  • Documentation added or updated, or N/A with justification.
  • Pre-commit checks pass locally (pre-commit run --all-files) (so CI lint/format checks pass).
  • New source files include the required Apache-2.0 SPDX header.
  • All commits have DCO sign-off (git commit -s) (so the DCO check passes).

🤖 Generated with Claude Code

abelatnvidia and others added 9 commits November 11, 2026 13:42
Grade a model that writes PostgreSQL the way the spark dialect grades Spark
SQL: the model's query runs on PostgreSQL and the untouched BIRD gold runs on
the original SQLite database; only the result sets are compared. No gold SQL
is transpiled.

setup_bird_postgres.py loads each SQLite database into its own schema (the
Spark loader's column typing, text columns with the "C" collation so
ordering matches SQLite) and creates a read-only grader role. The model's
query runs as one prepared statement in a read-only transaction with a
server-side statement_timeout and parallel query off. Connection and server
failures are scored unknown_error, not as the model's mistake.

The grader DSN (postgres_dsn) and the optional loader DSN
(postgres_admin_dsn) come from config; no password is built in. psycopg and
pandas are needed only for this dialect and are imported lazily, like
pyspark for spark.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
…d check

prepare renders sql_context for PostgreSQL: schema-qualified,
double-quoted names and the column types the loader creates.
prompts/default_postgres.yaml is the Spark prompt with the engine
changed and one sentence on quoting: PostgreSQL folds unquoted names to
lower case. eval.sh accepts --dialect postgres and passes BIRD_PG_DSN
(and BIRD_PG_ADMIN_DSN, if set) to the server.

postgres_gold_check.py mirrors spark_gold_check.py: it grades sqlglot
transpiles of the dev golds through the server's own comparison, raw and
with three mechanical fixes (identifier case, double-quoted strings, REAL).

The two tests that used "postgres" as their unsupported dialect now use
"mysql".

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
Two tests run without a server: dialect routing, and an unreachable server
scored unknown_error. The rest run when BIRD_PG_TEST_ADMIN_DSN points at a
PostgreSQL server that can create schemas and roles: loader types, string
fallback and collation, idempotent load, the read-only role, every
execute_spark case mirrored, statement timeout, "COMMIT; DROP" and an
end-to-end verify(). They load one small schema and drop it afterwards.
Without psycopg or a server they skip.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
The postgres dialect opened a new connection for every grade: a server
process forked and SCRAM authentication each time. That capped one server
near 350 grades/s and let concurrent grades run the server out of
max_connections. Each server process now keeps one psycopg_pool pool of
max_concurrency connections, opened on the first grade.

A grade holding the semaphore always finds a free connection, so grades
still queue in the semaphore as before, and the connection is taken before
the timeout backstop starts: waiting for one never counts against the
query. Every transaction is BEGIN READ ONLY and always rolls back, so
nothing a query sets (SET TimeZone, set_config(..., false)) reaches the next
grade on that connection. When the backstop fires, the connection is
cancelled and closed, and the pool replaces it. Connections are checked
before use, so a server restart doesn't fail grades. A missing psycopg_pool
fails startup instead of every grade.

postgres_gold_check.py passes a pool too. On BIRD dev it gives the same
1,402 of 1,534 matches as before; two questions swap status, both known
ties under ORDER BY ... LIMIT.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
Grades reuse a connection; concurrent grades never use more than the pool
size; a statement timeout returns its connection for reuse; a query's SET
doesn't reach later grades; waiting for a connection isn't charged to the
query; a backstop cancel discards the connection and leaves no query
running; verify() keeps one pool per server; a missing psycopg_pool fails
startup. The DSN-level tests now go through a pool.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
Declared like the Spark ones on main: psycopg[binary]>=3.2 and
psycopg_pool>=3.2 are plain dependencies of the bird_sql server,
imported only under dialect: postgres. prepare renders the column types
the loader creates, so the birdbench preparation project depends on
psycopg[binary] too. The READMEs drop the manual `uv pip install`.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
Main masks a row whose SQLite file is missing before any query runs, so
the routing and outage tests create the placeholder file. A grader
outage is now a masked verifier error (failure_reason unknown_error)
rather than a scored zero; the outage test checks both, and the
bird_sql README now says so. ServerClient comes from nemo_gym.client.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
A new table has no planner statistics until autovacuum reaches it,
about a minute after the load. A gold check started right after
loading dev timed out on two correct queries (card_games 345 and 346)
that run in well under a second once the tables are analyzed. With
postgres_admin_dsn, the server loads at startup and grades straight
away, so a correct answer could be scored as the model's timeout. The
loader now runs ANALYZE on each table inside the load transaction.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
PostgreSQL folds unquoted names to lower case, and the loader kept
BIRD's mixed-case column names, so an unquoted mixed-case column that
SQLite accepts failed as an undefined column. On saved predictions
from a model that often leaves names unquoted, lower-casing only the
columns raised dev accuracy from 32.99% to 45.96%.

The loader now lower-cases column names. Table names keep BIRD's case:
lower-casing them too broke answers that quote names like "Player"
(44.59% on the same predictions). prepare renders the lower-case names,
and the gold check spells identifiers as loaded. The gold check is
unchanged: 1,401 of 1,534 against 1,402 on the mixed-case load; the one
difference is a LIMIT over tied rows (question 1024), the same query in
both layouts. sqlglot's output before the three fixes now matches 1,051
golds instead of 537.

Each loaded db_id records layout 2 in _bird_meta.loaded. Startup, and a
load, stop with an error on a db_id loaded with an older layout, which
the grader would otherwise score as the model's mistakes, until it is
dropped and loaded again.

A separate commit so it can be dropped: it changes the names a model
sees.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
@copy-pr-bot

copy-pr-bot Bot commented Nov 12, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@nemo-automation-bot nemo-automation-bot Bot added the community-request Issue reported or requested by someone from the community label Nov 12, 2026
Brings the branch up to date with main for the lock-freshness check.
Main's 14 new commits change no lock this PR touches; every managed lock
still passes scripts/component_locks.py after the merge.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
@dhruvnathawani
dhruvnathawani self-requested a review November 12, 2026 17:40
@cmunley1

Copy link
Copy Markdown
Contributor

/ok to test d9bfee0

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 13, 2026 02:08
@yaoyu-33
yaoyu-33 requested a review from mpatel31415 November 13, 2026 02:33
except psycopg.errors.QueryCanceled:
raise TimeoutError(f"PostgreSQL query exceeded {timeout_s}s") from None
except psycopg.Error as e:
if e.sqlstate is None or e.sqlstate[:2] in _POSTGRES_INFRA_SQLSTATE_CLASSES:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A missing SQLSTATE doesn’t necessarily mean an infrastructure failure. For example, SELECT 'infinity'::date runs successfully in PostgreSQL, but psycopg cannot convert the result into a Python date and raises DataError with sqlstate=None.

Could we distinguish query/result-conversion errors from actual connection or server failures?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, thanks. Fixed in a294a0c: errors psycopg raises itself (no SQLSTATE) now count as a failed query unless the connection was lost or closed, so 'infinity'::date scores as wrong instead of crashing the grade. Server errors still use the SQLSTATE class, and tests cover both.

dhruvnathawani
dhruvnathawani previously approved these changes Nov 13, 2026

@dhruvnathawani dhruvnathawani left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

dhruvnathawani and others added 2 commits November 12, 2026 19:51
A query can succeed on the server and still fail in psycopg: SELECT
'infinity'::date returns 'infinity', which has no Python date, so
psycopg raises DataError. psycopg's own errors carry no SQLSTATE, and
the grader re-raised every error without one as an infrastructure
failure, so a model answer like this masked the row instead of scoring
it as wrong.

Errors without a SQLSTATE now raise only for a lost or closed
connection (OperationalError, InterfaceError) and otherwise score as
the query's fault. Server errors keep the SQLSTATE class rule, since
OperationalError also covers query faults such as 54001.

Tests: 'infinity'::date scores as pred_sql_error; a connection
terminated mid-query still raises; and a table of client and server
errors pins which side each lands on.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Abel Brown <[email protected]>
@dhruvnathawani
dhruvnathawani self-requested a review November 15, 2026 07:43
dhruvnathawani
dhruvnathawani previously approved these changes Nov 15, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the waiting-on-maintainers Waiting on maintainers to respond label Nov 15, 2026
@github-actions github-actions Bot added the sla:review-overdue Review response is over the one-business-day SLA label Nov 16, 2026
mpatel31415
mpatel31415 previously approved these changes Nov 16, 2026
@github-actions github-actions Bot removed the sla:review-overdue Review response is over the one-business-day SLA label Nov 16, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the feature New capabilities, enhancements, or enablement work label Nov 16, 2026
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers label Nov 16, 2026
@dhruvnathawani
dhruvnathawani self-requested a review November 17, 2026 01:59

@dhruvnathawani dhruvnathawani left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@svcnvidia-nemo-ci svcnvidia-nemo-ci added ready-to-merge PR is approved, current, and waiting for final CI or merge and removed waiting-on-maintainers Waiting on maintainers to respond labels Nov 17, 2026
@dkmisra
dkmisra enabled auto-merge November 17, 2026 22:44
@NVIDIA-NeMo NVIDIA-NeMo deleted a comment from copy-pr-bot Bot Nov 18, 2026
@dkmisra

dkmisra commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

/ok to test 89b76ff

@nemo-automation-bot
nemo-automation-bot Bot requested a review from a team November 18, 2026 01:43
@dkmisra
dkmisra added this pull request to the merge queue Nov 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44234950356

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 18, 2026
@dkmisra
dkmisra added this pull request to the merge queue Nov 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44246831968

@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 18, 2026
@dkmisra
dkmisra added this pull request to the merge queue Nov 18, 2026
@github-actions

Copy link
Copy Markdown
Contributor

🔄 Merge queue validation started! You can track the progress here: https://github.com/NVIDIA-NeMo/Gym/actions/runs/44248958938

Merged via the queue into NVIDIA-NeMo:main with commit 3d4fb51 Nov 18, 2026
66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:environment Individual environments, benchmarks, verifiers, and environment-specific resources servers community-request Issue reported or requested by someone from the community feature New capabilities, enhancements, or enablement work ready-to-merge PR is approved, current, and waiting for final CI or merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants