On preparation to the release of Doctrine DBAL 4, I want to migrate the identity column of my autoincrement based tables to "serial" type columns, as described here:
Migration to identity columns on PostgreSQL
There is a nice script included and everything.
The problem is that this script runs this:
-- change the dependency between column and sequence to internal
UPDATE pg_depend
SET deptype = 'i'
WHERE (classid, objid, objsubid) = ('pg_class'::regclass, seqid, 0)
AND deptype = 'a';
And since my db is hosted as managed database on DigitalOcean, my doadmin
user doesn’t have enough privileges to modify this table directly:
ERROR: permission denied for table pg_depend CONTEXT: SQL statement "UPDATE pg_depend SET deptype = 'i' WHERE (classid, objid, objsubid) = ('pg_class'::regclass, seqid, 0) AND deptype = 'a'" PL/pgSQL function upgrade_serial_to_identity(regclass,name) line 31 at SQL statement
How can I perform this migration, without having access to change this user’s privileges or this table permissions?
2
Answers
Using Laurenz's excellent advice in the other answer, I wrapped the whole thing into a function, so it's easier to update the couple dozens of tables in my applications.
Luckily the auto-increment ids were the minority!
Now to update table
xxx
, with columnid
I just execute:select serial_to_identity('xxx', 'id')
.Seems to be working.
Modifying catalog tables like that is dangerous and likely to lead to data corruption. Why so complicated, if there is a fast and simple way to do it?
Given a table like this:
The quick and clean way to switch to using an identity column is the following:
Now the table is fine: