diff --git a/src/backend/catalog/pg_depend.c b/src/backend/catalog/pg_depend.c
index 5366f7820c..8cbbb5393e 100644
--- a/src/backend/catalog/pg_depend.c
+++ b/src/backend/catalog/pg_depend.c
@@ -948,28 +948,51 @@ 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);
-		HeapTuple	ptup;
-
-		relid = llast_oid(ancestors);
-		ptup = SearchSysCacheAttName(relid, attname);
-		attnum = ((Form_pg_attribute) GETSTRUCT(ptup))->attnum;
-
-		ReleaseSysCache(ctup);
-		ReleaseSysCache(ptup);
-		list_free(ancestors);
+		List	   *ancestors;
+
+		/*
+		 * If the input relation is already the top-most parent, just check
+		 * itself.
+		*/
+		ancestors = get_partition_ancestors(relid);
+		if (ancestors != NIL)
+		{
+			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",
+								attnum, RelationGetRelationName(rel))));
+
+			attname = NameStr(((Form_pg_attribute) GETSTRUCT(ctup))->attname);
+			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)