try to fix combine_transition_tables parse error

This commit is contained in:
dullbananas 2023-12-29 13:44:34 -07:00 committed by GitHub
parent 91d8179276
commit 75d00a4626
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -36,42 +36,62 @@ BEGIN
END END
$$; $$;
-- Selects both old and new rows in a trigger and allows using `sum(count_diff)` to get the number to add to a count -- Create functions that select both old and new rows in a trigger. Column 1 is `-1` if old and `1` if new,
CREATE FUNCTION r.combine_transition_tables (tg_op text) -- which can be used with `sum` to get the number to add to a count. Column 2 is the original row as a composite
RETURNS SETOF record -- value. A separate function is called in each `IF` statement to prevent an error from referencing transition
LANGUAGE plpgsql -- tables that don't exist. All of this could be one function, but that would require using `RETURN QUERY EXECUTE`
AS $$ -- instead of calls to separate functions because of PostgreSQL's limited polymorphism. Parsing and planning dynamic
-- queries at runtime is worse for performance.
CREATE PROCEDURE r.combine_transition_tables_function (table_name text)
LANGUAGE plpgsql
AS $a$
BEGIN BEGIN
IF (TG_OP = 'UPDATE') THEN EXECUTE replace($b$
RETURN QUERY CREATE FUNCTION r.get_old_thing_rows ()
SELECT RETURNS SETOF record
-1 AS count_diff, LANGUAGE plpgsql
old_table AS affected_row AS $$
FROM BEGIN
old_table RETURN QUERY SELECT -1, old_table FROM old_table;
UNION ALL RETURN;
SELECT END
1 AS count_diff, $$;
new_table AS affected_row CREATE FUNCTION r.get_new_thing_rows ()
FROM RETURNS SETOF record
new_table; LANGUAGE plpgsql
ELSIF (TG_OP = 'INSERT') THEN AS $$
RETURN QUERY BEGIN
SELECT RETURN QUERY SELECT 1, new_table FROM new_table;
1 AS count_diff, RETURN;
* END
FROM $$;
new_table; CREATE FUNCTION r.combine_thing_transition_tables (tg_op text)
ELSE RETURNS SETOF record
RETURN QUERY LANGUAGE plpgsql
SELECT AS $$
-1 AS count_diff, BEGIN
* IF (TG_OP IN ('UPDATE', 'DELETE')) THEN
FROM RETURN QUERY SELECT * FROM r.get_old_thing_rows () AS (count_diff bigint, thing thing);
old_table; END IF;
END IF; IF (TG_OP IN ('UPDATE', 'INSERT')) THEN
RETURN QUERY SELECT * FROM r.get_new_thing_rows () AS (count_diff bigint, thing thing);
END IF;
RETURN;
END
$$;
$b$, 'thing', table_name);
END END
$$; $a$;
CALL r.combine_transition_tables_function('comment');
CALL r.combine_transition_tables_function('community');
CALL r.combine_transition_tables_function('community_follower');
CALL r.combine_transition_tables_function('person');
CALL r.combine_transition_tables_function('post');
-- Creates triggers for all operation types, which can't be 1 trigger when transition tables are used -- Creates triggers for all operation types, which can't be 1 trigger when transition tables are used
CREATE PROCEDURE r.create_triggers (table_name text, function_name text) CREATE PROCEDURE r.create_triggers (table_name text, function_name text)
@ -148,7 +168,7 @@ BEGIN
sum(count_diff) FILTER (WHERE (thing_like).score = 1) AS upvotes, sum(count_diff) FILTER (WHERE (thing_like).score = 1) AS upvotes,
sum(count_diff) FILTER (WHERE (thing_like).score != 1) AS downvotes sum(count_diff) FILTER (WHERE (thing_like).score != 1) AS downvotes
FROM FROM
r.combine_transition_tables (TG_OP) r.combine_thing_transition_tables (TG_OP)
AS (count_diff bigint, AS (count_diff bigint,
thing_like thing_like) thing_like thing_like)
GROUP BY GROUP BY
@ -198,7 +218,7 @@ BEGIN
(comment).local, (comment).local,
sum(count_diff) AS comments sum(count_diff) AS comments
FROM FROM
r.combine_transition_tables (TG_OP) r.combine_comment_transition_tables (TG_OP)
AS (count_diff bigint, AS (count_diff bigint,
comment comment) comment comment)
WHERE WHERE
@ -301,7 +321,7 @@ BEGIN
(post).local, (post).local,
sum(count_diff) AS posts sum(count_diff) AS posts
FROM FROM
r.combine_transition_tables (TG_OP) r.combine_post_transition_tables (TG_OP)
AS (count_diff bigint, AS (count_diff bigint,
post post) post post)
WHERE WHERE
@ -357,7 +377,7 @@ BEGIN
SELECT SELECT
sum(count_diff) AS communities sum(count_diff) AS communities
FROM FROM
r.combine_transition_tables (TG_OP) r.combine_community_transition_tables (TG_OP)
AS (count_diff bigint, community community) AS (count_diff bigint, community community)
WHERE (community).local AND NOT ((community).deleted OR (community).removed)) AS diff; WHERE (community).local AND NOT ((community).deleted OR (community).removed)) AS diff;
RETURN NULL; RETURN NULL;
@ -379,7 +399,7 @@ BEGIN
SELECT SELECT
sum(count_diff) AS users sum(count_diff) AS users
FROM FROM
r.combine_transition_tables (TG_OP) r.combine_person_transition_tables (TG_OP)
AS (count_diff bigint, person person) AS (count_diff bigint, person person)
WHERE (person).local) AS diff; WHERE (person).local) AS diff;
RETURN NULL; RETURN NULL;
@ -450,7 +470,7 @@ BEGIN
(community_follower).community_id, (community_follower).community_id,
sum(count_diff) AS subscribers sum(count_diff) AS subscribers
FROM FROM
r.combine_transition_tables (TG_OP) r.combine_community_follower_transition_tables (TG_OP)
AS (count_diff bigint, community_follower community_follower) AS (count_diff bigint, community_follower community_follower)
WHERE ( WHERE (
SELECT SELECT