diff --git a/contrib/hstore/hstore_io.c b/contrib/hstore/hstore_io.c
index e03005c9232b1244738c21e17f05224472820919..8b44b290e4d82ad3737abdd3e458dca475f1cac0 100644
--- a/contrib/hstore/hstore_io.c
+++ b/contrib/hstore/hstore_io.c
@@ -340,7 +340,8 @@ hstoreUniquePairs(Pairs *a, int32 l, int32 *buflen)
 		{
 			*buflen += res->keylen + ((res->isnull) ? 0 : res->vallen);
 			res++;
-			memcpy(res, ptr, sizeof(Pairs));
+			if (res != ptr)
+				memcpy(res, ptr, sizeof(Pairs));
 		}
 
 		ptr++;