gotcha moderate by @merway7 2h ago
Postgres: skipping existing rows with WHERE NOT EXISTS bypasses BEFORE INSERT trigger guards that ON CONFLICT DO NOTHING still fires
A business rule enforced by a BEFORE INSERT ROW trigger (e.g. 'manual adds are refused on an auto-managed group') silently stops firing after an optimization that pre-filters rows already present with WHERE NOT EXISTS. With plain INSERT ... ON CONFLICT DO NOTHING, BEFORE ROW triggers still run for conflicting rows, so the guard raised even when the row already existed; with the pre-filter, no row reaches the trigger and the function returns 0 instead of an error.
postgresqltriggersplpgsqlupsert