kemono2/db/schema/public/posts/triggers.sql
2024-11-26 00:11:49 +01:00

28 lines
480 B
PL/PgSQL

CREATE FUNCTION posts_added_max()
RETURNS TRIGGER
LANGUAGE plpgsql
AS $$
BEGIN
INSERT INTO posts_added_max AS pam("user", service, added)
SELECT
"user",
service,
max(added) AS added
FROM
posts
WHERE
posts.service = NEW.service
AND posts."user" = NEW."user"
GROUP BY
"user",
service
ON CONFLICT(service,
"user")
DO UPDATE SET
added = EXCLUDED.added
WHERE
EXCLUDED.added > pam.added;
RETURN NULL;
END;
$$;