Skip to content

Instantly share code, notes, and snippets.

@ichux
Last active September 15, 2018 07:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ichux/8fb8c1ed722d1b9738dd5c39aab31b2d to your computer and use it in GitHub Desktop.
Save ichux/8fb8c1ed722d1b9738dd5c39aab31b2d to your computer and use it in GitHub Desktop.
postgres after insert
BEGIN;
DROP TRIGGER IF EXISTS accumulate_ait
on accumulate;
DROP FUNCTION IF EXISTS adapt_to_service();
DROP TABLE IF EXISTS accumulate CASCADE;
CREATE TABLE IF NOT EXISTS accumulate (
id bigint NOT NULL,
monies bigint,
PRIMARY KEY (id)
);
CREATE OR REPLACE FUNCTION assumed()
RETURNS TABLE(the_id INTEGER)
LANGUAGE plpgsql
AS $function$
BEGIN
FOR the_id IN (SELECT * FROM generate_series(1, 3)) LOOP
RETURN NEXT;
END LOOP;
END;
$function$;
CREATE OR REPLACE FUNCTION adapt_to_service()
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
UPDATE accumulate SET monies = 25 WHERE ID = 1;
END;
$function$;
CREATE
OR REPLACE FUNCTION accumulate_ait()
RETURNS TRIGGER LANGUAGE plpgsql AS $body$
BEGIN
PERFORM adapt_to_service();
RETURN NULL;
END; $body$;
CREATE TRIGGER accumulate_ait
AFTER INSERT
ON accumulate
FOR EACH STATEMENT
EXECUTE PROCEDURE accumulate_ait();
END;
INSERT INTO accumulate (id) VALUES(1) RETURNING *;
-- SELECT * FROM accumulate;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment