Why don't you add a serial
primary key column to the table?
ALTER TABLE login ADD column login_id serial;
ALTER TABLE login ADD CONSTRAINT login_pkey PRIMARY KEY(login_id);
The first operation will rewrite the table and take a lock for some time. I would then run
VACCUM FULL ANALYZE login;
Inferior alternatives: row_number()
as pointed out by @Joachim. For maximum performance you can leave the OVER
clause empty:
row_number() OVER () AS rn
Aside: use the AS
keyword for column aliases (while they are just noise for table aliases).
Or you can use the ctid
as poor man's surrogate for a primary key. That would be even faster:
Details:
In-order sequence generation
Example on dba.SE:
numbering rows consecutively for a number of tables