diff --git a/pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java b/pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java
index a3a8b08d5a..dba70a8a5a 100644
--- a/pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java
+++ b/pgjdbc/src/main/java/org/postgresql/jdbc/PgPreparedStatement.java
@@ -1208,8 +1208,19 @@ public void addBatch() throws SQLException {
         flags |= QueryExecutor.QUERY_ONESHOT;
       }
       StatementResultHandler handler = new StatementResultHandler();
-      connection.getQueryExecutor().execute(preparedQuery.query, preparedParameters, handler, 0, 0,
-          flags);
+      try {
+        connection.getQueryExecutor().execute(preparedQuery.query, preparedParameters, handler, 0, 0,
+            flags);
+      } catch (SQLException e) {
+        if (preparedQuery.query.getSubqueries() != null
+            || !connection.getQueryExecutor().willHealOnRetry(e)) {
+          throw e;
+        }
+        preparedQuery.query.close();
+        handler = new StatementResultHandler();
+        connection.getQueryExecutor().execute(preparedQuery.query, preparedParameters, handler, 0, 0,
+            flags);
+      }
       ResultWrapper wrapper = handler.getResults();
       if (wrapper != null) {
         rs = wrapper.getResultSet();
diff --git a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PreparedStatementTest.java b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PreparedStatementTest.java
index 17ab9ea268..32ab4a75dc 100644
--- a/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PreparedStatementTest.java
+++ b/pgjdbc/src/test/java/org/postgresql/test/jdbc2/PreparedStatementTest.java
@@ -1769,6 +1769,66 @@ public void testGetMetaDataDoesNotBypassPrepareThreshold() throws SQLException {
     assertEquals(0, getNumberOfServerPreparedStatements("SELECT 44"), "prepareThreshold=5 and the statement was never executed, so describing it should not server-prepare it");
   }
 
+  @Test
+  public void testGetMetaDataRetriesInvalidatedPlan() throws SQLException {
+    assumeTrue(preferQueryMode != PreferQueryMode.SIMPLE,
+        "simple protocol does not support describe requests");
+    String table = "metadata_retry_" + binaryMode;
+    try (Connection other = TestUtil.openDB(); Statement ddl = other.createStatement()) {
+      TestUtil.createTable(other, table, "value int");
+      try (PreparedStatement pstmt = con.prepareStatement("select * from " + table)) {
+        ((PGStatement) pstmt).setPrepareThreshold(1);
+        ddl.executeUpdate("insert into " + table + " values (42)");
+        try (ResultSet rs = pstmt.executeQuery()) {
+          assertEquals(Types.INTEGER, rs.getMetaData().getColumnType(1));
+        }
+        ddl.executeUpdate("alter table " + table + " alter value type text");
+        assertEquals(Types.VARCHAR, pstmt.getMetaData().getColumnType(1));
+        try (ResultSet rs = pstmt.executeQuery()) {
+          assertTrue(rs.next());
+          assertEquals("42", rs.getString(1));
+        }
+      } finally {
+        TestUtil.dropTable(other, table);
+      }
+    }
+  }
+
+  @Test
+  public void testGetMetaDataDoesNotRetryFailedTransaction() throws SQLException {
+    assumeTrue(preferQueryMode != PreferQueryMode.SIMPLE,
+        "simple protocol does not support describe requests");
+    String table = "metadata_failed_" + binaryMode;
+    try (Connection other = TestUtil.openDB(); Statement ddl = other.createStatement()) {
+      TestUtil.createTable(other, table, "value int");
+      try (PreparedStatement pstmt = con.prepareStatement("select * from " + table)) {
+        ((PGStatement) pstmt).setPrepareThreshold(1);
+        try (ResultSet rs = pstmt.executeQuery()) {
+          assertEquals(Types.INTEGER, rs.getMetaData().getColumnType(1));
+        }
+        ddl.executeUpdate("alter table " + table + " alter value type text");
+        con.setAutoCommit(false);
+        try (Statement begin = con.createStatement()) {
+          begin.execute("select 1");
+        }
+        assertEquals("0A000", assertThrows(SQLException.class, pstmt::getMetaData).getSQLState());
+      } finally {
+        con.rollback();
+        con.setAutoCommit(true);
+        TestUtil.dropTable(other, table);
+      }
+    }
+  }
+
+  @Test
+  public void testGetMetaDataPreservesSyntaxError() throws SQLException {
+    assumeTrue(preferQueryMode != PreferQueryMode.SIMPLE,
+        "simple protocol does not support describe requests");
+    try (PreparedStatement pstmt = con.prepareStatement("select from")) {
+      assertEquals("42601", assertThrows(SQLException.class, pstmt::getMetaData).getSQLState());
+    }
+  }
+
   @Test
   public void testInappropriateStatementSharing() throws SQLException {
     PreparedStatement ps = con.prepareStatement("SELECT ?::timestamp");
