diff --git a/doc/src/sgml/ecpg.sgml b/doc/src/sgml/ecpg.sgml
index e7a53f3c9d..9c247ccc2e 100644
--- a/doc/src/sgml/ecpg.sgml
+++ b/doc/src/sgml/ecpg.sgml
@@ -5052,6 +5052,7 @@ struct
     long sqlerrd[6];
     char sqlwarn[8];
     char sqlstate[5];
+    char sqlstatepad;
 } sqlca;
 </programlisting>
     (In a multithreaded program, every thread automatically gets its
@@ -5201,6 +5202,11 @@ sqlstate: 42P01
     throughout all applications.  For further information see
     <xref linkend="errcodes-appendix"/>.
    </para>
+   <para>
+    <literal>sqlca.sqlstate</literal> field is a non-terminated character
+    array, in order to guard against accidental uses as a C string it is
+    followed by a padding nul character.
+   </para>
 
    <para>
     <literal>SQLCODE</literal>, the deprecated error code scheme, is a
diff --git a/src/interfaces/ecpg/ecpglib/misc.c b/src/interfaces/ecpg/ecpglib/misc.c
index 2ae989e3e5..a28481eca5 100644
--- a/src/interfaces/ecpg/ecpglib/misc.c
+++ b/src/interfaces/ecpg/ecpglib/misc.c
@@ -52,7 +52,8 @@ static struct sqlca_t sqlca_init =
 	},
 	{
 		'0', '0', '0', '0', '0'
-	}
+	},
+	'\0'
 };
 
 static pthread_key_t sqlca_key;
@@ -274,8 +275,8 @@ ecpg_log(const char *format,...)
 		/* dump out internal sqlca variables */
 		if (ecpg_internal_regression_mode && sqlca != NULL)
 		{
-			fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %s\n",
-					sqlca->sqlcode, sqlca->sqlstate);
+			fprintf(debugstream, "[NO_PID]: sqlca: code: %ld, state: %.*s\n",
+					sqlca->sqlcode, (int) sizeof(sqlca->sqlstate), sqlca->sqlstate);
 		}
 
 		fflush(debugstream);
