{"id":382067,"date":"2024-06-29T04:02:53","date_gmt":"2024-06-29T04:02:53","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=382067"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=382067","title":{"rendered":"<span>SQL HowTo: \u0442\u0440\u0438 WHERE \u0432 \u043e\u0434\u043d\u043e\u043c \u0437\u0430\u043f\u0440\u043e\u0441\u0435<\/span>"},"content":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u044b <a href=\"https:\/\/sbis.ru\/all_services\">\u0421\u0411\u0418\u0421<\/a> \u0441\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441 <strong>\u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 PostgreSQL<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 &#171;\u0432 \u043e\u0434\u0438\u043d \u0445\u043e\u0434&#187;.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u043f\u043e\u043b\u043d\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0430\u043a\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <code>INSERT ... ON CONFLICT<\/code>.<\/p>\n<figure class=\"\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w780q1\/getpro\/habr\/upload_files\/810\/ed9\/f42\/810ed9f42f60c8b5be56b901eae9cfb7.jpg\" width=\"425\" height=\"282\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/810\/ed9\/f42\/810ed9f42f60c8b5be56b901eae9cfb7.jpg\" data-blurred=\"true\"\/><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430: \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433<\/h2>\n<p>\u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0430\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0430\u043d\u043e\u0441\u0438\u0442 \u0432 \u0431\u0430\u0437\u0443 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u0443: <code>id<\/code> \u0434\u0430\u0442\u0447\u0438\u043a\u0430, \u043c\u043e\u043c\u0435\u043d\u0442 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f <code>ts<\/code> \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>temp_val<\/code>.<\/p>\n<p>\u0414\u0430\u0442\u0447\u0438\u043a\u0438 \u0441\u0442\u043e\u044f\u0442 \u0434\u0430\u043b\u0435\u043a\u043e, \u043a\u0430\u043d\u0430\u043b\u044b \u0441\u043b\u0430\u0431\u044b\u0435, \u043f\u043e\u043c\u0435\u0445 \u043c\u043d\u043e\u0433\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0434\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0411\u0414 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0442\u044c \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430, \u043d\u043e <strong>\u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0430\u0441\u0430<\/strong>.<\/p>\n<p>\u0410 \u0435\u0449\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u044e\u0431\u0438\u0442 \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u0430\u0444\u0438\u043a\u0438 \u043f\u043e \u044d\u0442\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u043c. \u041d\u043e \u0435\u043c\u0443 \u0441 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u0430\u0442\u0447\u0438\u043a\u0430 <strong>\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0442\u0441\u0447\u0435\u0442\u0430 \u0432 \u0447\u0430\u0441<\/strong>: \u043e\u0431\u0449\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0439 <code>qty<\/code> \u0438 \u0441\u0440\u0435\u0434\u043d\u0435\u0439 \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u044b.<\/p>\n<h4>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>\u0414\u043e\u043b\u0433\u043e \u043b\u0438, \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u043b\u0438, \u0440\u0435\u0448\u0438\u043b\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438, \u0447\u0442\u043e \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0441\u0447\u0438\u0442\u0430\u0442\u044c &#171;\u043d\u0430 \u043b\u0435\u0442\u0443&#187; &#8212; \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0432\u0435\u0434\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0438\u0437 \u043d\u0438\u0445 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0438\u043a\u0430\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0445 \u0441\u0440\u0430\u0437\u0443 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c, \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 <strong>\u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u043b\u0438\u0448\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445<\/strong>.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e <strong>\u043e\u0434\u043d\u043e\u0439 \u043e\u0431\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/strong>:<\/p>\n<pre><code class=\"sql\">CREATE TABLE metrics(   id     integer , ts     timestamp , temp_val     real , qty     integer );<\/code><\/pre>\n<p>\u0414\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438\u0441\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432 \u0437\u0430\u043f\u0438\u0441\u0438 <code>qty IS NULL<\/code>, \u0442\u043e \u044d\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 &#171;\u0444\u0430\u043a\u0442&#187; \u043e\u0442 \u0434\u0430\u0442\u0447\u0438\u043a\u0430, \u0430 <code>qty IS NOT NULL<\/code> &#8212; \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u0432\u043e\u0434\u043d\u044b\u0439 \u0430\u0433\u0440\u0435\u0433\u0430\u0442 \u043f\u043e \u043d\u0435\u043c\u0443 \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0447\u0430\u0441.<\/p>\n<h2>Worker-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u0432\u0435\u0436\u0438\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0432 \u0431\u0430\u0437\u0435, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0447\u0430\u0441\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0432 \u0431\u0430\u0437\u0435:<\/p>\n<pre><code class=\"sql\">INSERT INTO metrics(id, ts, temp_val, qty) SELECT   id , date_trunc('hour', ts) ts , avg(temp_val) temp_val , count(*) qty FROM   metrics WHERE   qty IS NULL AND -- \u0442\u043e\u043b\u044c\u043a\u043e \"\u043f\u0435\u0440\u0432\u0438\u0447\u043a\u0430\"   ts >= date_trunc('hour', now() - '1 hour'::interval) GROUP BY   1, 2;<\/code><\/pre>\n<blockquote>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 &#8212; \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/539016\/\">&#171;\u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0432 \u0411\u0414 \u2014 \u0437\u0430\u0447\u0435\u043c, \u043a\u0430\u043a, \u0430 \u0441\u0442\u043e\u0438\u0442 \u043b\u0438?&#187;<\/a>.<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430\u043c \u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439:<\/p>\n<pre><code class=\"sql\">CREATE INDEX ON metrics(ts)   WHERE qty IS NULL; -- \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0444\u0430\u043a\u0442\u043e\u0432<\/code><\/pre>\n<h2>\u0420\u0435\u0448\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u044b<\/h2>\n<p>\u041d\u043e \u0432\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b\u0438 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0438 \u0432 \u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u0440\u0430\u0437, \u0432\u0435\u0441\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0447\u0430\u0441 \u0443\u0436\u0435 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043d, \u0438 <strong>\u0437\u0430\u043f\u0438\u0441\u0438 \u0437\u0430\u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0442\u0441\u044f<\/strong>, \u0430 \u043c\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0445\u043e\u0442\u0438\u043c! \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0439 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u043e \u0434\u0430\u0442\u0447\u0438\u043a\u0443 \u0432 \u0447\u0430\u0441:<\/p>\n<pre><code class=\"sql\">CREATE UNIQUE INDEX ON metrics(id, ts)   WHERE qty IS NOT NULL; -- \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432<\/code><\/pre>\n<p>\u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448 <code>INSERT<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0430\u043b\u0438\u0442\u0441\u044f \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c&#8230; \u041d\u0435\u043f\u043e\u0440\u044f\u0434\u043e\u043a! \u041d\u0430\u043c \u0442\u043e \u0432\u0435\u0434\u044c \u043d\u0430\u0434\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u043b\u0438\u0441\u044c:<\/p>\n<pre><code class=\"sql\">INSERT INTO metrics(id, ts, temp_val, qty)   SELECT     id   , date_trunc('hour', ts)   , avg(temp_val)   , count(*)   FROM     metrics   WHERE     qty IS NULL AND     ts >= date_trunc('hour', now() - '1 hour'::interval)   GROUP BY     1, 2 ON CONFLICT -- \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442   (id, ts) WHERE qty IS NOT NULL   DO UPDATE SET -- \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043d\u043e\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435     (qty, temp_val) = (EXCLUDED.qty, EXCLUDED.temp_val);<\/code><\/pre>\n<h2>\u0411\u043e\u0440\u0435\u043c\u0441\u044f \u0441 &#171;\u043c\u0435\u0440\u0442\u0432\u0435\u0446\u0430\u043c\u0438&#187;<\/h2>\n<p>\u041d\u043e \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u0447\u0430\u0441\u0430 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e <code>UPDATE<\/code>&#8216;\u0438\u0442\u0441\u044f, \u0447\u0442\u043e <strong>\u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a &#171;\u0440\u0430\u0437\u0434\u0443\u0432\u0430\u043d\u0438\u044e&#187; \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/strong>! \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 <code>RETURNING *<\/code>.<\/p>\n<blockquote>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 &#8212; \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/491366\/\">&#171;PostgreSQL Antipatterns: \u0441\u0440\u0430\u0436\u0430\u0435\u043c\u0441\u044f \u0441 \u043e\u0440\u0434\u0430\u043c\u0438 \u00ab\u043c\u0435\u0440\u0442\u0432\u0435\u0446\u043e\u0432\u00bb&#187;<\/a>.<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438\u0441\u044c:<\/p>\n<pre><code class=\"sql\">INSERT INTO metrics AS m(id, ts, temp_val, qty)   SELECT     id   , date_trunc('hour', ts)   , avg(temp_val)   , count(*)   FROM     metrics   WHERE -- #1 : \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043e\u0442\u0431\u043e\u0440\u0430 \"\u043f\u0435\u0440\u0432\u0438\u0447\u043a\u0438\"     qty IS NULL AND     ts >= date_trunc('hour', now() - '1 hour'::interval)   GROUP BY     1, 2 ON CONFLICT   (id, ts) WHERE qty IS NOT NULL -- #2 : \u0443\u0441\u043b\u043e\u0432\u0438\u0435 UNIQUE-\u0438\u043d\u0434\u0435\u043a\u0441\u0430 \"\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432\"   DO UPDATE SET     (temp_val, qty) = (EXCLUDED.temp_val, EXCLUDED.qty)     WHERE -- #3 : \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u0438       (m.temp_val, m.qty) IS DISTINCT FROM (EXCLUDED.temp_val, EXCLUDED.qty);<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c <strong>\u0434\u0430\u0442\u044c \u043d\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0430\u043b\u0438\u0430\u0441<\/strong> <code>AS m<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u0439 \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u0438 \u043f\u043e \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u0438.<\/p>\n<p>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u043e\u0442 \u0438 \u0432\u0441\u0435 &#8212; \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0435\u0448\u0435\u043d\u0430 &#171;\u0432 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441&#187; &#8212; \u043f\u0440\u0430\u0432\u0434\u0430, \u0432 \u043d\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0446\u0435\u043b\u044b\u0445 \u0442\u0440\u0438 <code>WHERE<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/576894\/\"> https:\/\/habr.com\/ru\/articles\/576894\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><!--[--><!--]--><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041f\u0440\u0438 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0440\u0438\u043a\u043b\u0430\u0434\u043d\u044b\u0445 \u0437\u0430\u0434\u0430\u0447 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044d\u043a\u043e\u0441\u0438\u0441\u0442\u0435\u043c\u044b <a href=\"https:\/\/sbis.ru\/all_services\">\u0421\u0411\u0418\u0421<\/a> \u0441\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u0441\u0442\u0430\u043b\u043a\u0438\u0432\u0430\u0442\u044c\u0441\u044f \u0441 <strong>\u043d\u0435\u043e\u0447\u0435\u0432\u0438\u0434\u043d\u044b\u043c\u0438 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044f\u043c\u0438 PostgreSQL<\/strong>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0432\u043c\u0435\u0441\u0442\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u043b\u043e\u0433\u0438\u043a\u0438 \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 &#171;\u0432 \u043e\u0434\u0438\u043d \u0445\u043e\u0434&#187;.<\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043d\u0430 \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u0432\u043f\u043e\u043b\u043d\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u0442\u0430\u043a\u0438\u0435 \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u0430 <code>INSERT ... ON CONFLICT<\/code>.<\/p>\n<figure class=\"\"><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u0434\u0430\u0447\u0430: \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433<\/h2>\n<p>\u041f\u0443\u0441\u0442\u044c \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0434\u0430\u0442\u0447\u0438\u043a\u043e\u0432, \u043a\u0430\u0436\u0434\u044b\u0439 \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0437\u0430\u043d\u043e\u0441\u0438\u0442 \u0432 \u0431\u0430\u0437\u0443 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u0443: <code>id<\/code> \u0434\u0430\u0442\u0447\u0438\u043a\u0430, \u043c\u043e\u043c\u0435\u043d\u0442 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f <code>ts<\/code> \u0438 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 <code>temp_val<\/code>.<\/p>\n<p>\u0414\u0430\u0442\u0447\u0438\u043a\u0438 \u0441\u0442\u043e\u044f\u0442 \u0434\u0430\u043b\u0435\u043a\u043e, \u043a\u0430\u043d\u0430\u043b\u044b \u0441\u043b\u0430\u0431\u044b\u0435, \u043f\u043e\u043c\u0435\u0445 \u043c\u043d\u043e\u0433\u043e, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043e\u0442 \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u044f \u0434\u043e \u043c\u043e\u043c\u0435\u043d\u0442\u0430 \u043f\u043e\u043f\u0430\u0434\u0430\u043d\u0438\u044f \u044d\u0442\u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0445 \u0432 \u0411\u0414 \u043c\u043e\u0436\u0435\u0442 \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0442\u044c \u0437\u0430\u0434\u0435\u0440\u0436\u043a\u0430, \u043d\u043e <strong>\u043d\u0435 \u0431\u043e\u043b\u044c\u0448\u0435 \u0447\u0430\u0441\u0430<\/strong>.<\/p>\n<p>\u0410 \u0435\u0449\u0435 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043b\u044e\u0431\u0438\u0442 \u0440\u0438\u0441\u043e\u0432\u0430\u0442\u044c \u0433\u0440\u0430\u0444\u0438\u043a\u0438 \u043f\u043e \u044d\u0442\u0438\u043c \u0434\u0430\u043d\u043d\u044b\u043c. \u041d\u043e \u0435\u043c\u0443 \u0441 \u043a\u0430\u0436\u0434\u043e\u0433\u043e \u0434\u0430\u0442\u0447\u0438\u043a\u0430 <strong>\u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u0434\u043d\u043e\u0433\u043e \u043e\u0442\u0441\u0447\u0435\u0442\u0430 \u0432 \u0447\u0430\u0441<\/strong>: \u043e\u0431\u0449\u0435\u0433\u043e \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u0438\u0437\u043c\u0435\u0440\u0435\u043d\u0438\u0439 <code>qty<\/code> \u0438 \u0441\u0440\u0435\u0434\u043d\u0435\u0439 \u0442\u0435\u043c\u043f\u0435\u0440\u0430\u0442\u0443\u0440\u044b.<\/p>\n<h4>\u0421\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u0430 \u0434\u0430\u043d\u043d\u044b\u0445<\/h4>\n<p>\u0414\u043e\u043b\u0433\u043e \u043b\u0438, \u043a\u043e\u0440\u043e\u0442\u043a\u043e \u043b\u0438, \u0440\u0435\u0448\u0438\u043b\u0438 \u0440\u0430\u0437\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0438, \u0447\u0442\u043e \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0441\u0447\u0438\u0442\u0430\u0442\u044c &#171;\u043d\u0430 \u043b\u0435\u0442\u0443&#187; &#8212; \u043d\u0435 \u043e\u0441\u043e\u0431\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e, \u0432\u0435\u0434\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043d\u0441\u0442\u0432\u043e \u0438\u0437 \u043d\u0438\u0445 \u0441\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0435\u043c \u043d\u0435 \u0431\u0443\u0434\u0443\u0442 \u043c\u0435\u043d\u044f\u0442\u044c\u0441\u044f \u043d\u0438\u043a\u0430\u043a, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u043b\u0443\u0447\u0448\u0435 \u0438\u0445 \u0441\u0440\u0430\u0437\u0443 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0442\u044c, \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 <strong>\u043f\u0435\u0440\u0435\u0441\u0447\u0438\u0442\u044b\u0432\u0430\u044f \u043b\u0438\u0448\u044c \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0445<\/strong>.<\/p>\n<p>\u0414\u043b\u044f \u044d\u0442\u0438\u0445 \u0446\u0435\u043b\u0435\u0439 \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e <strong>\u043e\u0434\u043d\u043e\u0439 \u043e\u0431\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/strong>:<\/p>\n<pre><code class=\"sql\">CREATE TABLE metrics(   id     integer , ts     timestamp , temp_val     real , qty     integer );<\/code><\/pre>\n<p>\u0414\u043e\u0433\u043e\u0432\u043e\u0440\u0438\u043b\u0438\u0441\u044c, \u0447\u0442\u043e \u0435\u0441\u043b\u0438 \u0432 \u0437\u0430\u043f\u0438\u0441\u0438 <code>qty IS NULL<\/code>, \u0442\u043e \u044d\u0442\u043e \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0439 &#171;\u0444\u0430\u043a\u0442&#187; \u043e\u0442 \u0434\u0430\u0442\u0447\u0438\u043a\u0430, \u0430 <code>qty IS NOT NULL<\/code> &#8212; \u044d\u0442\u043e \u0443\u0436\u0435 \u0441\u0432\u043e\u0434\u043d\u044b\u0439 \u0430\u0433\u0440\u0435\u0433\u0430\u0442 \u043f\u043e \u043d\u0435\u043c\u0443 \u0437\u0430 \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u044b\u0439 \u0447\u0430\u0441.<\/p>\n<h2>Worker-\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0440<\/h2>\n<p>\u0427\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0441\u0432\u0435\u0436\u0438\u0435 \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0432 \u0431\u0430\u0437\u0435, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u0435\u0440\u0438\u043e\u0434\u0438\u0447\u0435\u0441\u043a\u0438 \u0431\u0443\u0434\u0435\u0442 \u0432\u044b\u0447\u0438\u0442\u044b\u0432\u0430\u0442\u044c \u0432\u0441\u0435 \u043f\u0435\u0440\u0432\u0438\u0447\u043d\u044b\u0435 \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0433\u043e \u0447\u0430\u0441\u0430 \u0438 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0432 \u0431\u0430\u0437\u0435:<\/p>\n<pre><code class=\"sql\">INSERT INTO metrics(id, ts, temp_val, qty) SELECT   id , date_trunc('hour', ts) ts , avg(temp_val) temp_val , count(*) qty FROM   metrics WHERE   qty IS NULL AND -- \u0442\u043e\u043b\u044c\u043a\u043e \"\u043f\u0435\u0440\u0432\u0438\u0447\u043a\u0430\"   ts >= date_trunc('hour', now() - '1 hour'::interval) GROUP BY   1, 2;<\/code><\/pre>\n<blockquote>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 &#8212; \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/539016\/\">&#171;\u0410\u0433\u0440\u0435\u0433\u0430\u0442\u044b \u0432 \u0411\u0414 \u2014 \u0437\u0430\u0447\u0435\u043c, \u043a\u0430\u043a, \u0430 \u0441\u0442\u043e\u0438\u0442 \u043b\u0438?&#187;<\/a>.<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u0434\u043b\u044f \u0442\u0430\u043a\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043d\u0430\u043c \u0438 \u0438\u043d\u0434\u0435\u043a\u0441 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439:<\/p>\n<pre><code class=\"sql\">CREATE INDEX ON metrics(ts)   WHERE qty IS NULL; -- \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0444\u0430\u043a\u0442\u043e\u0432<\/code><\/pre>\n<h2>\u0420\u0435\u0448\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u044b<\/h2>\n<p>\u041d\u043e \u0432\u0435\u0434\u044c \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u043b\u0438 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u0443 \u0430\u0433\u0440\u0435\u0433\u0430\u0446\u0438\u0438 \u0432 \u043f\u0440\u043e\u0448\u043b\u044b\u0439 \u0440\u0430\u0437, \u0432\u0435\u0441\u044c \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0438\u0439 \u0447\u0430\u0441 \u0443\u0436\u0435 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u043f\u043e\u0441\u0447\u0438\u0442\u0430\u043d, \u0438 <strong>\u0437\u0430\u043f\u0438\u0441\u0438 \u0437\u0430\u0434\u0443\u0431\u043b\u0438\u0440\u0443\u044e\u0442\u0441\u044f<\/strong>, \u0430 \u043c\u044b \u044d\u0442\u043e\u0433\u043e \u043d\u0435 \u0445\u043e\u0442\u0438\u043c! \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435, \u043a\u043e\u0442\u043e\u0440\u043e\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442 \u043d\u0430\u043c \u0433\u0430\u0440\u0430\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u043d\u0435 \u0431\u043e\u043b\u0435\u0435 \u043e\u0434\u043d\u043e\u0439 \u0430\u0433\u0440\u0435\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u043f\u0438\u0441\u0438 \u043f\u043e \u0434\u0430\u0442\u0447\u0438\u043a\u0443 \u0432 \u0447\u0430\u0441:<\/p>\n<pre><code class=\"sql\">CREATE UNIQUE INDEX ON metrics(id, ts)   WHERE qty IS NOT NULL; -- \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432<\/code><\/pre>\n<p>\u041d\u043e \u0442\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0448 <code>INSERT<\/code> \u043f\u0440\u043e\u0441\u0442\u043e \u0432\u0430\u043b\u0438\u0442\u0441\u044f \u0441 \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c&#8230; \u041d\u0435\u043f\u043e\u0440\u044f\u0434\u043e\u043a! \u041d\u0430\u043c \u0442\u043e \u0432\u0435\u0434\u044c \u043d\u0430\u0434\u043e, \u0447\u0442\u043e\u0431\u044b \u0434\u0430\u043d\u043d\u044b\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u043b\u0438\u0441\u044c:<\/p>\n<pre><code class=\"sql\">INSERT INTO metrics(id, ts, temp_val, qty)   SELECT     id   , date_trunc('hour', ts)   , avg(temp_val)   , count(*)   FROM     metrics   WHERE     qty IS NULL AND     ts >= date_trunc('hour', now() - '1 hour'::interval)   GROUP BY     1, 2 ON CONFLICT -- \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442   (id, ts) WHERE qty IS NOT NULL   DO UPDATE SET -- \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044c \u0432 \u043d\u043e\u0432\u043e\u0435 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435     (qty, temp_val) = (EXCLUDED.qty, EXCLUDED.temp_val);<\/code><\/pre>\n<h2>\u0411\u043e\u0440\u0435\u043c\u0441\u044f \u0441 &#171;\u043c\u0435\u0440\u0442\u0432\u0435\u0446\u0430\u043c\u0438&#187;<\/h2>\n<p>\u041d\u043e \u043f\u0440\u0438 \u0442\u0430\u043a\u043e\u043c \u043f\u043e\u0434\u0445\u043e\u0434\u0435 \u043a\u0430\u0436\u0434\u0430\u044f \u0437\u0430\u043f\u0438\u0441\u044c \u043f\u0440\u043e\u0448\u043b\u043e\u0433\u043e \u0447\u0430\u0441\u0430 \u043c\u043d\u043e\u0433\u043e\u043a\u0440\u0430\u0442\u043d\u043e <code>UPDATE<\/code>&#8216;\u0438\u0442\u0441\u044f, \u0447\u0442\u043e <strong>\u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442 \u043a &#171;\u0440\u0430\u0437\u0434\u0443\u0432\u0430\u043d\u0438\u044e&#187; \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/strong>! \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0443\u0432\u0438\u0434\u0435\u0442\u044c, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 \u043a \u043d\u0430\u0448\u0435\u043c\u0443 \u0437\u0430\u043f\u0440\u043e\u0441\u0443 <code>RETURNING *<\/code>.<\/p>\n<blockquote>\n<p>\u041f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 &#8212; \u0432 \u0441\u0442\u0430\u0442\u044c\u0435 <a href=\"https:\/\/habr.com\/ru\/post\/491366\/\">&#171;PostgreSQL Antipatterns: \u0441\u0440\u0430\u0436\u0430\u0435\u043c\u0441\u044f \u0441 \u043e\u0440\u0434\u0430\u043c\u0438 \u00ab\u043c\u0435\u0440\u0442\u0432\u0435\u0446\u043e\u0432\u00bb&#187;<\/a>.<\/p>\n<\/blockquote>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435 \u0437\u0430\u043f\u0438\u0441\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0440\u0435\u0430\u043b\u044c\u043d\u043e \u0438\u0437\u043c\u0435\u043d\u0438\u043b\u0438\u0441\u044c:<\/p>\n<pre><code class=\"sql\">INSERT INTO metrics AS m(id, ts, temp_val, qty)   SELECT     id   , date_trunc('hour', ts)   , avg(temp_val)   , count(*)   FROM     metrics   WHERE -- #1 : \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043e\u0442\u0431\u043e\u0440\u0430 \"\u043f\u0435\u0440\u0432\u0438\u0447\u043a\u0438\"     qty IS NULL AND     ts >= date_trunc('hour', now() - '1 hour'::interval)   GROUP BY     1, 2 ON CONFLICT   (id, ts) WHERE qty IS NOT NULL -- #2 : \u0443\u0441\u043b\u043e\u0432\u0438\u0435 UNIQUE-\u0438\u043d\u0434\u0435\u043a\u0441\u0430 \"\u0430\u0433\u0440\u0435\u0433\u0430\u0442\u043e\u0432\"   DO UPDATE SET     (temp_val, qty) = (EXCLUDED.temp_val, EXCLUDED.qty)     WHERE -- #3 : \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u0438       (m.temp_val, m.qty) IS DISTINCT FROM (EXCLUDED.temp_val, EXCLUDED.qty);<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043d\u0430\u043c \u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c <strong>\u0434\u0430\u0442\u044c \u043d\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u0430\u043b\u0438\u0430\u0441<\/strong> <code>AS m<\/code>, \u0447\u0442\u043e\u0431\u044b \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u043e\u0431\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u043a \u043d\u0435\u0439 \u0432 \u0443\u0441\u043b\u043e\u0432\u0438\u0438 \u043f\u043e \u043a\u043e\u0440\u043e\u0442\u043a\u043e\u043c\u0443 \u0438\u043c\u0435\u043d\u0438.<\/p>\n<p>\u0421\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u0432\u043e\u0442 \u0438 \u0432\u0441\u0435 &#8212; \u043d\u0430\u0448\u0430 \u0437\u0430\u0434\u0430\u0447\u0430 \u0440\u0435\u0448\u0435\u043d\u0430 &#171;\u0432 \u043e\u0434\u0438\u043d \u0437\u0430\u043f\u0440\u043e\u0441&#187; &#8212; \u043f\u0440\u0430\u0432\u0434\u0430, \u0432 \u043d\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c \u0446\u0435\u043b\u044b\u0445 \u0442\u0440\u0438 <code>WHERE<\/code>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p><!----><!----><\/div>\n<p><!----><!----><br \/> \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\/articles\/576894\/\"> https:\/\/habr.com\/ru\/articles\/576894\/<\/a><br \/><\/br><\/br><\/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-382067","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/382067","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=382067"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/382067\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=382067"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=382067"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=382067"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}