Skip to content

Commit a37d1b4

Browse files
committed
Add documentation for Postgresql
1 parent eab61bb commit a37d1b4

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

components/lock.rst

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,7 @@ Store Scope Blocking Expiring Sharing
293293
:ref:`MemcachedStore <lock-store-memcached>` remote no yes no
294294
:ref:`MongoDbStore <lock-store-mongodb>` remote no yes no
295295
:ref:`PdoStore <lock-store-pdo>` remote no yes no
296+
:ref:`PostgreSqlStore <lock-store-pgsql>` remote yes yes yes
296297
:ref:`RedisStore <lock-store-redis>` remote no yes yes
297298
:ref:`SemaphoreStore <lock-store-semaphore>` local yes no no
298299
:ref:`ZookeeperStore <lock-store-zookeeper>` remote no no no
@@ -340,8 +341,6 @@ support blocking, and expects a TTL to avoid stalled locks::
340341

341342
Memcached does not support TTL lower than 1 second.
342343

343-
.. _lock-store-pdo:
344-
345344
.. _lock-store-mongodb:
346345

347346
MongoDbStore
@@ -403,6 +402,7 @@ MongoDB Connection String:
403402
The ``collection`` querystring parameter is not part of the `MongoDB Connection String`_ definition.
404403
It is used to allow constructing a ``MongoDbStore`` using a `Data Source Name (DSN)`_ without ``$options``.
405404

405+
.. _lock-store-pdo:
406406

407407
PdoStore
408408
~~~~~~~~
@@ -427,6 +427,29 @@ You can also create this table explicitly by calling the
427427
:method:`Symfony\\Component\\Lock\\Store\\PdoStore::createTable` method in
428428
your code.
429429

430+
.. _lock-store-pgsql:
431+
432+
PostgreSqlStore
433+
~~~~~~~~~~~~~~~
434+
435+
The PostgreSqlStore uses `Advisory Locks`_ provided by PostgreSQL. It requires a
436+
`PDO`_ connection, a `Doctrine DBAL Connection`_, or a
437+
`Data Source Name (DSN)`_. it nativly supports blocking, as weel as sharing
438+
locks.
439+
440+
use Symfony\Component\Lock\Store\PostgreSqlStore;
441+
442+
// a PDO, a Doctrine DBAL connection or DSN for lazy connecting through PDO
443+
$databaseConnectionOrDSN = 'postgresql://myuser:mypassword@localhost:5634/lock';
444+
$store = new PostgreSqlStore($databaseConnectionOrDSN);
445+
446+
In opposite to the ``PdoStore``, the ``PostgreSqlStore`` does not need a table to
447+
stores locks and does not expires.
448+
449+
.. versionadded:: 5.2
450+
451+
PostgreSqlStore were introduced in Symfony 5.2.
452+
430453
.. _lock-store-redis:
431454

432455
RedisStore
@@ -526,6 +549,7 @@ Remote Stores
526549
Remote stores (:ref:`MemcachedStore <lock-store-memcached>`,
527550
:ref:`MongoDbStore <lock-store-mongodb>`,
528551
:ref:`PdoStore <lock-store-pdo>`,
552+
:ref:`PostgreSqlStore <lock-store-pgsql>`,
529553
:ref:`RedisStore <lock-store-redis>` and
530554
:ref:`ZookeeperStore <lock-store-zookeeper>`) use a unique token to recognize
531555
the true owner of the lock. This token is stored in the
@@ -735,6 +759,20 @@ have synchronized clocks.
735759
To ensure locks don't expire prematurely; the TTLs should be set with
736760
enough extra time to account for any clock drift between nodes.
737761

762+
PostgreSqlStore
763+
~~~~~~~~~~~~~~~
764+
765+
The PdoStore relies on the `Advisory Locks`_ properties of the PostgreSQL
766+
database. That means that by using :ref:`PostgreSqlStore <lock-store-pgsql>`
767+
the locks will be automatically released at the end of the session in case the
768+
client cannot unlock for any reason.
769+
770+
If the PostgreSQL service or the machine hosting it restarts, every lock would
771+
be lost without notifying the running processes.
772+
773+
If the TCP connection is lost, the PostgreSQL may release locks without
774+
notifying the application.
775+
738776
RedisStore
739777
~~~~~~~~~~
740778

@@ -839,6 +877,7 @@ are still running.
839877

840878
.. _`a maximum of 1024 bytes in length`: https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
841879
.. _`ACID`: https://en.wikipedia.org/wiki/ACID
880+
.. _`Advisory Locks`: https://www.postgresql.org/docs/current/explicit-locking.html
842881
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
843882
.. _`Doctrine DBAL Connection`: https://github.com/doctrine/dbal/blob/master/src/Connection.php
844883
.. _`Expire Data from Collections by Setting TTL`: https://docs.mongodb.com/manual/tutorial/expire-data/

lock.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ this behavior by using the ``lock`` key like:
5858
lock: 'sqlite:///%kernel.project_dir%/var/lock.db'
5959
lock: 'mysql:host=127.0.0.1;dbname=lock'
6060
lock: 'pgsql:host=127.0.0.1;dbname=lock'
61+
lock: 'pgsql+advisory:host=127.0.0.1;dbname=lock'
6162
lock: 'sqlsrv:server=localhost;Database=test'
6263
lock: 'oci:host=localhost;dbname=test'
6364
lock: '%env(LOCK_DSN)%'
@@ -106,6 +107,8 @@ this behavior by using the ``lock`` key like:
106107
107108
<framework:resource>pgsql:host=127.0.0.1;dbname=lock</framework:resource>
108109
110+
<framework:resource>pgsql+advisory:host=127.0.0.1;dbname=lock</framework:resource>
111+
109112
<framework:resource>sqlsrv:server=localhost;Database=test</framework:resource>
110113
111114
<framework:resource>oci:host=localhost;dbname=test</framework:resource>
@@ -137,6 +140,7 @@ this behavior by using the ``lock`` key like:
137140
'lock' => 'sqlite:///%kernel.project_dir%/var/lock.db',
138141
'lock' => 'mysql:host=127.0.0.1;dbname=lock',
139142
'lock' => 'pgsql:host=127.0.0.1;dbname=lock',
143+
'lock' => 'pgsql+advisory:host=127.0.0.1;dbname=lock',
140144
'lock' => 'sqlsrv:server=localhost;Database=test',
141145
'lock' => 'oci:host=localhost;dbname=test',
142146
'lock' => '%env(LOCK_DSN)%',

0 commit comments

Comments
 (0)