diff --git a/src/interfaces/ecpg/include/sqlca.h b/src/interfaces/ecpg/include/sqlca.h
index c5f107dd33..f0322f9191 100644
--- a/src/interfaces/ecpg/include/sqlca.h
+++ b/src/interfaces/ecpg/include/sqlca.h
@@ -51,6 +51,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/compat_informix-test_informix2.c b/src/interfaces/ecpg/test/expected/compat_informix-test_informix2.c
index fc30e3278c..1f2b023694 100644
--- a/src/interfaces/ecpg/test/expected/compat_informix-test_informix2.c
+++ b/src/interfaces/ecpg/test/expected/compat_informix-test_informix2.c
@@ -68,6 +68,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/preproc-init.c b/src/interfaces/ecpg/test/expected/preproc-init.c
index b0e04731fe..441ca1f97f 100644
--- a/src/interfaces/ecpg/test/expected/preproc-init.c
+++ b/src/interfaces/ecpg/test/expected/preproc-init.c
@@ -62,6 +62,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-array.c b/src/interfaces/ecpg/test/expected/sql-array.c
index e48f5fac67..cd2252ea30 100644
--- a/src/interfaces/ecpg/test/expected/sql-array.c
+++ b/src/interfaces/ecpg/test/expected/sql-array.c
@@ -75,6 +75,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-code100.c b/src/interfaces/ecpg/test/expected/sql-code100.c
index 4c85530a17..f410c63c7b 100644
--- a/src/interfaces/ecpg/test/expected/sql-code100.c
+++ b/src/interfaces/ecpg/test/expected/sql-code100.c
@@ -62,6 +62,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-copystdout.c b/src/interfaces/ecpg/test/expected/sql-copystdout.c
index d2599fb0e9..69f743443a 100644
--- a/src/interfaces/ecpg/test/expected/sql-copystdout.c
+++ b/src/interfaces/ecpg/test/expected/sql-copystdout.c
@@ -64,6 +64,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-declare.c b/src/interfaces/ecpg/test/expected/sql-declare.c
index 6248d99217..f515e954ee 100644
--- a/src/interfaces/ecpg/test/expected/sql-declare.c
+++ b/src/interfaces/ecpg/test/expected/sql-declare.c
@@ -70,6 +70,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-define.c b/src/interfaces/ecpg/test/expected/sql-define.c
index e97caec5b0..42f62a939e 100644
--- a/src/interfaces/ecpg/test/expected/sql-define.c
+++ b/src/interfaces/ecpg/test/expected/sql-define.c
@@ -77,6 +77,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-dynalloc.c b/src/interfaces/ecpg/test/expected/sql-dynalloc.c
index a95dddf15d..0c4efe96de 100644
--- a/src/interfaces/ecpg/test/expected/sql-dynalloc.c
+++ b/src/interfaces/ecpg/test/expected/sql-dynalloc.c
@@ -63,6 +63,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-dynalloc2.c b/src/interfaces/ecpg/test/expected/sql-dynalloc2.c
index 711857706a..db4d6b783e 100644
--- a/src/interfaces/ecpg/test/expected/sql-dynalloc2.c
+++ b/src/interfaces/ecpg/test/expected/sql-dynalloc2.c
@@ -63,6 +63,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-dyntest.c b/src/interfaces/ecpg/test/expected/sql-dyntest.c
index 513d44c630..9b3e540ff3 100644
--- a/src/interfaces/ecpg/test/expected/sql-dyntest.c
+++ b/src/interfaces/ecpg/test/expected/sql-dyntest.c
@@ -116,6 +116,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-indicators.c b/src/interfaces/ecpg/test/expected/sql-indicators.c
index 7cf43ad622..f10edb388f 100644
--- a/src/interfaces/ecpg/test/expected/sql-indicators.c
+++ b/src/interfaces/ecpg/test/expected/sql-indicators.c
@@ -64,6 +64,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-sqljson.c b/src/interfaces/ecpg/test/expected/sql-sqljson.c
index 39221f9ea5..5750b225c4 100644
--- a/src/interfaces/ecpg/test/expected/sql-sqljson.c
+++ b/src/interfaces/ecpg/test/expected/sql-sqljson.c
@@ -64,6 +64,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/sql-sqljson_jsontable.c b/src/interfaces/ecpg/test/expected/sql-sqljson_jsontable.c
index b2a0f11eb6..e586c24914 100644
--- a/src/interfaces/ecpg/test/expected/sql-sqljson_jsontable.c
+++ b/src/interfaces/ecpg/test/expected/sql-sqljson_jsontable.c
@@ -64,6 +64,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/thread-alloc.c b/src/interfaces/ecpg/test/expected/thread-alloc.c
index 3b31d27fd3..7e53368ea4 100644
--- a/src/interfaces/ecpg/test/expected/thread-alloc.c
+++ b/src/interfaces/ecpg/test/expected/thread-alloc.c
@@ -79,6 +79,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/thread-descriptor.c b/src/interfaces/ecpg/test/expected/thread-descriptor.c
index e34f4708d1..6742180087 100644
--- a/src/interfaces/ecpg/test/expected/thread-descriptor.c
+++ b/src/interfaces/ecpg/test/expected/thread-descriptor.c
@@ -75,6 +75,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
diff --git a/src/interfaces/ecpg/test/expected/thread-prep.c b/src/interfaces/ecpg/test/expected/thread-prep.c
index 052e27b634..8439126e32 100644
--- a/src/interfaces/ecpg/test/expected/thread-prep.c
+++ b/src/interfaces/ecpg/test/expected/thread-prep.c
@@ -79,6 +79,7 @@ struct sqlca_t
 	/* 7: empty						*/
 
 	char		sqlstate[5];
+	char		sqlstatepad;	/* nul terminator for sqlstate */
 };
 
 struct sqlca_t *ECPGget_sqlca(void);
