wal2json

repository·master·Indexed 22 days ago

https://github.com/eulerto/wal2json

A PostgreSQL output plugin for logical decoding that converts Write-Ahead Log (WAL) changes into JSON format. It enables streaming of database changes (INSERT, UPDATE, DELETE, etc.) to external consumers via the streaming replication protocol or a SQL-based API. Supports two output formats and provides various parameters to customize JSON output, including filtering by tables and origins.

Tokens
1.7K
Snippets
6
Records
10
Agent score
31%

What's inside wal2json

  1. What is wal2json?

    master

    wal2json is an output plugin for PostgreSQL logical decoding. It provides access to tuples produced by INSERT and UPDATE operations. Depending on the configured replica identity, UPDATE/DELETE old row versions can also be accessed.

    Changes can be consumed via:

    1. The streaming protocol (using logical replication slots).
    2. A special SQL API.
  2. Compare wal2json format versions

    master

    wal2json supports two primary output formats:

    • format version 1: Produces one JSON object per transaction. All new/old tuples are contained within this single object. Options include transaction timestamp, schema-qualification, data types, and transaction IDs.
    • format version 2: Produces one JSON object per tuple. It can optionally include JSON objects representing the beginning and end of a transaction.
  3. Build and install wal2json from source on Unix

    master

    Ensure PostgreSQL 9.4+ is installed with header files. If using a repository, install the development package (e.g., postgresql17-devel for yum or postgresql-server-dev-17 for apt) and add the bin directory to your PATH.

    Example for a custom PostgreSQL installation in /home/euler/pg17:

    $ tar -zxf wal2json-wal2json_2_6.tar.gz
    $ cd wal2json-wal2json_2_6
    $ export PATH=/home/euler/pg17/bin:$PATH
    $ make
    $ make install
  4. Consume changes using pg_recvlogical

    master

    To use pg_recvlogical, you must first configure a replication connection in pg_hba.conf.

    For PostgreSQL 9.4, 9.5, 9.6:

    local    replication     myuser                     trust

    For PostgreSQL 10 or later:

    local    mydatabase      myuser                     trust

    Ensure max_wal_senders is set to at least 1 in postgresql.conf and restart PostgreSQL.

    Usage Example:

    1. Create a slot:
    $ pg_recvlogical -d postgres --slot test_slot --create-slot -P wal2json
    1. Start streaming with options:
    $ pg_recvlogical -d postgres --slot test_slot --start -o pretty-print=1 -o add-msg-prefixes=wal2json -f -
    1. Drop the slot when finished:
    $ pg_recvlogical -d postgres --slot test_slot --drop-slot
    $ pg_recvlogical -d postgres --slot test_slot --create-slot -P wal2json
    $ pg_recvlogical -d postgres --slot test_slot --start -o pretty-print=1 -o add-msg-prefixes=wal2json -f -
  5. Build wal2json on Windows

    master

    To build out-of-tree:

    1. Edit the wal2json.vcxproj file and change c:\pg\17 to your PostgreSQL prefix directory.
    2. Open the project file in MS Visual Studio and compile.
    3. Copy the resulting wal2json.dll to the directory returned by pg_config --pkglibdir.
  6. Configure postgresql.conf for wal2json

    master

    To use wal2json, you must set wal_level to logical. For PostgreSQL versions 9.4, 9.5, and 9.6, you must also explicitly set max_replication_slots and max_wal_senders. For version 10 or later, the default values are sufficient.

    A restart of PostgreSQL is required after changing these parameters.

    wal_level = logical
    # Required for versions 9.4, 9.5, 9.6
    max_replication_slots = 10
    max_wal_senders = 10
    wal_level = logical
    #
    # these parameters only need to set in versions 9.4, 9.5 and 9.6
    # default values are ok in version 10 or later
    #
    max_replication_slots = 10
    max_wal_senders = 10
  7. Consume changes using SQL functions

    master

    You can retrieve logical changes directly via SQL using pg_logical_slot_get_changes.

    Workflow:

    1. Create a slot: SELECT pg_create_logical_replication_slot('slot_name', 'wal2json');
    2. Retrieve changes: SELECT data FROM pg_logical_slot_get_changes('slot_name', NULL, NULL, 'option', 'value', ...);
    3. Drop the slot: SELECT pg_drop_replication_slot('slot_name');

    Example (Format Version 2):

    SELECT data FROM pg_logical_slot_get_changes('test_slot', NULL, NULL, 'format-version', '2', 'add-msg-prefixes', 'wal2json');
  8. Reference wal2json plugin parameters

    master

    The following parameters can be passed to the wal2json plugin to customize the JSON output:

    ParameterDescription
    format-versionDefines which format to use (1 or 2). Default is 1.
    include-xidsAdd _xid_ to each changeset. Default is false.
    include-timestampAdd _timestamp_ to each changeset. Default is false.
    include-schemasAdd _schema_ to each change. Default is true.
    include-typesAdd _type_ to each change. Default is true.
    include-typmodAdd modifier to types (e.g., varchar(20)). Default is true.
    include-type-oidsAdd type OIDs. Default is false.
    include-domain-data-typeReplace domain name with underlying data type. Default is false.
    include-column-positionsAdd column position (_pg_attribute.attnum_). Default is false.
    include-originAdd origin of a piece of data. Default is false.
    include-not-nullAdd not null info as _columnoptionals_. Default is false.
    include-defaultAdd default expression. Default is false.
    include-pkAdd primary key info as _pk_. Default is false.
    include-lsnAdd _nextlsn_ to each changeset. Default is false.
    include-transactionEmit records for start/end of transaction. Default is true.
    include-unchanged-toast(Deprecated) Do not use.
    numeric-data-types-as-stringUse string for numeric types to avoid JSON interoperability issues with Infinity/NaN. Default is false.
    pretty-printAdd spaces and indentation. Default is false.
    write-in-chunksWrite after every change instead of every changeset (only for format-version 1). Default is false.
    filter-originsComma-separated list of origins to exclude.
    filter-tablesComma-separated list of schema-qualified tables to exclude. Supports *.foo or bar.*.
    add-tablesComma-separated list of schema-qualified tables to include.
    filter-msg-prefixesComma-separated list of message prefixes to exclude.
    add-msg-prefixesComma-separated list of message prefixes to include.
    actionsDefine operations to send (insert, update, delete, truncate). Default is all. Note: truncate is disabled in format-version 1.