diff --git a/contrib/dblink/dblink.c b/contrib/dblink/dblink.c
index 9613f881985..29c19e0017f 100644
--- a/contrib/dblink/dblink.c
+++ b/contrib/dblink/dblink.c
@@ -1105,7 +1105,9 @@ storeQueryResult(storeInfo *sinfo, PGconn *conn, const char *sql)
 	PGresult   *res;
 
 	if (!PQsendQuery(conn, sql))
-		elog(ERROR, "could not send query: %s", pchomp(PQerrorMessage(conn)));
+		ereport(ERROR,
+				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+				 errmsg("could not send query: %s", pchomp(PQerrorMessage(conn)))));
 
 	if (!PQsetSingleRowMode(conn))	/* shouldn't fail */
 		elog(ERROR, "failed to set single-row mode for dblink query");
diff --git a/src/backend/access/brin/brin_bloom.c b/src/backend/access/brin/brin_bloom.c
index 5558a2edea5..788d89bbe82 100644
--- a/src/backend/access/brin/brin_bloom.c
+++ b/src/backend/access/brin/brin_bloom.c
@@ -342,8 +342,10 @@ bloom_init(int ndistinct, double false_positive_rate)
 	 * filters that are small individually, but too large when combined.
 	 */
 	if (nbytes > BloomMaxFilterSize)
-		elog(ERROR, "the bloom filter is too large (%d > %zu)", nbytes,
-			 BloomMaxFilterSize);
+		ereport(ERROR,
+				(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
+				 errmsg("the bloom filter is too large (%d > %zu)", nbytes,
+						BloomMaxFilterSize)));
 
 	/*
 	 * We allocate the whole filter. Most of it is going to be 0 bits, so the
diff --git a/src/backend/catalog/objectaddress.c b/src/backend/catalog/objectaddress.c
index 09c9c751133..75aa4b9e714 100644
--- a/src/backend/catalog/objectaddress.c
+++ b/src/backend/catalog/objectaddress.c
@@ -2819,7 +2819,8 @@ get_object_property_data(Oid class_id)
 	}
 
 	ereport(ERROR,
-			(errmsg_internal("unrecognized class ID: %u", class_id)));
+			(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+			 errmsg("unrecognized class ID: %u", class_id)));
 
 	return NULL;				/* keep MSC compiler happy */
 }
@@ -4728,7 +4729,9 @@ getObjectTypeDescription(const ObjectAddress *object, bool missing_ok)
 			break;
 
 		default:
-			elog(ERROR, "unsupported object class: %u", object->classId);
+			ereport(ERROR,
+					(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+					 errmsg("unsupported object class: %u", object->classId)));
 	}
 
 	/* the result can never be empty */
diff --git a/src/backend/commands/functioncmds.c b/src/backend/commands/functioncmds.c
index 4d46013d7de..1f4c0b156b0 100644
--- a/src/backend/commands/functioncmds.c
+++ b/src/backend/commands/functioncmds.c
@@ -2348,7 +2348,9 @@ ExecuteCallStmt(CallStmt *stmt, ParamListInfo params, bool atomic, DestReceiver
 		TupleTableSlot *slot;
 
 		if (fcinfo->isnull)
-			elog(ERROR, "procedure returned null record");
+			ereport(ERROR,
+					(errcode(ERRCODE_INVALID_OBJECT_DEFINITION),
+					 errmsg("procedure returned null record")));
 
 		/*
 		 * Ensure there's an active snapshot whilst we execute whatever's
diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c
index adbafd0ecd3..4e4424f98d7 100644
--- a/src/backend/utils/adt/jsonfuncs.c
+++ b/src/backend/utils/adt/jsonfuncs.c
@@ -5611,7 +5611,9 @@ parse_jsonb_index_flags(Jsonb *jb)
 
 	/* expect end of array now */
 	if (type != WJB_END_ARRAY)
-		elog(ERROR, "unexpected end of flag array");
+		ereport(ERROR,
+				(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
+				 errmsg("unexpected end of flag array")));
 
 	/* get final WJB_DONE and free iterator */
 	type = JsonbIteratorNext(&it, &v, false);
