Add support for PostgreSQL as store engine #255
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR adds PostgreSQL as a store engine. The implementation is kept as similar as possible to other supported engines.
Two tables are created:
kv
andlocks
tables. Thekv
table stores data regarding get, set, list and delete operations. Thelocks
table is used for leader election. Design is based on publish/subscribe so there is no polling mechanism. This is achieved by utilizing the trigger and LISTEN/NOTIFY combination.In other engines, we would assign a session to the lock so it expires after a defined period, but here we don't have this functionality so we use the
pg_cron
extension to define a job that will be executed every 6 seconds (session TTL for other engines) and deletes created lock entry. Job is scheduled by INSERT trigger created on thelocks
table. DELETE trigger created on thelocks
table removes this cronjob. If the cronjob is scheduled only once to run every 6 seconds, this time and TTL of lock entry will diverge. By creating and removing a cronjob at the same time when lock entry is created and removed we ensure TTL of 6 seconds is correct.Testing
Tested the implementation by running Postgres as Docker container locally and running UTs