Track daily counters keyed by «user + day»
Analytics writes a per-user daily activity counter: the pair (user, day) is unique, and every event either creates a row with one or increments the existing one. Here the primary key is composite, so the "insert-with-update" conflict clause must reference both columns at once — that's the difference from the classic single-column-PK case.
The table daily_count(user_id, day, n DEFAULT 0, PRIMARY KEY (user_id, day)) is empty. Run three insert attempts on the pair (1, '2024-01-10') and two on (1, '2024-01-11') — on conflict by this pair the existing n goes up by one. After the runs the pair (1, '2024-01-10') should have n = 3 and (1, '2024-01-11') — n = 2.
Just INSERT/UPDATE/DELETE — no trailing SELECT needed.
Sign in to see submission history
Sign inSign in to use AI Mentor
Sign in