mirror of https://github.com/citusdata/citus.git
Refactor the code that prepares constraint objects in an alter table stmt into a func
parent
613cced1ae
commit
ba1ea9b5bd
|
@ -1028,30 +1028,7 @@ PreprocessAlterTableAddConstraint(AlterTableStmt *alterTableStatement, Oid
|
||||||
relationId,
|
relationId,
|
||||||
Constraint *constraint)
|
Constraint *constraint)
|
||||||
{
|
{
|
||||||
/*
|
PrepareAlterTableStmtForConstraint(alterTableStatement, relationId, constraint);
|
||||||
* We should only preprocess an ADD CONSTRAINT command if we have empty conname
|
|
||||||
* This only happens when we have to create a constraint name in citus since the client does
|
|
||||||
* not specify a name.
|
|
||||||
* indexname should also be NULL to make sure this is not an
|
|
||||||
* ADD {PRIMARY KEY, UNIQUE} USING INDEX command
|
|
||||||
* which doesn't need a conname since the indexname will be used
|
|
||||||
*/
|
|
||||||
Assert(constraint->conname == NULL && constraint->indexname == NULL);
|
|
||||||
|
|
||||||
Relation rel = RelationIdGetRelation(relationId);
|
|
||||||
|
|
||||||
/*
|
|
||||||
* Change the alterTableCommand so that the standard utility
|
|
||||||
* hook runs it with the name we created.
|
|
||||||
*/
|
|
||||||
|
|
||||||
constraint->conname = GenerateConstraintName(RelationGetRelationName(rel),
|
|
||||||
RelationGetNamespace(rel),
|
|
||||||
constraint);
|
|
||||||
|
|
||||||
RelationClose(rel);
|
|
||||||
|
|
||||||
SwitchToSequentialAndLocalExecutionIfConstraintNameTooLong(relationId, constraint);
|
|
||||||
|
|
||||||
char *ddlCommand = DeparseTreeNode((Node *) alterTableStatement);
|
char *ddlCommand = DeparseTreeNode((Node *) alterTableStatement);
|
||||||
|
|
||||||
|
@ -1067,11 +1044,6 @@ PreprocessAlterTableAddConstraint(AlterTableStmt *alterTableStatement, Oid
|
||||||
Oid rightRelationId = RangeVarGetRelid(constraint->pktable, NoLock,
|
Oid rightRelationId = RangeVarGetRelid(constraint->pktable, NoLock,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
if (IsCitusTableType(rightRelationId, REFERENCE_TABLE))
|
|
||||||
{
|
|
||||||
EnsureSequentialModeForAlterTableOperation();
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* If one of the relations involved in the FOREIGN KEY constraint is not a distributed table, citus errors out eventually.
|
* If one of the relations involved in the FOREIGN KEY constraint is not a distributed table, citus errors out eventually.
|
||||||
* PreprocessAlterTableStmt function returns an empty tasklist in those cases.
|
* PreprocessAlterTableStmt function returns an empty tasklist in those cases.
|
||||||
|
@ -1099,6 +1071,47 @@ PreprocessAlterTableAddConstraint(AlterTableStmt *alterTableStatement, Oid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
* PrepareAlterTableStmtForConstraint assigns a name to the constraint if it
|
||||||
|
* does not have one and switches to sequential and local execution if the
|
||||||
|
* constraint name is too long.
|
||||||
|
*/
|
||||||
|
void
|
||||||
|
PrepareAlterTableStmtForConstraint(AlterTableStmt *alterTableStatement,
|
||||||
|
Oid relationId,
|
||||||
|
Constraint *constraint)
|
||||||
|
{
|
||||||
|
if (constraint->conname == NULL && constraint->indexname == NULL)
|
||||||
|
{
|
||||||
|
Relation rel = RelationIdGetRelation(relationId);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Change the alterTableCommand so that the standard utility
|
||||||
|
* hook runs it with the name we created.
|
||||||
|
*/
|
||||||
|
|
||||||
|
constraint->conname = GenerateConstraintName(RelationGetRelationName(rel),
|
||||||
|
RelationGetNamespace(rel),
|
||||||
|
constraint);
|
||||||
|
|
||||||
|
RelationClose(rel);
|
||||||
|
}
|
||||||
|
|
||||||
|
SwitchToSequentialAndLocalExecutionIfConstraintNameTooLong(relationId, constraint);
|
||||||
|
|
||||||
|
if (constraint->contype == CONSTR_FOREIGN)
|
||||||
|
{
|
||||||
|
Oid rightRelationId = RangeVarGetRelid(constraint->pktable, NoLock,
|
||||||
|
false);
|
||||||
|
|
||||||
|
if (IsCitusTableType(rightRelationId, REFERENCE_TABLE))
|
||||||
|
{
|
||||||
|
EnsureSequentialModeForAlterTableOperation();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* PreprocessAlterTableStmt determines whether a given ALTER TABLE statement
|
* PreprocessAlterTableStmt determines whether a given ALTER TABLE statement
|
||||||
* involves a distributed table. If so (and if the statement does not use
|
* involves a distributed table. If so (and if the statement does not use
|
||||||
|
|
|
@ -582,6 +582,8 @@ extern bool ShouldEnableLocalReferenceForeignKeys(void);
|
||||||
extern List * PreprocessAlterTableStmtAttachPartition(AlterTableStmt *alterTableStatement,
|
extern List * PreprocessAlterTableStmtAttachPartition(AlterTableStmt *alterTableStatement,
|
||||||
const char *queryString);
|
const char *queryString);
|
||||||
extern List * PostprocessAlterTableSchemaStmt(Node *node, const char *queryString);
|
extern List * PostprocessAlterTableSchemaStmt(Node *node, const char *queryString);
|
||||||
|
extern void PrepareAlterTableStmtForConstraint(AlterTableStmt *alterTableStatement,
|
||||||
|
Oid relationId, Constraint *constraint);
|
||||||
extern List * PreprocessAlterTableStmt(Node *node, const char *alterTableCommand,
|
extern List * PreprocessAlterTableStmt(Node *node, const char *alterTableCommand,
|
||||||
ProcessUtilityContext processUtilityContext);
|
ProcessUtilityContext processUtilityContext);
|
||||||
extern List * PreprocessAlterTableMoveAllStmt(Node *node, const char *queryString,
|
extern List * PreprocessAlterTableMoveAllStmt(Node *node, const char *queryString,
|
||||||
|
|
Loading…
Reference in New Issue