Internal system functions
=========================

Listed below are internal system functions. Many are used in the
creation of various views and we recommend that you do not use the
functions directly, but rather use the views that they serve.

General internal functions
--------------------------

bdr.bdr_get_commit_decisions
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Convenience routine to inspect shared memory state

Synopsis
^^^^^^^^

.. code:: sql

   bdr.bdr_get_commit_decisions(dbid OID,
               origin_node_id OID,
               origin_xid xid,
               local_xid xid,
               decision "char",
               decision_ts timestamptz,
               is_camo boolean)

bdr.bdr_track_commit_decision
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Save the transaction commit status in the shared memory hash table. This
is used by the upgrade scripts to transfer commit decisions saved in
bdr.node_pre_commit catalog to the shared memory hash table. This will
also be logged to the WAL and hence can be reloaded from WAL.

.. _synopsis-1:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.bdr_track_commit_decision(OID, xid, xid, "char", timestamptz, boolean);

bdr.consensus_kv_fetch
^^^^^^^^^^^^^^^^^^^^^^

Fetch value from the consistent KV Store in JSON format.

.. _synopsis-2:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.consensus_kv_fetch(IN key text) RETURNS jsonb

Parameters
^^^^^^^^^^

-  ``key`` — An arbitrary key to fetch.

Notes
^^^^^

This is an internal function, mainly used by HARP.

..  Warning::
   Don't use this function in user applications.

bdr.consensus_kv_store
^^^^^^^^^^^^^^^^^^^^^^

Stores value in the consistent KV Store.

Returns timestamp of the value expiration time. This depends on ``ttl``
. If ``ttl`` is ``NULL`` , then this returns ``infinity`` . If the value
was deleted, it returns ``-infinity`` .

.. _synopsis-3:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.consensus_kv_store(key text, value jsonb,
           prev_value jsonb DEFAULT NULL, ttl int DEFAULT NULL)

.. _parameters-1:

Parameters
^^^^^^^^^^

-  ``key`` — An arbitrary unique key to insert, update, or delete.

-  ``value`` — JSON value to store. If NULL, any existing record is
   deleted.

-  ``prev_value`` — If set, the write operation is done only if the
   current value is equal to ``prev_value`` .

-  ``ttl`` — Time to live of the new value, in milliseconds.

.. _notes-1:

Notes
^^^^^

This is an internal function, mainly used by HARP.

..  Warning::
   Don't use this function in user applications.

bdr.decode_message_payload
^^^^^^^^^^^^^^^^^^^^^^^^^^

PGD message payload function that decodes the consensus payloads to a
more human-readable output.Used primarily by the
``bdr.global_consensus_journal_details`` debug view.

bdr.decode_message_response_payload
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

PGD message payload function that decodes the consensus payloads to a
more human-readable output. Used primarily by the
``bdr.global_consensus_journal_details`` debug view.

bdr.difference_fix_origin_create
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Creates a replication origin with a given name passed as an argument but
adding a ``bdr_`` prefix. It returns the internal id of the origin. This
performs the same functionality as ``pg_replication_origin_create()`` ,
except this requires ``bdr_superuser`` rather than postgres superuser
permissions.

bdr.difference_fix_session_reset
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Marks the current session as not replaying from any origin, essentially
resetting the effect of ``bdr.difference_fix_session_setup()`` . It
returns void. This function has the same functionality as
``pg_replication_origin_session_reset()`` except this function requires
bdr_superuser rather than postgres superuser permissions.

.. _synopsis-4:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.difference_fix_session_reset()

bdr.difference_fix_session_setup
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Marks the current session as replaying from the current origin. The
function uses the pre-created ``bdr_local_only_origin`` local
replication origin implicitly for the session. It allows replay progress
to be reported and returns void. This function performs the same
functionality as ``pg_replication_origin_session_setup()`` except that
this function requires bdr_superuser rather than postgres superuser
permissions. The earlier form of the function,
``bdr.difference_fix_session_setup(text)`` , was deprecated and will be
removed in upcoming releases.

.. _synopsis-5:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.difference_fix_session_setup()

bdr.difference_fix_xact_set_avoid_conflict
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Marks the current transaction as replaying a transaction that committed
at LSN ‘0/0’ and timestamp ‘2000-01-01’. This function has the same
functionality as
``pg_replication_origin_xact_setup('0/0', '2000-01-01')`` except this
requires bdr_superuser rather than postgres superuser permissions.

