contrib/postgres_fdw/connection.c
void
pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
				   bool clear, const char *sql)
{
	/* If requested, PGresult must be released before leaving this function. */
	PG_TRY();
	{
		char	   *diag_sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
		char	   *message_primary = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
		char	   *message_detail = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
		char	   *message_hint = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
		char	   *message_context = PQresultErrorField(res, PG_DIAG_CONTEXT);
		int			sqlstate;

		if (diag_sqlstate)
			sqlstate = MAKE_SQLSTATE(diag_sqlstate[0],
									 diag_sqlstate[1],
									 diag_sqlstate[2],
									 diag_sqlstate[3],
									 diag_sqlstate[4]);
		else
			sqlstate = ERRCODE_CONNECTION_FAILURE;

		/*
		 * If we don't get a message from the PGresult, try the PGconn.  This
		 * is needed because for connection-level failures, PQexec may just
		 * return NULL, not a PGresult at all.
		 */
		if (message_primary == NULL)
			message_primary = PQerrorMessage(conn);

		ereport(elevel,
				(errcode(sqlstate),
				 message_primary ? errmsg_internal("%s", message_primary) :
				 errmsg("could not obtain message string for remote error"),
			   message_detail ? errdetail_internal("%s", message_detail) : 0,
				 message_hint ? errhint("%s", message_hint) : 0,
				 message_context ? errcontext("%s", message_context) : 0,
				 sql ? errcontext("Remote SQL command: %s", sql) : 0));
	}
	PG_CATCH();
	{
		if (clear)
			PQclear(res);
		PG_RE_THROW();
	}
	PG_END_TRY();
	if (clear)
		PQclear(res);
}
/*
 * Report an error we got from the remote server.
 *
 * elevel: error level to use (typically ERROR, but might be less)
 * res: PGresult containing the error
 * conn: connection we did the query on
 * clear: if true, PQclear the result (otherwise caller will handle it)
 * sql: NULL, or text of remote command we tried to execute
 *
 * Note: callers that choose not to throw ERROR for a remote error are
 * responsible for making sure that the associated ConnCacheEntry gets
 * marked with have_error = true.
 */
Report an error we got from the remote server.

callers that choose not to throw ERROR for a remote error are
responsible for making sure that the associated ConnCacheEntry gets
marked with have_error = true.
Report an error we got from the remote server . callers that choose not to throw ERROR for a remote error are responsible for making sure that the associated ConnCacheEntry gets marked with have_error = true .
void
pgfdw_report_error(int elevel, PGresult *res, PGconn *conn,
				   bool clear, const char *sql)
{
	PG_TRY();
	{
		char	   *diag_sqlstate = PQresultErrorField(res, PG_DIAG_SQLSTATE);
		char	   *message_primary = PQresultErrorField(res, PG_DIAG_MESSAGE_PRIMARY);
		char	   *message_detail = PQresultErrorField(res, PG_DIAG_MESSAGE_DETAIL);
		char	   *message_hint = PQresultErrorField(res, PG_DIAG_MESSAGE_HINT);
		char	   *message_context = PQresultErrorField(res, PG_DIAG_CONTEXT);
		int			sqlstate;
		if (diag_sqlstate)
			sqlstate = MAKE_SQLSTATE(diag_sqlstate[0],
									 diag_sqlstate[1],
									 diag_sqlstate[2],
									 diag_sqlstate[3],
									 diag_sqlstate[4]);
		else
			sqlstate = ERRCODE_CONNECTION_FAILURE;
		if (message_primary == NULL)
			message_primary = PQerrorMessage(conn);
		ereport(elevel,
				(errcode(sqlstate),
				 message_primary ? errmsg_internal("%s", message_primary) :
				 errmsg("could not obtain message string for remote error"),
			   message_detail ? errdetail_internal("%s", message_detail) : 0,
				 message_hint ? errhint("%s", message_hint) : 0,
				 message_context ? errcontext("%s", message_context) : 0,
				 sql ? errcontext("Remote SQL command: %s", sql) : 0));
	}
	PG_CATCH();
	{
		if (clear)
			PQclear(res);
		PG_RE_THROW();
	}
	PG_END_TRY();
	if (clear)
		PQclear(res);
}
void pgfdw_report_error ( int elevel , PGresult * res , PGconn * conn , bool clear , const char * sql ) { PG_TRY ( ) ; { char * diag_sqlstate = PQresultErrorField ( res , PG_DIAG_SQLSTATE ) ; char * message_primary = PQresultErrorField ( res , PG_DIAG_MESSAGE_PRIMARY ) ; char * message_detail = PQresultErrorField ( res , PG_DIAG_MESSAGE_DETAIL ) ; char * message_hint = PQresultErrorField ( res , PG_DIAG_MESSAGE_HINT ) ; char * message_context = PQresultErrorField ( res , PG_DIAG_CONTEXT ) ; int sqlstate ; if ( diag_sqlstate ) sqlstate = MAKE_SQLSTATE ( diag_sqlstate [ 0 ] , diag_sqlstate [ 1 ] , diag_sqlstate [ 2 ] , diag_sqlstate [ 3 ] , diag_sqlstate [ 4 ] ) ; else sqlstate = ERRCODE_CONNECTION_FAILURE ; if ( message_primary == NULL ) message_primary = PQerrorMessage ( conn ) ; ereport ( elevel , ( errcode ( sqlstate ) , message_primary ? errmsg_internal ( " " , message_primary ) : errmsg ( " " ) , message_detail ? errdetail_internal ( " " , message_detail ) : 0 , message_hint ? errhint ( " " , message_hint ) : 0 , message_context ? errcontext ( " " , message_context ) : 0 , sql ? errcontext ( " " , sql ) : 0 ) ) ; } PG_CATCH ( ) ; { if ( clear ) PQclear ( res ) ; PG_RE_THROW ( ) ; } PG_END_TRY ( ) ; if ( clear ) PQclear ( res ) ; }
Report an error we got from the remote server.
Report an error we got from the remote server .
/* If requested, PGresult must be released before leaving this function. */ /*
		 * If we don't get a message from the PGresult, try the PGconn.  This
		 * is needed because for connection-level failures, PQexec may just
		 * return NULL, not a PGresult at all.
		 */