37.24. element_types #

Представление element_types показывает дескрипторы типов элементов массива. Когда столбец таблицы, атрибут составного типа, параметр или результат функции объявлены с типом массива, соответствующее представление информационной схемы будет содержать только ARRAY в столбце data_type. Чтобы получить информацию о типе элемента массива, можно связать соответствующее представление с данным. Например, просмотреть столбцы таблицы с типами данных и типами элементов массива (если это применимо) можно так:

SELECT c.column_name, c.data_type, e.data_type AS element_type
FROM information_schema.columns c LEFT JOIN information_schema.element_types e
     ON ((c.table_catalog, c.table_schema, c.table_name, 'TABLE', c.dtd_identifier)
       = (e.object_catalog, e.object_schema, e.object_name, e.object_type, e.collection_type_identifier))
WHERE c.table_schema = '...' AND c.table_name = '...'
ORDER BY c.ordinal_position;

Это представление показывает только те объекты, к которым имеет доступ текущий пользователь, являясь владельцем или имея некоторые права.

Таблица 37.22. Столбцы element_types

Тип столбца

Описание

object_catalog sql_identifier

Имя базы данных, содержащей объект, связанный с описываемым массивом (всегда текущая база)

object_schema sql_identifier

Имя схемы, содержащей объект, связанный с описываемым массивом

object_name sql_identifier

Имя объекта, связанного с описываемым массивом

object_type character_data

Тип объекта, связанного с описываемым массивом: TABLE (массив задействован в столбце этой таблицы), USER-DEFINED TYPE (массив задействован в атрибуте составного типа), DOMAIN (массив задействован в домене), ROUTINE (массив задействован в типе данных параметра или результата функции).

collection_type_identifier sql_identifier

Идентификатор дескриптора типа данных для описываемого массива. Его можно использовать для соединения со столбцами dtd_identifier других представлений информационной схемы.

data_type character_data

Тип данных элементов массива, если это встроенный тип, либо USER-DEFINED (в этом случае тип определяется в udt_name и связанных столбцах).

character_maximum_length cardinal_number

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

character_octet_length cardinal_number

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

character_set_catalog sql_identifier

Относится к функциональности, отсутствующей в PostgreSQL

character_set_schema sql_identifier

Относится к функциональности, отсутствующей в PostgreSQL

character_set_name sql_identifier

Относится к функциональности, отсутствующей в PostgreSQL

collation_catalog sql_identifier

Имя базы данных, содержащей правило сортировки типа элемента (это всегда текущая база), либо NULL, если это правило по умолчанию или тип элемента несортируемый

collation_schema sql_identifier

Имя схемы, содержащей правило сортировки типа элемента, либо NULL, если это правило по умолчанию или тип элемента несортируемый

collation_name sql_identifier

Имя правила сортировки типа элемента, либо NULL, если это правило по умолчанию или тип элемента несортируемый

numeric_precision cardinal_number

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

numeric_precision_radix cardinal_number

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

numeric_scale cardinal_number

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

datetime_precision cardinal_number

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

interval_type character_data

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

interval_precision cardinal_number

Всегда NULL, так как эта информация неприменима к типам элементов массива в PostgreSQL

domain_default character_data

Ещё не реализовано

udt_catalog sql_identifier

Имя базы данных, в которой определён тип данных элемента (всегда текущая база)

udt_schema sql_identifier

Имя схемы, в которой определён тип данных элемента

udt_name sql_identifier

Имя типа данных элемента

scope_catalog sql_identifier

Относится к функциональности, отсутствующей в PostgreSQL

scope_schema sql_identifier

Относится к функциональности, отсутствующей в PostgreSQL

scope_name sql_identifier

Относится к функциональности, отсутствующей в PostgreSQL

maximum_cardinality cardinal_number

Всегда NULL, так как массивы имеют неограниченную максимальную ёмкость в PostgreSQL

dtd_identifier sql_identifier

Идентификатор дескриптора типа данных элемента. В настоящее время бесполезен.


51.74. pg_locks

The view pg_locks provides access to information about the locks held by active processes within the database server. See Chapter 13 for more discussion of locking.

pg_locks contains one row per active lockable object, requested lock mode, and relevant process. Thus, the same lockable object might appear many times, if multiple processes are holding or waiting for locks on it. However, an object that currently has no locks on it will not appear at all.

There are several distinct types of lockable objects: whole relations (e.g., tables), individual pages of relations, individual tuples of relations, transaction IDs (both virtual and permanent IDs), and general database objects (identified by class OID and object OID, in the same way as in pg_description or pg_depend). Also, the right to extend a relation is represented as a separate lockable object, as is the right to update pg_database.datfrozenxid. Also, advisory locks can be taken on numbers that have user-defined meanings.

Table 51.75. pg_locks Columns

