diff options
author | Michael Paquier <michael@paquier.xyz> | 2021-11-25 15:05:31 +0900 |
---|---|---|
committer | Michael Paquier <michael@paquier.xyz> | 2021-11-25 15:05:31 +0900 |
commit | 216156fec3d885842e2ceffa3a5321fbcd2fe575 (patch) | |
tree | 176bb11ec9ae18d4f748e0bec8d3fbdc9d98fe06 /src/backend/commands/tablecmds.c | |
parent | 6365e3a0535fc5e6cd55ec4a97c562a11e371fdd (diff) | |
download | postgresql-216156fec3d885842e2ceffa3a5321fbcd2fe575.tar.gz postgresql-216156fec3d885842e2ceffa3a5321fbcd2fe575.zip |
Block ALTER TABLE .. DROP NOT NULL on columns in replica identity index
Replica identities that depend directly on an index rely on a set of
properties, one of them being that all the columns defined in this index
have to be marked as NOT NULL. There was a hole in the logic with ALTER
TABLE DROP NOT NULL, where it was possible to remove the NOT NULL
property of a column part of an index used as replica identity, so block
it to avoid problems with logical decoding down the road.
The same check was already done columns part of a primary key, so the
fix is straight-forward.
Author: Haiying Tang, Hou Zhijie
Reviewed-by: Dilip Kumar, Michael Paquier
Discussion: https://postgr.es/m/OS0PR01MB6113338C102BEE8B2FFC5BD9FB619@OS0PR01MB6113.jpnprd01.prod.outlook.com
Backpatch-through: 10
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r-- | src/backend/commands/tablecmds.c | 31 |
1 files changed, 22 insertions, 9 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index dc0001a089e..57056744927 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -6364,7 +6364,8 @@ ATExecDropNotNull(Relation rel, const char *colName, LOCKMODE lockmode) colName, RelationGetRelationName(rel)))); /* - * Check that the attribute is not in a primary key + * Check that the attribute is not in a primary key or in an index used as + * a replica identity. * * Note: we'll throw error even if the pkey index is not valid. */ @@ -6384,20 +6385,32 @@ ATExecDropNotNull(Relation rel, const char *colName, LOCKMODE lockmode) elog(ERROR, "cache lookup failed for index %u", indexoid); indexStruct = (Form_pg_index) GETSTRUCT(indexTuple); - /* If the index is not a primary key, skip the check */ - if (indexStruct->indisprimary) + /* + * If the index is not a primary key or an index used as replica + * identity, skip the check. + */ + if (indexStruct->indisprimary || indexStruct->indisreplident) { /* - * Loop over each attribute in the primary key and see if it - * matches the to-be-altered attribute + * Loop over each attribute in the primary key or the index used + * as replica identity and see if it matches the to-be-altered + * attribute. */ for (i = 0; i < indexStruct->indnkeyatts; i++) { if (indexStruct->indkey.values[i] == attnum) - ereport(ERROR, - (errcode(ERRCODE_INVALID_TABLE_DEFINITION), - errmsg("column \"%s\" is in a primary key", - colName))); + { + if (indexStruct->indisprimary) + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("column \"%s\" is in a primary key", + colName))); + else + ereport(ERROR, + (errcode(ERRCODE_INVALID_TABLE_DEFINITION), + errmsg("column \"%s\" is in index used as replica identity", + colName))); + } } } |