|
| 1 | + |
| 2 | +:sectnums: |
| 3 | +:sectnumlevels: 5 |
| 4 | + |
| 5 | += wal2json |
| 6 | + |
| 7 | +== Overview |
| 8 | +wal2json is an output plugin for logical decoding. It generates JSON object for each transaction. |
| 9 | + |
| 10 | +== Installation |
| 11 | + |
| 12 | +[TIP] |
| 13 | +The source code installation environment is Ubuntu 24.04 (x86_64), in which IvorySQL 5 or a later version has been installed. The installation path is /usr/ivory-5. |
| 14 | + |
| 15 | +=== Source Code Installation |
| 16 | + |
| 17 | +[literal] |
| 18 | +---- |
| 19 | +# download source code package from: https://github.com/eulerto/wal2json/releases/tag/wal2json_2_6 |
| 20 | +
|
| 21 | +unzip wal2json_2_6.zip |
| 22 | +cd wal2json_2_6 |
| 23 | +
|
| 24 | +# compile and install the extension |
| 25 | +make PG_CONFIG=/usr/ivory-5/bin/pg_config |
| 26 | +make PG_CONFIG=/usr/ivory-5/bin/pg_config install |
| 27 | +---- |
| 28 | + |
| 29 | +[TIP] |
| 30 | +If there is error "xlocale.h: No such file or directory" during compilation, user should remove the |
| 31 | +line of "#define HAVE_XLOCALE_H 1" from file /usr/ivory-5/include/postgresql/server/pg_config.h. |
| 32 | + |
| 33 | +=== Modify the configuration file |
| 34 | + |
| 35 | +Modify the postgresql.conf file to set wal_level as "logical", and set max_replication_slots/max_wal_senders. |
| 36 | +[literal] |
| 37 | +---- |
| 38 | +wal_level = logical |
| 39 | +max_replication_slots = 10 |
| 40 | +max_wal_senders = 10 |
| 41 | +---- |
| 42 | + |
| 43 | +Make sure the following content to be in pg_hba.conf. |
| 44 | +[literal] |
| 45 | +---- |
| 46 | + local replication all trust |
| 47 | + host replication all 127.0.0.1/32 trust |
| 48 | + host replication all ::1/128 trust |
| 49 | +---- |
| 50 | + |
| 51 | +Then restart the database. |
| 52 | + |
| 53 | +== Use |
| 54 | + |
| 55 | +Open the first terminal and execute command: |
| 56 | + |
| 57 | +[literal] |
| 58 | +---- |
| 59 | +sudo -u ivorysql /usr/ivory-5/bin/pg_recvlogical -d postgres --slot wal2json_slot --create-slot -P wal2json |
| 60 | +
|
| 61 | +Start monitoring, output the changes in JSON format and in real time. |
| 62 | +sudo -u ivorysql /usr/ivory-5/bin/pg_recvlogical -d postgres --slot wal2json_slot --start -o pretty-print=1 -f - |
| 63 | +---- |
| 64 | + |
| 65 | +Connect database in the second terminal: |
| 66 | +[literal] |
| 67 | +---- |
| 68 | +/usr/ivory-5/bin/psql -d postgres -p 1521 |
| 69 | +---- |
| 70 | + |
| 71 | +Execute the following SQL statement: |
| 72 | +[literal] |
| 73 | +---- |
| 74 | +CREATE TABLE test_cdc (id int primary key, name varchar(50)); |
| 75 | +INSERT INTO test_cdc VALUES (1, 'test1'); |
| 76 | +UPDATE test_cdc SET name = 'test1_update' WHERE id = 1; |
| 77 | +DELETE FROM test_cdc WHERE id = 1; |
| 78 | +DROP TABLE test_cdc; |
| 79 | +---- |
| 80 | + |
| 81 | +The following output will appear in the first terminal: |
| 82 | +[literal] |
| 83 | +---- |
| 84 | +{ |
| 85 | + "change": [ |
| 86 | + ] |
| 87 | +} |
| 88 | +{ |
| 89 | + "change": [ |
| 90 | + { |
| 91 | + "kind": "insert", |
| 92 | + "schema": "public", |
| 93 | + "table": "test_cdc", |
| 94 | + "columnnames": ["id", "name"], |
| 95 | + "columntypes": ["integer", "sys.oravarcharbyte(50)"], |
| 96 | + "columnvalues": [1, "test1"] |
| 97 | + } |
| 98 | + ] |
| 99 | +} |
| 100 | +{ |
| 101 | + "change": [ |
| 102 | + { |
| 103 | + "kind": "update", |
| 104 | + "schema": "public", |
| 105 | + "table": "test_cdc", |
| 106 | + "columnnames": ["id", "name"], |
| 107 | + "columntypes": ["integer", "sys.oravarcharbyte(50)"], |
| 108 | + "columnvalues": [1, "test1_update"], |
| 109 | + "oldkeys": { |
| 110 | + "keynames": ["id"], |
| 111 | + "keytypes": ["integer"], |
| 112 | + "keyvalues": [1] |
| 113 | + } |
| 114 | + } |
| 115 | + ] |
| 116 | +} |
| 117 | +{ |
| 118 | + "change": [ |
| 119 | + { |
| 120 | + "kind": "delete", |
| 121 | + "schema": "public", |
| 122 | + "table": "test_cdc", |
| 123 | + "oldkeys": { |
| 124 | + "keynames": ["id"], |
| 125 | + "keytypes": ["integer"], |
| 126 | + "keyvalues": [1] |
| 127 | + } |
| 128 | + } |
| 129 | + ] |
| 130 | +} |
| 131 | +{ |
| 132 | + "change": [ |
| 133 | + ] |
| 134 | +} |
| 135 | +---- |
0 commit comments