pgmq is a PostgreSQL-based lightweight message queue extension. Its API design is similar to Amazon SQS and RSMQ, providing users with simple yet powerful message processing capabilities. It is fully implemented through SQL/PLpgSQL without any backend worker processes or external dependencies, greatly simplifying deployment and maintenance.
pgmq adopts a "queue as table" architecture, storing messages in regular tables in JSONB format. It supports visibility timeout, delayed delivery, long polling, batch operations (send/archive/delete), and rich monitoring metrics, making it an ideal choice for building event-driven architectures, handling background tasks, and decoupling microservices.
- Asynchronous task processing: place time-consuming operations such as sending emails, generating reports, and image processing into the queue for background consumers to handle asynchronously, improving system response speed.
- Service decoupling: in microservice architectures, use message queues to transmit events, reducing direct dependencies between services.
- Data pipeline: act as a lightweight data pipeline to reliably transfer data between different systems or processing stages.
- Delayed tasks: use the delay delivery feature to implement timed reminders and timed task triggers.
Create and delete extension
Execute the following SQL command to enable the pgmq extension.
If no longer needed, execute the following command to uninstall it. Uninstalling the extension removes all related queues, messages, and functions.
- Create queue (pgmq.create)
- Create a new message queue. Each queue physically corresponds to a data table.
- Delete queue (pgmq.drop_queue)
- Delete a message queue and all its related data tables (including primary and archive queues).
- Send message (pgmq.send)
- Send a message to the specified queue.
- queue_name (text): required, the name of the target queue.
- msg (jsonb): required, the message body to send, must be in JSONB format.
- delay (integer): optional, message delay delivery time (seconds). Messages will only be visible to consumers after the specified delay.
- Batch send messages (pgmq.send_batch)
- Send multiple messages to the queue at once to improve performance.
- msgs (jsonb[]): required, a JSONB array containing multiple messages.
- Read messages (pgmq.read)
- Read a batch of messages from the queue without deleting them immediately. After reading, messages enter an “invisible” state until the visibility timeout (vt) ends.
- vt (integer): required, visibility timeout duration (seconds). During this period, other consumers cannot read these messages.
- qty (integer): required, the maximum number of messages to read.
- If successfully retrieved, the function returns a collection containing message details, including msg_id, and message. If no new messages arrive within the specified time, the long polling mode will wait. Otherwise, it returns an empty set.
- The pop action combines reading and deleting the message into an atomic step. It reads a message from the queue and deletes it immediately.
- This applies to simple scenarios without message acknowledgment mechanisms.
- Archive message (pgmq.archive)
- After processing, the message moves from the primary queue to the archive queue. This is useful for auditing and issue tracing. Archive tables are prefixed with a_.
- msg_id (bigint)/msg_ids (bigint[]): required, the single or multiple message IDs to archive.
- After archiving, you can query the archive table pgmq.a_my_queue to view historical messages.
- Delete message (pgmq.delete)
- Permanently delete one or more messages. These messages will not be archived.
pgmq provides views and functions to monitor the queue state.
- pgmq.metrics_all() view: provides real-time metrics for all queues, such as queue name, total message quantity, visible message quantity, and invisible message quantity.
- pgmq.metrics(queue_name) function: retrieves detailed metrics for a specified single queue.