PG Bug reporting form <noreply(at)postgresql(dot)org> writes:
> ALTER TABLE IF EXIST foo ADD COLUMN IF NOT EXISTS id BIGSERIAL PRIMARY KEY;
> Fails if "id" exists and is already the primary key.
Yeah, known problem. This is fixed in v13, but the fix seemed too
invasive for a back-patch (see [1] and follow-on fixes).
regards, tom lane
[1] https://git.postgresql.org/gitweb/?p=postgresql.git&a=commitdiff&h=1281a5c90