PHPackages                             dimitri/libphp-pgq - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. dimitri/libphp-pgq

ActiveLibrary[Queues &amp; Workers](/categories/queues)

dimitri/libphp-pgq
==================

PHP level API for PGQ PostgreSQL Queuing solution

1.0.2(8y ago)172.2k8PHP

Since Feb 9Pushed 8y ago4 watchersCompare

[ Source](https://github.com/dimitri/libphp-pgq)[ Packagist](https://packagist.org/packages/dimitri/libphp-pgq)[ RSS](/packages/dimitri-libphp-pgq/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (4)DependenciesVersions (5)Used By (0)

PGQ-PHP
=======

[](#pgq-php)

Introduction
------------

[](#introduction)

This project provides PHP level API to use the PGQ SQL API.

Read more about PGQ here:

It then provides several kinds of Consumer that you’ll want to extends. All the consumer wants you to implement your own process\_event(&amp;$event) callback function, and will care about PGQ inner fonctionning.

PGQ is about producing events in a (work) queue and having one or more consumer get those events, in batches, and work on them. What the PHP class to be presented here do is fetching the batches and calling your own process\_event() function for each event.

It’s available on [packagist.org](https://packagist.org/packages/dimitri/libphp-pgq).

Tools
-----

[](#tools)

### SimpleLogger

[](#simplelogger)

This class handles logging with a loglevel: only messages more important than current loglevel are written to the logfile. The levels are, by increasing order of importance:

- DEBUG
- VERBOSE
- NOTICE
- WARNING
- ERROR
- FATAL

The SimpleLogger offers a method for each loglevel, taking a format string then arguments, just like sprintf() does. You have for example

```
$logger->debug("a message with a '%s' string", $str);
```

### SystemDaemon

[](#systemdaemon)

This class implements a System Daemon controlled with commands. The principle is to run in the background and loop forever, taking a $this-&gt;delay rest whenever possible between two loops.

SystemDaemon uses SimpleLogger in $this-&gt;log instance, and have it reopens its logfile at reload time. This means it plays well with logrotate, just don’t forget to reload the daemon at logrotate postprocess time.

The commands available for any SystemDaemon are: start, stop, kill, reload, restart, status, logless, logmore.

The difference between stop and kill is that in the first case the daemon will terminate current processing and only stops at next opportunity to sleep for $this-&gt;delay, while kill forces it into exiting (calling $this-&gt;stop()) first.

The reload command will have the daemon call user defined $this-&gt;reload() command at next sleep opportunity. This command could e.g. read a configuration file and change settings.

Commands logless and logmore are considered without delay and makes the used logger to get instantly more or less verbose by increasing or decreasing its loglevel. This is useful when you want to know exactly what a daemon is doing now, but can’t afford to restart it.

When implementing a SystemDaemon, you get to just implement $this-&gt;config() and $this-&gt;process() functions, which will get called in the infinite looping. The former will only get called when a reload has been ordered (SIGHUP), and the latter in each and every loop.

The SystemDaemon will also register its SimpleLogger instance as the PHP error handler, and consider quitting when confronted to a PHP FATAL errors (one of E\_STRICT, E\_PARSE, E\_CORE\_ERROR, E\_CORE\_WARNING, E\_COMPILE\_ERROR, E\_COMPILE\_WARNING), and will call user defined $this-&gt;php\_error\_hook() for PHP E\_ERROR or E\_USER\_ERROR level errors.

### PGQ

[](#pgq)

This class is abstract and has a public static method for each SQL function that the PGQ SQL API offers. If PHP knew about namespaces or modules, this would be a module.

Consumers
---------

[](#consumers)

Those class are the one handling how PGQ really works. They will get batches from the queue, get events from them, allow you to process the event by calling user defined $this-&gt;process\_event(&amp;$event) function, and call finish\_batch() as appropriate.

The $this-&gt;process\_event() function should return one of those return codes:

PGQ\_EVENT\_OKWhen the event processing is satisfactory.

PGQ\_EVENT\_FAILEDThe event is tagged as failed with $event-&gt;failed\_reason reason, and when using PGQEventRemoteConsumer the related processing done on remote database is rollbacked.

PGQ\_EVENT\_RETRYThe event is tagged as retry and will get reinserted into main queue after a minimum delay of $event-&gt;retry\_delay seconds. When using PGQEventRemoteConsumer the related processing done on the remote database is rollbacked.

PGQ\_ABORT\_BATCHAll current batch processing is canceled, at both remote and queue database when using some sort of RemoteConsumer. The batch is not finished, so you’ll get the events back at next run(s).

### PGQConsumer

[](#pgqconsumer)

This is a SystemDaemon which implements some more commands in order to install the queue and register the consumer.

When extending a PGQConsumer, you get to implement $this-&gt;config() and $this-&gt;process\_event() functions, and you’re done.

The constructor needs a queue name (qname), a consumer name (cname) and a database connection string.

The added commands are:

installCreate the queue qname and register a consumer cname.

uninstallUnregister the consumer cname and drop the queue qname.

checkTrue only if the queue qname exists and the consumer cname is registered.

create\_queueCreate the queue qname.

drop\_queueDrop the queue qname.

registerRegister the consumer cname.

unregisterUnregister the consumer cname.

failedPrint out a list of failed events for queue qname and consumer cname.

deleteDelete given event id, or all failed events if given all as an event id.

retryRetry given event id, or all failed events if given all as an event id.

### PGQInteractiveConsumer

[](#pgqinteractiveconsumer)

This class assume the looping will get done elsewhere, at the calling site for example. It consumes all available events (up until next\_batch() returns null).

The lag is not controlled by the implementer class but rather by the user of it.

Implementer have to call $this-&gt;process(), which will start consuming all available events and call the $this-&gt;process\_event(&amp;$event) hook for each event.

Internal design notePGQInteractiveRemoteConsumer needs to implement all PGQ methods for itself because of PHP limitation of extending from only one base class: PGQConsumer could not extends both SystemDaemon and PGQClass, where we should put the class abstraction over the API module.

### PGQRemoteConsumer

[](#pgqremoteconsumer)

PGQRemoteConsumer is a PGQConsumer controlling two PostgreSQL connections and which will handle COMMIT and ROLLBACK nicely on both of them.

This means you want to use PGQRemoteConsumer when you’re processing events from one database and apply changes to another one, the remote one. The PGQRemoteConsumer takes advantage of the fact that the remote processing is transactionnal (happens on a database) to get sure any COMMIT ed work on remote connection is associated with events properly consumed.

Any error in event consuming or remote processing will cause the current batch processing to be ROLLBACK ed at both points, meaning the events will get consumed again later.

### PGQEventRemoteConsumer

[](#pgqeventremoteconsumer)

When you need to be able to COMMIT or ROLLBACK both transaction at event level, PGQEventRemoteConsumer is what you’re after. It will use a subtransaction (SAVEPOINT) for each event and will be able to ROLLBACK TO SAVEPOINT on the remote side for any processing error related to a single event processing.

### PGQCoopConsumer

[](#pgqcoopconsumer)

This Consumer will share batches in between all its subconsumer processes. You need to register each of the subconsumer separately.

###  Health Score

35

—

LowBetter than 77% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity26

Limited adoption so far

Community17

Small or concentrated contributor base

Maturity66

Established project with proven stability

 Bus Factor1

Top contributor holds 71.4% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~7 days

Total

4

Last Release

3036d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/6000618?v=4)[Sergey](/maintainers/wapmorgan)[@wapmorgan](https://github.com/wapmorgan)

![](https://www.gravatar.com/avatar/452246db1be91f48249209e8985456f83636886fe1b118ef1f5b5e2cd6050f5f?d=identicon)[dimitri](/maintainers/dimitri)

---

Top Contributors

[![dimitri](https://avatars.githubusercontent.com/u/102900?v=4)](https://github.com/dimitri "dimitri (30 commits)")[![wapmorgan](https://avatars.githubusercontent.com/u/6000618?v=4)](https://github.com/wapmorgan "wapmorgan (8 commits)")[![lepidosteus](https://avatars.githubusercontent.com/u/810589?v=4)](https://github.com/lepidosteus "lepidosteus (4 commits)")

### Embed Badge

![Health badge](/badges/dimitri-libphp-pgq/health.svg)

```
[![Health](https://phpackages.com/badges/dimitri-libphp-pgq/health.svg)](https://phpackages.com/packages/dimitri-libphp-pgq)
```

###  Alternatives

[league/geotools

Geo-related tools PHP 7.3+ library

1.4k5.5M29](/packages/league-geotools)[illuminate/bus

The Illuminate Bus package.

6045.5M504](/packages/illuminate-bus)[uecode/qpush-bundle

Asynchronous processing for Symfony using Push Queues

1672.5M2](/packages/uecode-qpush-bundle)[jayazhao/think-queue-rabbitmq

为 ThinkPHP5.1 队列增加 RabbitMQ 驱动

141.5k](/packages/jayazhao-think-queue-rabbitmq)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
