fix: refresh_location_access_scoped_tables trigger fails on multi-row operations
Resolved 💬 3 comments Opened Nov 29, 2025 by evanmcgillivray Closed Jan 29, 2026
Problem
The refresh_location_access_scoped_tables trigger function fails when multiple rows are deleted/updated in a single transaction. The function creates a temp table _refresh_perms with ON COMMIT DROP, but when called multiple times within the same transaction, it fails with:
ERROR: relation "_refresh_perms" already exists
CONTEXT: SQL statement "CREATE TEMP TABLE _refresh_perms ON COMMIT DROP AS
SELECT * FROM public.location_access_permission_rows()
WHERE location_id = ANY(target_location_ids)"
PL/pgSQL function refresh_location_access_scoped_tables(uuid[]) line 33 at SQL statement
Impact
- Cannot delete multiple locations in a single DELETE statement
- Test cleanup fails when multiple test records need to be removed
- Any bulk operations on
locationstable will fail
Reproduction
DELETE FROM locations WHERE name LIKE 'Test Location%';
-- Fails if more than one row matches
Suggested Fix
Either:
- Use
CREATE TEMP TABLE IF NOT EXISTSand truncate before use - Use a unique table name per invocation (e.g., append a random suffix)
- Drop the table at the end of the function instead of relying on
ON COMMIT DROP - Use a CTE instead of a temp table
Location
- Function:
public.refresh_location_access_scoped_tables(uuid[]) - Migration:
20251219120000_materialize_location_access_scoped_tables.sql
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