Cover DROP COLUMN commands dropping columns in the either side of fkeys

pull/4453/head
Onur Tirtir 2020-12-25 10:35:09 +03:00
parent f29d7f1983
commit db5bae8ffa
3 changed files with 27 additions and 1 deletions

View File

@ -483,6 +483,21 @@ ForeignConstraintFindDistKeys(HeapTuple pgConstraintTuple,
} }
/*
* ColumnAppearsInForeignKey returns true if there is a foreign key constraint
* from/to given column.
*/
bool
ColumnAppearsInForeignKey(char *columnName, Oid relationId)
{
int searchForeignKeyColumnFlags = SEARCH_REFERENCING_RELATION |
SEARCH_REFERENCED_RELATION;
List *foreignKeyIdsColumnAppeared =
GetForeignKeyIdsForColumn(columnName, relationId, searchForeignKeyColumnFlags);
return list_length(foreignKeyIdsColumnAppeared) > 0;
}
/* /*
* ColumnAppearsInForeignKeyToReferenceTable checks if there is a foreign key * ColumnAppearsInForeignKeyToReferenceTable checks if there is a foreign key
* constraint from/to any reference table on the given column. * constraint from/to any reference table on the given column.

View File

@ -1099,6 +1099,17 @@ AlterTableCmdDropsFkey(AlterTableCmd *command, Oid relationId)
} }
} }
} }
else if (alterTableType == AT_DropColumn)
{
char *columnName = command->name;
if (ColumnAppearsInForeignKey(columnName, relationId))
{
return true;
}
}
return false;
}
/* /*

View File

@ -159,7 +159,7 @@ extern void ErrorIfUnsupportedForeignConstraintExists(Relation relation,
Var *distributionColumn, Var *distributionColumn,
uint32 colocationId); uint32 colocationId);
extern void ErrorOutForFKeyBetweenPostgresAndCitusLocalTable(Oid localTableId); extern void ErrorOutForFKeyBetweenPostgresAndCitusLocalTable(Oid localTableId);
extern bool ColumnReferencedByAnyForeignKey(char *columnName, Oid relationId); extern bool ColumnAppearsInForeignKey(char *columnName, Oid relationId);
extern bool ColumnAppearsInForeignKeyToReferenceTable(char *columnName, Oid extern bool ColumnAppearsInForeignKeyToReferenceTable(char *columnName, Oid
relationId); relationId);
extern List * GetReferencingForeignConstaintCommands(Oid relationOid); extern List * GetReferencingForeignConstaintCommands(Oid relationOid);