{"id":195898,"date":"2013-09-30T23:24:08","date_gmt":"2013-09-30T19:24:08","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=195898"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=195898","title":{"rendered":"<span class=\"post_title\">PostgreSQL 9.3 \u0427\u0442\u043e \u043d\u043e\u0432\u043e\u0433\u043e?<\/span>"},"content":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/f06\/661\/735\/f0666173533dd132950603eb6e245731.jpg\"\/><br \/>  \u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0445\u0430\u0431\u0440\u0430\u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0438! \u041d\u0435 \u0442\u0430\u043a \u0443\u0436 \u0434\u0430\u0432\u043d\u043e \u0432\u044b\u0448\u0435\u043b \u0440\u0435\u043b\u0438\u0437 <b>PostgreSQL<\/b> 9.3 \u0438 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c \u0412\u0430\u0441 \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c\u0438 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432\u0430\u043c\u0438, \u043a\u0430\u0441\u0430\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u0440\u0438\u0433\u043e\u0434\u044f\u0442\u0441\u044f \u0412\u0430\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:  <\/p>\n<ul>\n<li>\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/li>\n<li>\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/li>\n<li>\u0442\u0440\u0438\u0433\u0433\u0435\u0440\u044b \u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u043c<\/li>\n<li>\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/li>\n<li>\u043b\u0430\u0442\u0435\u0440\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435<\/li>\n<li>\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/li>\n<li>\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0438\u043f\u043e\u043c <b>JSON<\/b><\/li>\n<\/ul>\n<p>  <a name=\"habracut\"><\/a>  <\/p>\n<h4><b>\u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/b><\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/9b5\/7b6\/3c8\/9b57b63c844f2fce04c20d32cff42461.jpeg\"\/><br \/>  \u041c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u2014 \u0444\u0438\u0437\u0438\u0447\u0435\u0441\u043a\u0438\u0439 \u043e\u0431\u044a\u0435\u043a\u0442 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445, \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0449\u0438\u0439 \u0432 \u0441\u0435\u0431\u0435 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u044b \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u0411\u0435\u0441\u0441\u043f\u043e\u0440\u043d\u043e, \u043e\u0434\u043d\u043e \u0438\u0437 \u0441\u0430\u043c\u044b\u0445 \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u044b\u0445 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u043a\u0430\u043a\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u043d\u0438\u043c \u0432 <b>PostgreSQL<\/b>.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0438 \u0441\u043f\u0440\u0430\u0432\u043e\u0447\u043d\u0438\u043a \u043a\u043d\u0438\u0433, \u0438\u043c\u0435\u044e\u0449\u0438\u0439 \u0441\u0441\u044b\u043b\u043a\u0443 \u043d\u0430 \u0430\u0432\u0442\u043e\u0440\u0430:  <\/p>\n<pre><code class=\"sql\">CREATE TABLE author ( \tid serial NOT NULL, \tfirst_name text NOT NULL, \tlast_name text NOT NULL,  \tCONSTRAINT pk_author_id PRIMARY KEY ( id ), \tCONSTRAINT uk_author_name UNIQUE ( first_name, last_name ) );  CREATE TABLE book ( \tid serial NOT NULL, \tauthor_id integer NOT NULL, \tname text NOT NULL,  \tCONSTRAINT pk_book_id PRIMARY KEY ( id ), \tCONSTRAINT fk_book_author_id FOREIGN KEY ( author_id ) REFERENCES author ( id ), \tCONSTRAINT uk_book_name UNIQUE ( author_id, name ) ); <\/code><\/pre>\n<p>  \u041d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u044d\u0442\u0438 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0434\u0430\u043d\u043d\u044b\u043c\u0438 \u2014 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u0440\u0443 \u0430\u0432\u0442\u043e\u0440\u043e\u0432 \u0438 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u0443\u0435\u043c \u043c\u043d\u043e\u0433\u043e-\u043c\u043d\u043e\u0433\u043e \u043a\u043d\u0438\u0433:  <\/p>\n<pre><code class=\"sql\">INSERT INTO author ( first_name, last_name ) VALUES ( '\u0418\u0432\u0430\u043d',  '\u0418\u0432\u0430\u043d\u043e\u0432' ); -- \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d id = 1 INSERT INTO author ( first_name, last_name ) VALUES ( '\u041f\u0451\u0442\u0440', '\u041f\u0435\u0442\u0440\u043e\u0432' ); -- \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d id = 2  INSERT INTO book ( author_id, name ) VALUES ( 1, '\u0422\u0440\u0430\u043a\u0442\u0430\u0442 \u043e \u043f\u0443\u0441\u0442\u043e\u0442\u0435 (\u0447\u0430\u0441\u0442\u044c ' || generate_series ( 1, 100000 ) || ')' );  INSERT INTO book ( author_id, name ) VALUES ( 2, '\u041d\u0435\u0432\u044b\u043d\u043e\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0431\u044b\u0442\u0438\u044f' ), ( 2, '\u0421\u0447\u0430\u0441\u0442\u043b\u0438\u0432\u044b\u0439 \u0444\u0438\u043d\u0430\u043b' ); <\/code><\/pre>\n<p>  \u0414\u043b\u044f \u0441\u0440\u0430\u0432\u043d\u0435\u043d\u0438\u044f, \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043e\u0431\u044b\u0447\u043d\u043e\u0435 \u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 (\u043e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0435\u0433\u043e \u0442\u0440\u0435\u0431\u0443\u0435\u0442\u0441\u044f \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0432\u0440\u0435\u043c\u0435\u043d\u0438, \u0434\u043b\u044f \u0432\u044b\u0431\u043e\u0440\u043a\u0438 \u0438 \u0437\u0430\u043f\u0438\u0441\u0438 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0430):  <\/p>\n<pre><code class=\"sql\">CREATE VIEW vw_book AS SELECT book.id, author.first_name || ' ' || author.last_name AS author_name, book.name FROM book INNER JOIN author ON author.id = book.author_id;  CREATE MATERIALIZED VIEW mvw_book AS SELECT book.id, author.first_name || ' ' || author.last_name AS author_name, book.name FROM book INNER JOIN author ON author.id = book.author_id; <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c, \u0434\u0430\u0432\u0430\u0439\u0442\u0435 \u0432\u0437\u0433\u043b\u044f\u043d\u0435\u043c \u043d\u0430 \u043f\u043b\u0430\u043d \u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u0441 \u0443\u0441\u043b\u043e\u0432\u0438\u0435\u043c \u0434\u043b\u044f \u043e\u0431\u044b\u0447\u043d\u043e\u0433\u043e \u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f:  <\/p>\n<pre><code class=\"sql\">EXPLAIN ANALYZE SELECT * FROM vw_book WHERE author_name = '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432'; -- Hash Join  (cost=24.58..2543.83 rows=482 width=119) (actual time=19.389..19.390 rows=2 loops=1)   Hash Cond: (book.author_id = author.id)   -&gt;  Seq Scan on book  (cost=0.00..2137.02 rows=100002 width=59) (actual time=0.017..9.231 rows=100002 loops=1)   -&gt;  Hash  (cost=24.53..24.53 rows=4 width=68) (actual time=0.026..0.026 rows=1 loops=1)         Buckets: 1024  Batches: 1  Memory Usage: 1kB         -&gt;  Seq Scan on author  (cost=0.00..24.53 rows=4 width=68) (actual time=0.019..0.020 rows=1 loops=1)               Filter: (((first_name || ' '::text) || last_name) = '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432'::text)               Rows Removed by Filter: 1 Total runtime: 19.452 ms  EXPLAIN ANALYZE SELECT * FROM mvw_book WHERE author_name = '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432'; -- Seq Scan on mvw_book  (cost=0.00..2584.03 rows=7 width=77) (actual time=15.869..15.870 rows=2 loops=1)   Filter: (author_name = '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432'::text)   Rows Removed by Filter: 100000 Total runtime: 15.905 ms <\/code><\/pre>\n<p>  \u0414\u0430\u043d\u043d\u044b\u0435 \u0434\u043b\u044f \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u043b\u0435\u0436\u0430\u0442 \u043a\u0443\u0447\u043d\u043e \u0438 \u0438\u0445 \u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u0441\u043e\u0431\u0438\u0440\u0430\u0442\u044c \u0441 \u0440\u0430\u0437\u043d\u044b\u0445 \u0442\u0430\u0431\u043b\u0438\u0446. \u041d\u043e \u044d\u0442\u043e \u0435\u0449\u0435 \u043d\u0435 \u0432\u0441\u0451, \u0442\u0430\u043a \u043a\u0430\u043a \u0434\u043b\u044f \u043d\u0438\u0445 \u0435\u0441\u0442\u044c \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0442\u044c \u0438\u043d\u0434\u0435\u043a\u0441\u044b. \u0423\u043b\u0443\u0447\u0448\u0430\u0435\u043c \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442:  <\/p>\n<pre><code class=\"sql\">CREATE INDEX idx_book_name ON mvw_book ( author_name );  EXPLAIN ANALYZE SELECT * FROM mvw_book WHERE author_name = '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432'; -- Index Scan using idx_book_name on mvw_book  (cost=0.42..8.54 rows=7 width=77) (actual time=0.051..0.055 rows=2 loops=1)   Index Cond: (author_name = '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432'::text) Total runtime: 0.099 ms <\/code><\/pre>\n<p>  \u041d\u0435\u043f\u043b\u043e\u0445\u043e, \u043f\u043e\u0438\u0441\u043a \u0432\u0435\u0434\u0451\u0442\u0441\u044f \u043f\u043e \u0438\u043d\u0434\u0435\u043a\u0441\u0443 \u0438 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0438\u0441\u043a\u0430 \u0441\u0443\u0449\u0435\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u043e\u043a\u0440\u0430\u0442\u0438\u043b\u043e\u0441\u044c.<\/p>\n<p>  \u041d\u043e \u0435\u0441\u0442\u044c \u0438 \u043d\u044c\u044e\u0430\u043d\u0441 \u043f\u0440\u0438 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u0438 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0445 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0439 \u2014 \u043f\u043e\u0441\u043b\u0435 <b>DML<\/b> \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u0442\u0430\u0431\u043b\u0438\u0446\u0430\u043c\u0438, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u043e\u0441\u0442\u043e\u0438\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u0442\u0441\u044f \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0442\u044c:  <\/p>\n<pre><code class=\"sql\">INSERT INTO book ( author_id, name ) VALUES ( 2, '\u041f\u043e\u0442\u0435\u0440\u044f\u043d\u043d\u044b\u0439 \u0432\u043e \u043c\u0433\u043b\u0435' );  REFRESH MATERIALIZED VIEW mvw_book; <\/code><\/pre>\n<p>  \u042d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u043c:  <\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION mvw_book_refresh ( ) RETURNS trigger AS $BODY$ BEGIN \tREFRESH MATERIALIZED VIEW mvw_book; \tRETURN NULL; END $BODY$ LANGUAGE plpgsql VOLATILE;  CREATE TRIGGER tr_book_refresh AFTER INSERT OR UPDATE OR DELETE ON book FOR EACH ROW EXECUTE PROCEDURE mvw_book_refresh ( );  CREATE TRIGGER tr_author_refresh AFTER INSERT OR UPDATE OR DELETE ON author FOR EACH ROW EXECUTE PROCEDURE mvw_book_refresh ( ); <\/code><\/pre>\n<p>  \u0425\u043e\u0442\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b, \u0441\u0438\u043c\u0443\u043b\u0438\u0440\u0443\u044e\u0449\u0438\u0439 \u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f, \u043c\u043e\u0436\u043d\u043e \u0431\u044b\u043b\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0438 \u0432 <b>PostgreSQL 9.2<\/b> (\u0441\u043e\u0437\u0434\u0430\u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0438\u043d\u0434\u0435\u043a\u0441\u044b \u043a \u043d\u0435\u0439 \u0438 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u0430, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0431\u044b \u0432\u044b\u043f\u043e\u043b\u043d\u044f\u043b\u0438 \u0445\u0438\u0442\u0440\u044b\u0439 \u0437\u0430\u043f\u0440\u043e\u0441), \u043d\u043e \u0432 \u0446\u0435\u043b\u043e\u043c \u044d\u0442\u043e \u0443\u0434\u043e\u0431\u043d\u043e\u0435 \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435.<\/p>\n<h4><b>\u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/b><\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/418\/12c\/20d\/41812c20dd611166097d0b4ed660e677.jpg\"\/><br \/>  \u041a \u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c\u044b\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c\u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c <b>DML<\/b>-\u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438. \u041f\u0440\u0430\u0432\u0434\u0430 \u0432\u043e\u0442 \u0442\u0440\u0435\u0431\u043e\u0432\u0430\u043d\u0438\u044f \u043a \u0442\u0430\u043a\u0438\u043c \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c \u0432\u044b\u0441\u043e\u043a\u0438\u0435: \u0442\u043e\u043b\u044c\u043a\u043e \u043e\u0434\u043d\u0430 \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044c (\u0442\u0430\u0431\u043b\u0438\u0446\u0430, \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435) \u0432 \u0441\u043f\u0438\u0441\u043a\u0435 <b>FROM<\/b>, \u0431\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 <b>WITH<\/b>, <b>DISTINCT<\/b>, <b>GROUP BY<\/b>, <b>HAVING<\/b>, <b>LIMIT<\/b> \u0438 <b>OFFSET<\/b>, \u0431\u0435\u0437 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0439 \u043d\u0430\u0434 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u0430\u043c\u0438 (<b>UNION<\/b>, <b>INTERSECT<\/b> \u0438 <b>EXCEPT<\/b>) \u0438 \u043a \u043f\u043e\u043b\u044f\u043c \u043d\u0435 \u0434\u043e\u043b\u0436\u043d\u044b \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u0441\u044f \u043d\u0438\u043a\u0430\u043a\u0438\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438\u043b\u0438 \u043e\u043f\u0435\u0440\u0430\u0446\u0438\u0438.<\/p>\n<p>  \u041e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0438:  <\/p>\n<pre><code class=\"sql\">CREATE TABLE employee ( \tid serial NOT NULL, \tfullname text NOT NULL, \tbirthday date, \tsalary numeric NOT NULL DEFAULT 0.0,  \tCONSTRAINT pk_employee_id PRIMARY KEY ( id ), \tCONSTRAINT uk_employee_fullname UNIQUE ( fullname ), \tCONSTRAINT ch_employee_salary CHECK ( salary &gt;= 0.0 ) );  INSERT INTO employee ( fullname, salary ) VALUES ( '\u0418\u0432\u0430\u043d \u0418\u0432\u0430\u043d\u043e\u0432', 800.0 ); INSERT INTO employee ( fullname, salary ) VALUES ( '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432', 2000.0 ); INSERT INTO employee ( fullname, salary ) VALUES ( '\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439', 1500.0 );  CREATE VIEW vw_employee_top_salary AS SELECT employee.fullname AS name, employee.salary FROM employee WHERE employee.salary &gt;= 1000.0;  -- \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u043c \u0441 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435\u043c INSERT INTO vw_employee_top_salary ( name, salary ) VALUES ( '\u0421\u0451\u043c\u0451\u043d \u0421\u0438\u0434\u043e\u0440\u043e\u0432', 2500.0 ); UPDATE vw_employee_top_salary SET salary = 2200.0 WHERE name = '\u041f\u0451\u0442\u0440 \u041f\u0435\u0442\u0440\u043e\u0432'; DELETE FROM vw_employee_top_salary WHERE name = '\u041d\u0435\u0438\u0437\u0432\u0435\u0441\u0442\u043d\u044b\u0439';  -- \u0432\u044b\u0432\u043e\u0434 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u043e\u0432 SELECT * FROM vw_employee_top_salary; <\/code><\/pre>\n<p>  \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e <b>INSERT<\/b> \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0432 \u043b\u044e\u0431\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435, \u0430 <b>UPDATE<\/b> \u0438 <b>DELETE<\/b> \u2014 \u0442\u043e\u043b\u044c\u043a\u043e, \u043a\u043e\u0433\u0434\u0430 \u043d\u0430\u0431\u043e\u0440 \u0438\u0437 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u043f\u043e\u043f\u0430\u0434\u0430\u0435\u0442 \u043f\u043e\u0434 \u0443\u0441\u043b\u043e\u0432\u0438\u0435 \u0432 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0438:  <\/p>\n<pre><code class=\"sql\">INSERT INTO vw_employee_top_salary ( name, salary ) VALUES ( '\u0410\u043d\u043e\u043d\u0438\u043c\u0443\u0441', 0.0 ); -- \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u0441\u044f \u0441\u0442\u0440\u043e\u043a\u0430 UPDATE vw_employee_top_salary SET salary = 3000.0 WHERE name = '\u0410\u043d\u043e\u043d\u0438\u043c\u0443\u0441'; -- \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f, \u0442\u0430\u043a \u043a\u0430\u043a salary \u0440\u0430\u0432\u043d\u043e 0.0 DELETE FROM vw_employee_top_salary WHERE name = '\u0410\u043d\u043e\u043d\u0438\u043c\u0443\u0441'; -- \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0438\u0437\u043c\u0435\u043d\u0438\u0442\u0441\u044f, \u0442\u0430\u043a \u043a\u0430\u043a salary \u0440\u0430\u0432\u043d\u043e 0.0 <\/code><\/pre>\n<p>  \u0411\u043e\u043b\u0435\u0435 \u043f\u0440\u043e\u0434\u0432\u0438\u043d\u0443\u0442\u044b\u0435 \u0432\u0435\u0449\u0438 \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044f <a href=\"http:\/\/www.postgresql.org\/docs\/9.3\/static\/rules-update.html\">\u043f\u0440\u0430\u0432\u0438\u043b\u0430<\/a> \u043a \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f\u043c.<\/p>\n<h4><b>\u0422\u0440\u0438\u0433\u0433\u0435\u0440\u044b \u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u043c<\/b><\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/1cd\/57c\/24e\/1cd57c24e8f519a0d5ef7bada460419a.jpg\"\/><br \/>  \u0422\u043e\u0436\u0435, \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u043e\u0436\u0438\u0434\u0430\u0435\u043c\u043e\u0435 \u043d\u043e\u0432\u043e\u0432\u0432\u0435\u0434\u0435\u043d\u0438\u0435. \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u043f\u0435\u0440\u0435\u0445\u0432\u0430\u0442\u044b\u0432\u0430\u0442\u044c <b>DDL<\/b> \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0411\u0414. \u041e\u0442\u043b\u0438\u0447\u0430\u044e\u0442\u0441\u044f \u043e\u0442 \u043e\u0431\u044b\u0447\u043d\u044b\u0445 \u0442\u0440\u0438\u0433\u0433\u0435\u0440\u043e\u0432 \u0432 \u043f\u0435\u0440\u0432\u0443\u044e \u043e\u0447\u0435\u0440\u0435\u0434\u044c \u0442\u0435\u043c, \u0447\u0442\u043e \u043e\u043d\u0438 \u0433\u043b\u043e\u0431\u0430\u043b\u044c\u043d\u044b\u0435, \u0431\u0435\u0437 \u043f\u0440\u0438\u0432\u044f\u0437\u043a\u0438 \u043a \u043a\u043e\u043d\u043a\u0440\u0435\u0442\u043d\u043e\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435, \u043d\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c, \u043d\u0430 \u043a\u0430\u043a\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0440\u0435\u0430\u0433\u0438\u0440\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u044e\u0442\u0441\u044f \u0442\u0430\u043a:  <\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE FUNCTION event_trigger_begin ( ) RETURNS event_trigger AS $BODY$ BEGIN \tRAISE NOTICE '(begin) tg_event = %, tg_tag = %', TG_EVENT, TG_TAG; END; $BODY$ LANGUAGE plpgsql;  CREATE OR REPLACE FUNCTION event_trigger_end ( ) RETURNS event_trigger AS $BODY$ BEGIN \tRAISE NOTICE '(end) tg_event = %, tg_tag = %', TG_EVENT, TG_TAG; END; $BODY$ LANGUAGE plpgsql;  CREATE EVENT TRIGGER tr_event_begin ON ddl_command_start EXECUTE PROCEDURE event_trigger_begin ( ); CREATE EVENT TRIGGER tr_event_end ON ddl_command_end EXECUTE PROCEDURE event_trigger_end ( ); <\/code><\/pre>\n<p>  \u041f\u0440\u043e\u0432\u043e\u0434\u0438\u043c \u0440\u0430\u0437\u043d\u044b\u0435 <b>DDL<\/b>-\u043c\u0430\u043d\u0438\u043f\u0443\u043b\u044f\u0446\u0438\u0438 \u0441 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439:  <\/p>\n<pre><code class=\"sql\">CREATE TABLE article ( \tid SERIAL NOT NULL, \tname text NOT NULL,  \tCONSTRAINT pk_article_id PRIMARY KEY ( id ), \tCONSTRAINT uk_article_name UNIQUE ( name ) );  ALTER TABLE article ADD COLUMN misc numeric; ALTER TABLE article ALTER COLUMN misc TYPE text; ALTER TABLE article DROP COLUMN misc;  DROP TABLE article; <\/code><\/pre>\n<p>  \u0412\u044b\u0432\u043e\u0434 \u0434\u043e\u043b\u0436\u0435\u043d \u0431\u044b\u0442\u044c \u0442\u0430\u043a\u0438\u043c:  <\/p>\n<pre><code class=\"sql\">tg_event = ddl_command_start, tg_tag = CREATE TABLE tg_event = ddl_command_end, tg_tag = CREATE TABLE tg_event = ddl_command_start, tg_tag = ALTER TABLE tg_event = ddl_command_end, tg_tag = ALTER TABLE tg_event = ddl_command_start, tg_tag = ALTER TABLE tg_event = ddl_command_end, tg_tag = ALTER TABLE tg_event = ddl_command_start, tg_tag = ALTER TABLE tg_event = ddl_command_end, tg_tag = ALTER TABLE tg_event = ddl_command_start, tg_tag = DROP TABLE tg_event = ddl_command_end, tg_tag = DROP TABLE <\/code><\/pre>\n<p>  \u0427\u0435\u0440\u0435\u0437 plpgsql \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u0430 \u0442\u043e\u043b\u044c\u043a\u043e \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f \u043e \u0441\u043e\u0431\u044b\u0442\u0438\u0438 (<i>TG_EVENT<\/i>) \u0438, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e, \u043e \u043a\u043e\u043c\u0430\u043d\u0434\u0435 (<i>TG_TAG<\/i>), \u043d\u043e, \u043d\u0430\u0434\u0435\u044e\u0441\u044c, \u0432 \u0431\u0443\u0434\u0443\u0449\u0435\u043c \u0431\u0443\u0434\u0435\u0442 \u043b\u0443\u0447\u0448\u0435.<\/p>\n<h4><b>\u0420\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/b><\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/718\/9df\/e1d\/7189dfe1d37a7cab83e6e04ac827e867.jpg\"\/><br \/>  \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0442 \u0443\u043f\u0440\u043e\u0441\u0442\u0438\u0442\u044c \u043a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u044e <b>WITH RECURSIVE<\/b>, \u0435\u0441\u043b\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0442\u044c \u043f\u043e \u043d\u0435\u0439 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0446\u0443 \u0438 \u043d\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0435\u0435 \u0442\u0435\u0441\u0442\u043e\u0432\u044b\u043c\u0438 \u0434\u0430\u043d\u043d\u044b\u043c\u0438:  <\/p>\n<pre><code class=\"sql\">CREATE TABLE directory ( \tid serial NOT NULL, \tparent_id integer, \tname text NOT NULL,  \tCONSTRAINT pk_directory_id PRIMARY KEY ( id ), \tCONSTRAINT fk_directory_parent_id FOREIGN KEY ( parent_id ) REFERENCES directory ( id ), \tCONSTRAINT uk_directory_name UNIQUE ( parent_id, name ) );  INSERT INTO directory ( parent_id, name ) VALUES ( NULL, 'usr' ); -- \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d id = 1 INSERT INTO directory ( parent_id, name ) VALUES ( 1, 'lib' ); INSERT INTO directory ( parent_id, name ) VALUES ( 1, 'include' ); INSERT INTO directory ( parent_id, name ) VALUES ( NULL, 'var' ); -- \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d id = 4 INSERT INTO directory ( parent_id, name ) VALUES ( 4, 'opt' ); -- \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d id = 5 INSERT INTO directory ( parent_id, name ) VALUES ( 5, 'tmp' ); INSERT INTO directory ( parent_id, name ) VALUES ( 4, 'log' ); -- \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d id = 7 INSERT INTO directory ( parent_id, name ) VALUES ( 7, 'samba' );  INSERT INTO directory ( parent_id, name ) VALUES ( 7, 'news' ); <\/code><\/pre>\n<p>  \u0417\u0430\u043f\u0440\u043e\u0441 \u0447\u0435\u0440\u0435\u0437 WITH RECURSIVE \u0438 \u0430\u043d\u0430\u043b\u043e\u0433\u0438\u0447\u043d\u044b\u0439 \u0435\u043c\u0443, \u0447\u0435\u0440\u0435\u0437 \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435:  <\/p>\n<pre><code class=\"sql\">WITH RECURSIVE vw_directory ( id, parent_id, name, path ) AS ( \tSELECT id, parent_id, name, '\/' || name \tFROM directory \tWHERE parent_id IS NULL AND name = 'var' \tUNION ALL \tSELECT d.id, d.parent_id, d.name, t.path || '\/' || d.name \tFROM directory d \tINNER JOIN vw_directory t ON d.parent_id = t.id ) SELECT * FROM vw_directory ORDER BY path;  CREATE RECURSIVE VIEW vw_directory ( id, parent_id, name, path ) AS SELECT id, parent_id, name, '\/' || name FROM directory WHERE parent_id IS NULL AND name = 'var' UNION ALL SELECT d.id, d.parent_id, d.name, t.path || '\/' || d.name FROM directory d INNER JOIN vw_directory t ON d.parent_id = t.id;  SELECT * FROM vw_directory ORDER BY path; <\/code><\/pre>\n<p>  \u041d\u0430 \u0441\u0430\u043c\u043e\u043c \u0434\u0435\u043b\u0435, \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u2014 \u044d\u0442\u043e \u043e\u0431\u0451\u0440\u0442\u043a\u0430 \u043d\u0430\u0434 <b>WITH RECURSIVE<\/b>, \u0432 \u0447\u0435\u043c \u043c\u043e\u0436\u043d\u043e \u0443\u0431\u0435\u0434\u0438\u0442\u0441\u044f, \u043f\u0440\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0432 \u0442\u0435\u043a\u0441\u0442 \u0441\u0444\u043e\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u043e\u0433\u043e \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f:  <\/p>\n<pre><code class=\"sql\">CREATE OR REPLACE VIEW vw_directory AS  WITH RECURSIVE vw_directory(id, parent_id, name, path) AS (                  SELECT directory.id,                     directory.parent_id,                     directory.name,                     '\/'::text || directory.name                    FROM directory                   WHERE directory.parent_id IS NULL AND directory.name = 'var'::text         UNION ALL                  SELECT d.id,                     d.parent_id,                     d.name,                     (t.path || '\/'::text) || d.name                    FROM directory d               JOIN vw_directory t ON d.parent_id = t.id         )  SELECT vw_directory.id,     vw_directory.parent_id,     vw_directory.name,     vw_directory.path    FROM vw_directory; <\/code><\/pre>\n<h4><b>\u041b\u0430\u0442\u0435\u0440\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435<\/b><\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/2f9\/dbe\/d75\/2f9dbed7559d5d8b2207320d7396336b.jpg\"\/><br \/>  \u041f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043e\u0431\u0440\u0430\u0449\u0430\u0442\u0441\u044f \u0438\u0437 \u043f\u043e\u0434\u0437\u0430\u043f\u0440\u043e\u0441\u0430 \u043a \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u044f\u043c\u0438 \u0438\u0437 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0437\u0430\u043f\u0440\u043e\u0441\u0430. \u041f\u0440\u0438\u043c\u0435\u0440 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f (\u043f\u043e\u0434\u0441\u0447\u0435\u0442 \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u0430 \u043f\u043e\u043b\u0435\u0439 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u0441\u0443\u0449\u043d\u043e\u0441\u0442\u0435\u0439 \u0438\u0437 \u0441\u0445\u0435\u043c\u044b public):<\/p>\n<pre><code class=\"sql\">SELECT t.table_schema || '.' || t.table_name, \t   q.columns_count FROM information_schema.tables t, LATERAL ( \t\t\tSELECT sum ( 1 ) AS columns_count \t\t\tFROM information_schema.columns c \t\t\tWHERE t.table_schema IN ( 'public' ) AND \t\t\t\t  t.table_schema || '.' || t.table_name = c.table_schema || '.' || c.table_name \t\t) q ORDER BY 1; <\/code><\/pre>\n<h4><b>\u0418\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/b><\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/180\/c66\/611\/180c6661114de887a0db4e3d5f686ca2.jpg\"\/><br \/>  \u041d\u043e\u0432\u044b\u0439 \u043c\u043e\u0434\u0443\u043b\u044c <i>postgres_fdw<\/i>, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0439 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c read\/write \u0434\u043e\u0441\u0442\u0443\u043f \u043a \u0434\u0430\u043d\u043d\u044b\u043c, \u0440\u0430\u0441\u043f\u043e\u043b\u043e\u0436\u0435\u043d\u043d\u044b\u043c \u0432 \u0434\u0440\u0443\u0433\u043e\u0439 \u0411\u0414. \u0420\u0430\u043d\u0435\u0435 \u0442\u0430\u043a\u0430\u044f \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b\u044c\u043d\u043e\u0441\u0442\u044c \u0431\u044b\u043b\u0430 \u0432 <i>dblink<\/i>, \u043d\u043e \u0432 <i>postgres_fdw<\/i> \u0432\u0441\u0451 \u043f\u0440\u043e\u0437\u0440\u0430\u0447\u043d\u0435\u0435, \u0441\u0442\u0430\u043d\u0434\u0430\u0440\u0442\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u0441\u0438\u043d\u0442\u0430\u043a\u0441\u0438\u0441 \u0438 \u043c\u043e\u0436\u043d\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043b\u0443\u0447\u0448\u0443\u044e \u043f\u0440\u043e\u0438\u0437\u0432\u043e\u0434\u0438\u0442\u0435\u043b\u044c\u043d\u043e\u0441\u0442\u044c. \u041f\u043e\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043a\u0430\u043a\u0438\u043c \u0441\u043f\u043e\u0441\u043e\u0431\u043e\u043c \u043c\u043e\u0436\u043d\u043e \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c <i>postgres_fdw<\/i>.<\/p>\n<p>  \u0421\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u0443\u044e \u0411\u0414 <i>fdb<\/i> \u0438 \u0432 \u043d\u0435\u0439 \u0442\u0435\u0441\u0442\u043e\u0432\u0443\u044e \u0442\u0430\u0431\u043b\u0438\u0446\u0443 (\u043e\u043d\u0430 \u0431\u0443\u0434\u0435\u0442 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u043f\u043e \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u044e \u043a \u0442\u0435\u043a\u0443\u0449\u0435\u0439 \u0411\u0414):  <\/p>\n<pre><code class=\"sql\">CREATE TABLE city ( \tcountry text NOT NULL, \tname text NOT NULL,  \tCONSTRAINT uk_city_name UNIQUE ( country, name ) ); <\/code><\/pre>\n<p>  \u0412\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u0432 \u0442\u0435\u043a\u0443\u0449\u0443\u044e \u0411\u0414 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u043c \u0432\u043d\u0435\u0448\u043d\u0438\u0439 \u0438\u0441\u0442\u043e\u0447\u043d\u0438\u043a \u0434\u0430\u043d\u043d\u044b\u0445:  <\/p>\n<pre><code class=\"sql\">-- \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u044f CREATE EXTENSION postgres_fdw;  -- \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430 CREATE SERVER fdb_server FOREIGN DATA WRAPPER postgres_fdw OPTIONS ( host 'localhost', dbname 'fdb' );  -- \u043e\u0442\u043e\u0431\u0440\u0430\u0436\u0435\u043d\u0438\u044f \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u044f CREATE USER MAPPING FOR PUBLIC SERVER fdb_server OPTIONS ( password 'pwd' );  -- \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b CREATE FOREIGN TABLE fdb_city ( country text, name text ) SERVER fdb_server OPTIONS ( table_name 'city' ); <\/code><\/pre>\n<p>  \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u0432\u043d\u0435\u0448\u043d\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u0435\u0439:  <\/p>\n<pre><code class=\"sql\">-- \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u0437\u0430\u043f\u0438\u0441\u044c INSERT INTO fdb_city ( country, name ) VALUES ( 'USA', 'Las Vegas' );  -- \u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c \u0435\u0435 UPDATE fdb_city SET name = 'New Vegas' WHERE name = 'New Vegas';  -- \u0441\u043c\u043e\u0442\u0440\u0438\u043c, \u0447\u0442\u043e \u043f\u043e\u043b\u0443\u0447\u0438\u043b\u043e\u0441\u044c SELECT * FROM fdb_city; <\/code><\/pre>\n<p>  \u0427\u0442\u043e\u0431\u044b \u0443\u0434\u043e\u0441\u0442\u043e\u0432\u0435\u0440\u0438\u0442\u0441\u044f, \u0447\u0442\u043e \u0434\u0430\u043d\u043d\u044b\u0435 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u043f\u043e\u043f\u0430\u043b\u0438 \u0442\u0443\u0434\u0430, \u043a\u0443\u0434\u0430 \u043d\u0430\u0434\u043e, \u043f\u0435\u0440\u0435\u043a\u043b\u044e\u0447\u0430\u0435\u043c\u0441\u044f \u0432 \u0411\u0414 fdb \u0438 \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c:  <\/p>\n<pre><code class=\"sql\">SELECT * FROM city; <\/code><\/pre>\n<h4><b>\u0424\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0438\u043f\u043e\u043c JSON<\/b><\/h4>\n<p>  <img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/b75\/dec\/69b\/b75dec69b96af6bec2e00f43c5c2805d.jpg\"\/><br \/>  \u0422\u0438\u043f <b>JSON<\/b> \u043f\u043e\u044f\u0432\u0438\u043b\u0441\u044f \u0432 <b>PostgreSQL<\/b> 9.2, \u043d\u043e \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0431\u044b\u043b\u043e \u043b\u0438\u0448\u044c \u0434\u0432\u0435 \u2014 array_to_json (\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 \u0432 <b>JSON<\/b>) \u0438 row_to_json (\u043a\u043e\u043d\u0432\u0435\u0440\u0442\u0430\u0446\u0438\u044f \u0437\u0430\u043f\u0438\u0441\u0438 \u0432 <b>JSON<\/b>). \u0422\u0435\u043f\u0435\u0440\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0439 \u0441\u0442\u0430\u043b\u043e \u0431\u043e\u043b\u044c\u0448\u0435 \u0438 \u043c\u043e\u0436\u043d\u043e \u0432\u043f\u043e\u043b\u043d\u0435 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u0441 \u044d\u0442\u0438\u043c \u0442\u0438\u043f\u043e\u043c:  <\/p>\n<pre><code class=\"sql\">CREATE TYPE t_link AS ( \t&quot;from&quot; text, \t&quot;to&quot; text );  CREATE TABLE param ( \tid serial NOT NULL, \tname text NOT NULL, \tvalue json NOT NULL,  \tCONSTRAINT pk_param_id PRIMARY KEY ( id ), \tCONSTRAINT uk_param_name UNIQUE ( name ) );  INSERT INTO param ( name, value ) VALUES ( 'connection', '{ &quot;username&quot; : &quot;Administrator&quot;, &quot;login&quot; : &quot;root&quot;, &quot;databases&quot; : [ &quot;db0&quot;, &quot;db1&quot; ], &quot;enable&quot; : { &quot;day&quot; : 0, &quot;night&quot; : 1 } }'::json ), ( 'link', '{ &quot;from&quot; : &quot;db0&quot;, &quot;to&quot; : &quot;db1&quot; }'::json );  -- \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f (\u0437\u0430\u043f\u0440\u043e\u0441) SELECT value -&gt;&gt; 'username' FROM param WHERE name = 'connection';   -- \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 Administrator  -- \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u043f\u043e\u043b\u044f (\u043f\u043e \u0437\u0430\u0434\u0430\u043d\u043d\u043e\u043c\u0443 \u043f\u0443\u0442\u0438) (\u0437\u0430\u043f\u0440\u043e\u0441) SELECT value #&gt;&gt; '{databases,0}' FROM param WHERE name = 'connection';  -- \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 db0  -- \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u0435 \u0432 SETOF ( key, value ) \u0441 \u0442\u0438\u043f\u043e\u043c text (\u0437\u0430\u043f\u0440\u043e\u0441) SELECT json_each_text ( value ) FROM param;  -- \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 (username,Administrator) (login,root) (databases,&quot;[ &quot;&quot;db0&quot;&quot;, &quot;&quot;db1&quot;&quot; ]&quot;) (enable,&quot;{ &quot;&quot;day&quot;&quot; : 0, &quot;&quot;night&quot;&quot; : 1 }&quot;) (from,db0) (to,db1)  -- \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043a\u043b\u044e\u0447\u0435\u0439 (\u0437\u0430\u043f\u0440\u043e\u0441) SELECT json_object_keys ( value ) FROM param;  -- \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 username login databases enable from to  -- \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435 \u0432 \u0432\u0438\u0434\u0435 \u0437\u0430\u043f\u0438\u0441\u0438 (\u0437\u0430\u043f\u0440\u043e\u0441) SELECT * FROM json_populate_record ( null::t_link, ( SELECT value FROM param WHERE name = 'link' ) );  -- \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 db0;db1  -- \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u043c\u0430\u0441\u0441\u0438\u0432\u0430 (\u0437\u0430\u043f\u0440\u043e\u0441) SELECT json_array_elements ( value -&gt; 'databases' ) FROM param;  -- \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442 &quot;db0&quot; &quot;db1&quot; <\/code><\/pre>\n<p>  \u041f\u043e\u0434\u0432\u043e\u0434\u044f \u0438\u0442\u043e\u0433, \u0445\u043e\u0447\u0443 \u0441\u043a\u0430\u0437\u0430\u0442\u044c, \u0447\u0442\u043e \u0440\u0430\u0434 \u0440\u0430\u0437\u0432\u0438\u0442\u0438\u044e <b>PostgreSQL<\/b>, \u043f\u0440\u043e\u0435\u043a\u0442 \u0440\u0430\u0437\u0432\u0438\u0432\u0430\u0435\u0442\u0441\u044f, \u0445\u043e\u0442\u044c \u0438 \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0441\u044b\u0440\u044b\u0435 \u0432\u0435\u0449\u0438.<\/p>\n<p>  P.S. \u0421\u043f\u0430\u0441\u0438\u0431\u043e, \u0435\u0441\u043b\u0438 \u0434\u043e\u0447\u0438\u0442\u0430\u043b\u0438 \u0434\u043e \u043a\u043e\u043d\u0446\u0430.<\/p>\n<p>  \u0421\u0441\u044b\u043b\u043a\u0438:  <\/p>\n<ul>\n<li><a href=\"http:\/\/wiki.postgresql.org\/wiki\/What%27s_new_in_PostgreSQL_9.3\">What&#8217;s new in PostgreSQL 9.3<\/a><\/li>\n<li><a href=\"http:\/\/www.depesz.com\">select * from depesz<\/a><\/li>\n<li><a href=\"http:\/\/www.postgresql.org\/docs\/9.3\/static\">\u041e\u0444\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u0430\u044f \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u043e PostgreSQL<\/a><\/li>\n<\/ul>\n<div class=\"clear\"><\/div>\n<\/p><\/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\/195898\/\"> http:\/\/habrahabr.ru\/post\/195898\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"content html_format\">   \t<img decoding=\"async\" src=\"http:\/\/habrastorage.org\/storage3\/f06\/661\/735\/f0666173533dd132950603eb6e245731.jpg\"\/><br \/>  \u0417\u0434\u0440\u0430\u0432\u0441\u0442\u0432\u0443\u0439\u0442\u0435, \u0445\u0430\u0431\u0440\u0430\u0447\u0435\u043b\u043e\u0432\u0435\u043a\u0438! \u041d\u0435 \u0442\u0430\u043a \u0443\u0436 \u0434\u0430\u0432\u043d\u043e \u0432\u044b\u0448\u0435\u043b \u0440\u0435\u043b\u0438\u0437 <b>PostgreSQL<\/b> 9.3 \u0438 \u044f \u0445\u043e\u0442\u0435\u043b \u0431\u044b \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u0442\u044c \u0412\u0430\u0441 \u0441 \u043d\u0430\u0438\u0431\u043e\u043b\u0435\u0435 \u0432\u0430\u0436\u043d\u044b\u043c\u0438 \u043d\u043e\u0432\u0448\u0435\u0441\u0442\u0432\u0430\u043c\u0438, \u043a\u0430\u0441\u0430\u044e\u0449\u0438\u043c\u0438\u0441\u044f \u043a\u043b\u0438\u0435\u043d\u0442\u0441\u043a\u043e\u0439 \u0447\u0430\u0441\u0442\u0438, \u043a\u043e\u0442\u043e\u0440\u044b\u0435, \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e, \u043f\u0440\u0438\u0433\u043e\u0434\u044f\u0442\u0441\u044f \u0412\u0430\u043c. \u0412 \u044d\u0442\u043e\u0439 \u0441\u0442\u0430\u0442\u044c\u0435 \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0435\u043d\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435:  <\/p>\n<ul>\n<li>\u043c\u0430\u0442\u0435\u0440\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/li>\n<li>\u043e\u0431\u043d\u043e\u0432\u043b\u044f\u0435\u043c\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/li>\n<li>\u0442\u0440\u0438\u0433\u0433\u0435\u0440\u044b \u043a \u0441\u043e\u0431\u044b\u0442\u0438\u044f\u043c<\/li>\n<li>\u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u044b\u0435 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0438\u044f<\/li>\n<li>\u043b\u0430\u0442\u0435\u0440\u0430\u043b\u044c\u043d\u043e\u0435 \u043f\u0440\u0438\u0441\u043e\u0435\u0434\u0438\u043d\u0435\u043d\u0438\u0435<\/li>\n<li>\u0438\u0437\u043c\u0435\u043d\u044f\u0435\u043c\u044b\u0435 \u0432\u043d\u0435\u0448\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b<\/li>\n<li>\u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0438 \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u044b \u0434\u043b\u044f \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0442\u0438\u043f\u043e\u043c <b>JSON<\/b><\/li>\n<\/ul>\n<p>  <\/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-195898","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/195898","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=195898"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/195898\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=195898"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=195898"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=195898"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}