NameTypeReferencesDescription
locktypetext  Type of the lockable object: relation, extend, frozenid, page, tuple, transactionid, virtualxid, object, userlock, or advisory
databaseoidpg_database.oid OID of the database in which the lock target exists, or zero if the target is a shared object, or null if the target is a transaction ID
relationoidpg_class.oid OID of the relation targeted by the lock, or null if the target is not a relation or part of a relation
pageinteger  Page number targeted by the lock within the relation, or null if the target is not a relation page or tuple
tuplesmallint  Tuple number targeted by the lock within the page, or null if the target is not a tuple
virtualxidtext  Virtual ID of the transaction targeted by the lock, or null if the target is not a virtual transaction ID
transactionidxid  ID of the transaction targeted by the lock, or null if the target is not a transaction ID
classidoidpg_class.oid OID of the system catalog containing the lock target, or null if the target is not a general database object
objidoidany OID column OID of the lock target within its system catalog, or null if the target is not a general database object
objsubidsmallint  Column number targeted by the lock (the classid and objid refer to the table itself), or zero if the target is some other general database object, or null if the target is not a general database object
virtualtransactiontext  Virtual ID of the transaction that is holding or awaiting this lock
pidinteger  Process ID of the server process holding or awaiting this lock, or null if the lock is held by a prepared transaction
modetext Name of the lock mode held or desired by this process (see Section 13.3.1 and Section 13.2.3)
grantedboolean True if lock is held, false if lock is awaited
fastpathboolean True if lock was taken via fast path, false if taken via main lock table

granted is true in a row representing a lock held by the indicated process. False indicates that this process is currently waiting to acquire this lock, which implies that at least one other process is holding or waiting for a conflicting lock mode on the same lockable object. The waiting process will sleep until the other lock is released (or a deadlock situation is detected). A single process can be waiting to acquire at most one lock at a time.

Throughout running a transaction, a server process holds an exclusive lock on the transaction's virtual transaction ID. If a permanent ID is assigned to the transaction (which normally happens only if the transaction changes the state of the database), it also holds an exclusive lock on the transaction's permanent transaction ID until it ends. When a process finds it necessary to wait specifically for another transaction to end, it does so by attempting to acquire share lock on the other transaction's ID (either virtual or permanent ID depending on the situation). That will succeed only when the other transaction terminates and releases its locks.

Although tuples are a lockable type of object, information about row-level locks is stored on disk, not in memory, and therefore row-level locks normally do not appear in this view. If a process is waiting for a row-level lock, it will usually appear in the view as waiting for the permanent transaction ID of the current holder of that row lock.

Advisory locks can be acquired on keys consisting of either a single bigint value or two integer values. A bigint key is displayed with its high-order half in the classid column, its low-order half in the objid column, and objsubid equal to 1. The original bigint value can be reassembled with the expression (classid::bigint << 32) | objid::bigint. Integer keys are displayed with the first key in the classid column, the second key in the objid column, and objsubid equal to 2. The actual meaning of the keys is up to the user. Advisory locks are local to each database, so the database column is meaningful for an advisory lock.

pg_locks provides a global view of all locks in the database cluster, not only those relevant to the current database. Although its relation column can be joined against pg_class.oid to identify locked relations, this will only work correctly for relations in the current database (those for which the database column is either the current database's OID or zero).

The pid column can be joined to the pid column of the pg_stat_activity view to get more information on the session holding or awaiting each lock, for example

SELECT * FROM pg_locks pl LEFT JOIN pg_stat_activity psa
    ON pl.pid = psa.pid;

Also, if you are using prepared transactions, the virtualtransaction column can be joined to the transaction column of the pg_prepared_xacts view to get more information on prepared transactions that hold locks. (A prepared transaction can never be waiting for a lock, but it continues to hold the locks it acquired while running.) For example:

SELECT * FROM pg_locks pl LEFT JOIN pg_prepared_xacts ppx
    ON pl.virtualtransaction = '-1/' || ppx.transaction;

While it is possible to obtain information about which processes block which other processes by joining pg_locks against itself, this is very difficult to get right in detail. Such a query would have to encode knowledge about which lock modes conflict with which others. Worse, the pg_locks view does not expose information about which processes are ahead of which others in lock wait queues, nor information about which processes are parallel workers running on behalf of which other client sessions. It is better to use the pg_blocking_pids() function (see Table 9.63) to identify which process(es) a waiting process is blocked behind.

The pg_locks view displays data from both the regular lock manager and the predicate lock manager, which are separate systems; in addition, the regular lock manager subdivides its locks into regular and fast-path locks. This data is not guaranteed to be entirely consistent. When the view is queried, data on fast-path locks (with fastpath = true) is gathered from each backend one at a time, without freezing the state of the entire lock manager, so it is possible for locks to be taken or released while information is gathered. Note, however, that these locks are known not to conflict with any other lock currently in place. After all backends have been queried for fast-path locks, the remainder of the regular lock manager is locked as a unit, and a consistent snapshot of all remaining locks is collected as an atomic action. After unlocking the regular lock manager, the predicate lock manager is similarly locked and all predicate locks are collected as an atomic action. Thus, with the exception of fast-path locks, each lock manager will deliver a consistent set of results, but as we do not lock both lock managers simultaneously, it is possible for locks to be taken or released after we interrogate the regular lock manager and before we interrogate the predicate lock manager.

Locking the regular and/or predicate lock manager could have some impact on database performance if this view is very frequently accessed. The locks are held only for the minimum amount of time necessary to obtain data from the lock managers, but this does not completely eliminate the possibility of a performance impact.