How It Works
Like many similar projects, SearchReplica abuses Logical Replication protocol, decoding all the transactions commited to Write-Ahead-Log and forwarding them into Elasticsearch/OpenSearch in a form a JSON documents
Following is the simplified workflow.
- Fetch and parse all comments related to puslished tables using monstrous query.
- Check if key fields are awailable in WAL (see: Upsert Mode)
- Discover User-Defined types, like enums or composite types.
- (re)Create Replication Slot if needed.
- Fetch all existing data via
COPY TO
command. And process them 1 by 1. - Push to Elasticsearch in batches
After initial cold-start reindexing
- Subscribe
- Get Messages in a loop and process them 1 by 1.
- Periodically push to Elasticsearch in batches.
- After successfull push, commit LSN of last pushed document back to postgres
This approach provides you with near-to-realtime consistent data without additioan efforts.