.. _synopsis-6:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.difference_fix_xact_set_avoid_conflict()

bdr.drop_node
^^^^^^^^^^^^^

Drops a node.

..  Warning::
   This function isn't intended for regular use. Execute it only if instructed by Technical Support.

This function removes the metadata for a given node from the local
database. The node can be either:

-  The local node, in which case all the node metadata is removed,
   including information about remote nodes.

-  A remote node, in which case only metadata for that specific node is
   removed.

.. _synopsis-7:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.drop_node(node_name text, cascade boolean DEFAULT false, force boolean DEFAULT false)

.. _parameters-2:

Parameters
^^^^^^^^^^

-  ``node_name`` — Name of an existing node.

-  ``cascade`` — Deprecated, will be removed in the future.

-  ``force`` — Circumvents all sanity checks and forces the removal of
   all metadata for the given PGD node despite a possible danger of
   causing inconsistencies. Only Technical Support uses a forced node
   drop in case of emergencies related to parting.

.. _notes-2:

Notes
^^^^^

Before you run this, part the node using ``bdr.part_node()`` .

This function removes metadata for a given node from the local database.
The node can be the local node, in which case all the node metadata are
removed, including information about remote nodes. Or it can be the
remote node, in which case only metadata for that specific node is
removed.

..  Note::
   PGD can have a maximum of 1024 node records (both ACTIVE and PARTED) at one time because each node has a unique sequence number assigned to it, for use by snowflakeid and timeshard sequences. PARTED nodes aren't automatically cleaned up. If this becomes a problem, you can use this function to remove those records.

bdr.get_global_locks
^^^^^^^^^^^^^^^^^^^^

This function shows information about global locks held on the local
node.

Used to implement the ``bdr.global_locks`` view to provide a more
detailed overview of the locks.

bdr.get_node_conflict_resolvers
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Displays a text string of all the conflict resolvers on the local node.

bdr.get_slot_flush_timestamp
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Retrieves the timestamp of the last flush position confirmation for a
given replication slot.

Used internally to implement the ``bdr.node_slots`` view.

bdr.internal_alter_sequence_set_kind
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

PGD internal function used internally for replication of the various
function calls.

No longer used by the current version of PGD. Exists only for backward
compatibility during rolling upgrades.

bdr.internal_replication_set_add_table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

PGD internal function used internally for replication of the various
function calls.

No longer used by the current version of PGD. Exists only for backward
compatibility during rolling upgrades.

bdr.internal_replication_set_remove_table
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

PGD internal function used internally for replication of the various
function calls.

No longer used by the current version of PGD. Exists only for backward
compatibility during rolling upgrades.

bdr.internal_submit_join_request
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Submits a consensus request for joining a new node.

Needed by the PGD group reconfiguration internal mechanisms.

bdr.isolation_test_session_is_blocked
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

A helper function, extending (and actually invoking) the original
``pg_isolation_test_session_is_blocked`` with an added check for blocks
on global locks.

Used for isolation/concurrency tests.

bdr.local_node_info
^^^^^^^^^^^^^^^^^^^

This function displays information for the local node, needed by the PGD
group reconfiguration internal mechanisms.

The view ``bdr.local_node_summary`` provides similar information useful
for user consumption.

bdr.msgb_connect
^^^^^^^^^^^^^^^^

Function for connecting to the connection pooler of another node, used
by the consensus protocol.

bdr.msgb_deliver_message
^^^^^^^^^^^^^^^^^^^^^^^^

Function for sending messages to another node’s connection pooler, used
by the consensus protocol.

bdr.node_catchup_state_name
^^^^^^^^^^^^^^^^^^^^^^^^^^^

Convert catchup state code in name

.. _synopsis-8:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.node_catchup_state_name(catchup_state oid);

.. _parameters-3:

Parameters
^^^^^^^^^^

-  ``catchup_state`` — Oid code of the catchup state.

bdr.node_kind_name
^^^^^^^^^^^^^^^^^^

Return human friendly name of the node kind
(data|standby|witness|subscriber-only)

bdr.peer_state_name
^^^^^^^^^^^^^^^^^^^

This function transforms the node state (``node_state`` ) into a textual
representation and is used mainly to implement the ``bdr.node_summary``
view.

bdr.pg_xact_origin
^^^^^^^^^^^^^^^^^^

Returns origin id of a given transaction.

