Revert "try to fix combine_transition_tables parse error"

This reverts commit 75d00a4626.
This commit is contained in:
Dull Bananas 2023-12-29 21:22:18 +00:00
parent 75d00a4626
commit 5a18acacff

View file

@ -36,62 +36,42 @@ BEGIN
END END
$$; $$;
-- Create functions that select both old and new rows in a trigger. Column 1 is `-1` if old and `1` if new, -- Selects both old and new rows in a trigger and allows using `sum(count_diff)` to get the number to add to a count
-- which can be used with `sum` to get the number to add to a count. Column 2 is the original row as a composite CREATE FUNCTION r.combine_transition_tables (tg_op text)
-- value. A separate function is called in each `IF` statement to prevent an error from referencing transition
-- tables that don't exist. All of this could be one function, but that would require using `RETURN QUERY EXECUTE`
-- 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
EXECUTE replace($b$
CREATE FUNCTION r.get_old_thing_rows ()
RETURNS SETOF record RETURNS SETOF record
LANGUAGE plpgsql LANGUAGE plpgsql
AS $$ AS $$
BEGIN BEGIN
RETURN QUERY SELECT -1, old_table FROM old_table; IF (TG_OP = 'UPDATE') THEN
RETURN; RETURN QUERY
END SELECT
$$; -1 AS count_diff,
CREATE FUNCTION r.get_new_thing_rows () old_table AS affected_row
RETURNS SETOF record FROM
LANGUAGE plpgsql old_table
AS $$ UNION ALL
BEGIN SELECT
RETURN QUERY SELECT 1, new_table FROM new_table; 1 AS count_diff,
RETURN; new_table AS affected_row
END FROM
$$; new_table;
CREATE FUNCTION r.combine_thing_transition_tables (tg_op text) ELSIF (TG_OP = 'INSERT') THEN
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); new_table;
ELSE
RETURN QUERY
SELECT
-1 AS count_diff,
*
FROM
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 END
$$; $$;
$b$, 'thing', table_name);
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)
@ -168,7 +148,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_thing_transition_tables (TG_OP) r.combine_transition_tables (TG_OP)
AS (count_diff bigint, AS (count_diff bigint,
thing_like thing_like) thing_like thing_like)
GROUP BY GROUP BY
@ -218,7 +198,7 @@ BEGIN
(comment).local, (comment).local,
sum(count_diff) AS comments sum(count_diff) AS comments
FROM FROM
r.combine_comment_transition_tables (TG_OP) r.combine_transition_tables (TG_OP)
AS (count_diff bigint, AS (count_diff bigint,
comment comment) comment comment)
WHERE WHERE
@ -321,7 +301,7 @@ BEGIN
(post).local, (post).local,
sum(count_diff) AS posts sum(count_diff) AS posts
FROM FROM
r.combine_post_transition_tables (TG_OP) r.combine_transition_tables (TG_OP)
AS (count_diff bigint, AS (count_diff bigint,
post post) post post)
WHERE WHERE
@ -377,7 +357,7 @@ BEGIN
SELECT SELECT
sum(count_diff) AS communities sum(count_diff) AS communities
FROM FROM
r.combine_community_transition_tables (TG_OP) r.combine_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;
@ -399,7 +379,7 @@ BEGIN
SELECT SELECT
sum(count_diff) AS users sum(count_diff) AS users
FROM FROM
r.combine_person_transition_tables (TG_OP) r.combine_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;
@ -470,7 +450,7 @@ BEGIN
(community_follower).community_id, (community_follower).community_id,
sum(count_diff) AS subscribers sum(count_diff) AS subscribers
FROM FROM
r.combine_community_follower_transition_tables (TG_OP) r.combine_transition_tables (TG_OP)
AS (count_diff bigint, community_follower community_follower) AS (count_diff bigint, community_follower community_follower)
WHERE ( WHERE (
SELECT SELECT