{"id":305380,"date":"2020-06-15T21:00:40","date_gmt":"2020-06-15T21:00:40","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=305380"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=305380","title":{"rendered":"DBA: \u043a\u0442\u043e \u0441\u043a\u0440\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0437\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/tensor\/blog\/506360\/\">\u0412 <a href=\"https:\/\/habr.com\/ru\/post\/503680\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043c\u044b \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0441\u043d\u0438\u043c\u0430\u0442\u044c <b>\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/b> \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 PostgreSQL \u0440\u043e\u0432\u043d\u043e \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442. \u0412 \u044d\u0442\u043e\u0439 \u2014 \u043d\u0430\u0443\u0447\u0438\u043c\u0441\u044f \u0442\u0440\u0430\u043a\u0442\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u043e\u0435 \u0438 \u0443\u0437\u043d\u0430\u0432\u0430\u0442\u044c, <b>\u043a\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u043a\u0440\u044b\u0432\u0430\u0442\u044c\u0441\u044f<\/b> \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432, \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/dc\/p1\/ua\/dcp1uaiz4dxf5demrrzgsu_pqgg.png\"><br \/>  <a name=\"habracut\"><\/a><br \/>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0434\u043e\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0438, \u0447\u0442\u043e\u0431\u044b \u043e\u043d \u0441\u0442\u0430\u043b \u0447\u0443\u0442\u044c \u0443\u0434\u043e\u0431\u043d\u0435\u0435 \u0438 \u043d\u0430\u0433\u043b\u044f\u0434\u043d\u0435\u0435, \u0430 \u0432\u0441\u044f \u0441\u0430\u043c\u0430\u044f \u0432\u0430\u0436\u043d\u0430\u044f \u0434\u043b\u044f \u0432\u043e\u0441\u043f\u0440\u0438\u044f\u0442\u0438\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e\u043a\u0430\u0437\u044b\u0432\u0430\u043b\u0430\u0441\u044c \u0432 \u043e\u0434\u043d\u043e\u043c \u043c\u0435\u0441\u0442\u0435:<\/p>\n<div class=\"spoiler\" role=\"button\" tabindex=\"0\">                         <b class=\"spoiler_title\">\u043c\u0435\u0433\u0430\u0437\u0430\u043f\u0440\u043e\u0441<\/b>                         <\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"sql\">WITH lm(ld, lr) AS (   VALUES     ('AccessShareLock', '{AccessExclusiveLock}'::text[])   , ('RowShareLock', '{ExclusiveLock,AccessExclusiveLock}'::text[])   , ('RowExclusiveLock', '{ShareLock,ShareRowExclusiveLock,ExclusiveLock,AccessExclusiveLock}'::text[])   , ('ShareUpdateExclusiveLock', '{ShareUpdateExclusiveLock,ShareLock,ShareRowExclusiveLock,ExclusiveLock,AccessExclusiveLock}'::text[])   , ('ShareLock', '{RowExclusiveLock,ShareUpdateExclusiveLock,ShareRowExclusiveLock,ExclusiveLock,AccessExclusiveLock}'::text[])   , ('ShareRowExclusiveLock', '{RowExclusiveLock,ShareUpdateExclusiveLock,ShareLock,ShareRowExclusiveLock,ExclusiveLock,AccessExclusiveLock}'::text[])   , ('ExclusiveLock', '{RowShareLock,RowExclusiveLock,ShareUpdateExclusiveLock,ShareLock,ShareRowExclusiveLock,ExclusiveLock,AccessExclusiveLock}'::text[])   , ('AccessExclusiveLock', '{AccessShareLock,RowShareLock,RowExclusiveLock,ShareUpdateExclusiveLock,ShareLock,ShareRowExclusiveLock,ExclusiveLock,AccessExclusiveLock}'::text[]) ) , locks AS (   SELECT     (       locktype     , database     , relation     , page     , tuple     , virtualxid     , transactionid::text::bigint     , classid     , objid     , objsubid     ) target   , *   FROM     pg_locks ) , ld AS (   SELECT     *   FROM     locks   WHERE     NOT granted ) , lr AS (   SELECT     *   FROM     locks   WHERE     target::text = ANY(ARRAY(       SELECT DISTINCT         target::text       FROM         ld     )) AND     granted ) , lcx AS (   SELECT     lr.target   , ld.pid ldp   , ld.mode ldm   , lr.pid lrp   , lr.mode lrm   FROM     ld   JOIN     lr       ON lr.pid &lt;&gt; ld.pid AND         lr.target IS NOT DISTINCT FROM ld.target ) , cfl AS ( SELECT   lc.locktype &quot;type&quot; , CASE lc.locktype     WHEN 'relation' THEN       ARRAY[relation]     WHEN 'extend' THEN       ARRAY[relation]     WHEN 'page' THEN       ARRAY[relation, page]     WHEN 'tuple' THEN       ARRAY[relation, page, tuple]     WHEN 'transactionid' THEN       ARRAY[transactionid::text::oid]     WHEN 'virtualxid' THEN       string_to_array(virtualxid::text, '\/')::oid[]     WHEN 'object' THEN       ARRAY[classid, objid, objsubid]     WHEN 'userlock' THEN       ARRAY[classid]     WHEN 'advisory' THEN       ARRAY[classid, objid, objsubid]   END target , nullif(lc.pid = lcx.ldp, FALSE) as locked , lc.pid , regexp_replace(lc.mode, 'Lock$', '') &quot;mode&quot; , nullif(lc.granted, TRUE) &quot;granted&quot; , nullif(lc.target IS NOT DISTINCT FROM lcx.target, FALSE) &quot;conflict&quot; FROM   lcx JOIN   locks lc     ON lc.pid IN (lcx.ldp, lcx.lrp) ) SELECT   cfl.* , CASE     WHEN &quot;type&quot; NOT IN ('virtualxid', 'transactionid') THEN target[1]::regclass   END relname , cl.relkind FROM   cfl LEFT JOIN LATERAL(     SELECT       *     FROM       pg_class     WHERE       cfl.type = 'relation' AND       oid = target[1]     LIMIT 1   ) cl     ON TRUE ORDER BY                                            -- \u0441\u043e\u0440\u0442\u0438\u0440\u0443\u0435\u043c ...   locked                                            -- \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043a\u043e\u0433\u043e \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0442 , pid                                               -- \u043f\u043e \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 (1 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 = 1 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f) , CASE &quot;type&quot;                                       -- \u043f\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0443 \u0442\u0438\u043f\u043e\u0432 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a     WHEN 'virtualxid'    THEN 0     WHEN 'transactionid' THEN 1     WHEN 'relation'      THEN 2     WHEN 'tuple'         THEN 3     WHEN 'object'        THEN 4     WHEN 'advisory'      THEN 5   END , CASE relkind                                       -- \u043f\u043e \u043f\u0440\u0438\u043d\u0430\u0434\u043b\u0435\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0435     WHEN 'r' THEN cl.oid     WHEN 't' THEN regexp_replace(cl.relname, E'^.*\\\\D(\\\\d+)$', E'\\\\1', '')::oid     WHEN 'i' THEN (       SELECT         indrelid       FROM         pg_index       WHERE         indexrelid = cl.oid       LIMIT 1     )     WHEN 'S' THEN (       SELECT         (           SELECT             adrelid           FROM             pg_attrdef           WHERE             oid = dp.objid         )       FROM         pg_depend dp       WHERE         (refclassid, refobjid) = ('pg_class'::regclass, cl.oid) AND         (deptype, classid) = ('n', 'pg_attrdef'::regclass)       LIMIT 1     )   END   -- https:\/\/postgrespro.ru\/docs\/postgresql\/12\/catalog-pg-class , CASE relkind                                       -- \u043f\u043e \u0442\u0438\u043f\u0443 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0411\u0414     WHEN 'r' THEN 0 -- relation     WHEN 'm' THEN 1 -- materialized view     WHEN 'p' THEN 2 -- partitioned table     WHEN 'f' THEN 3 -- foreign table     WHEN 't' THEN 4 -- TOAST     WHEN 'i' THEN 5 -- index     WHEN 'I' THEN 6 -- partitioned index     WHEN 'S' THEN 7 -- sequence     WHEN 'c' THEN 8 -- composite type     WHEN 'v' THEN 9 -- view   END , CASE                                               -- \u043f\u043e \u0442\u0438\u043f\u0443 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u2013 PK \u0432\u043f\u0435\u0440\u0435\u0434     WHEN relkind = 'i' THEN       NOT (         SELECT           indisprimary         FROM           pg_index         WHERE           indexrelid = cl.oid         LIMIT 1       )   END , cl.relname                                         -- \u043f\u043e \u0438\u043c\u0435\u043d\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 , CASE mode                                          -- \u043f\u043e \u043f\u0440\u0438\u043e\u0440\u0438\u0442\u0435\u0442\u0443 \u0440\u0435\u0436\u0438\u043c\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438     WHEN 'AccessExclusive'      THEN 0     WHEN 'Exclusive'            THEN 1     WHEN 'ShareRowExclusive'    THEN 2     WHEN 'Share'                THEN 3     WHEN 'ShareUpdateExclusive' THEN 4     WHEN 'RowExclusive'         THEN 5     WHEN 'RowShare'             THEN 6     WHEN 'AccessShare'          THEN 7   END; <\/code><\/pre>\n<p>  <\/div>\n<\/p><\/div>\n<p>  \u0421 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043d\u0435\u0433\u043e \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0440\u0438\u043c\u0435\u0440\u044b \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u0432\u0430\u043c \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c\u0441\u044f \u0432 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0440\u0430\u0431\u043e\u0442\u0435:<\/p>\n<ol>\n<li><a href=\"#01\">\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446<\/a><\/li>\n<li><a href=\"#02\">\u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432<\/a><\/li>\n<li><a href=\"#03\">\u0441\u0430\u043c\u043e\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 (CONCURRENTLY vs \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f)<\/a><\/li>\n<li><a href=\"#04\">\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e UNIQUE-\u0438\u043d\u0434\u0435\u043a\u0441\u0443<\/a><\/li>\n<li><a href=\"#05\">\u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438<\/a><\/li>\n<li><a href=\"#06\">\u043e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/a><\/li>\n<\/ol>\n<p>  \u041d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u0438\u0435 \u0432\u043e\u043e\u0431\u0449\u0435 \u0440\u0435\u0441\u0443\u0440\u0441\u044b \u043c\u043e\u0433\u0443\u0442 \u0432\u044b\u0437\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442, \u0438 \u0447\u0435\u043c \u043e\u043d\u0438 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u0446\u0438\u0440\u0443\u044e\u0442\u0441\u044f \u0432 <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/view-pg-locks\"><code><b>pg_locks<\/b><\/code><\/a>:  <\/p>\n<div class=\"scrollable-table\">\n<table>\n<tr>\n<td><b>locktype<\/b><\/td>\n<td><b>\u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435<\/b><\/td>\n<td><b>ID \u0440\u0435\u0441\u0443\u0440\u0441\u0430<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>\u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0435 (\u0442\u0430\u0431\u043b\u0438\u0446\u0430)<\/code><\/td>\n<td><code>(relation)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>extend<\/code><\/td>\n<td><code><a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/storage-toast\">\u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f (TOAST<\/a>)<\/code><\/td>\n<td><code>(relation)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>page<\/code><\/td>\n<td><code>\u0441\u0442\u0440\u0430\u043d\u0438\u0446\u0430 (\u0431\u043b\u043e\u043a \u0434\u0430\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446\u044b\/\u0438\u043d\u0434\u0435\u043a\u0441\u0430)<\/code><\/td>\n<td><code>(relation, page)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>tuple<\/code><\/td>\n<td><code>\u043a\u043e\u0440\u0442\u0435\u0436 (\u0437\u0430\u043f\u0438\u0441\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u044b\/\u0438\u043d\u0434\u0435\u043a\u0441\u0430)<\/code><\/td>\n<td><code>(relation, page, tuple)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>transactionid<\/code><\/td>\n<td><code>\u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438<\/code><\/td>\n<td><code>(transactionid)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>virtualxid<\/code><\/td>\n<td><code>\u0432\u0438\u0440\u0442\u0443\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u0434\u0435\u043d\u0442\u0438\u0444\u0438\u043a\u0430\u0442\u043e\u0440<\/code><\/td>\n<td><code>(virtualxid)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>object<\/code><\/td>\n<td><code>\u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442<\/code><\/td>\n<td><code>(classid, objid, objsubid)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>userlock<\/code><\/td>\n<td><code>\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u0441\u043a\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430<\/code><\/td>\n<td><code>(classid)<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>advisory<\/code><\/td>\n<td><code>\u0440\u0435\u043a\u043e\u043c\u0435\u043d\u0434\u0430\u0442\u0435\u043b\u044c\u043d\u0430\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430<\/code><\/td>\n<td><code>(classid, objid, objsubid)<\/code><\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u0412 \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u0435 \u0441\u043b\u0443\u0447\u0430\u0435\u0432 \u00ab\u0432 \u0431\u044b\u0442\u0443\u00bb \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442, \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u0445 \u0438 \u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u044f\u0445. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u044b <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/explicit-locking#LOCKING-TABLES\">\u0440\u0435\u0436\u0438\u043c\u043e\u0432 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/a> \u043a\u0430\u043a \u043d\u0430 \u043c\u0430\u0442\u0440\u0438\u0446\u0443 \u043c\u0435\u0448\u0430\u044e\u0449\u0438\u0445 \u0434\u0440\u0443\u0433 \u0434\u0440\u0443\u0433\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/8e\/dl\/f9\/8edlf9pcodujbxodfuztber0nnu.png\"><\/p>\n<p>  \u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 \u0440\u0435\u0430\u043b\u044c\u043d\u044b\u0445 \u043f\u0440\u0438\u043c\u0435\u0440\u0430\u0445.<\/p>\n<p>  <a name=\"01\"><\/a><\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446<\/h2>\n<p>  <\/p>\n<pre><code class=\"sql\">-- tx1 BEGIN; CREATE TABLE tbl(pk integer, val integer);     -- tx2     CREATE TABLE tbl(pk integer, val integer);<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a:  <\/p>\n<div class=\"scrollable-table\">\n<table>\n<tr>\n<td>type<\/td>\n<td>mode<\/td>\n<td>relname<\/td>\n<td>relkind<\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>RowExclusive<\/code><\/td>\n<td><code><b>pg_type<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>RowExclusive<\/code><\/td>\n<td><code><b>pg_class<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>object<\/code><\/td>\n<td><code>AccessShare<\/code><\/td>\n<td><code>pg_namespace<\/code><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>AccessExclusive<\/code><\/td>\n<td><b><code>oid<\/code> \u0442\u0430\u0431\u043b\u0438\u0446\u044b (\u0447\u0438\u0441\u043b\u043e)<\/b><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td><code>object<\/code><\/td>\n<td><code>AccessShare<\/code><\/td>\n<td><code>pg_namespace<\/code><\/td>\n<td><\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u0422\u0430\u0431\u043b\u0438\u0446\u0430, \u043a\u0430\u043a \u0438 \u043b\u044e\u0431\u043e\u0439 \u00ab\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u043d\u044b\u0439\u00bb \u043e\u0431\u044a\u0435\u043a\u0442 \u0432 PostgreSQL \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u043a\u0430\u043a \u0437\u0430\u043f\u0438\u0441\u044c \u0432 <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/catalog-pg-class\"><code>\u0442\u0430\u0431\u043b\u0438\u0446\u0435 <b>pg_class<\/b><\/code><\/a>. \u041e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u0434\u0440\u0443\u0433 \u043e\u0442 \u0434\u0440\u0443\u0433\u0430 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0438\u043f\u043e\u0432 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043e\u043b\u044f <code>pg_class.relkind<\/code>:  <\/p>\n<blockquote>\n<ul>\n<li>r = \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 (Relation)<\/li>\n<li>i = \u0438\u043d\u0434\u0435\u043a\u0441 (Index)<\/li>\n<li>S = \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c (Sequence)<\/li>\n<li>t = \u0442\u0430\u0431\u043b\u0438\u0446\u0430 TOAST<\/li>\n<li>v = \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 (View)<\/li>\n<li>m = \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 (Materialized view)<\/li>\n<li>c = \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0439 \u0442\u0438\u043f (Composite type)<\/li>\n<li>f = \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u044f\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 (Foreign table)<\/li>\n<li>p = \u0441\u0435\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430 (Partitioned table)<\/li>\n<li>I = \u0441\u0435\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 (partitioned Index)<\/li>\n<\/ul>\n<\/blockquote>\n<p>  \u041d\u043e, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0437\u0430\u043f\u0438\u0441\u044c \u0441 \u0442\u043e\u0447\u043a\u0438 \u0437\u0440\u0435\u043d\u0438\u044f \u043d\u0430\u0448\u0435\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430 \u0435\u0449\u0435 \u00ab\u043d\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442\u00bb, \u0440\u0430\u0441\u043a\u0430\u0441\u0442\u043e\u0432\u043a\u0430 \u0435\u0435 <code>oid<\/code> \u0432 <code>regclass<\/code> \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u0439\u0442\u0438, \u0438 \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0432\u043c\u0435\u0441\u0442\u043e \u0438\u043c\u0435\u043d\u0438 \u0442\u043e\u043b\u044c\u043a\u043e \u043b\u0438\u0448\u044c \u0447\u0438\u0441\u043b\u043e.<\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043f\u043e\u043c\u0438\u043c\u043e \u0437\u0430\u043f\u0438\u0441\u0438 \u043e \u0441\u0430\u043c\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0432 \u043c\u0435\u0442\u0430\u0434\u0430\u043d\u043d\u044b\u0435 (\u0432 <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/catalog-pg-type\"><code><b>pg_type<\/b><\/code><\/a>) \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0430\u0431\u043b\u0438\u0447\u043d\u044b\u0439 \u0442\u0438\u043f, \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u043a\u043e\u0442\u043e\u0440\u043e\u043c\u0443 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043a\u0430\u0441\u0442\u043e\u0432\u0430\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u043f\u0440\u044f\u043c\u043e \u0432 \u0444\u043e\u0440\u043c\u0430\u0442 \u0437\u0430\u043f\u0438\u0441\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b:<\/p>\n<pre><code class=\"sql\">SELECT NULL::tbl;<\/code><\/pre>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/5k\/mh\/vd\/5kmhvdacbr_tb9lhjuwacy9w8jc.png\"><\/p>\n<p>  <a name=\"02\"><\/a><\/p>\n<h2>\u0421\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u0432<\/h2>\n<p>  <\/p>\n<pre><code class=\"sql\">-- tx1 BEGIN; CREATE UNIQUE INDEX idx ON tbl(pk);     -- tx2     CREATE UNIQUE INDEX idx ON tbl(pk);<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a:  <\/p>\n<div class=\"scrollable-table\">\n<table>\n<tr>\n<td>type<\/td>\n<td>mode<\/td>\n<td>relname<\/td>\n<td>relkind<\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>RowExclusive<\/code><\/td>\n<td><code><b>pg_class<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>Share<\/code><\/td>\n<td><code>tbl<\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>AccessExclusive<\/code><\/td>\n<td><code>oid<\/code> \u0438\u043d\u0434\u0435\u043a\u0441\u0430 (\u0447\u0438\u0441\u043b\u043e)<\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>Share<\/code><\/td>\n<td><code>tbl<\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>AccessExclusive<\/code><\/td>\n<td><code>oid<\/code> \u0438\u043d\u0434\u0435\u043a\u0441\u0430 (\u0447\u0438\u0441\u043b\u043e)<\/td>\n<td><\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0441\u043d\u043e\u0432\u0430 \u0432\u0438\u0434\u0438\u043c \u0442\u043e\u0447\u043d\u043e \u0442\u0430\u043a\u0443\u044e \u0436\u0435 RowExclusive-\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 <code><b>pg_class<\/b><\/code>, \u043a\u043e\u0442\u043e\u0440\u0430\u044f \u043d\u0435 \u0434\u0430\u0435\u0442 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u043e\u0434\u043d\u043e\u0438\u043c\u0435\u043d\u043d\u044b\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u044b. \u041d\u043e \u0443\u0436\u0435 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 <code>pg_type<\/code> \u2014 \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u043d\u0435 \u043f\u043e\u0440\u043e\u0436\u0434\u0430\u0435\u0442 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0441\u043f\u0435\u0446\u0442\u0438\u043f\u043e\u0432. \u0412\u043c\u0435\u0441\u0442\u043e \u044d\u0442\u043e\u0433\u043e \u043c\u044b \u0432\u0438\u0434\u0438\u043c Share-\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u043d\u0430\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u0435 \u043d\u0430 \u043e\u0431\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b (\u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043e\u043d\u0430 \u043e\u0434\u043d\u0430 \u0438 \u0442\u0430 \u0436\u0435).<\/p>\n<p>  \u0417\u0430\u043c\u0435\u0442\u0438\u043c, \u0447\u0442\u043e Share \u043c\u0435\u0436\u0434\u0443 \u0441\u043e\u0431\u043e\u0439 \u043d\u0435 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u0443\u044e\u0442, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c <b>\u0441\u0440\u0430\u0437\u0443 \u0434\u0432\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435<\/b>:<\/p>\n<pre><code class=\"sql\">-- tx1 BEGIN; CREATE UNIQUE INDEX idx1 ON tbl(pk);     -- tx2     CREATE UNIQUE INDEX idx2 ON tbl(pk); -- \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442, \u0438 \u043d\u0438\u043a\u0430\u043a\u0438\u0445 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a!<\/code><\/pre>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/a6\/kg\/s3\/a6kgs372urdjwh86dn-ocjl7sma.png\"><\/p>\n<p>  <a name=\"03\"><\/a><\/p>\n<h2>\u0421\u0430\u043c\u043e\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 (CONCURRENTLY vs \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f)<\/h2>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0435\u0449\u0435 \u043e\u0434\u0438\u043d \u0438\u043d\u0434\u0435\u043a\u0441 \u2014 \u043d\u043e \u043d\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0442\u0430\u043a, \u0430 <code>CONCURRENTLY<\/code>, \u0447\u0442\u043e\u0431\u044b \u043d\u0438\u043a\u043e\u043c\u0443 \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0442\u0430\u0431\u043b\u0438\u0446\u0435.<\/p>\n<p>  \u041d\u0443 \u0430 \u0435\u0441\u043b\u0438 \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u0438\u0437 PL-\u043a\u043e\u0434\u0430, \u0433\u0434\u0435 <code>CONCURRENTLY<\/code>-\u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043d\u0435\u043b\u044c\u0437\u044f, \u043a\u0430\u043a \u0438 \u0432 \u043b\u044e\u0431\u043e\u0439 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0438?.. \u041a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435, \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/dblink\">\u043c\u043e\u0434\u0443\u043b\u0435\u043c dblink<\/a> \u0434\u043b\u044f \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u044f <b>\u043a \u044d\u0442\u043e\u0439 \u0436\u0435 \u0411\u0414<\/b>!<\/p>\n<pre><code class=\"sql\">DO $$ BEGIN   SELECT dblink_exec('dbname=' || current_database() || ' user=' || current_user, 'CREATE INDEX CONCURRENTLY idx_cic ON tbl(pk);'); END$$;<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a:  <\/p>\n<div class=\"scrollable-table\">\n<table>\n<tr>\n<td>type<\/td>\n<td>mode<\/td>\n<td>relname<\/td>\n<td>relkind<\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code><b>virtualxid<\/b><\/code><\/td>\n<td><code><b>Share<\/b><\/code><\/td>\n<td><code>---<\/code><\/td>\n<td><\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code><b>virtualxid<\/b><\/code><\/td>\n<td><code><b>Exclusive<\/b><\/code><\/td>\n<td><code>---<\/code><\/td>\n<td><\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u0412\u043e\u0442 \u0432\u0430\u043c \u043c\u0430\u043b\u0435\u043d\u044c\u043a\u0430\u044f \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u044f, \u043f\u043e\u0447\u0435\u043c\u0443 \u0441 dblink \u043d\u0430\u0434\u043e \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u044c\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0430\u043a\u043a\u0443\u0440\u0430\u0442\u043d\u043e. \u0417\u0434\u0435\u0441\u044c \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u043a\u043b\u0430\u0441\u0441\u0438\u0447\u0435\u0441\u043a\u0443\u044e \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044e \u00ab\u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f \u0436\u0434\u0435\u0442 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044e\u00bb \u2014 \u043f\u0440\u043e\u0441\u0442\u043e \u0435\u0435 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f. \u0418 \u043d\u0435 \u0434\u043e\u0436\u0434\u0435\u0442\u0441\u044f \u043d\u0438\u043a\u043e\u0433\u0434\u0430 \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u0441\u0430\u043c DO-\u0431\u043b\u043e\u043a \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u0435\u0439.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/-d\/q6\/vp\/-dq6vp5jer-cb8ekcldzbgj_puw.png\"><\/p>\n<p>  <a name=\"04\"><\/a><\/p>\n<h2>\u0411\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e UNIQUE-\u0438\u043d\u0434\u0435\u043a\u0441\u0443<\/h2>\n<p>  \u041f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043e\u0442 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438 \u043a \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u043c\u0438 \u0437\u0430\u043f\u0438\u0441\u044f\u043c\u0438 \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0440\u0430\u043d\u0437\u0430\u043a\u0446\u0438\u044f\u0445 <b>\u0437\u0430\u043f\u0438\u0441\u0438 \u0441 \u043e\u0434\u0438\u043d\u0430\u043a\u043e\u0432\u044b\u043c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043a\u043b\u044e\u0447\u0430<\/b>.<\/p>\n<pre><code class=\"sql\">-- tx1 BEGIN; INSERT INTO tbl(pk, val)VALUES(1, 1);     -- tx2     INSERT INTO tbl(pk, val)VALUES(1, 1);<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a:  <\/p>\n<div class=\"scrollable-table\">\n<table>\n<tr>\n<td>type<\/td>\n<td>mode<\/td>\n<td>relname<\/td>\n<td>relkind<\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code><b>RowExclusive<\/b><\/code><\/td>\n<td><code><b>tbl<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code><b>RowExclusive<\/b><\/code><\/td>\n<td><code><b>tbl<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code><b>RowExclusive<\/b><\/code><\/td>\n<td><code><b>idx<\/b><\/code><\/td>\n<td><code>i<\/code><\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u0422\u0443\u0442 \u043c\u044b \u0443\u0436\u0435 \u0432\u0438\u0434\u0438\u043c RowExclusive \u043d\u0435 \u043d\u0430 \u043e\u0434\u043d\u043e\u0439 \u0438\u0437 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446, \u0430 \u043d\u0430 \u043d\u0430\u0448\u0435\u0439. \u0410 \u0432 \u043f\u0430\u0440\u0443 \u043a \u043d\u0435\u0439 \u2014 \u0438 <b>\u043d\u0430 \u0442\u043e\u043c \u0438\u043d\u0434\u0435\u043a\u0441\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0437\u0432\u0430\u043b \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b<\/b> [\u043d\u0435]\u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/jp\/wl\/uk\/jpwluksawxqavuefmomyhxdcwnc.png\"><\/p>\n<p>  <a name=\"05\"><\/a><\/p>\n<h2>\u041e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438<\/h2>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0442\u0435\u043f\u0435\u0440\u044c \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043d\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 <code>UPDATE<\/code>&#8216;\u043d\u0443\u0442\u044c:<\/p>\n<pre><code class=\"sql\">-- tx1 BEGIN; UPDATE tbl SET val = val + 1 WHERE pk = 1;     -- tx2     UPDATE tbl SET val = val + 1 WHERE pk = 1;<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a:  <\/p>\n<div class=\"scrollable-table\">\n<table>\n<tr>\n<td>type<\/td>\n<td>mode<\/td>\n<td>relname<\/td>\n<td>relkind<\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code><b>tuple<\/b><\/code><\/td>\n<td><code><b>Exclusive<\/b><\/code><\/td>\n<td><code><b>tbl<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>RowExclusive<\/code><\/td>\n<td><code>tbl<\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code>RowExclusive<\/code><\/td>\n<td><code>tbl<\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u043e\u0431\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430\u043b\u043e\u0436\u0438\u043b\u0438 RowExclusive \u043d\u0430 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0440\u0430\u0437 \u0445\u043e\u0442\u044f\u0442 \u0442\u0443\u0434\u0430 \u0447\u0442\u043e-\u0442\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u0442\u044c. \u041d\u043e \u0432\u0430\u0436\u043d\u0435\u0435, \u0447\u0442\u043e \u0447\u0442\u043e \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0436\u0434\u0435\u0442 tuple-\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u043d\u0430 \u044d\u0442\u043e\u0439 \u0436\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u0435. \u041f\u0440\u0438\u0447\u0435\u043c \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0434\u0432\u0430 \u0447\u0438\u0441\u043b\u0430 \u0432 ID \u043e\u0431\u044a\u0435\u043a\u0442\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 (\u0438\u043b\u0438 <b>\u043f\u043e\u043b\u044f page, tuple<\/b> \u0432 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 <code>pg_locks<\/code>) \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043d\u0430\u043c \u0443\u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u0437\u0430 \u0437\u0430\u043f\u0438\u0441\u044c \u043c\u044b \u0442\u0443\u0442 \u0436\u0434\u0435\u043c:<\/p>\n<pre><code class=\"sql\">SELECT * FROM &lt;relation&gt; WHERE ctid = '(&lt;page&gt;,&lt;tuple&gt;)';<\/code><\/pre>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/yz\/wc\/f8\/yzwcf8j0thdbkkd80l-p743mmk0.png\"><\/p>\n<p>  <a name=\"06\"><\/a><\/p>\n<h2>\u041e\u0431\u0441\u043b\u0443\u0436\u0438\u0432\u0430\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/h2>\n<p>  \u041c\u044b \u0442\u0443\u0442 \u043e\u0447\u0435\u043d\u044c \u043c\u043d\u043e\u0433\u043e \u0447\u0435\u0433\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0443\u0436\u0435 \u043d\u0430\u043c\u0443\u0441\u043e\u0440\u0438\u043b\u0438 \u2014 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0435\u0435 \u0437\u0430\u0447\u0438\u0441\u0442\u0438\u043c. \u041d\u043e \u0437\u0430\u043f\u0438\u0441\u044c-\u0442\u043e \u043f\u043e\u043a\u0430 \u0432 \u043d\u0435\u0435 \u0438\u0434\u0435\u0442:<\/p>\n<pre><code class=\"sql\">-- tx1 BEGIN; INSERT INTO tbl(pk, val)VALUES(2, 2);     -- tx2     TRUNCATE TABLE tbl;<\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0444\u0438\u043b\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a:  <\/p>\n<div class=\"scrollable-table\">\n<table>\n<tr>\n<td>type<\/td>\n<td>mode<\/td>\n<td>relname<\/td>\n<td>relkind<\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u043e\u0436\u0438\u0434\u0430\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code><b>AccessExclusive<\/b><\/code><\/td>\n<td><code><b>tbl<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<tr>\n<td colspan=\"4\"><b>\u0431\u043b\u043e\u043a\u0438\u0440\u0443\u044e\u0449\u0438\u0439 PID<\/b><\/td>\n<\/tr>\n<tr>\n<td><code>relation<\/code><\/td>\n<td><code><b>RowExclusive<\/b><\/code><\/td>\n<td><code><b>tbl<\/b><\/code><\/td>\n<td><code>r<\/code><\/td>\n<\/tr>\n<\/table>\n<\/div>\n<p>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0437\u0430\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0445\u043e\u0447\u0435\u0442 AccessExclusive \u2014 \u0441\u0430\u043c\u044b\u0439 \u00ab\u0442\u044f\u0436\u0435\u043b\u044b\u0439\u00bb \u0440\u0435\u0436\u0438\u043c, \u043d\u043e \u043d\u0435 \u043c\u043e\u0436\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0438\u0437-\u0437\u0430 RowExclusive. \u0421\u043c\u043e\u0442\u0440\u0438\u043c \u043d\u0430 <b>\u043a\u0430\u0440\u0442\u0438\u043d\u043a\u0443 \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432<\/b> \u2014 \u0438 \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c, \u0447\u0442\u043e \u044d\u0442\u043e \u043a\u0442\u043e-\u0442\u043e \u0438\u0437 <code>{TRUNCATE, VACUUM FULL, ...}<\/code> \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0441\u044f c <code>{INSERT, UPDATE, DELETE}<\/code>, \u0434\u0430\u0436\u0435 \u043d\u0435 \u0437\u0430\u0433\u043b\u044f\u0434\u044b\u0432\u0430\u044f \u0432 <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/monitoring-stats#PG-STAT-ACTIVITY-VIEW\"><code><b>pg_stat_activity<\/b><\/code><\/a>.<\/p>\n<p>  \u041f\u0440\u0438\u043c\u0435\u0440 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/jm\/uv\/ww\/jmuvwwoechridakg-j5wb_zc9iy.png\"><\/p>\n<p>  \u041d\u0430\u0434\u0435\u044e\u0441\u044c, \u0435\u0441\u043b\u0438 \u0442\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u0435\u0441\u044c \u0441 \u043a\u0430\u043a\u043e\u0439-\u0442\u043e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439, \u0432\u0441\u0435\u0433\u0434\u0430 \u0441\u043c\u043e\u0436\u0435\u0442\u0435 <b>\u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c \u0435\u0435 \u043f\u0440\u0438\u0447\u0438\u043d\u0443<\/b>, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0438\u0441\u0445\u043e\u0434\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0438\u0433\u0434\u0435 \u0432 \u043b\u043e\u0433\u0438 \u043d\u0435 \u043f\u043e\u043f\u0430\u043b\u0438\u0441\u044c.<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/tensor\/blog\/506360\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/506360\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/tensor\/blog\/506360\/\">\u0412 <a href=\"https:\/\/habr.com\/ru\/post\/503680\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u043c\u044b \u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u0441\u043d\u0438\u043c\u0430\u0442\u044c <b>\u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/b> \u043d\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0435 PostgreSQL \u0440\u043e\u0432\u043d\u043e \u0432 \u0442\u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a\u043e\u0433\u0434\u0430 \u043e\u043d\u0438 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u044f\u0442. \u0412 \u044d\u0442\u043e\u0439 \u2014 \u043d\u0430\u0443\u0447\u0438\u043c\u0441\u044f \u0442\u0440\u0430\u043a\u0442\u043e\u0432\u0430\u0442\u044c \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u043e\u0435 \u0438 \u0443\u0437\u043d\u0430\u0432\u0430\u0442\u044c, <b>\u043a\u0442\u043e \u0438\u043c\u0435\u043d\u043d\u043e \u043c\u043e\u0436\u0435\u0442 \u0441\u043a\u0440\u044b\u0432\u0430\u0442\u044c\u0441\u044f<\/b> \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u043c\u0430\u0442\u0440\u0438\u0446\u0435\u0439 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432, \u0438 \u043f\u043e\u0447\u0435\u043c\u0443 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0438\u043c\u0435\u043d\u043d\u043e \u0442\u0430\u043a.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/dc\/p1\/ua\/dcp1uaiz4dxf5demrrzgsu_pqgg.png\">  <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-305380","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/305380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=305380"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/305380\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=305380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=305380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=305380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}