.. _synopsis-9:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.pg_xact_origin(xmin xid)

.. _parameters-4:

Parameters
^^^^^^^^^^

-  ``xid`` — Transaction id whose origin is returned,

bdr.request_replay_progress_update
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Requests the immediate writing of a ‘replay progress update’ Raft
message. It’s used mainly for test purposes but can be also used to test
if the consensus mechanism is working.

bdr.reset_relation_stats
^^^^^^^^^^^^^^^^^^^^^^^^

Returns a Boolean result after resetting the relation stats, as viewed
by ``bdr.stat_relation`` .

bdr.reset_subscription_stats
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Returns a Boolean result after resetting the statistics created by
subscriptions, as viewed by ``bdr.stat_subscription`` .

bdr.resynchronize_table_from_node
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Resynchronizes the relation from a remote node.

.. _synopsis-10:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.resynchronize_table_from_node(node_name name, relation regclass)

.. _parameters-5:

Parameters
^^^^^^^^^^

-  ``node_name`` — The node from which to copy or resync the relation
   data.

-  ``relation`` — The relation to copy from the remote node.

.. _notes-3:

Notes
^^^^^

This function acquires a global DML lock on the relation, truncates the
relation locally, and copies data into it from the remote node.

The relation must exist on both nodes with the same name and definition.

The following are supported:

-  Resynchronizing partitioned tables with identical partition
   definitions

-  Resynchronizing partitioned table to nonpartitioned table and vice
   versa

-  Resynchronizing referenced tables by temporarily dropping and
   recreating foreign key constraints

After running the function on a referenced table, if the referenced
column data no longer matches the referencing column values, it throws
an error. After resynchronizing the referencing table data, rerun the
function.

Furthermore, it supports resynchronization of tables with generated
columns by computing the generated column values locally after copying
the data from remote node.

Currently, row_filters are ignored by this function.

The ``bdr.resynchronize_table_from_node`` function can be executed only
by the owner of the table, provided the owner has bdr_superuser
privileges.

bdr.seq_nextval
^^^^^^^^^^^^^^^

Internal implementation of sequence increments.

Use this function instead of standard ``nextval`` in queries that
interact with :ref:`PGD global sequences <PGD global sequences>`  .

.. _notes-4:

Notes
^^^^^

The following are also internal PGD sequence manipulation functions.
``bdr.seq_currval`` and ``bdr.seq_lastval`` are used automatically.

bdr.show_subscription_status
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Retrieves information about the subscription status and is used mainly
to implement the ``bdr.subscription_summary`` view.

bdr.show_workers
^^^^^^^^^^^^^^^^

Information related to the bdr workers.

.. _synopsis-11:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.show_workers(
       worker_pid int,
       worker_role int,
       worker_role_name text,
       worker_subid oid

bdr.show_writers
^^^^^^^^^^^^^^^^

Function used in the ``bdr.writers`` view.

Task Manager Functions
----------------------

bdr.taskmgr_set_leader
^^^^^^^^^^^^^^^^^^^^^^

Request the given ``node`` to be the task manager leader node. The
leader node is responsible for creating new tasks (currently only
autopartition makes use of this facility). A witness node, a logical
standby or a subscriber-only node can’t become a leader. Such requests
will fail with an ERROR.

.. _synopsis-12:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.taskmgr_set_leader(node name, wait_for_completion boolean DEFAULT true);

bdr.taskmgr_get_last_completed_workitem
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Return the ``id`` of the last workitem successfully completed on all
nodes in the cluster.

.. _synopsis-13:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.taskmgr_get_last_completed_workitem();

bdr.taskmgr_work_queue_check_status
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Using this function, you can see the status of the background workers
that are doing their job to generate and finish the tasks.

The status can be seen through these views:
``bdr.taskmgr_work_queue_local_status``
``bdr.taskmgr_work_queue_global_status``

.. _synopsis-14:

Synopsis
^^^^^^^^

.. code:: sql

   bdr.taskmgr_work_queue_check_status(workid bigint
                              local boolean DEFAULT false);

.. _parameters-6:

Parameters
^^^^^^^^^^

-  ``workid`` — The key of the task.

-  ``local`` — Check the local status only.

.. _notes-5:

Notes
^^^^^

Taskmgr workers are always running in the background, even before the
``bdr.autopartition`` function is called for the first time. If an
invalid ``workid`` is used, the function returns ``unknown`` .
``In-progress`` is the typical status.
