Skip to content

Commit

Permalink
[yugabyte#24981] YSQL: Fix schema version mismatch error after failed…
Browse files Browse the repository at this point in the history
… ALTER TABLE
  • Loading branch information
arpang committed Nov 26, 2024
1 parent 1783a11 commit fb532c1
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 11 deletions.
10 changes: 4 additions & 6 deletions src/postgres/src/backend/commands/tablecmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -5220,9 +5220,8 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE lockmode,
AT_NUM_PASSES,
main_relid,
&rollbackHandle,
false /* isPartitionOfAlteredTable */);
if (handles)
*ybAlteredTableIds = lappend_oid(*ybAlteredTableIds, main_relid);
false /* isPartitionOfAlteredTable */,
ybAlteredTableIds);
if (rollbackHandle)
*rollbackHandles = lappend(*rollbackHandles, rollbackHandle);

Expand All @@ -5246,9 +5245,8 @@ ATRewriteCatalogs(List **wqueue, LOCKMODE lockmode,
AT_NUM_PASSES,
childrelid,
&childRollbackHandle,
true /*isPartitionOfAlteredTable */);
if (child_handles)
*ybAlteredTableIds = lappend_oid(*ybAlteredTableIds, childrelid);
true /*isPartitionOfAlteredTable */,
ybAlteredTableIds);
ListCell *listcell = NULL;
foreach(listcell, child_handles)
{
Expand Down
12 changes: 8 additions & 4 deletions src/postgres/src/backend/commands/ybccmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -1215,7 +1215,8 @@ static List*
YBCPrepareAlterTableCmd(AlterTableCmd* cmd, Relation rel, List *handles,
int* col, bool* needsYBAlter,
YBCPgStatement* rollbackHandle,
bool isPartitionOfAlteredTable)
bool isPartitionOfAlteredTable,
List *volatile *ybAlteredTableIds)
{
Oid relationId = RelationGetRelid(rel);
Oid relfileNodeId = YbGetRelfileNodeId(rel);
Expand Down Expand Up @@ -1609,6 +1610,7 @@ YBCPrepareAlterTableCmd(AlterTableCmd* cmd, Relation rel, List *handles,
HandleYBStatus(
YBCPgAlterTableIncrementSchemaVersion(alter_cmd_handle));
handles = lappend(handles, alter_cmd_handle);
*ybAlteredTableIds = lappend_oid(*ybAlteredTableIds, relationId);
table_close(dependent_rel, AccessExclusiveLock);
}
*needsYBAlter = true;
Expand Down Expand Up @@ -1665,7 +1667,8 @@ YBCPrepareAlterTable(List** subcmds,
int subcmds_size,
Oid relationId,
YBCPgStatement *rollbackHandle,
bool isPartitionOfAlteredTable)
bool isPartitionOfAlteredTable,
List *volatile *ybAlteredTableIds)
{
/* Appropriate lock was already taken */
Relation rel = relation_open(relationId, NoLock);
Expand Down Expand Up @@ -1693,7 +1696,8 @@ YBCPrepareAlterTable(List** subcmds,
handles = YBCPrepareAlterTableCmd(
(AlterTableCmd *) lfirst(lcmd), rel, handles,
&col, &needsYBAlter, rollbackHandle,
isPartitionOfAlteredTable);
isPartitionOfAlteredTable,
ybAlteredTableIds);
}
}
relation_close(rel, NoLock);
Expand All @@ -1702,7 +1706,7 @@ YBCPrepareAlterTable(List** subcmds,
{
return NULL;
}

*ybAlteredTableIds = lappend_oid(*ybAlteredTableIds, relationId);
return handles;
}

Expand Down
3 changes: 2 additions & 1 deletion src/postgres/src/include/commands/ybccmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ extern List* YBCPrepareAlterTable(List** subcmds,
int subcmds_size,
Oid relationId,
YBCPgStatement *rollbackHandle,
bool isPartitionOfAlteredTable);
bool isPartitionOfAlteredTable,
List *volatile *ybAlteredTableIds);

extern void YBCExecAlterTable(YBCPgStatement handle, Oid relationId);

Expand Down
16 changes: 16 additions & 0 deletions src/postgres/src/test/regress/expected/yb_alter_table.out
Original file line number Diff line number Diff line change
Expand Up @@ -493,3 +493,19 @@ ERROR: check constraint "test_validate_constraint_part_check" of relation "test
DELETE FROM test_validate_constraint_part WHERE a % 2 = 1;
ALTER TABLE test_validate_constraint_part
VALIDATE CONSTRAINT test_validate_constraint_part_check;
-- validate fix for "schema version mismatch" after failed ALTER TABLE.
CREATE TABLE pk(a int primary key);
INSERT INTO pk values (1);
CREATE TABLE fk(a int);
INSERT INTO fk values (2);
ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk; -- should fail due to FK constraint violation.
ERROR: insert or update on table "fk" violates foreign key constraint "fk_a_fkey"
DETAIL: Key (a)=(2) is not present in table "pk".
BEGIN;
SELECT * from pk;
a
---
1
(1 row)

COMMIT;
10 changes: 10 additions & 0 deletions src/postgres/src/test/regress/sql/yb_alter_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -222,3 +222,13 @@ ALTER TABLE test_validate_constraint_part
DELETE FROM test_validate_constraint_part WHERE a % 2 = 1;
ALTER TABLE test_validate_constraint_part
VALIDATE CONSTRAINT test_validate_constraint_part_check;

-- validate fix for "schema version mismatch" after failed ALTER TABLE.
CREATE TABLE pk(a int primary key);
INSERT INTO pk values (1);
CREATE TABLE fk(a int);
INSERT INTO fk values (2);
ALTER TABLE fk ADD FOREIGN KEY (a) REFERENCES pk; -- should fail due to FK constraint violation.
BEGIN;
SELECT * from pk;
COMMIT;

0 comments on commit fb532c1

Please sign in to comment.