{"id":368496,"date":"2024-05-21T03:43:53","date_gmt":"2024-05-21T03:43:53","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=368496"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=368496","title":{"rendered":"<span>PostgreSQL: \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0435\u043d\u0438\u0435 \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0441 \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u043e\u0439 \u0434\u0430\u0442\u044b \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u0431\u044b \u0432\u044b \u0440\u0435\u0448\u0430\u043b\u0438 \u0442\u0430\u043a\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443? \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043a\u0443\u043f\u043e\u043d\u0430\u043c\u0438, \u0438 \u0443 \u043a\u0443\u043f\u043e\u043d\u043e\u0432 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0430\u044f \u0434\u0430\u0442\u0430 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0430\u043d\u0438\u044f valid_until. \u0412\u0430\u043c \u043d\u0430\u0434\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 (constraint) \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0411\u0414, \u0447\u0442\u043e\u0431\u044b \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u0443\u043f\u043e\u043d.<\/p>\n<p>  <\/p>\n<p>\u0422.\u0435., \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"sql\">CREATE TABLE coupons (     id  bigint primary key generated by default as identity,     user_id bigint not null,     created_at timestamp not null,     valid_until timestamp not null )<\/code><\/pre>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"popytka-resheniya-v-lob\">\u041f\u043e\u043f\u044b\u0442\u043a\u0430 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0432 \u043b\u043e\u0431<\/h2>\n<p>  <\/p>\n<p>\u0414\u0443\u043c\u0430\u044e, \u0432\u0441\u0435\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e \u0435\u0441\u0442\u044c \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e UNIQUE, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0443\u043d\u0438\u043a\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u043a\u0430\u043a\u043e\u0433\u043e-\u0442\u043e \u043f\u043e\u043b\u044f\/\u043d\u0430\u0431\u043e\u0440\u0430 \u043f\u043e\u043b\u0435\u0439. \u041d\u043e \u043d\u0430\u043f\u0440\u044f\u043c\u0443\u044e \u043e\u043d\u043e \u043d\u0430\u043c \u043d\u0435 \u043f\u043e\u0434\u043e\u0439\u0434\u0451\u0442, \u0442\u0430\u043a \u043a\u0430\u043a \u0432 \u0431\u0430\u0437\u0435 \u043c\u043e\u0433\u0443\u0442 \u043e\u0441\u0442\u0430\u0442\u044c\u0441\u044f \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0448\u0438\u0435 \u043a\u0443\u043f\u043e\u043d\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u0443\u0434\u0443\u0442 \u043a\u043e\u043d\u0444\u043b\u0438\u043a\u0442\u043e\u0432\u0430\u0442\u044c \u0441\u043e \u0441\u0432\u0435\u0436\u0438\u043c\u0438.<\/p>\n<p>  <\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u043c\u043d\u043e\u0433\u0438\u043c \u0438\u0437\u0432\u0435\u0441\u0442\u043d\u043e, \u0447\u0442\u043e UNIQUE \u043c\u043e\u0436\u0435\u0442 \u0438\u0434\u0442\u0438 \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c. \u041a\u0430\u0437\u0430\u043b\u043e\u0441\u044c \u0431\u044b, \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u043f\u0440\u043e\u0441\u0442\u043e \u043a \u043a\u043e\u043d\u0441\u0442\u0440\u0435\u0439\u043d\u0442\u0443 <code>WHERE (valid_until > NOW())<\/code>, \u0438 \u0434\u0435\u043b\u043e \u0441 \u043a\u043e\u043d\u0446\u043e\u043c. \u041e\u0434\u043d\u0430\u043a\u043e \u0442\u0430\u043a \u043d\u0435\u043b\u044c\u0437\u044f: \u043d\u0435\u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0435\u0439\u0442 \u043f\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0438, \u043a\u043e\u0442\u043e\u0440\u0443\u044e \u0437\u0430\u0440\u0430\u043d\u0435\u0435 \u043d\u0435 \u0432\u044b\u0447\u0438\u0441\u043b\u0438\u0442\u044c. \u0422.\u0435. WHERE (is_valid) \u0431\u044b \u043f\u0440\u043e\u043a\u0430\u0442\u0438\u043b\u043e, \u043d\u043e \u043d\u0435 \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u0435 \u0441 NOW().<\/p>\n<p>  <\/p>\n<p>\u041c\u043e\u0436\u043d\u043e \u0432\u044b\u043a\u0440\u0443\u0442\u0438\u0442\u044c\u0441\u044f \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430. \u041d\u043e \u0435\u0441\u0442\u044c \u0441\u043f\u043e\u0441\u043e\u0431 \u043f\u043e\u044d\u043b\u0435\u0433\u0430\u043d\u0442\u043d\u0435\u0435.<\/p>\n<p>  <\/p>\n<h2 id=\"reshenie-cherez-exclude-i-tsrange\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 EXCLUDE \u0438 tsrange<\/h2>\n<p>  <\/p>\n<p>\u0412 \u043f\u043e\u0441\u0442\u0433\u0440\u0435\u0441\u0435 \u0435\u0441\u0442\u044c \u0442\u0438\u043f \u0434\u0430\u043d\u043d\u044b\u0445 tsrange (\u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d \u0432\u0440\u0435\u043c\u0435\u043d\u0438), \u0430 \u0442\u0430\u043a\u0436\u0435 \u043a\u043b\u044e\u0447\u0435\u0432\u043e\u0435 \u0441\u043b\u043e\u0432\u043e EXCLUDE, \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u043f\u0440\u0435\u0442\u0438\u0442\u044c \u0432\u0441\u0442\u0430\u0432\u043a\u0443 \u0441\u0442\u0440\u043e\u043a, \u0433\u0434\u0435 \u0434\u0438\u0430\u043f\u0430\u0437\u043e\u043d\u044b \u043f\u0435\u0440\u0435\u0441\u0435\u043a\u0430\u044e\u0442\u0441\u044f. \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u043e\u0431\u044a\u0435\u0434\u0438\u043d\u0438\u0442\u044c:<\/p>\n<p>  <\/p>\n<p><code>EXCLUDE USING GIST (tsrange(created_at, valid_until) WITH &amp;&amp;)<\/code><\/p>\n<p>  <\/p>\n<p>\u041d\u043e \u0442\u0430\u043a\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u0442 \u043d\u0430 \u0432\u0441\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0430 \u043d\u0435 \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u044e\u0437\u0435\u0440\u0430. \u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u043d\u0430\u0434\u043e \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0440\u0430\u0441\u0448\u0438\u0440\u0438\u0442\u044c \u0432\u044b\u0440\u0430\u0436\u0435\u043d\u0438\u0435, \u0434\u043e\u0431\u0430\u0432\u0438\u0432 user_id:<\/p>\n<p>  <\/p>\n<p><code>EXCLUDE USING GIST (user_id WITH =, tsrange(created_at, valid_until) WITH &amp;&amp;)<\/code><\/p>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u044d\u0442\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u043b\u043e, \u043d\u0443\u0436\u043d\u043e \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u0432\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435 btree_gist.<\/p>\n<p>  <\/p>\n<p>\u0418\u0442\u043e\u0433\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:<\/p>\n<p>  <\/p>\n<pre><code class=\"sql\">CREATE EXTENSION IF NOT EXISTS btree_gist;  CREATE TABLE coupons (     id  bigint primary key generated by default as identity,     user_id bigint not null,     created_at timestamp not null,     valid_until timestamp not null,     EXCLUDE USING GIST (         user_id WITH =,          tsrange(created_at, valid_until) WITH &amp;&amp;     ) );  INSERT INTO coupons(user_id, created_at, valid_until) VALUES (100500, now(), now() + interval '1 day');  INSERT INTO coupons(user_id, created_at, valid_until) VALUES     (100501, now(), now() + interval '10 day');  INSERT INTO coupons(user_id, created_at, valid_until) VALUES     (100500, now(), now() + interval '10 day');<\/code><\/pre>\n<p>  <\/p>\n<p>\u041f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 insert \u043d\u0435 \u043f\u0440\u043e\u0439\u0434\u0451\u0442: &#171;ERROR: conflicting key value violates exclusion constraint&#187;<\/p>\n<p>  <\/p>\n<p>\u0422.\u0435. \u043f\u043e \u0441\u0443\u0442\u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0438\u0434\u0435\u0442 \u0432 \u043e\u0434\u043d\u0443-\u0435\u0434\u0438\u043d\u0441\u0442\u0432\u0435\u043d\u043d\u0443\u044e \u0441\u0442\u0440\u043e\u0447\u043a\u0443 EXCLUDE. <\/p>\n<p>  <\/p>\n<h2 id=\"reshenie-cherez-triggery\">\u0420\u0435\u0448\u0435\u043d\u0438\u0435 \u0447\u0435\u0440\u0435\u0437 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u044b<\/h2>\n<p>  <\/p>\n<p>\u0412 \u043f\u0440\u043e\u0435\u043a\u0442\u0430\u0445 \u041a\u0430\u0440\u0443\u043d\u044b \u043c\u043d\u0435 \u0438\u043d\u043e\u0433\u0434\u0430 \u0434\u043e\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043f\u0438\u0441\u0430\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u044b, \u0438 \u043e\u043d\u0438 \u0432\u0441\u0435\u0433\u0434\u0430 \u043c\u043d\u043e\u0433\u043e\u0441\u043b\u043e\u0432\u043d\u044b. \u0414\u043b\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438 \u044d\u0442\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u043b\u043e \u0431\u044b \u043f\u0440\u0438\u043c\u0435\u0440\u043d\u043e \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION check_uniq() RETURNS trigger     LANGUAGE plpgsql AS $$ BEGIN     IF (SELECT true         FROM coupons         WHERE id != NEW.id           AND user_id = NEW.user_id           AND valid_until > NEW.created_at) THEN         RAISE EXCEPTION 'Coupon for this user already exists';     END IF;     RETURN NEW; END $$;  CREATE TRIGGER coupon_unique_trigger     AFTER INSERT     ON coupons     FOR EACH ROW EXECUTE PROCEDURE check_uniq();<\/code><\/pre>\n<p>  <\/p>\n<p>\u041a\u0430\u043a \u043f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e \u0437\u0430\u043c\u0435\u0442\u0438\u043b\u0438 \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0445, \u0442\u0440\u0438\u0433\u0433\u0435\u0440 \u043d\u0430\u043f\u0438\u0441\u0430\u043d \u043d\u0435\u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u2014 \u0435\u0433\u043e \u043d\u0430\u0434\u043e \u0434\u043e\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0442\u044c \u0438 \u043e\u0442\u043b\u0430\u0436\u0438\u0432\u0430\u0442\u044c. \u0412 \u043e\u0431\u0449\u0435\u043c, \u0434\u043e\u043b\u0433\u043e \u0438 \u043d\u0435\u043f\u0440\u0438\u044f\u0442\u043d\u043e. \u041c\u043e\u0436\u043d\u043e \u0435\u0449\u0435 \u0438\u0437\u0432\u0440\u0430\u0442\u0438\u0442\u044c\u0441\u044f \u0438 \u043f\u043e \u0443\u0441\u043b\u043e\u0432\u043d\u043e\u043c\u0443 \u043a\u0440\u043e\u043d\u0443 \u044f\u0432\u043d\u043e \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0432\u0430\u043b\u0438\u0434\u043d\u043e\u0441\u0442\u0438 \u043a\u0443\u043f\u043e\u043d\u043e\u0432 (is_valid), \u0442\u043e\u0433\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c unique \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c, \u043d\u043e \u044d\u0442\u043e \u0442\u043e\u0436\u0435 \u043f\u0438\u0441\u0430\u043d\u0438\u043d\u0430 \u0435\u0449\u0435 \u0442\u0430.<\/p>\n<p>  <\/p>\n<p><em>\u0421\u0442\u0430\u0442\u044c\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0430 \u043d\u0430 \u043e\u0441\u043d\u043e\u0432\u0435 \u043f\u043e\u0441\u0442\u043e\u0432 \u043a\u0430\u043d\u0430\u043b\u0430 <a href=\"https:\/\/t.me\/crossjoin\">Cross Join<\/a><\/em><\/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\/794468\/\"> https:\/\/habr.com\/ru\/articles\/794468\/<\/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-1\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u041a\u0430\u043a \u0431\u044b \u0432\u044b \u0440\u0435\u0448\u0430\u043b\u0438 \u0442\u0430\u043a\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443? \u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c, \u0435\u0441\u0442\u044c \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0441 \u043a\u0443\u043f\u043e\u043d\u0430\u043c\u0438, \u0438 \u0443 \u043a\u0443\u043f\u043e\u043d\u043e\u0432 \u0435\u0441\u0442\u044c \u043d\u0435\u043a\u0430\u044f \u0434\u0430\u0442\u0430 \u0443\u0441\u0442\u0430\u0440\u0435\u0432\u0430\u043d\u0438\u044f valid_until. \u0412\u0430\u043c \u043d\u0430\u0434\u043e \u043e\u0431\u0435\u0441\u043f\u0435\u0447\u0438\u0442\u044c \u0442\u0430\u043a\u043e\u0435 \u043e\u0433\u0440\u0430\u043d\u0438\u0447\u0435\u043d\u0438\u0435 (constraint) \u043d\u0430 \u0443\u0440\u043e\u0432\u043d\u0435 \u0411\u0414, \u0447\u0442\u043e\u0431\u044b \u0443 \u043e\u0434\u043d\u043e\u0433\u043e \u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0430 \u043c\u043e\u0433 \u0431\u044b\u0442\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u0438\u043d \u0434\u0435\u0439\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0439 \u043a\u0443\u043f\u043e\u043d.<\/p>\n<p>  <\/p>\n<p>\u0422.\u0435., \u0442\u0430\u0431\u043b\u0438\u0446\u0430 \u0438\u0437\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u043e \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0442\u0430\u043a:<\/p>\n<p>  <\/p>\n<pre><code class=\"sql\">CREATE TABLE coupons (     id  bigint primary key generated by default as identity,     user_id bigint not null,     created_at timestamp not null,     valid_until timestamp not null )<\/code><\/pre>\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-368496","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/368496","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=368496"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/368496\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=368496"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=368496"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=368496"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}