diff --git a/src/interfaces/ecpg/compatlib/informix.c b/src/interfaces/ecpg/compatlib/informix.c
index 8d81c83..9f7776e 100644
--- a/src/interfaces/ecpg/compatlib/informix.c
+++ b/src/interfaces/ecpg/compatlib/informix.c
@@ -1032,6 +1032,8 @@ void
 ECPG_informix_reset_sqlca(void)
 {
 	struct sqlca_t *sqlca = ECPGget_sqlca();
+	if (sqlca == NULL)
+		return;
 
 	memcpy((char *) sqlca, (char *) &sqlca_init, sizeof(struct sqlca_t));
 }
diff --git a/src/interfaces/ecpg/ecpglib/connect.c b/src/interfaces/ecpg/ecpglib/connect.c
index 55c5680..ccc5fcc 100644
--- a/src/interfaces/ecpg/ecpglib/connect.c
+++ b/src/interfaces/ecpg/ecpglib/connect.c
@@ -223,6 +223,12 @@ ECPGnoticeReceiver(void *arg, const PGresult *result)
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 	int			sqlcode;
 
+	if (sqlca == NULL)
+	{
+		ecpg_log("out of memory");
+		return;
+	}
+
 	(void) arg;					/* keep the compiler quiet */
 	if (sqlstate == NULL)
 		sqlstate = ECPG_SQLSTATE_ECPG_INTERNAL_ERROR;
