{"id":262571,"date":"2015-07-29T21:17:02","date_gmt":"2015-07-29T17:17:02","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=262571"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=262571","title":{"rendered":"PostgreSQL: \u041f\u0440\u0438\u0435\u043c\u044b \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435"},"content":{"rendered":"<p>     \t\u041c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043a\u043d\u0438\u0433 \u043f\u043e \u0431\u0430\u0437\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0443\u0447\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 \u0430\u0443\u0442\u0441\u043e\u0440\u0441 \u0438\u043b\u0438 \u0434\u043b\u044f \u0441\u0435\u0431\u044f. \u041d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043d\u0430 \u0433\u0440\u0430\u0431\u043b\u0438, \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0431\u0430\u0437\u0430\u043c\u0438\/\u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 downtime \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0441\u0442\u0438 \u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443, \u0430 \u0435\u0449\u0435 \u043b\u0443\u0447\u0448\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c. \u0412\u043e\u0442 \u0437\u0434\u0435\u0441\u044c \u0441\u0430\u043c\u044b\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u0430\u043a \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439. \u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0433\u0440\u0430\u0431\u043b\u0438 \u0438 \u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438\u0437 \u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u043f\u044b\u0442\u0430 \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043d\u0430\u043c \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 <a href=\"https:\/\/pushwoosh.com\">Pushwoosh <\/a>\u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/f00\/044\/190\/f00044190f4e4370bbd9e423b4d93530.jpg\" alt=\"image\"\/><br \/>  <a name=\"habracut\"><\/a><\/p>\n<h4>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443<\/h4>\n<p>  \u0412\u0440\u043e\u0434\u0435 \u0431\u044b \u043e\u0431\u044b\u0447\u043d\u0430\u044f \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u044f, \u043d\u0443\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043d\u043e\u0432\u0443\u044e \u0444\u0438\u0447\u0443 \u0438 \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0443\u0436\u0435 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443. \u041d\u043e \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0435 \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435 50Gb \u0434\u0430\u043d\u043d\u044b\u0445, \u0438 \u043a \u043d\u0435\u0439 \u043a\u0430\u0436\u0434\u0443\u044e \u0441\u0435\u043a\u0443\u043d\u0434\u0443 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0442\u044b\u0441\u044f\u0447 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043e\u0442 \u0436\u0438\u0432\u044b\u0445 \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432, \u0434\u043b\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u043e\u0432 \u044d\u0442\u043e \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u043d\u0435\u0437\u0430\u043c\u0435\u0442\u043d\u043e.<\/p>\n<p>  <u>\u0417\u0430\u0434\u0430\u0447\u0430<\/u>: \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0434\u043b\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435.<br \/>  <u>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/u>: \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443. <br \/>  <u>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/u>: \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0441\u0442\u043e\u043b\u0431\u0446\u0430 \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0435\u0441\u043b\u0438 DEFAULT \u043e\u043f\u0443\u0449\u0435\u043d \u0438\u043b\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f DEFAULT NULL.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u0435\u0445 \u043a\u043e\u043c\u0443, \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u0443\u044e <a href=\"http:\/\/www.postgresql.org\/docs\/9.4\/static\/sql-altertable.html\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e <\/a>.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043e\u0442\u0440\u044b\u0432\u043e\u043a \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/b><\/p>\n<div class=\"spoiler_text\">When a column is added with ADD COLUMN, all existing rows in the table are initialized with the column&#8217;s default value (NULL if no DEFAULT clause is specified). If there is no DEFAULT clause, this is merely a metadata change and does not require any immediate update of the table&#8217;s data; the added NULL values are supplied on readout, instead.<\/p>\n<p>  Adding a column with a DEFAULT clause or changing the type of an existing column will require the entire table and its indexes to be rewritten. As an exception when changing the type of an existing column, if the USING clause does not change the column contents and the old type is either binary coercible to the new type or an unconstrained domain over the new type, a table rewrite is not needed; but any indexes on the affected columns must still be rebuilt. Adding or removing a system oid column also requires rewriting the entire table. Table and\/or index rebuilds may take a significant amount of time for a large table; and will temporarily require as much as double the disk space.  <\/div>\n<\/div>\n<p>  \u041d\u043e \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0435\u0441\u043b\u0438 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u043f\u0440\u043e\u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u0441\u0442\u043e\u043b\u0431\u0435\u0446 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e? \u0412 \u0434\u0430\u043d\u043d\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u0441\u043a\u0440\u0438\u043f\u0442\u0438\u043a, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0438\u0434\u0442\u0438 \u043f\u043e \u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u0438 \u0438\u0437\u043c\u0435\u043d\u044f\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u043f\u043e\u0440\u0446\u0438\u044f\u043c\u0438 \u0437\u0430\u043f\u0438\u0441\u0435\u0439.<\/p>\n<h4>\u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0432 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443<\/h4>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u0443\u0436\u0435 \u043a \u043d\u0430\u043c \u043f\u0440\u0438\u0448\u0435\u043b \u043e\u0442\u0434\u0435\u043b \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0438, \u0438 \u0445\u043e\u0447\u0435\u0442 \u043f\u043e\u043b\u0443\u0447\u0430\u0442\u044c \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0443\u044e\u0449\u0438\u0435 \u0438\u0445 \u0434\u0430\u043d\u043d\u044b\u0435 \u043a\u0430\u0436\u0434\u044b\u0439 \u0447\u0430\u0441, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u043c \u043d\u0443\u0436\u0435\u043d \u043d\u043e\u0432\u044b\u0439 sql-\u0437\u0430\u043f\u0440\u043e\u0441, \u043d\u043e \u043e\u043d \u0441\u043b\u043e\u0436\u043d\u044b\u0439, \u0430 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0443 \u043d\u0430\u0441 \u043d\u0435\u0442.<\/p>\n<p>  <u>\u0417\u0430\u0434\u0430\u0447\u0430<\/u>: \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u0434\u043b\u044f \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u044e\u0449\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435<br \/>  <u>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/u>: \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u0430 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0437\u0430\u043f\u0438\u0441\u044c(insert\/update\/delete) \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443. \u0427\u0438\u0442\u0430\u0442\u044c \u0438\u0437 \u0442\u0430\u043a\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e-\u043f\u0440\u0435\u0436\u043d\u0435\u043c\u0443 \u043c\u043e\u0436\u043d\u043e.<br \/>  <u>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/u>: \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c CREATE INDEX CONCURRENTLY, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043d\u0435 \u0431\u043b\u043e\u043a\u0438\u0440\u0443\u0435\u0442 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0432 \u0434\u0432\u0430 \u0440\u0430\u0437\u0430 \u0434\u043e\u043b\u044c\u0448\u0435 \u0438 \u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0431\u043e\u043b\u044c\u0448\u0435 \u0441\u0438\u0441\u0442\u0435\u043c\u043d\u044b\u0445 \u0440\u0435\u0441\u0443\u0440\u0441\u043e\u0432.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u0435\u0445, \u043a\u043e\u043c\u0443 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043f\u043e\u043b\u043d\u0443\u044e <a href=\"http:\/\/www.postgresql.org\/docs\/9.4\/static\/sql-createindex.html#SQL-CREATEINDEX-CONCURRENTLY\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044e<\/a>.<\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u041e\u0441\u043d\u043e\u0432\u043d\u043e\u0439 \u043e\u0442\u0440\u044b\u0432\u043e\u043a \u0438\u0437 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/b><\/p>\n<div class=\"spoiler_text\">Creating an index can interfere with regular operation of a database. Normally PostgreSQL locks the table to be indexed against writes and performs the entire index build with a single scan of the table. Other transactions can still read the table, but if they try to insert, update, or delete rows in the table they will block until the index build is finished. This could have a severe effect if the system is a live production database. Very large tables can take many hours to be indexed, and even for smaller tables, an index build can lock out writers for periods that are unacceptably long for a production system.<\/p>\n<p>  PostgreSQL supports building indexes without locking out writes. This method is invoked by specifying the CONCURRENTLY option of CREATE INDEX. When this option is used, PostgreSQL must perform two scans of the table, and in addition it must wait for all existing transactions that could potentially use the index to terminate. Thus this method requires more total work than a standard index build and takes significantly longer to complete. However, since it allows normal operations to continue while the index is built, this method is useful for adding new indexes in a production environment. Of course, the extra CPU and I\/O load imposed by the index creation might slow other operations.  <\/div>\n<\/div>\n<h4>\u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0442\u0435\u043a\u0443\u0449\u0438\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432 \u043a \u0431\u0430\u0437\u0435<\/h4>\n<p>  \u0412\u0430\u0436\u043d\u043e \u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442, \u043a\u0430\u043a\u043e\u0439 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0438 \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u0443\u0436\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442. \u0423 PostgreSQL \u0435\u0441\u0442\u044c \u043e\u0442\u043b\u0438\u0447\u043d\u0430\u044f \u0441\u043b\u0443\u0436\u0435\u0431\u043d\u0430\u044f \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0430 pg_stat_activity \u043f\u043e\u043b\u043d\u043e\u0435 \u0435\u0435 \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0439\u0442\u0438 \u0432 <a href=\"http:\/\/www.postgresql.org\/docs\/9.2\/static\/monitoring-stats.html\">\u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438<\/a>. \u041f\u0440\u0438\u0432\u0435\u0434\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u043f\u043e\u043b\u044f  <\/p>\n<table>\n<tr>\n<td>pid<\/td>\n<td>integer<\/td>\n<td>Process ID of this backend<\/td>\n<\/tr>\n<tr>\n<td>query_start<\/td>\n<td>timestamp with time zone<\/td>\n<td>Time when the currently active query was started, or if state is not active, when the last query was started<\/td>\n<\/tr>\n<tr>\n<td>query<\/td>\n<td>text<\/td>\n<td>Text of this backend&#8217;s most recent query. If state is active this field shows the currently executing query. In all other states, it shows the last query that was executed.<\/td>\n<\/tr>\n<\/table>\n<p>  \u0412 \u0438\u0442\u043e\u0433\u0435 \u0440\u0430\u0437 \u0443 \u043d\u0430\u0441 \u0435\u0441\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 query_start, \u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043b\u0435\u0433\u043a\u043e \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c, \u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0440\u043e\u0441 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0438 \u043e\u0442\u0441\u043e\u0440\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u043e \u0432\u0440\u0435\u043c\u0435\u043d\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f.<\/p>\n<pre><code class=\"sql\">pushwoosh=# select now() - query_start, pid, waiting, query from pg_stat_activity where state != 'idle' order by 1 desc;     ?column?     |  pid  | waiting |  query -----------------------------------------------------------------------------------------------------------------------------------------------------------------------  23:42:13.468115 | 6877  | f       | DELETE FROM application_goals WHERE applicationid = '9254'  00:30:51.943691 | 24106 | f       | SELECT applicationid, pushtoken FROM application_devices WHERE hwid in ('1abd5e5fd79318cd','1abd5f3eda7acbca','1abd601f2bafabf8','1abd62a0a092ac17','1abd6303b83accf9','1abd64726a98fb63','1abd676c087c3617','1abd67ebecb6f3ce','1abd68a3b78fb730','1abd697af6bc8552','1abd70ebb654aeb2','1abd7114a8576a67','1abd729a385caff8','1abd731ff62c4521','1abd738bd2d457eb','1abd7760f7210155','1abd79dbc085c2c0','1abd7ab46dc24304','1abd7d48bd5e04ab','1abd7e7aee3c0e58','1abd7e8129a53ab3','1abd827c8c21630','1abd82cd204c69a9','1abd843ee3dedb1','1abd88d346c74d67','1abd88e8bd01c168','1abd8ceac00808cc','1abd8d3b2cb72de3','1abd8e139f267260','1abd8e74a288204c','1abd8f00bb4a0433','1abd8fd7e8f4f125','1abd91c193455ada','1abd92448396a9bf','1abd946ac4cf0e22','1abd9594ed1bd791','1abd96cc0df2202b','1abd975a98849a0b','1abd997c96d3c9b1','1abd9b3cfb66852c','1abd9bead472be5','1abd9f5bed3cbbd8','1abd9f73b8122bf1','1abda233b9a00633','1abda2ee3db5bccb','1abda486901c3a14','1abdac09e0e3267b','1abdae8235cf19dd','1abdaf9e3a143041','1abdb54fe96'  00:04:49.592503 | 18899 | f       | autovacuum: ANALYZE public.device_tags_values  00:00:00.040265 | 11748 | f       | INSERT INTO device_tags_values (hwid,valueid) VALUES ('27976b81cc72c7ac','8470317') RETURNING uid <\/code><\/pre>\n<p>  \u0418\u0437 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432\u044b\u0432\u043e\u0434\u0430 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0441 PID&#8217;\u043e\u043c 6877 \u0443\u0436\u0435 \u043f\u043e\u0447\u0442\u0438 \u0441\u0443\u0442\u043a\u0438 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0435\u0440\u043e\u044f\u0442\u043d\u043e \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u0435\u043d \u0438 \u043d\u0443\u0436\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u0431\u043e\u043b\u0435\u0435 \u0434\u0435\u0442\u0430\u043b\u044c\u043d\u043e\u043c \u043f\u0440\u043e\u0444\u0438\u043b\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u0438. \u0422\u0430\u043a \u0436\u0435 \u043c\u044b \u0432\u0438\u0434\u0438\u043c, \u0447\u0442\u043e \u0432\u0442\u043e\u0440\u043e\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u043b\u0447\u0430\u0441\u0430 \u0438 \u0441\u043a\u043e\u0440\u0435\u0435 \u0432\u0441\u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u0435\u043d, \u043d\u043e \u043c\u044b \u043d\u0435 \u0432\u0438\u0434\u0438\u043c \u0437\u0430\u043f\u0440\u043e\u0441 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e, \u043e\u043d \u043e\u0431\u0440\u0435\u0437\u0430\u043d, \u0430 \u043d\u0430\u043c \u0436\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u0435\u043d \u0437\u0430\u043f\u0440\u043e\u0441 \u0446\u0435\u043b\u0438\u043a\u043e\u043c.<br \/>  <u>\u0417\u0430\u0434\u0430\u0447\u0430<\/u>: \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u0432 \u0431\u0430\u0437\u0435 \u0434\u0430\u043d\u043d\u044b\u0445.<br \/>  <u>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/u>: pg_stat_activity \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442 \u0442\u0435\u043a\u0443\u0449\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043d\u0435 \u043f\u043e\u043b\u043d\u043e\u0441\u0442\u044c\u044e(\u043e\u0431\u0440\u0435\u0437\u0430\u0435\u0442).<br \/>  <u>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/u>: <b>gdb<\/b>.<\/p>\n<p>  \u0412\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u0443 \u044d\u0442\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u043a\u0438 \u0435\u0441\u0442\u044c \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u043f\u0440\u043e\u0449\u0435, \u043d\u043e \u043c\u044b \u0435\u0433\u043e \u043d\u0435 \u043d\u0430\u0448\u043b\u0438. \u0411\u0435\u0440\u0435\u043c PID \u0438\u0437 \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0432\u044b\u0448\u0435 \u0438 \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u043a \u043d\u0435\u043c\u0443   <\/p>\n<pre><code>gdb [path_to_postgres] [pid] <\/code><\/pre>\n<p>  \u0438 \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u043b\u0438\u0441\u044c \u043a \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0443 \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u0435\u043c  <\/p>\n<pre><code>printf &quot;%s\\n&quot;, debug_query_string <\/code><\/pre>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0412 \u043d\u0430\u0448\u0435\u043c \u0441\u043b\u0443\u0447\u0430\u0435<\/b><\/p>\n<div class=\"spoiler_text\">bash$ gdb postgres 24106<br \/>  GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-64.el7<br \/>  Copyright \u00a9 2013 Free Software Foundation, Inc.<br \/>  License GPLv3+: GNU GPL version 3 or later &lt;<a href=\"http:\/\/gnu.org\/licenses\/gpl.html\">gnu.org\/licenses\/gpl.html<\/a>&gt;<br \/>  This is free software: you are free to change and redistribute it.<br \/>  There is NO WARRANTY, to the extent permitted by law. Type \u00abshow copying\u00bb<br \/>  and \u00abshow warranty\u00bb for details.<br \/>  This GDB was configured as \u00abx86_64-redhat-linux-gnu\u00bb.<br \/>  For bug reporting instructions, please see:<br \/>  &lt;<a href=\"http:\/\/www.gnu.org\/software\/gdb\/bugs\/\">www.gnu.org\/software\/gdb\/bugs<\/a>&gt;\u2026<br \/>  Reading symbols from \/usr\/bin\/postgres&#8230;Reading symbols from \/usr\/bin\/postgres&#8230;(no debugging symbols found)&#8230;done.<br \/>  (no debugging symbols found)&#8230;done.<br \/>  Attaching to program: \/bin\/postgres, process 24106<br \/>  Reading symbols from \/lib64\/libxml2.so.2&#8230;Reading symbols from \/lib64\/libxml2.so.2&#8230;(no debugging symbols found)&#8230;done.<br \/>  (no debugging symbols found)&#8230;done.<br \/>  \u2026<br \/>  (gdb) printf &quot;%s\\n&quot;, debug_query_string  <\/div>\n<\/div>\n<h4>Slow log<\/h4>\n<p>  \u0414\u043b\u044f \u0431\u0430\u0437\u044b \u0432\u0441\u0435\u0433\u0434\u0430 \u043d\u0443\u0436\u043d\u043e \u0438\u043c\u0435\u0442\u044c \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u043d\u0433 \u0438 \u0432\u0438\u0434\u0435\u0442\u044c, \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u044e\u0442\u0441\u044f \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e. \u041c\u043e\u0436\u043d\u043e \u0440\u0435\u0448\u0438\u0442\u044c \u044d\u0442\u043e \u043a\u043e\u0434\u043e\u043c, \u0438 \u0432 ORM \u0438\u043b\u0438 \u0433\u0434\u0435-\u0442\u043e \u0433\u043b\u0443\u0431\u0436\u0435 \u0438\u0437\u043c\u0435\u0440\u044f\u0442\u044c \u0432\u0440\u0435\u043c\u044f \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0438 \u0435\u0441\u043b\u0438 \u043e\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u043f\u043e\u0440\u043e\u0433\u043e\u0432\u043e\u0439 \u0432\u0435\u043b\u0438\u0447\u0438\u043d\u044b \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u0442\u044c \u0434\u0430\u043d\u043d\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441 \u0432 \u043b\u043e\u0433. \u041d\u043e \u0432\u0441\u0435\u0433\u0434\u0430 \u043b\u0443\u0447\u0448\u0435 \u043d\u0435 \u043f\u0438\u0441\u0430\u0442\u044c \u0432\u0435\u043b\u043e\u0441\u0438\u043f\u0435\u0434.<br \/>  <u>\u0417\u0430\u0434\u0430\u0447\u0430<\/u>: \u041c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u0442\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b<br \/>  <u>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/u>: \u0425\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u044d\u0442\u043e \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445<br \/>  <u>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/u>: log_min_duration_statement<\/p>\n<p>  \u041d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 <a href=\"http:\/\/www.postgresql.org\/docs\/9.1\/static\/runtime-config-logging.html\">log_min_duration_statement <\/a>\u0437\u0430\u0434\u0430\u0435\u0442\u0441\u044f \u0432 \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u0445, \u0438 \u043b\u043e\u0433\u0438\u0440\u0443\u0435\u0442 \u0432\u0441\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0431\u0430\u0437\u0435, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u043b\u0438\u0441\u044c \u0434\u043e\u043b\u044c\u0448\u0435 \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f.<\/p>\n<p>  \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u043e\u043f\u0440\u0430\u0432\u0438\u043c \u043a\u043e\u043d\u0444\u0438\u0433 PostgreSQL vim \/var\/lib\/pgsql\/9.4\/data\/posgresql.conf \u0438 \u043f\u043e\u0441\u0442\u0430\u0432\u0438\u043c \u0432 \u043d\u0435\u043c 3 \u0441\u0435\u043a\u0443\u043d\u0434\u044b \u043a\u0430\u043a \u043f\u043e\u0440\u043e\u0433\u043e\u0432\u043e\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435  <\/p>\n<pre><code>log_min_duration_statement = 3000       # -1 is disabled, 0 logs all statements                                         # and their durations, &gt; 0 logs only                                         # statements running at least this number                                         # of milliseconds <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432\u0441\u0442\u0443\u043f\u0438\u043b\u0438 \u0432 \u0441\u0438\u043b\u0443 \u043d\u0435 \u043e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u0435\u0440\u0435\u0437\u0430\u0433\u0440\u0443\u0436\u0430\u0442\u044c \u0431\u0430\u0437\u0443, \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0438\u0437 psql, pgadmin \u0438\u043b\u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043a \u0431\u0430\u0437\u0435  <\/p>\n<pre><code>SELECT pg_reload_conf(); <\/code><\/pre>\n<p>  \u0438\u043b\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0438\u0442\u044c \u0438\u0437 \u043a\u043e\u043c\u0430\u043d\u0434\u043d\u043e\u0439 \u0441\u0442\u0440\u043e\u043a\u0438  <\/p>\n<pre><code>su - postgres \/usr\/bin\/pg_ctl reload <\/code><\/pre>\n<p>  \u041d\u0443\u0436\u043d\u043e \u043f\u043e\u043c\u043d\u0438\u0442\u044c, \u0447\u0442\u043e \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u0432 \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u043e\u043c \u0444\u0430\u0439\u043b\u0435 \u0432\u0441\u0442\u0443\u043f\u044f\u0442 \u0432 \u0441\u0438\u043b\u0443 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u043e\u0441\u043b\u0435 \u043f\u0435\u0440\u0435\u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445.<\/p>\n<p>  \u0418 \u043f\u043e\u0441\u043b\u0435 \u044d\u0442\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0432 \u043b\u043e\u0433 PostgreSQL, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0443 \u043d\u0430\u0441 \u043d\u0430\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u043e \u0442\u0430\u043a\u043e\u043c\u0443 \u043f\u0443\u0442\u0438 \/var\/lib\/pgsql\/9.4\/data\/pg_log\/postgresql-2015-07-07.log \u0438 \u0437\u0434\u0435\u0441\u044c \u0432\u0438\u0434\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u043d\u044f\u0435\u0442\u0441\u044f \u043f\u043e\u0447\u0442\u0438 6 \u0441\u0435\u043a\u0443\u043d\u0434.  <\/p>\n<pre><code>2015-07-07 09:39:30 UTC 192.168.100.82(45276) LOG:  duration: 5944.540 ms  statement: SELECT * FROM application_devices WHERE applicationid='1234' AND hwid='95ea842e368f6a64' LIMIT 1 <\/code><\/pre>\n<p>  \u041a\u0430\u043a \u0432\u0430\u0440\u0438\u0430\u043d\u0442 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c, \u0447\u0442\u043e\u0431\u044b \u043c\u043e\u043d\u0438\u0442\u043e\u0440\u0438\u0442\u044c \u043b\u043e\u0433-\u0444\u0430\u0439\u043b \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u0432\u044f\u0437\u043a\u0443 logstash+elasticsearch+kibana \u0438 \u0441\u0440\u0430\u0437\u0443 \u0441\u043b\u0430\u0442\u044c \u0447\u0435\u0440\u0435\u0437 zabbix \u0443\u0432\u0435\u0434\u043e\u043c\u043b\u0435\u043d\u0438\u0435 \u043e \u043f\u043e\u044f\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u044b\u0445 \u0437\u0430\u043f\u0440\u043e\u0441\u043e\u0432, \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043a\u0440\u0438\u0442\u0438\u0447\u043d\u044b\u043c \u0434\u043b\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430.<\/p>\n<h4>\u0423\u0437\u043d\u0430\u0442\u044c \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u043c\u043e\u043c\u0435\u043d\u0442 \u0434\u0435\u043b\u0430\u0435\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0430 \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d\u0435<\/h4>\n<p>  \u0415\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 \u043c\u043d\u043e\u0433\u043e \u0434\u0435\u043c\u043e\u043d\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0447\u0430\u0441\u0442\u043e \u043e\u0431\u0449\u0430\u044e\u0442\u0441\u044f \u0441 \u0431\u0430\u0437\u043e\u0439, \u0438 \u0432 \u043e\u0434\u0438\u043d \u043e\u0431\u044b\u0447\u043d\u044b\u0439 \u0434\u0435\u043d\u044c \u0434\u0435\u043c\u043e\u043d \u043d\u0430\u0447\u0430\u043b \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u043e \u0438\u043b\u0438 \u0441\u0442\u0430\u043b\u043e \u043d\u0435 \u043f\u043e\u043d\u044f\u0442\u043d\u043e, \u0447\u0442\u043e \u043e\u043d \u0434\u0435\u043b\u0430\u0435\u0442, \u0442\u043e \u043d\u0430 \u043f\u043e\u043c\u043e\u0449\u044c \u043f\u0440\u0438\u0434\u0435\u0442 <b>strace<\/b>, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u043a\u0430\u0436\u0435\u0442 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043a \u0431\u0430\u0437\u0435 \u0438 \u0432\u0440\u0435\u043c\u044f \u0438\u0445 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f, \u0431\u0435\u0437 \u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0438 \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430, \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043b\u043e\u0433\u043e\u0432 \u0432 \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0443 \u0438 \u043e\u0436\u0438\u0434\u0430\u043d\u0438\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0433\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u043d\u043e\u0432\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b \u2014 \u043d\u0435\u0432\u0430\u0436\u043d\u043e, \u043d\u0430 \u0447\u0435\u043c \u0432\u044b \u043f\u0438\u0448\u0438\u0442\u0435 php, python, ruby, etc. \u2014 strace \u043f\u043e\u0434\u0445\u043e\u0434\u0438\u0442 \u0434\u043b\u044f \u0432\u0441\u0435\u0433\u043e.<br \/>  <u>\u0417\u0430\u0434\u0430\u0447\u0430<\/u>: \u0443\u0437\u043d\u0430\u0442\u044c, \u0447\u0442\u043e \u0434\u0435\u043b\u0430\u0435\u0442 \u043f\u0440\u043e\u0446\u0435\u0441\u0441(\u043a\u0430\u043a \u043f\u0440\u0438\u043c\u0435\u0440 \u043a\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u0448\u043b\u0435\u0442 \u0432 \u0431\u0430\u0437\u0443)<br \/>  <u>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/u>: \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0435\u043b\u044c\u0437\u044f \u043f\u0440\u0435\u0440\u044b\u0432\u0430\u0442\u044c \u0438\u043b\u0438 \u043e\u0441\u0442\u0430\u043d\u0430\u0432\u043b\u0438\u0432\u0430\u0442\u044c.<br \/>  <u>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/u>: <b>strace<\/b><\/p>\n<p>  \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0432\u0437\u044f\u0442\u044c pid \u043f\u0440\u043e\u0446\u0435\u0441\u0441\u0430 \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u0434\u043b\u0438\u043d\u0443 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043e\u043f\u0446\u0438\u044e -T. \u0412 \u0438\u0442\u043e\u0433\u0435 \u0432\u044b\u0432\u043e\u0434 strace \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a\u0438\u043c<\/p>\n<p>  strace -p 27345 -s 1024 -T 2&gt; out  <\/p>\n<pre><code>gettimeofday({1437846841, 447186}, NULL) = 0 &lt;0.000004&gt; sendto(8, &quot;Q\\0\\0\\0005SELECT * FROM accounts WHERE uid='25143' LIMIT 1\\0&quot;, 54, MSG_NOSIGNAL, NULL, 0) = 54 &lt;0.000013&gt; poll([{fd=8, events=POLLIN|POLLERR}], 1, -1) = 1 ([{fd=8, revents=POLLIN}]) &lt;0.000890&gt; <\/code><\/pre>\n<h4>NULL \u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0435 \u0438\u043d\u0434\u0435\u043a\u0441\u044b<\/h4>\n<p>  \u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043a \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d \u0441\u0440\u0435\u0434\u0435. \u041d\u0430\u0447\u043d\u0435\u043c \u0441 \u043f\u0440\u043e\u0441\u0442\u043e\u0433\u043e \u0444\u0430\u043a\u0442\u0430 NULL \u043d\u0435\u0440\u0430\u0432\u0435\u043d NULL.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/b><\/p>\n<div class=\"spoiler_text\">Do not write expression = NULL because NULL is not \u00abequal to\u00bb NULL. (The null value represents an unknown value, and it is not known whether two unknown values are equal.) This behavior conforms to the SQL standard.  <\/div>\n<\/div>\n<p>  \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0447\u0442\u043e \u043c\u044b \u0445\u043e\u0442\u0438\u043c \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441, \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u043e\u0434\u043d\u043e \u0438\u0437 \u043f\u043e\u043b\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u0438\u043c\u0435\u0442\u044c \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 NULL, \u0430 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c \u0447\u0438\u0441\u043b\u043e\u043c. \u041f\u0440\u0438 \u044d\u0442\u043e\u043c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0435 \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0439 \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 NULL, \u043d\u043e \u043e\u0447\u0435\u043d\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0444\u0438\u043b\u044c\u0442\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0430\u043a\u0438\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 \u0438\u043d\u0434\u0435\u043a\u0441\u043e\u043c.  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">\u0414\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f<\/b><\/p>\n<div class=\"spoiler_text\">When an index is declared unique, multiple table rows with equal indexed values are not allowed. Null values are not considered equal. A multicolumn unique index will only reject cases where all indexed columns are equal in multiple rows.  <\/div>\n<\/div>\n<p>  \u0420\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u0438\u043c\u0435\u0440<\/p>\n<pre><code class=\"sql\">psql=# create table test ( psql(#     a varchar NOT NULL, psql(#     b varchar default null psql(# ); CREATE TABLE psql=# create unique index on test (a, b); CREATE INDEX psql=# insert into test values (1, null); INSERT 0 1 psql=# insert into test values (1, null); INSERT 0 1 psql=# select * from test;  a | b ---+---  1 |  1 | (2 rows) <\/code><\/pre>\n<p>  \u041d\u0435\u0441\u043c\u043e\u0442\u0440\u044f \u043d\u0430 \u0442\u043e, \u0447\u0442\u043e \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u043b\u0438 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441, \u0437\u0430\u043f\u0438\u0441\u044c (1, null) \u0432\u0441\u0442\u0430\u0432\u0438\u043b\u0430\u0441\u044c \u0434\u0432\u0430\u0436\u0434\u044b. <\/p>\n<p>  <u>\u0417\u0430\u0434\u0430\u0447\u0430<\/u>: \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0441\u043e\u0441\u0442\u0430\u0432\u043d\u043e\u0439 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441, \u043e\u0434\u043d\u043e \u0438\u0437 \u043f\u043e\u043b\u0435\u0439 \u043c\u043e\u0436\u0435\u0442 \u0431\u044b\u0442\u044c null<br \/>  <u>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/u>: \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0435 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u0442 null \u043f\u043e\u043b\u044f<br \/>  <u>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/u>: \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 2 \u0440\u0430\u0437\u043d\u044b\u0445 \u0438\u043d\u0434\u0435\u043a\u0441\u0430.<\/p>\n<p>  \u0414\u043b\u044f \u0442\u043e\u0433\u043e \u0447\u0442\u043e\u0431\u044b \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c \u0442\u0430\u043a\u043e\u0433\u043e \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0437\u0431\u0438\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441 \u043d\u0430 2 \u0438\u043d\u0434\u0435\u043a\u0441\u0430.  <\/p>\n<pre><code class=\"sql\">sql=# create table test ( sql(#     a varchar NOT NULL, sql(#     b varchar default null sql(# ); CREATE TABLE sql=# create unique index on test (a, b) where b is not null; CREATE INDEX sql=# create unique index on test (a) where b is null; CREATE INDEX sql=# insert into test values (1, null); INSERT 0 1 sql=# insert into test values (1, null); ERROR:  duplicate key value violates unique constraint &quot;test_a_idx&quot; DETAIL:  Key (a)=(1) already exists. <\/code><\/pre>\n<h4>\u041e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0430 \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u043e\u0432<\/h4>\n<p>  \u042d\u0442\u043e\u0442 \u043f\u0440\u0438\u043c\u0435\u0440 \u0442\u0430\u043a \u0436\u0435 \u043d\u0435 \u0438\u043c\u0435\u0435\u0442 \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044f \u043a \u043f\u0440\u043e\u0434\u0430\u043a\u0448\u0435\u043d \u0441\u0440\u0435\u0434\u0435.<br \/>  <u>\u0417\u0430\u0434\u0430\u0447\u0430<\/u>: \u0423\u043c\u0435\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0434\u0443\u0431\u043b\u0438\u043a\u0430\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043d\u0435 \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u043b \u0441\u043e\u0437\u0434\u0430\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0434\u0435\u043a\u0441.<br \/>  <u>\u041f\u0440\u043e\u0431\u043b\u0435\u043c\u0430<\/u>: \u041d\u0443\u0436\u043d\u043e \u043b\u043e\u0432\u0438\u0442\u044c \u044d\u043a\u0441\u0435\u043f\u0448\u0435\u043d\u044b, \u043f\u0430\u0440\u0441\u0438\u0442\u044c \u043a\u043e\u0434\u044b \u043e\u0448\u0438\u0431\u043e\u043a<br \/>  <u>\u0420\u0435\u0448\u0435\u043d\u0438\u0435<\/u>: keep calm and catch exception \u0438 \u0436\u0434\u0430\u0442\u044c \u043f\u043e\u043a\u0430 \u043d\u0435 \u0432\u044b\u0439\u0434\u0435\u0442 PostgreSQL <a href=\"http:\/\/www.postgresql.org\/developer\/roadmap\/\">9.5<\/a> \u0432 \u043a\u043e\u0442\u043e\u0440\u043e\u043c \u0431\u0443\u0434\u0435\u0442 <a href=\"https:\/\/wiki.postgresql.org\/wiki\/UPSERT\">upsert<\/a>     \t<\/p>\n<div class=\"clear\"><\/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=\"http:\/\/habrahabr.ru\/post\/263541\/\"> http:\/\/habrahabr.ru\/post\/263541\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>     \t\u041c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u0442\u044c \u043c\u043d\u043e\u0433\u043e \u043a\u043d\u0438\u0433 \u043f\u043e \u0431\u0430\u0437\u0430\u043c \u0434\u0430\u043d\u043d\u044b\u0445, \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u043a\u0443\u0447\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0439 \u043d\u0430 \u0430\u0443\u0442\u0441\u043e\u0440\u0441 \u0438\u043b\u0438 \u0434\u043b\u044f \u0441\u0435\u0431\u044f. \u041d\u043e \u043f\u0440\u0438 \u044d\u0442\u043e\u043c \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u043d\u0430\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043d\u0430 \u0433\u0440\u0430\u0431\u043b\u0438, \u043f\u0440\u0438 \u0440\u0430\u0431\u043e\u0442\u0435 \u0441 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0431\u043e\u043b\u044c\u0448\u0438\u043c\u0438 \u0431\u0430\u0437\u0430\u043c\u0438\/\u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438 \u043e\u0441\u043e\u0431\u0435\u043d\u043d\u043e, \u043a\u043e\u0433\u0434\u0430 downtime \u043d\u0430 \u0431\u043e\u043b\u044c\u0448\u043e\u043c \u043f\u0440\u043e\u0435\u043a\u0442\u0435 \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0441\u0432\u0435\u0441\u0442\u0438 \u043a \u043c\u0438\u043d\u0438\u043c\u0443\u043c\u0443, \u0430 \u0435\u0449\u0435 \u043b\u0443\u0447\u0448\u0435 \u0441\u043e\u0432\u0441\u0435\u043c \u0438\u0437\u0431\u0435\u0436\u0430\u0442\u044c. \u0412\u043e\u0442 \u0437\u0434\u0435\u0441\u044c \u0441\u0430\u043c\u044b\u0435 \u043f\u0440\u043e\u0441\u0442\u044b\u0435 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438, \u043a\u0430\u043a \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u0442\u0440\u0443\u043a\u0442\u0443\u0440\u044b \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043c\u043e\u0436\u0435\u0442 \u0441\u0442\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u0441\u043b\u043e\u0436\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0435\u0439. \u041d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u044b\u0435 \u0441\u043b\u0443\u0447\u0430\u0438, \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u044b, \u0433\u0440\u0430\u0431\u043b\u0438 \u0438 \u0438\u0445 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0438\u0437 \u043b\u0438\u0447\u043d\u043e\u0433\u043e \u043e\u043f\u044b\u0442\u0430 \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u043d\u0430\u043c \u043d\u0430 \u043f\u0440\u043e\u0435\u043a\u0442\u0435 <a href=\"https:\/\/pushwoosh.com\">Pushwoosh <\/a>\u043f\u0440\u0438\u0448\u043b\u043e\u0441\u044c \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u0442\u044c\u0441\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u044b \u043f\u043e\u0434 \u043a\u0430\u0442\u043e\u043c.<\/p>\n<p>  <img decoding=\"async\" src=\"https:\/\/habrastorage.org\/files\/f00\/044\/190\/f00044190f4e4370bbd9e423b4d93530.jpg\" alt=\"image\"\/>  <\/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-262571","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262571","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=262571"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/262571\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=262571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=262571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=262571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}