Skip to content

Commit

Permalink
chore: add a trigger for sload update notification (#328)
Browse files Browse the repository at this point in the history
* chore: add a trigger for sload update notification

* chore: add triggers into default schema

* fix typo

* fix: trigger query

* lint
  • Loading branch information
renancloudwalk authored Mar 8, 2024
1 parent 99c5ab3 commit efb3268
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ services:
volumes:
- "./static/schema/001-init.sql:/docker-entrypoint-initdb.d/001-schema.sql"
- "./static/schema/002-schema-external-rpc.sql:/docker-entrypoint-initdb.d/002-schema.sql"
- "./static/schema/003-triggers-and-procedures.sql:/docker-entrypoint-initdb.d/003-triggers-and-procedures.sql"
- "postgres-data:/var/lib/postgresql/data"

prometheus:
Expand Down
4 changes: 4 additions & 0 deletions src/infra/docker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ impl Docker {
.with_mapped_port((5432, 5432))
.with_volume(("./static/schema/001-init.sql", "/docker-entrypoint-initdb.d/001-schema.sql"))
.with_volume(("./static/schema/002-schema-external-rpc.sql", "/docker-entrypoint-initdb.d/002-schema.sql"))
.with_volume((
"./static/schema/003-triggers-and-procedures.sql",
"/docker-entrypoint-initdb.d/003-triggers-and-procedures.sql",
))
.with_tag("16.2");

self.cli.run(image)
Expand Down
22 changes: 22 additions & 0 deletions static/schema/003-triggers-and-procedures.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CREATE OR REPLACE FUNCTION sload_cache_notify()
RETURNS trigger AS
$$
BEGIN
PERFORM pg_notify(
'sload_cache_channel',
json_build_object(
'index', encode(NEW.idx, 'hex'),
'value', encode(NEW.value, 'hex'),
'address', encode(NEW.account_address, 'hex'),
'block', NEW.creation_block
)::text
);
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER sload_cache
AFTER INSERT OR UPDATE
ON account_slots
FOR EACH ROW
EXECUTE PROCEDURE sload_cache_notify();

0 comments on commit efb3268

Please sign in to comment.