Skip to content

Instantly share code, notes, and snippets.

@quommit
Last active August 29, 2015 14:16
Show Gist options
  • Save quommit/3c8cd42164dea83bbcc6 to your computer and use it in GitHub Desktop.
Save quommit/3c8cd42164dea83bbcc6 to your computer and use it in GitHub Desktop.
PostgreSQL trigger function for geometry change notification
CREATE OR REPLACE FUNCTION redis_pubsub.notifygeometrychange_trigger()
RETURNS trigger AS
$BODY$
DECLARE
message text;
geom geometry;
xmin double precision;
ymin double precision;
xmax double precision;
ymax double precision;
BEGIN
message := concat_ws(';', TG_TABLE_NAME, TG_OP);
IF TG_OP = 'UPDATE' THEN
IF NOT st_equals(OLD.geometria, NEW.geometria) THEN
IF NEW.geometria IS NULL THEN
geom := OLD.geometria;
ELSE
geom := NEW.geometria;
END IF;
END IF;
ELSIF TG_OP = 'INSERT' THEN
geom := NEW.geometria;
ELSIF TG_OP = 'DELETE' THEN
geom := OLD.geometria;
END IF;
xmin := st_xmin(box2d(geom));
ymin := st_ymin(box2d(geom));
xmax := st_xmax(box2d(geom));
ymax := st_ymax(box2d(geom));
message := concat_ws(';', message, xmin::text, ymin::text, xmax::text, ymax::text);
PERFORM redis_pubsub.notify('mapproxy', message, '127.0.0.1', 6379);
RETURN NULL;
END;
$BODY$
LANGUAGE plpgsql VOLATILE;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment