Skip to content
Percona PostgreSQL Blog· Jobin Augustine·

PgQ and PgQue: Workflow Engines You Might Need in PostgreSQL

PgQ and PgQue: Workflow Engines you might need in PostgreSQL

Key Takeaway

Percona's post presents PgQ and PgQue, PostgreSQL-native workflow engines that append events to a partitioned log instead of updating status flags in place, avoiding bloat and concurrency bottlenecks.

Why It Matters

For queue tables bloated by high-frequency status updates, PgQue moves consumption position to consumers over an append-only log, at the cost of tick-based cursors and no per-key ordering.

Technical Breakdown · Distilled from the original source

Architecture

  • Common FOR UPDATE SKIP LOCKED queues cause bloat, concurrency bottlenecks and degradation; PgQue inserts events into an append-only partitioned table and leaves position tracking to consumers.
  • PgQ's mechanics are carried over: snapshot-based batching, tick generation, TRUNCATE-based rotation and per-consumer cursors. PgQ was created by Marko Kreen in Skytools and has run in production since 2007.
  • Consumption does not delete events; ack advances only that consumer's cursor, so multiple consumers read the same stored events.

Trade-offs vs Kafka

  • No partitions, hence no per-key ordering; the cursor points at a tick (time slice), not an offset.
  • Space is reclaimed by rotation and truncation only after the slowest consumer passes; the guaranteed rewind window is one rotation period, 2 hours by default, and a stalled consumer blocks reclamation rather than losing data.
  • PgQue adds per-consumer redelivery (nacked events re-appended as private copies), a built-in DLQ, and cooperative consumers (Pulsar Shared-style, marked experimental).

Deployment and API

  • No binary extensions; pgque.ticker_loop() is a procedure that commits between iterations, triggered by pg_cron once per second, with pgque.config.tick_period_ms defaulting to 100 ms and tunable to exact divisors of 1000 ms.
  • Each tick emits pg_notify('pgque_', tick_id); roles are pgque_writer, pgque_reader and pgque_admin.
  • APIs include send(), send_batch(), subscribe(), receive(), ack(), nack(), send_idem(), register_subconsumer() and receive_coop(); observability functions load separately from devel/sql/experimental/observability.sql, may change, and are executable by PUBLIC.
  • Client libraries exist for Python, Go, TypeScript and Ruby; ack is per batch, so failed messages must be nacked first.

0.2.x fixes

  • In 0.2.0, receive() with default max_return 100 silently returned only the first 100 events of a larger batch; ack then skipped the rest, losing data.
  • 0.2.1 raises SQLSTATE 54000 and returns no events; nothing is lost, but the batch does not shrink, so the consumer stalls until the limit is raised.
  • 0.2.2 changes only client libraries, raising dedicated overflow errors (PgqueReceiveOverflowError, ReceiveOverflowError) so high-level consumers stop immediately; treat max_return as a memory ceiling, raise it on overflow, and alert on SQLSTATE 54000 and consumer lag.
Source: Percona PostgreSQL Blog· percona.com

PG Radar summarizes verified changes and impact. Read the original post for full technical details and code samples.

Read Official Source

Related PostgreSQL Reference