diff --git a/src/backend/executor/spi.c b/src/backend/executor/spi.c
index 4ef987c26a5d079cd684c4cf35210a5c14e61afc..1c285297c044e41ff483aa0cc67c590d9d83b9e6 100644
--- a/src/backend/executor/spi.c
+++ b/src/backend/executor/spi.c
@@ -2453,6 +2453,9 @@ _SPI_error_callback(void *arg)
 	const char *query = (const char *) arg;
 	int			syntaxerrposition;
 
+	if (query == NULL)			/* in case arg wasn't set yet */
+		return;
+
 	/*
 	 * If there is a syntax error position, convert to internal syntax error;
 	 * otherwise treat the query as an item of context stack