@@ -278,6 +284,14 @@ ECPGconnect(int lineno, int c, const char *name, const char *user, const char *p
 	const char **conn_keywords;
 	const char **conn_values;
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		ecpg_free(dbname);
+		return false;
+	}
+
 	ecpg_init_sqlca(sqlca);
 
 	/*
@@ -654,6 +668,13 @@ ECPGdisconnect(int lineno, const char *connection_name)
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 	struct connection *con;
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		return (false);
+	}
+
 #ifdef ENABLE_THREAD_SAFETY
 	pthread_mutex_lock(&connections_mutex);
 #endif
diff --git a/src/interfaces/ecpg/ecpglib/data.c b/src/interfaces/ecpg/ecpglib/data.c
index c3cd946..8d36484 100644
--- a/src/interfaces/ecpg/ecpglib/data.c
+++ b/src/interfaces/ecpg/ecpglib/data.c
@@ -132,6 +132,13 @@ ecpg_get_data(const PGresult *results, int act_tuple, int act_field, int lineno,
 	int			value_for_indicator = 0;
 	long		log_offset;
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		return (false);
+	}
+
 	/*
 	 * If we are running in a regression test, do not log the offset variable,
 	 * it depends on the machine's alignment.
diff --git a/src/interfaces/ecpg/ecpglib/descriptor.c b/src/interfaces/ecpg/ecpglib/descriptor.c
index 956c035..15fd7a0 100644
--- a/src/interfaces/ecpg/ecpglib/descriptor.c
+++ b/src/interfaces/ecpg/ecpglib/descriptor.c
@@ -93,6 +93,13 @@ ECPGget_desc_header(int lineno, const char *desc_name, int *count)
 	PGresult   *ECPGresult;
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		return false;
+	}
+
 	ecpg_init_sqlca(sqlca);
 	ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
 	if (!ECPGresult)
@@ -245,6 +252,13 @@ ECPGget_desc(int lineno, const char *desc_name, int index,...)
 	struct variable data_var;
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		return false;
+	}
+
 	va_start(args, index);
 	ecpg_init_sqlca(sqlca);
 	ECPGresult = ecpg_result_by_descriptor(lineno, desc_name);
@@ -701,6 +715,13 @@ ECPGdeallocate_desc(int line, const char *name)
 	struct descriptor *prev;
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(line, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		return false;
+	}
+
 	ecpg_init_sqlca(sqlca);
 	for (desc = get_descriptors(), prev = NULL; desc; prev = desc, desc = desc->next)
 	{
@@ -740,6 +761,13 @@ ECPGallocate_desc(int line, const char *name)
 	struct descriptor *new;
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(line, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		return false;
+	}
+
 	ecpg_init_sqlca(sqlca);
 	new = (struct descriptor *) ecpg_alloc(sizeof(struct descriptor), line);
 	if (!new)
diff --git a/src/interfaces/ecpg/ecpglib/error.c b/src/interfaces/ecpg/ecpglib/error.c
index 715bedb..656b2c4 100644
--- a/src/interfaces/ecpg/ecpglib/error.c
+++ b/src/interfaces/ecpg/ecpglib/error.c
@@ -14,6 +14,13 @@ ecpg_raise(int line, int code, const char *sqlstate, const char *str)
 {
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 
+	if (sqlca == NULL)
+	{
+		ecpg_log("out of memory");
+		ECPGfree_auto_mem();
+		return;
+	}
+
 	sqlca->sqlcode = code;
 	strncpy(sqlca->sqlstate, sqlstate, sizeof(sqlca->sqlstate));
 
@@ -215,6 +222,13 @@ ecpg_raise_backend(int line, PGresult *result, PGconn *conn, int compat)
 	char	   *sqlstate;
 	char	   *message;
 
+	if (sqlca == NULL)
+	{
+		ecpg_log("out of memory");
+		ECPGfree_auto_mem();
+		return;
+	}
+
 	if (result)
 	{
 		sqlstate = PQresultErrorField(result, PG_DIAG_SQLSTATE);
@@ -323,6 +337,12 @@ sqlprint(void)
 {
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 
+	if (sqlca == NULL)
+	{
+		ecpg_log("out of memory");
+		return;
+	}
+
 	sqlca->sqlerrm.sqlerrmc[sqlca->sqlerrm.sqlerrml] = '\0';
 	fprintf(stderr, ecpg_gettext("SQL error: %s\n"), sqlca->sqlerrm.sqlerrmc);
 }
diff --git a/src/interfaces/ecpg/ecpglib/execute.c b/src/interfaces/ecpg/ecpglib/execute.c
index bcb38d2..9a56a5c 100644
--- a/src/interfaces/ecpg/ecpglib/execute.c
+++ b/src/interfaces/ecpg/ecpglib/execute.c
@@ -1493,6 +1493,13 @@ ecpg_process_output(struct statement * stmt, bool clear_result)
 				ntuples,
 				act_field;
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(stmt->lineno, ECPG_OUT_OF_MEMORY,
+				   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+		return (false);
+	}
+
 	var = stmt->outlist;
 	switch (PQresultStatus(stmt->results))
 	{
diff --git a/src/interfaces/ecpg/ecpglib/misc.c b/src/interfaces/ecpg/ecpglib/misc.c
index 2e62260..a13aa0a 100644
--- a/src/interfaces/ecpg/ecpglib/misc.c
+++ b/src/interfaces/ecpg/ecpglib/misc.c
@@ -106,6 +106,13 @@ ecpg_init(const struct connection * con, const char *connection_name, const int
 {
 	struct sqlca_t *sqlca = ECPGget_sqlca();
 
+	if (sqlca == NULL)
+	{
+		ecpg_raise(lineno, ECPG_OUT_OF_MEMORY, ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY,
+				   NULL);
+		return (false);
+	}
+
 	ecpg_init_sqlca(sqlca);
 	if (con == NULL)
 	{
@@ -143,6 +150,8 @@ ECPGget_sqlca(void)
 	if (sqlca == NULL)
 	{
 		sqlca = malloc(sizeof(struct sqlca_t));
+		if (sqlca == NULL)
+			return NULL;
 		ecpg_init_sqlca(sqlca);
 		pthread_setspecific(sqlca_key, sqlca);
 	}
@@ -286,9 +295,11 @@ ecpg_log(const char *format,...)
 	va_end(ap);
 
 	/* dump out internal sqlca variables */
-	if (ecpg_internal_regression_mode)
+	if (ecpg_internal_regression_mode && sqlca != NULL)
+	{
 		fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
 				sqlca->sqlcode, sqlca->sqlstate);
+	}
 
 	fflush(debugstream);
 
@@ -524,6 +535,13 @@ ECPGset_var(int number, void *pointer, int lineno)
 	{
 		struct sqlca_t *sqlca = ECPGget_sqlca();
 
+		if (sqlca == NULL)
+		{
+			ecpg_raise(lineno, ECPG_OUT_OF_MEMORY,
+					   ECPG_SQLSTATE_ECPG_OUT_OF_MEMORY, NULL);
+			return;
+		}
+
 		sqlca->sqlcode = ECPG_OUT_OF_MEMORY;
 		strncpy(sqlca->sqlstate, "YE001", sizeof(sqlca->sqlstate));
 		snprintf(sqlca->sqlerrm.sqlerrmc, sizeof(sqlca->sqlerrm.sqlerrmc), "out of memory on line %d", lineno);
