diff --git a/src/backend/catalog/pg_depend.c b/src/backend/catalog/pg_depend.c
index 5366f7820c..8b5b103c16 100644
--- a/src/backend/catalog/pg_depend.c
+++ b/src/backend/catalog/pg_depend.c
@@ -948,28 +948,43 @@ getIdentitySequence(Relation rel, AttrNumber attnum, bool missing_ok)
 	Oid			relid;
 	List	   *seqlist;
 
+	relid = RelationGetRelid(rel);
+
 	/*
 	 * The identity sequence is associated with the topmost partitioned table,
 	 * which might have column order different than the given partition.
 	 */
 	if (RelationGetForm(rel)->relispartition)
 	{
-		List	   *ancestors =
-			get_partition_ancestors(RelationGetRelid(rel));
-		HeapTuple	ctup = SearchSysCacheAttNum(RelationGetRelid(rel), attnum);
-		const char *attname = NameStr(((Form_pg_attribute) GETSTRUCT(ctup))->attname);
+		List	   *ancestors;
+		HeapTuple	ctup;
 		HeapTuple	ptup;
+		const char *attname;
+
+		ctup = SearchSysCacheAttNum(relid, attnum);
+		if (!HeapTupleIsValid(ctup))
+			ereport(ERROR,
+					(errcode(ERRCODE_UNDEFINED_COLUMN),
+					 errmsg("column number %d of relation \"%s\" does not exist",
+							colNum, RelationGetRelationName(rel))));
 
+		attname = NameStr(((Form_pg_attribute) GETSTRUCT(ctup))->attname);
+		ancestors = get_partition_ancestors(relid);
 		relid = llast_oid(ancestors);
+
 		ptup = SearchSysCacheAttName(relid, attname);
+		if (!HeapTupleIsValid(ptup))
+			ereport(ERROR,
+					(errcode(ERRCODE_UNDEFINED_COLUMN),
+					errmsg("column \"%s\" of relation \"%s\" does not exist",
+							attname, RelationGetRelationName(rel))));
+
 		attnum = ((Form_pg_attribute) GETSTRUCT(ptup))->attnum;
 
 		ReleaseSysCache(ctup);
 		ReleaseSysCache(ptup);
 		list_free(ancestors);
 	}
-	else
-		relid = RelationGetRelid(rel);
 
 	seqlist = getOwnedSequences_internal(relid, attnum, DEPENDENCY_INTERNAL);
 	if (list_length(seqlist) > 1)