Locking
=======

To prevent conflicts between various operations in the cluster, PGD uses
a distributed locking mechanism to ensure the only one node can perform
a specific operation at a time.

This is particularly important in a distributed environment where
multiple nodes may attempt to modify the same data concurrently. As PGD
Essential is a single-write node cluster, it does not have to deal with
distributed locking in the same way, as there is only one node that can
perform write operations at any time. PGD Expaned, however, has multiple
write nodes, and so it must will always use distributed locking to
ensure integerity.

Kinds of Locks
--------------

PGD uses several kinds of locks to manage concurrent access to data and
resources in the cluster:

DDL locking
^^^^^^^^^^^

DDL (Data Definition Language) locks are used to manage access to
database objects such as tables, indexes, and schemas. When a DDL
operation is performed, a lock is acquired on the object being modified
to prevent other operations from interfering with the change. This
ensures that the structure of the database remains consistent and
prevents conflicts between concurrent DDL operations. Read more about
DDL locking in the :ref:`DDL Locking reference <Locking>`  documentation.

DML locking
^^^^^^^^^^^

DML locking is closly related to DDL locking and is used to add an extra
layer of protection to a DDL operation being replicated by also halting
any DML operations that would conflict with the DDL operation. Again,
this is only needed in a multi-write node cluster, and is not used in
PGD Essential.

Which locks are used when?
^^^^^^^^^^^^^^^^^^^^^^^^^^

The locks used in PGD depend on the type of operation being performed
and the configuration of the cluster. In general, DDL locks are used for
schema changes, while DML locks are used for data modifications. A full
list of the locks used in PGD can be found in the `DDL command handling matrix <https://www.enterprisedb.com/docs/pgd/latest/reference/ddl/ddl-command-handling>`_ 
documentation.
