Files
postcard-api/migrations/003_functions.sql
T
2026-03-22 12:57:16 -05:00

14 lines
342 B
PL/PgSQL

-- Atomic sequence increment for message ordering
CREATE OR REPLACE FUNCTION next_message_sequence(conv_id UUID)
RETURNS BIGINT AS $$
DECLARE
seq BIGINT;
BEGIN
UPDATE conversations
SET next_sequence = next_sequence + 1, updated_at = now()
WHERE id = conv_id
RETURNING next_sequence INTO seq;
RETURN seq;
END;
$$ LANGUAGE plpgsql;