PostgreSQL
 sql >> Base de données >  >> RDS >> PostgreSQL

Postgres 9.5 EN CONFLIT DO SELECT

Vous pouvez utiliser CTE :

WITH cte AS (
   INSERT INTO "user"(timestamp, user_id, member_id)
   values ($1, $2, $3)
   ON CONFLICT (user_id, member_id) DO NOTHING
   RETURNING user_id
)
SELECT NULL AS result
WHERE EXISTS (SELECT 1 FROM cte)          -- success
UNION ALL
SELECT id 
FROM "user" 
WHERE user_id = $2 
  AND NOT EXISTS (SELECT 1 FROM cte);     -- conflict

Démo DBFiddle