You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I have two tables linked by a foreign key the parent has a computed field, when performing a mutation the parent computed columns return their original values rather than the value post mutation.
I have confirmed the row is updated on refresh web page or when querying the database directly.
To Reproduce
Steps to reproduce the behavior:
CREATETABLEparent
(
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
count int2
);
CREATETABLEchild
(
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
parent_id uuid REFERENCES parent NOT NULL,
count int2
);
CREATE OR REPLACEFUNCTION_count(rec parent)
RETURNS smallint
STABLE
LANGUAGE sql
AS
$$
SELECTSUM(count)
FROM child
WHERE parent_id =rec.id
$$;
ncrmro
changed the title
Computed columns on related table are not recalculated when returned in mutation data.
Computed fields on related table are not recalculated when returned in mutation data.
Mar 7, 2023
Computed field functions written in SQL should be marked volatile to avoid stale results from mutations. This is a direct consequence of Postgres volatility category behaviour. Quoting from the docs:
For functions written in SQL or in any of the standard procedural languages, there is a second important property determined by the volatility category, namely the visibility of any data changes that have been made by the SQL command that is calling the function. A VOLATILE function will see such changes, a STABLE or IMMUTABLE function will not. This behavior is implemented using the snapshotting behavior of MVCC (see Chapter 13): STABLE and IMMUTABLE functions use a snapshot established as of the start of the calling query, whereas VOLATILE functions obtain a fresh snapshot at the start of each query they execute.
I have updated the docs in #443 with this suggestion.
In other words, to fix stale results, change your function to this:
CREATE OR REPLACEFUNCTION_count(rec parent)
RETURNS smallint
VOLATILE
LANGUAGE sql
AS
$$
SELECTSUM(count)
FROM child
WHERE parent_id =rec.id
$$;
Describe the bug
I have two tables linked by a foreign key the parent has a computed field, when performing a mutation the parent computed columns return their original values rather than the value post mutation.
I have confirmed the row is updated on refresh web page or when querying the database directly.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
I expect the related fields computed column to correctly return an updated value.
Screenshots
If applicable, add screenshots to help explain your problem.
Versions:
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: