gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
7.9KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>48.1. Logical Decoding Examples</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="logicaldecoding.html" title="Chapter 48. Logical Decoding" /><link rel="next" href="logicaldecoding-explanation.html" title="48.2. Logical Decoding Concepts" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">48.1. Logical Decoding Examples</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="logicaldecoding.html" title="Chapter 48. Logical Decoding">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="logicaldecoding.html" title="Chapter 48. Logical Decoding">Up</a></td><th width="60%" align="center">Chapter 48. Logical Decoding</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="logicaldecoding-explanation.html" title="48.2. Logical Decoding Concepts">Next</a></td></tr></table><hr></hr></div><div class="sect1" id="LOGICALDECODING-EXAMPLE"><div class="titlepage"><div><div><h2 class="title" style="clear: both">48.1. Logical Decoding Examples</h2></div></div></div><p>
  3. The following example demonstrates controlling logical decoding using the
  4. SQL interface.
  5. </p><p>
  6. Before you can use logical decoding, you must set
  7. <a class="xref" href="runtime-config-wal.html#GUC-WAL-LEVEL">wal_level</a> to <code class="literal">logical</code> and
  8. <a class="xref" href="runtime-config-replication.html#GUC-MAX-REPLICATION-SLOTS">max_replication_slots</a> to at least 1. Then, you
  9. should connect to the target database (in the example
  10. below, <code class="literal">postgres</code>) as a superuser.
  11. </p><pre class="programlisting">
  12. postgres=# -- Create a slot named 'regression_slot' using the output plugin 'test_decoding'
  13. postgres=# SELECT * FROM pg_create_logical_replication_slot('regression_slot', 'test_decoding');
  14. slot_name | lsn
  15. -----------------+-----------
  16. regression_slot | 0/16B1970
  17. (1 row)
  18. postgres=# SELECT slot_name, plugin, slot_type, database, active, restart_lsn, confirmed_flush_lsn FROM pg_replication_slots;
  19. slot_name | plugin | slot_type | database | active | restart_lsn | confirmed_flush_lsn
  20. -----------------+---------------+-----------+----------+--------+-------------+-----------------
  21. regression_slot | test_decoding | logical | postgres | f | 0/16A4408 | 0/16A4440
  22. (1 row)
  23. postgres=# -- There are no changes to see yet
  24. postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
  25. lsn | xid | data
  26. -----+-----+------
  27. (0 rows)
  28. postgres=# CREATE TABLE data(id serial primary key, data text);
  29. CREATE TABLE
  30. postgres=# -- DDL isn't replicated, so all you'll see is the transaction
  31. postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
  32. lsn | xid | data
  33. -----------+-------+--------------
  34. 0/BA2DA58 | 10297 | BEGIN 10297
  35. 0/BA5A5A0 | 10297 | COMMIT 10297
  36. (2 rows)
  37. postgres=# -- Once changes are read, they're consumed and not emitted
  38. postgres=# -- in a subsequent call:
  39. postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
  40. lsn | xid | data
  41. -----+-----+------
  42. (0 rows)
  43. postgres=# BEGIN;
  44. postgres=# INSERT INTO data(data) VALUES('1');
  45. postgres=# INSERT INTO data(data) VALUES('2');
  46. postgres=# COMMIT;
  47. postgres=# SELECT * FROM pg_logical_slot_get_changes('regression_slot', NULL, NULL);
  48. lsn | xid | data
  49. -----------+-------+---------------------------------------------------------
  50. 0/BA5A688 | 10298 | BEGIN 10298
  51. 0/BA5A6F0 | 10298 | table public.data: INSERT: id[integer]:1 data[text]:'1'
  52. 0/BA5A7F8 | 10298 | table public.data: INSERT: id[integer]:2 data[text]:'2'
  53. 0/BA5A8A8 | 10298 | COMMIT 10298
  54. (4 rows)
  55. postgres=# INSERT INTO data(data) VALUES('3');
  56. postgres=# -- You can also peek ahead in the change stream without consuming changes
  57. postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL);
  58. lsn | xid | data
  59. -----------+-------+---------------------------------------------------------
  60. 0/BA5A8E0 | 10299 | BEGIN 10299
  61. 0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
  62. 0/BA5A990 | 10299 | COMMIT 10299
  63. (3 rows)
  64. postgres=# -- The next call to pg_logical_slot_peek_changes() returns the same changes again
  65. postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL);
  66. lsn | xid | data
  67. -----------+-------+---------------------------------------------------------
  68. 0/BA5A8E0 | 10299 | BEGIN 10299
  69. 0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
  70. 0/BA5A990 | 10299 | COMMIT 10299
  71. (3 rows)
  72. postgres=# -- options can be passed to output plugin, to influence the formatting
  73. postgres=# SELECT * FROM pg_logical_slot_peek_changes('regression_slot', NULL, NULL, 'include-timestamp', 'on');
  74. lsn | xid | data
  75. -----------+-------+---------------------------------------------------------
  76. 0/BA5A8E0 | 10299 | BEGIN 10299
  77. 0/BA5A8E0 | 10299 | table public.data: INSERT: id[integer]:3 data[text]:'3'
  78. 0/BA5A990 | 10299 | COMMIT 10299 (at 2017-05-10 12:07:21.272494-04)
  79. (3 rows)
  80. postgres=# -- Remember to destroy a slot you no longer need to stop it consuming
  81. postgres=# -- server resources:
  82. postgres=# SELECT pg_drop_replication_slot('regression_slot');
  83. pg_drop_replication_slot
  84. -----------------------
  85. (1 row)
  86. </pre><p>
  87. The following example shows how logical decoding is controlled over the
  88. streaming replication protocol, using the
  89. program <a class="xref" href="app-pgrecvlogical.html" title="pg_recvlogical"><span class="refentrytitle"><span class="application">pg_recvlogical</span></span></a> included in the PostgreSQL
  90. distribution. This requires that client authentication is set up to allow
  91. replication connections
  92. (see <a class="xref" href="warm-standby.html#STREAMING-REPLICATION-AUTHENTICATION" title="26.2.5.1. Authentication">Section 26.2.5.1</a>) and
  93. that <code class="varname">max_wal_senders</code> is set sufficiently high to allow
  94. an additional connection.
  95. </p><pre class="programlisting">
  96. $ pg_recvlogical -d postgres --slot=test --create-slot
  97. $ pg_recvlogical -d postgres --slot=test --start -f -
  98. <span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>Z</strong></span>
  99. $ psql -d postgres -c "INSERT INTO data(data) VALUES('4');"
  100. $ fg
  101. BEGIN 693
  102. table public.data: INSERT: id[integer]:4 data[text]:'4'
  103. COMMIT 693
  104. <span class="keycap"><strong>Control</strong></span>+<span class="keycap"><strong>C</strong></span>
  105. $ pg_recvlogical -d postgres --slot=test --drop-slot
  106. </pre></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="logicaldecoding.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="logicaldecoding.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="logicaldecoding-explanation.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 48. Logical Decoding </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> 48.2. Logical Decoding Concepts</td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1