-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5396 from morozov/pgsql-identity
Autoincrement via identity columns on PostgreSQL
- Loading branch information
Showing
7 changed files
with
102 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
Migration to identity columns on PostgreSQL | ||
=========================================== | ||
|
||
As of version 4, the DBAL uses identity columns to implement the `autoincrement` | ||
behavior on PostgreSQL instead of `SERIAL*` column types. | ||
|
||
If you have a database with `autoincrement` columns created using DBAL 3 or earlier, | ||
you will need to perform the following schema migration before being able to continue managing | ||
the schema with the DBAL: | ||
|
||
1. Identify all `autoincrement` columns and their tables. | ||
2. Create the `upgrade_serial_to_identity()` function in the database as described in | ||
`PostgreSQL 10 identity columns explained <https://www.2ndquadrant.com/en/blog/postgresql-10-identity-columns/>`_: | ||
|
||
.. code-block:: sql | ||
CREATE OR REPLACE FUNCTION upgrade_serial_to_identity(tbl regclass, col name) | ||
RETURNS void | ||
LANGUAGE plpgsql | ||
AS $$ | ||
DECLARE | ||
colnum smallint; | ||
seqid oid; | ||
count int; | ||
BEGIN | ||
-- find column number | ||
SELECT attnum INTO colnum FROM pg_attribute WHERE attrelid = tbl AND attname = col; | ||
IF NOT FOUND THEN | ||
RAISE EXCEPTION 'column does not exist'; | ||
END IF; | ||
-- find sequence | ||
SELECT INTO seqid objid | ||
FROM pg_depend | ||
WHERE (refclassid, refobjid, refobjsubid) = ('pg_class'::regclass, tbl, colnum) | ||
AND classid = 'pg_class'::regclass AND objsubid = 0 | ||
AND deptype = 'a'; | ||
GET DIAGNOSTICS count = ROW_COUNT; | ||
IF count < 1 THEN | ||
RAISE EXCEPTION 'no linked sequence found'; | ||
ELSIF count > 1 THEN | ||
RAISE EXCEPTION 'more than one linked sequence found'; | ||
END IF; | ||
-- drop the default | ||
EXECUTE 'ALTER TABLE ' || tbl || ' ALTER COLUMN ' || quote_ident(col) || ' DROP DEFAULT'; | ||
-- 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'; | ||
-- mark the column as identity column | ||
UPDATE pg_attribute | ||
SET attidentity = 'd' | ||
WHERE attrelid = tbl | ||
AND attname = col; | ||
END; | ||
$$; | ||
3. For each column and their table, run `upgrade_serial_to_identity(<table>, <column>)`. | ||
|
||
Without this migration, next time when DBAL 4 is used to manage the schema, it will perform a similar migration | ||
but instead of reusing the existing sequence, it will drop it and create a new one. As a result, | ||
all new sequence numbers will be generated from `1`, which is most likely undesired. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,3 +21,5 @@ | |
reference/testing | ||
|
||
explanation/implicit-indexes | ||
|
||
how-to/postgresql-identity-migration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters