diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 721d2478..491ba87c 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -9757,6 +9757,39 @@ ATAddForeignKeyConstraint(List **wqueue, AlteredTableInfo *tab, Relation rel,
 				(errcode(ERRCODE_INVALID_FOREIGN_KEY),
 				 errmsg("number of referencing and referenced columns for foreign key disagree")));
 
+	for (i = 0; i < numpks; i++)
+	{
+		int32		keycoltypmod;
+		Oid			keycoltype;
+		Oid			keycolcollation;
+
+		get_atttypetypmodcoll(RelationGetRelid(pkrel), pkattnum[i],
+								&keycoltype, &keycoltypmod,
+								&keycolcollation);
+		if (OidIsValid(keycolcollation) &&
+				!get_collation_isdeterministic(keycolcollation))
+			ereport(ERROR,
+					(errcode(ERRCODE_DATATYPE_MISMATCH),
+						 errmsg("foreign key constraint \"%s\" cannot be implemented",
+								fkconstraint->conname),
+						 errdetail("Cannot have indeterministic collations in primary key for referenced table \"%s\"",
+								RelationGetRelationName(pkrel)),
+						 errhint("You may make the primary key collations deterministic.")));
+
+		get_atttypetypmodcoll(RelationGetRelid(rel), fkattnum[i],
+								&keycoltype, &keycoltypmod,
+								&keycolcollation);
+		if (OidIsValid(keycolcollation) &&
+				!get_collation_isdeterministic(keycolcollation))
+			ereport(ERROR,
+					(errcode(ERRCODE_DATATYPE_MISMATCH),
+					 errmsg("foreign key constraint \"%s\" cannot be implemented",
+							fkconstraint->conname),
+					 errdetail("Foreign key cannot use indeterministic collations for referencing table \"%s\"",
+						RelationGetRelationName(rel)),
+					 errhint("You may make the foreign key collations deterministic.")));
+	}
+
 	/*
 	 * On the strength of a previous constraint, we might avoid scanning
 	 * tables to validate this one.  See below.
