Comments (4)
Does this also happen when you have the trigger on the table, but both tables are hidden and exposed via views, through which you access those tables?
If yes: What if the views have INSTEAD OF triggers managing the insert to the base table - does it still happen, then?
Another workaround could be to turn the after trigger into a before trigger, get a sequence number manually and set NEW.id
to it - then do the same "related insert" query.
Does it still happen if the CTE in which we do the INSERT is MATERIALIZED?
But overall, I don't really understand the use-case of such an after trigger, when everything happens on a table. I can't pass any data for the related insert, because everything is a table.
I have many similar situations, but in all of them I use a view, insert into the view - and then have an INSTEAD OF trigger, which runs both inserts (orders
and documents
in this case).
from postgrest.
As a disclaimer, this was an issue reported by a user in a Discord channel for Supabase. They mentioned that they can use workarounds, but was just curious to know why it behaved this way. The answer is mentioned in the linked PR and goes as follows:
"The sub-statements in WITH are executed concurrently with each other and with the main query", according to the PostgreSQL docs) and confirmed by this this PostgreSQL Q/A. This applies to how PostgREST inserts data and selects related tables afterwards.
Does this also happen when you have the trigger on the table, but both tables are hidden and exposed via views, through which you access those tables?
If yes: What if the views have INSTEAD OF triggers managing the insert to the base table - does it still happen, then?
Yes and yes. It behaves in the same way.
Another workaround could be to turn the after trigger into a before trigger, get a sequence number manually and set NEW.id to it - then do the same "related insert" query.
Not sure if it may work since the related insert still wouldn't have the newly inserted rows from the trigger. The results can't "see" each other between CTEs according to the docs (except when using RETURNING
).
Does it still happen if the CTE in which we do the INSERT is MATERIALIZED?
Using WITH ... AS MATERIALIZED ( INSERT INTO...)
right? Then yes, it still happens. It seems that it applies to all the WITH
sub-statements regardless.
But overall, I don't really understand the use-case of such an after trigger, when everything happens on a table.
Yes, I agree, it seems to be for a special case that uses only the ID of the base table (in the example by generating two documents
using default values and the ID from orders
). Nevertheless, it was an issue encountered in the wild by a user so I considered it was worth mentioning.
from postgrest.
The results can't "see" each other between CTEs according to the docs (except when using RETURNING).
Wait, but we are using RETURNING, right?
from postgrest.
Wait, but we are using RETURNING, right?
Ah yes, but we're only RETURNING
the base table columns, not the modified columns for the table inside the TRIGGER
. Something like this:
WITH pgrst_source AS (
INSERT INTO base_table (...)
SELECT ...
RETURNING * -- returns only columns from the base_table
)
SELECT *
-- pgrst_source "sees" the values of base_table inside the WITH due to the RETURNING
FROM pgrst_source
-- other_table doesn't "see" the change inside the trigger (any trigger I believe)
-- because it was done inside the WITH and it isn't RETURNING those values
JOIN other_table ...
Unless I misunderstood something.
from postgrest.
Related Issues (20)
- Wrong `503 Service Unavailable` on pg `53400` HOT 1
- Dependency Dashboard
- Clarify that domain representations need implicit casts
- Domain representations for RPC
- Static executables on other platforms: Native builds via Nix
- Static executables on other platforms: Cross compilation via Nix
- Hard to upgrade from `v9.0.1` HOT 8
- A "match" pattern inside an "or()" construct cannot contain parens HOT 2
- Cannot select field from inherited table HOT 2
- Channel retrying message doesn't doesn't follow log format HOT 3
- Re-Request mapping of internal variables for stored procedures HOT 2
- Responses are slow when the schema cache reloads HOT 3
- Stack Overflow error in dev enviroment when table and embed resource do not exist HOT 8
- Admin server `/ready` endpoint is incorrect on slow schema cache loads
- `pgrst.server_trace_header` in-database config not working HOT 1
- Filtering With Or Logical Operator on Embedded Resources Inconsistent Behavior
- Postgrest Server Does'nt respond for DB error SQL Error [57014]: ERROR: canceling statement due to statement timeout .
- Inserts and Updates now require an up-to-date schema cache HOT 2
- Building static executables directly via nix' `pkgsStatic.postgrest`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from postgrest.