public inbox for [email protected]  
help / color / mirror / Atom feed
From: Lakshmi N <[email protected]>
To: [email protected] <[email protected]>
Cc: [email protected]
Subject: Reject invalid databases in pg_get_database_ddl()
Date: Thu, 16 Apr 2026 01:19:44 -0700
Message-ID: <CA+3i_M8m1k2gFch+tU0JmAQh9FRV+pFrfTXDrJo+BqmwsTmOhg@mail.gmail.com> (raw)

Hi,

pg_get_database_ddl() is not checking for databases in an invalid state
before producing ddl statements. This caused the function to emit
CONNECTION_LIMIT = -2, which is invalid SQL that Postgres rejects.
A database row can be in this inconsistent state longer, for example
server crashed during a drop database.

Attached patch to fix this issue by doing a database_is_invalid_form()
check early in pg_get_database_ddl_internal().

Regards,
Lakshmi


Attachments:

  [application/octet-stream] 0001-Reject-pg_get_database_ddl-for-invalid-databases.patch (929B, 3-0001-Reject-pg_get_database_ddl-for-invalid-databases.patch)
  download | inline diff:
diff --git a/src/backend/utils/adt/ddlutils.c b/src/backend/utils/adt/ddlutils.c
index c4f9f86c43e..7ffe231417a 100644
--- a/src/backend/utils/adt/ddlutils.c
+++ b/src/backend/utils/adt/ddlutils.c
@@ -887,6 +887,17 @@ pg_get_database_ddl_internal(Oid dbid, bool pretty,
 	dbform = (Form_pg_database) GETSTRUCT(tuple);
 	dbname = pstrdup(NameStr(dbform->datname));
 
+	/*
+	 * Reject invalid databases. This is a safety measure to avoid generating DDL
+	 * that can't be executed. For example, invalid connection limit.
+	 */
+	if (database_is_invalid_form(dbform))
+		ereport(ERROR,
+				(errcode(ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE),
+				 errmsg("cannot generate DDL for invalid database \"%s\"",
+						dbname),
+				 errhint("Use DROP DATABASE to drop invalid databases.")));
+
 	/*
 	 * We don't support generating DDL for system databases.  The primary
 	 * reason for this is that users shouldn't be recreating them.


view thread (13+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Reject invalid databases in pg_get_database_ddl()
  In-Reply-To: <CA+3i_M8m1k2gFch+tU0JmAQh9FRV+pFrfTXDrJo+BqmwsTmOhg@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox