Skip to content

Commit c0dd926

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

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

components/lock.rst

Lines changed: 37 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,25 @@ 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 stores
447+
locks and does not expires.
448+
430449
.. _lock-store-redis:
431450

432451
RedisStore
@@ -526,6 +545,7 @@ Remote Stores
526545
Remote stores (:ref:`MemcachedStore <lock-store-memcached>`,
527546
:ref:`MongoDbStore <lock-store-mongodb>`,
528547
:ref:`PdoStore <lock-store-pdo>`,
548+
:ref:`PostgreSqlStore <lock-store-pgsql>`,
529549
:ref:`RedisStore <lock-store-redis>` and
530550
:ref:`ZookeeperStore <lock-store-zookeeper>`) use a unique token to recognize
531551
the true owner of the lock. This token is stored in the
@@ -735,6 +755,20 @@ have synchronized clocks.
735755
To ensure locks don't expire prematurely; the TTLs should be set with
736756
enough extra time to account for any clock drift between nodes.
737757

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

@@ -839,6 +873,7 @@ are still running.
839873

840874
.. _`a maximum of 1024 bytes in length`: https://docs.mongodb.com/manual/reference/limits/#Index-Key-Limit
841875
.. _`ACID`: https://en.wikipedia.org/wiki/ACID
876+
.. _`Advisory Locks`: https://www.postgresql.org/docs/current/explicit-locking.html
842877
.. _`Data Source Name (DSN)`: https://en.wikipedia.org/wiki/Data_source_name
843878
.. _`Doctrine DBAL Connection`: https://github.com/doctrine/dbal/blob/master/src/Connection.php
844879
.. _`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)