Implement get_relation_constraint_oid_compat helper (#3836)

pull/3842/head
Onur Tirtir 2020-05-15 17:36:59 +03:00 committed by GitHub
parent 4ad10adbc4
commit 8f9ef63e8a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 27 additions and 3 deletions

View File

@ -42,6 +42,7 @@ static void ForeignConstraintFindDistKeys(HeapTuple pgConstraintTuple,
Var *referencedDistColumn, Var *referencedDistColumn,
int *referencingAttrIndex, int *referencingAttrIndex,
int *referencedAttrIndex); int *referencedAttrIndex);
static Oid get_relation_constraint_oid_compat(HeapTuple heapTuple);
/* /*
* ConstraintIsAForeignKeyToReferenceTable checks if the given constraint is a * ConstraintIsAForeignKeyToReferenceTable checks if the given constraint is a
@ -514,9 +515,7 @@ GetTableForeignConstraintCommands(Oid relationId)
if (!inheritedConstraint && constraintForm->contype == CONSTRAINT_FOREIGN) if (!inheritedConstraint && constraintForm->contype == CONSTRAINT_FOREIGN)
{ {
Oid constraintId = get_relation_constraint_oid(relationId, Oid constraintId = get_relation_constraint_oid_compat(heapTuple);
constraintForm->conname.data,
true);
char *statementDef = pg_get_constraintdef_command(constraintId); char *statementDef = pg_get_constraintdef_command(constraintId);
tableForeignConstraints = lappend(tableForeignConstraints, statementDef); tableForeignConstraints = lappend(tableForeignConstraints, statementDef);
@ -536,6 +535,31 @@ GetTableForeignConstraintCommands(Oid relationId)
} }
/*
* get_relation_constraint_oid_compat returns OID of the constraint represented
* by the constraintForm, which is passed as an heapTuple. OID of the contraint
* is already stored in the constraintForm struct if major PostgreSQL version is
* 12. However, in the older versions, we should utilize HeapTupleGetOid to deduce
* that OID with no cost.
*/
static Oid
get_relation_constraint_oid_compat(HeapTuple heapTuple)
{
Assert(heapTuple != NULL);
Oid constraintOid = InvalidOid;
#if PG_VERSION_NUM >= PG_VERSION_12
Form_pg_constraint constraintForm = (Form_pg_constraint) GETSTRUCT(heapTuple);
constraintOid = constraintForm->oid;
#else
constraintOid = HeapTupleGetOid(heapTuple);
#endif
return constraintOid;
}
/* /*
* HasForeignKeyToReferenceTable function scans the pgConstraint table to * HasForeignKeyToReferenceTable function scans the pgConstraint table to
* fetch all of the constraints on the given relationId and see if at least one * fetch all of the constraints on the given relationId and see if at least one