pg_ortools
pg_extensions · Constraint optimization extension using the HiGHS MIP/LP solver from PostgreSQL, with optional async execution.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 101077 | pg_ortools | pg_extensions | 0.3.5 | FUNC | Matroid-SAL-1.0 | Rust |
PostgreSQL · 14, 15, 16, 17, 18
Usage
Sources:
- extensions/pg_ortools/Cargo.toml
- extensions/pg_ortools/src/worker.rs
- crates/pg_bgworker/src/supervision.rs
- CHANGELOG.md
- extensions/pg_ortools/pgbrew.toml
- LICENSE
- Official pg_ortools README
- Extension control file
- SQL API implementation
- 0.3.3 upgrade
- 0.3.5 upgrade
pg_ortools version 0.3.5 defines mixed-integer and linear optimization problems in SQL and solves them with HiGHS. Use it for bounded assignment, allocation, feasibility, and objective-optimization workloads that are naturally represented as integer or Boolean variables and linear constraints.
Core Workflow
For asynchronous work, solve returns a job ID; use solve_status, cancel_solve, LISTEN pgortools_solve, and get_solution to manage it. solve_greedy returns the first feasible solution, while solve_local, solve_with_strategy, and solve_auto expose alternative search strategies. The declarative solve_assignment and parse_assignment functions build common table-driven assignment models.
Operational Notes
The extension stores problems, variables, constraints, jobs, and solutions in pgortools. Constraint expressions support linear comparisons, but the official README explicitly excludes !=. The default solver time limit is controlled by pg_ortools.solver_time_limit; worker enablement, polling interval, and worker database are server settings described as restart-sensitive upstream. Confirm that the background worker is running before relying on async jobs. The control file is non-relocatable but not superuser-only, and the install SQL grants public DML on its schema tables; review privileges before multi-tenant use.
Version 0.3.0 Boundary
This is an unsupported proof of concept under Matroid Source Available License 1.0. APIs may change. Version 0.3.0 is the new upgrade baseline: earlier 0.2.0 installations require a rehearsed data migration/recreation, not ordinary ALTER EXTENSION UPDATE. Back up data and dependencies before following that destructive upstream path.
Preload pg_ortools and restart for asynchronous jobs. The default build now also includes the Pumpkin CP-SAT scheduling engine; solve_cp is time-bounded and cancellable. HiGHS and its SQL workflow remain a separate solver path. The added eidos_catalog_* interfaces describe the live optimization catalog.
Current Release and Upgrade
Extension version 0.3.2 uses pg_ortools.database for background workers. Create the extension in that database; a worker now waits instead of repeatedly exiting when it is absent. Changing restart-sensitive worker configuration requires a restart. pg_ortools.solver_database remains a deprecated alias. From extension 0.3.0 onward, install matching files and use ALTER EXTENSION UPDATE. Earlier versions still require the migration described above. Withdrawn repository 0.4.0 bottles must be replaced by 0.5.0; upstream bottles do not imply Pigsty package availability.
Worker Supervision
Version 0.3.2 adds pgortools.worker_status() and the superuser-only pgortools.reset_workers(). Status reports worker name, state, failures, restarts, PID, time of entry into the current state and last failure. pg_ortools.max_worker_failures defaults to 10 consecutive failures; 0 retries indefinitely. Restart delays back off from 5 to 60 seconds, and reaching the limit leaves a failed worker idle until reset.
Install matching libraries, restart PostgreSQL to initialize the new shared memory, and update the extension SQL in the configured worker database. Inspect the failure before resetting; a reset does not repair its cause.
Local Search in 0.3.3
Repository release 0.5.1 contains extension 0.3.3. The typed constraint assignment with {"each": "slot"} gives every shift one employee while allowing an employee to take multiple shifts. Local search transposes the problem internally and maps results back; no_overlap, skill_match, costs and pin_current keep the caller’s orientation, while group_balance is rejected in this mode. Invalid typed-constraint configuration now fails the solve rather than being silently skipped.
The 0.3.2 to 0.3.3 SQL upgrade changes no objects. Install matching libraries and update the extension; asynchronous workers still require preload and restart. Source features cover PostgreSQL 14–18; published bottles target PostgreSQL 18.
Idle Workers in 0.3.4
Repository release 0.7.2 contains extension 0.3.4. Read-only or empty-claim polls no longer allocate a transaction ID merely because they use the SPI bridge; an actual row write still does. This removes empty commit WAL from idle polling. Install matching libraries, restart the preloaded workers, and update the SQL extension. The existing privilege, external-service and pre-0.3.0 migration boundaries remain.
Worker Signals in 0.3.5
Repository release 0.7.5 contains extension 0.3.5 and an empty SQL upgrade script. Worker-created solver threads now block signals before starting. This prevents a signal delivered on a non-main thread from panicking in PostgreSQL FFI and causing the postmaster to restart every session. Install matching libraries, restart the preloaded workers, and run ALTER EXTENSION pg_ortools UPDATE; no SQL objects change.