{"id":306617,"date":"2020-07-08T09:00:50","date_gmt":"2020-07-08T09:00:50","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=306617"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=306617","title":{"rendered":"PostgreSQL Antipatterns: \u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u0443\u0435\u043c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u2014 SELF JOIN vs WINDOW"},"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\/508184\/\">\u0420\u0430\u043d\u0435\u0435 \u043c\u044b \u0443\u0436\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/503680\/\">\u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/a> \u0438\u0437 \u043b\u043e\u0433\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 PostgreSQL. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0438\u0445 \u0432 \u0411\u0414 \u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u043a\u0430\u043a\u0438\u0435 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430.<\/p>\n<p>  \u0412 \u043b\u043e\u0433\u0430\u0445 \u0443 \u043d\u0430\u0441 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e 3 \u0432\u0438\u0434\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439:<\/p>\n<ul>\n<li><b>\u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/b><br \/>  <code>LOG: process 38162 <b>still waiting<\/b> for ExclusiveLock on advisory lock [225382138,225386226,141586103,2] after 100.047 ms<\/code><\/li>\n<li><b>\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/b><br \/>  <code>LOG: process 38162 <b>acquired<\/b> ExclusiveLock on advisory lock [225382138,225386226,141586103,2] after 150.741 ms<\/code><\/li>\n<li><b>\u0432\u0437\u0430\u0438\u043c\u043e\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430<\/b><br \/>  <code>ERROR: <b>deadlock detected<\/b><\/code><\/li>\n<\/ul>\n<p>  deadlock&#8217;\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u043c \u0438\u0437 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0438, \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u044f\u0441\u043d\u0438\u0442\u044c, <b>\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0441\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043c\u044b \u043f\u043e\u0442\u0435\u0440\u044f\u043b\u0438 \u0438\u0437-\u0437\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/b> \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0434\u0435\u043d\u044c \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u0445\u043e\u0441\u0442\u0435.<br \/>  <a name=\"habracut\"><\/a><br \/>  \u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u043c \u043f\u043e\u043d\u0430\u0434\u043e\u0431\u0438\u0442\u0441\u044f \u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043a\u0443\u0434\u0430 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0432\u0441\u0435 \u0442\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438:<\/p>\n<pre><code class=\"sql\">CREATE TABLE lock(   dt       -- \u043a\u043b\u044e\u0447 \u0445\u0440\u043e\u043d\u043e\u043b\u043e\u0433\u0438\u0447\u0435\u0441\u043a\u043e\u0433\u043e \u0441\u0435\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f     date , host     -- \u0441\u0435\u0440\u0432\u0435\u0440, \u043d\u0430 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430     uuid , pid      -- PID \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0438\u0437 \u0441\u0442\u0440\u043e\u043a\u0438 \u043b\u043e\u0433\u0430     integer , ts       -- \u043c\u043e\u043c\u0435\u043d\u0442 \u0441\u043e\u0431\u044b\u0442\u0438\u044f     timestamp , event    -- { lock-wait | lock-acquire | deadlock-detect }     lockevent , type     -- { relation | extend | ... }     locktype , mode     -- { AccessShare | RowShare | ... }     lockmode , lock     -- \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438     uuid , exectime -- \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c     numeric(32,2) );<\/code><\/pre>\n<p>  <\/p>\n<blockquote><p>\u0411\u043e\u043b\u0435\u0435 \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u043e <u>\u043f\u0440\u043e \u043e\u0440\u0433\u0430\u043d\u0438\u0437\u0430\u0446\u0438\u044e \u0441\u0435\u043a\u0446\u0438\u043e\u043d\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f<\/u> \u0432 <a href=\"https:\/\/habr.com\/ru\/post\/487380\/\">\u043d\u0430\u0448\u0435\u0439 \u0441\u0438\u0441\u0442\u0435\u043c\u0435 \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433\u0430<\/a> \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/497008\/\">\u00ab\u041f\u0438\u0448\u0435\u043c \u0432 PostgreSQL \u043d\u0430 \u0441\u0443\u0431\u0441\u0432\u0435\u0442\u043e\u0432\u043e\u0439: 1 host, 1 day, 1TB\u00bb<\/a>, \u0430 <u>\u043f\u0440\u043e \u0440\u0430\u0437\u043b\u0438\u0447\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0438 \u0440\u0435\u0436\u0438\u043c\u044b \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/u> \u2014 \u0432 <a href=\"https:\/\/habr.com\/ru\/post\/506360\/\">\u00abDBA: \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\u00bb<\/a>.  <\/p><\/blockquote>\n<p>  <\/p>\n<h2>\u041a\u0430\u043a \u0441\u043b\u044b\u0448\u0438\u0442\u0441\u044f, \u0442\u0430\u043a \u0438 \u043f\u0438\u0448\u0435\u0442\u0441\u044f<\/h2>\n<p>  \u041f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u043e\u0442\u0432\u0435\u0442\u0438\u0442\u044c \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441, \u0432\u044b\u043d\u0435\u0441\u0435\u043d\u043d\u044b\u0439 \u0432 \u043d\u0430\u0447\u0430\u043b\u043e \u0441\u0442\u0430\u0442\u044c\u0438, \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/5r\/zh\/by\/5rzhbyybkg8ukfu1unxl_6drt3y.png\"><\/p>\n<p>  \u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438? \u041d\u0443, \u043e\u0447\u0435\u0432\u0438\u0434\u043d\u043e \u0436\u0435, \u2014 \u044d\u0442\u043e \u0432\u0440\u0435\u043c\u044f \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0441\u043b\u0443\u0447\u0430\u044f \u0435\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f:<\/p>\n<ul>\n<li>\u0431\u0435\u0440\u0435\u043c \u043a\u0430\u0436\u0434\u044b\u0439 \u0441\u043b\u0443\u0447\u0430\u0439 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f (<code>lock-wait<\/code>)<\/li>\n<li>\u0434\u043b\u044f \u043d\u0435\u0433\u043e \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0431\u043b\u0438\u0436\u0430\u0439\u0448\u0443\u044e \u00ab\u0441\u043d\u0438\u0437\u0443\u00bb \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0437\u0430\u043f\u0438\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f (<code>lock-acquire<\/code>) \u00ab\u044d\u0442\u043e\u0439 \u0436\u0435\u00bb <code>(lock, pid, mode)<\/code> \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u2014 \u0442\u043e \u0435\u0441\u0442\u044c \u043d\u0430 \u0442\u043e\u0442 \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442, \u0432 \u0442\u043e\u043c \u0436\u0435 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0435, \u0441 \u0442\u0435\u043c \u0436\u0435 \u0440\u0435\u0436\u0438\u043c\u043e\u043c<\/li>\n<\/ul>\n<p>  \u0422\u0438\u043f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 (<code>type<\/code>) \u0432 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c, \u043f\u043e\u0441\u043a\u043e\u043b\u044c\u043a\u0443 \u043e\u043d \u043e\u0434\u043d\u043e\u0437\u043d\u0430\u0447\u043d\u043e \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u044f\u0435\u0442\u0441\u044f \u0441\u0430\u043c\u0438\u043c \u043e\u0431\u044a\u0435\u043a\u0442\u043e\u043c (<code>lock<\/code>).<\/p>\n<p>  \u0414\u0430\u043b\u044c\u0448\u0435 \u043e\u0441\u0442\u0430\u043d\u0435\u0442\u0441\u044f \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u043e\u0441\u0443\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u044b\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b.<\/p>\n<pre><code class=\"sql\">SELECT   ts , pid , event , type , mode , lock , exectime , T.* FROM   lock lc , LATERAL (     SELECT       exectime waittime     FROM       lock     WHERE       (         dt       , host       , lock       , pid       , mode       , event       ) = (         '2020-06-19'::date       , lc.host       , lc.lock       , lc.pid       , lc.mode       , 'lock-acquire'       ) AND       ts &gt;= lc.ts     ORDER BY       ts     LIMIT 1   ) T WHERE   (     lc.dt   , lc.host   , lc.event   ) = (     '2020-06-19'::date   , 'e828a54d-7e8a-43dd-b213-30c3201a6d8e'::uuid   , 'lock-wait'::lockevent   );<\/code><\/pre>\n<p>  \u0412\u0441\u0435 \u043f\u0440\u043e\u0441\u0442\u043e \u0438 \u044f\u0441\u043d\u043e! \u0410 \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u043e\u043a\u0430\u0436\u0435\u0442 <code>EXPLAIN<\/code>?..<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/pm\/yq\/te\/pmyqtexj_cshi8p-dpmujljzqaw.png\"><\/p>\n<p>  \u041e\u0443\u2026 \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c <b>\u043f\u043e\u0447\u0442\u0438 900MB \u0434\u0430\u043d\u043d\u044b\u0445<\/b>, \u043f\u0440\u0438\u0447\u0435\u043c \u043f\u043e\u0447\u0442\u0438 \u0432\u0441\u0435 \u2014 \u0438\u0437-\u0437\u0430 \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438, \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u043e.<\/p>\n<p>  \u041d\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043b\u0438 \u044d\u0442\u043e\u0442 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432\u043e\u043e\u0431\u0449\u0435 \u043a\u043e\u0440\u0440\u0435\u043a\u0442\u043d\u044b\u043c \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0439 \u0437\u0430\u0434\u0430\u0447\u0438? \u041d\u0435\u0442! \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0432\u043d\u0438\u043c\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u0432 \u0441\u043e\u0431\u0440\u0430\u043d\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/tm\/4o\/9q\/tm4o9qycfzpkqyxd2zsao9tbiwm.png\"><\/p>\n<p>  \u041e\u0439\u2026 \u041e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f, \u0441\u0435\u0440\u0432\u0435\u0440 \u00ab\u0436\u0430\u043b\u0443\u0435\u0442\u0441\u044f\u00bb \u0432 \u043b\u043e\u0433\u0438 \u043d\u0430 \u043e\u0434\u043d\u0443 \u0438 \u0442\u0443 \u0436\u0435 \u043d\u0435\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u0443\u044e \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0443 \u0438\u043d\u043e\u0433\u0434\u0430 \u043c\u043d\u043e\u0433\u043e-\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0437. \u0410 \u044d\u0442\u043e \u043e\u0437\u043d\u0430\u0447\u0430\u0435\u0442, \u0447\u0442\u043e \u043c\u044b <b>\u0443\u0447\u043b\u0438 \u0432\u0440\u0435\u043c\u044f \u0435\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u043a\u0440\u0430\u0442\u043d\u043e<\/b> \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0443 \u0442\u0430\u043a\u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0432 \u043b\u043e\u0433\u0435, \u0447\u0442\u043e \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0436\u0435\u043b\u0430\u0435\u043c\u043e\u043c\u0443.<\/p>\n<h2>\u041f\u043e\u043c\u043d\u0438 \u043e \u0446\u0435\u043b\u0438!<\/h2>\n<p>  \u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0430 \u0437\u0430\u0447\u0435\u043c \u043c\u044b \u0432\u043e\u043e\u0431\u0449\u0435 \u0434\u043b\u044f \u043a\u0430\u0436\u0434\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0438\u0449\u0435\u043c \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u0443\u044e? \u041c\u044b \u0436\u0435 \u0445\u043e\u0442\u0438\u043c \u0443\u0437\u043d\u0430\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043d\u044f\u043b\u043e \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435, \u0430 \u043e\u043d\u043e \u043f\u0440\u044f\u043c\u043e \u0437\u0430\u043f\u0438\u0441\u0430\u043d\u043e \u0432 <code>lock-acquire<\/code>. \u0422\u0430\u043a \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0441\u0440\u0430\u0437\u0443 \u043e\u0442\u0431\u0438\u0440\u0430\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u0445, \u0442\u043e\u0433\u0434\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0435\u0433\u043e \u043b\u0438\u0448\u044c \u043e\u0434\u0438\u043d <code>Index Scan<\/code> \u2014 \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e?<\/p>\n<p>  \u041f\u043e\u0447\u0442\u0438, \u0434\u0430 \u043d\u0435 \u0441\u043e\u0432\u0441\u0435\u043c. \u0414\u0435\u043b\u043e \u0432 \u0442\u043e\u043c, \u0447\u0442\u043e <b>\u043f\u043e\u0434 \u043d\u0430\u0433\u0440\u0443\u0437\u043a\u043e\u0439 \u043b\u043e\u0433-\u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u043e\u043f\u0443\u0441\u043a\u0430\u0442\u044c \u043b\u044e\u0431\u044b\u0435 \u0438\u0437 \u0437\u0430\u043f\u0438\u0441\u0435\u0439<\/b> \u2014 \u0445\u043e\u0442\u044c \u043e \u043d\u0430\u0447\u0430\u043b\u0435 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0435, \u0445\u043e\u0442\u044c \u043e \u0444\u0430\u043a\u0442\u0435 \u0435\u0435 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u044f:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/sm\/k8\/l7\/smk8l78qf7ezfg_lgfu-2d9dxeu.png\"><\/p>\n<p>  \u0422\u0430\u043a \u043d\u0435\u0443\u0436\u0435\u043b\u0438 \u043d\u0435\u0442 \u0441\u043f\u043e\u0441\u043e\u0431\u0430 \u0437\u0430 \u043e\u0434\u043d\u043e \u0447\u0442\u0435\u043d\u0438\u0435 \u0441\u0440\u0430\u0437\u0443 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0432\u0441\u0435 \u043d\u0443\u0436\u043d\u044b\u0435 \u043d\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0435?<\/p>\n<h2>Window Functions: \u0441\u0435\u043c\u0435\u0440\u044b\u0445 \u043e\u0434\u043d\u0438\u043c \u0443\u0434\u0430\u0440\u043e\u043c<\/h2>\n<p>  \u041d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043d\u0430\u043c \u043f\u0440\u0438\u0434\u0443\u0442 <a href=\"https:\/\/postgrespro.ru\/docs\/postgresql\/12\/tutorial-window\">\u043e\u043a\u043e\u043d\u043d\u044b\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438<\/a>.<br \/>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/2g\/uf\/6i\/2guf6isju2vemqjuqzmmhhpflck.png\"><\/p>\n<p>  \u0410 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u0435\u0435 \u2014 <b>\u043c\u043e\u0434\u0435\u043b\u044c \u0432\u044b\u0434\u0435\u043b\u0435\u043d\u0438\u044f \u00ab\u0446\u0435\u043f\u043e\u0447\u0435\u043a\u00bb<\/b> \u0432 \u0433\u043e\u0442\u043e\u0432\u043e\u0439 \u0432\u044b\u0431\u043e\u0440\u043a\u0435 \u0438\u0437 \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/483460\/\">\u00abSQL HowTo: \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u00ab\u0446\u0435\u043f\u043e\u0447\u043a\u0438\u00bb \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e window functions\u00bb<\/a>.<\/p>\n<p>  \u0421\u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u043e\u0439\u043c\u0435\u043c, \u0447\u0442\u043e \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u00ab\u0446\u0435\u043f\u043e\u0447\u043a\u0438\u00bb \u2014 \u0442\u043e \u0435\u0441\u0442\u044c \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u0434\u0440\u044f\u0434 \u0438\u0434\u0443\u0449\u0438\u0445 \u043f\u043e \u043a\u043b\u044e\u0447\u0443 <code>(host, lock, pid, mode)<\/code> \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u2014 \u0434\u043b\u044f \u043d\u0430\u0441 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0438\u043b\u0438 \u044f\u0432\u043d\u043e\u0435 \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u0435 <b><code>event = 'lock-acquire'<\/code><\/b> \u0438\u043b\u0438 (\u0447\u0442\u043e \u043e\u0447\u0435\u043d\u044c \u0440\u0435\u0434\u043a\u043e, \u043d\u043e \u0431\u044b\u0432\u0430\u0435\u0442) <b>\u043d\u0430\u0447\u0430\u043b\u043e \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u0435\u0433\u043c\u0435\u043d\u0442\u0430<\/b> \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 \u0442\u043e\u0433\u043e \u0436\u0435 \u043e\u0431\u044a\u0435\u043a\u0442\u0430, \u0447\u044c\u044f \u0434\u043b\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c (<code>exectime<\/code>) \u043d\u0430\u0447\u0430\u043b\u0430 \u0441\u0447\u0438\u0442\u0430\u0442\u044c\u0441\u044f \u0437\u0430\u043d\u043e\u0432\u043e.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/vb\/od\/ok\/vbodokvqrfqur_tpqf8kajtuspa.png\"><\/p>\n<p>  \u0422\u0430\u043a\u0436\u0435 \u043d\u0430\u0434\u043e \u0443\u0447\u0435\u0441\u0442\u044c \u0442\u043e\u0442 \u0444\u0430\u043a\u0442, \u0447\u0442\u043e <b>\u0432\u0440\u0435\u043c\u044f \u043c\u043e\u0436\u0435\u0442 \u0441\u043e\u0432\u043f\u0430\u0434\u0430\u0442\u044c<\/b> \u0434\u043b\u044f \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u0438\u0445 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u043b\u043e\u0433\u0430 \u0434\u0430\u0436\u0435 \u0441 \u043e\u0434\u043d\u043e\u0433\u043e PID. \u0412 \u044d\u0442\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u0434\u043e \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043f\u043e <code>exectime<\/code>, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u0443\u044e \u043f\u043e\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c:<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/xn\/ns\/l7\/xnnsl7fuzroyfyzbc-onstvhlwu.png\"><\/p>\n<pre><code class=\"sql\">-- \u0444\u043e\u0440\u043c\u0438\u0440\u0443\u0435\u043c \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 WITH lc AS (   SELECT     *   , CASE       WHEN event = 'lock-wait' THEN         exectime &gt; coalesce(lead(exectime) OVER(PARTITION BY lock, pid, mode ORDER BY ts, exectime), 0) -- &quot;\u043f\u0435\u0440\u0435\u043b\u043e\u043c&quot; \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f       ELSE TRUE -- 'lock-acquire' - \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0430     END cond -- \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f &quot;\u0446\u0435\u043f\u043e\u0447\u043a\u0438&quot;   FROM     lock lc   WHERE     event &lt;&gt; 'deadlock-detect' AND -- \u0438\u0441\u043a\u043b\u044e\u0447\u0430\u0435\u043c \u0432\u0441\u0435 deadlock     (       lc.dt     , lc.host     ) = (       '2020-06-19'::date     , 'e828a54d-7e8a-43dd-b213-30c3201a6d8e'::uuid     ) ) -- \u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c \u0442\u043e\u043b\u044c\u043a\u043e &quot;\u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435&quot; \u0437\u0430\u043f\u0438\u0441\u0438 - \u0438\u0445 exectime \u0438 \u0435\u0441\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f &quot;\u0432\u0441\u0435\u0439&quot; \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438 SELECT   ts , pid , event , type , mode , lock , exectime FROM   lc WHERE   cond;<\/code><\/pre>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/us\/ng\/6b\/usng6blt5kk8behu6e6saaaxkbw.png\"><\/p>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438 <b>\u0432\u0441\u0435\u0433\u043e 8MB<\/b> \u0434\u0430\u043d\u043d\u044b\u0445 (\u0432 100 \u0440\u0430\u0437 \u043c\u0435\u043d\u044c\u0448\u0435!), \u0447\u0443\u0442\u044c-\u0447\u0443\u0442\u044c \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0432 \u0438\u0442\u043e\u0433\u043e\u0432\u043e\u0435 \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0415\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0442\u044c \u0435\u0449\u0435, \u0435\u0441\u043b\u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441, \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u043f\u043e\u0434\u0445\u043e\u0434\u044f\u0449\u0438\u0439 \u043f\u043e\u0434 <code>OVER<\/code> (\u0442\u043e \u0435\u0441\u0442\u044c \u0432\u043a\u043b\u044e\u0447\u0430\u044e\u0449\u0438\u0439 <code>lock, pid, mode, <b>ts, exectime<\/b><\/code>), \u0438\u0437\u0431\u0430\u0432\u0438\u0432\u0448\u0438\u0441\u044c \u043e\u0442 <code>Sort<\/code>-\u0443\u0437\u043b\u0430. \u041d\u043e \u043e\u0431\u044b\u0447\u043d\u043e <a href=\"https:\/\/habr.com\/ru\/post\/488104\/\">\u043f\u043e\u043b\u0435 \u0432 \u0438\u043d\u0434\u0435\u043a\u0441\u0435 \u00ab\u0437\u0430 timestamp\u00bb \u0434\u0435\u043b\u0430\u0442\u044c \u043d\u0435 \u0441\u0442\u043e\u0438\u0442<\/a>.<\/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\/508184\/\"> https:\/\/habr.com\/ru\/company\/tensor\/blog\/508184\/<\/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\/508184\/\">\u0420\u0430\u043d\u0435\u0435 \u043c\u044b \u0443\u0436\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/503680\/\">\u043d\u0430\u0443\u0447\u0438\u043b\u0438\u0441\u044c \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/a> \u0438\u0437 \u043b\u043e\u0433\u0430 \u0441\u0435\u0440\u0432\u0435\u0440\u0430 PostgreSQL. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u0442\u0435\u043f\u0435\u0440\u044c \u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0438\u0445 \u0432 \u0411\u0414 \u0438 \u0440\u0430\u0437\u0431\u0435\u0440\u0435\u043c, \u043a\u0430\u043a\u0438\u0435 \u0444\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438\u0435 \u043e\u0448\u0438\u0431\u043a\u0438 \u0438 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u043e\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0438\u0445 \u043f\u0440\u043e\u0441\u0442\u0435\u0439\u0448\u0435\u0433\u043e \u0430\u043d\u0430\u043b\u0438\u0437\u0430.<\/p>\n<p>  \u0412 \u043b\u043e\u0433\u0430\u0445 \u0443 \u043d\u0430\u0441 \u043e\u0442\u0440\u0430\u0436\u0430\u0435\u0442\u0441\u044f \u0432\u0441\u0435\u0433\u043e 3 \u0432\u0438\u0434\u0430 \u0441\u043e\u0431\u044b\u0442\u0438\u0439, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u043e\u0433\u0443\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u0441 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u043e\u0439:<\/p>\n<ul>\n<li><b>\u043e\u0436\u0438\u0434\u0430\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/b><br \/>  <code>LOG: process 38162 <b>still waiting<\/b> for ExclusiveLock on advisory lock [225382138,225386226,141586103,2] after 100.047 ms<\/code><\/li>\n<li><b>\u043f\u043e\u043b\u0443\u0447\u0435\u043d\u0438\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0438<\/b><br \/>  <code>LOG: process 38162 <b>acquired<\/b> ExclusiveLock on advisory lock [225382138,225386226,141586103,2] after 150.741 ms<\/code><\/li>\n<li><b>\u0432\u0437\u0430\u0438\u043c\u043e\u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043a\u0430<\/b><br \/>  <code>ERROR: <b>deadlock detected<\/b><\/code><\/li>\n<\/ul>\n<p>  deadlock&#8217;\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u043c \u0438\u0437 \u0430\u043d\u0430\u043b\u0438\u0437\u0430 \u2014 \u044d\u0442\u043e \u043f\u0440\u043e\u0441\u0442\u043e \u043e\u0448\u0438\u0431\u043a\u0438, \u0438 \u043f\u043e\u043f\u0440\u043e\u0431\u0443\u0435\u043c \u0432\u044b\u044f\u0441\u043d\u0438\u0442\u044c, <b>\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0432\u0441\u0435\u0433\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u043c\u044b \u043f\u043e\u0442\u0435\u0440\u044f\u043b\u0438 \u0438\u0437-\u0437\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u043e\u0432\u043e\u043a<\/b> \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0434\u0435\u043d\u044c \u043d\u0430 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u043e\u043c \u0445\u043e\u0441\u0442\u0435.  <\/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-306617","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306617","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=306617"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/306617\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=306617"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=306617"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=306617"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}