Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wCtAf-002OuO-2U for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Apr 2026 05:52:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1wCtAe-00EpIC-0N for pgsql-hackers@arkaria.postgresql.org; Wed, 15 Apr 2026 05:52:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1wCtAd-00EpI4-2g for pgsql-hackers@lists.postgresql.org; Wed, 15 Apr 2026 05:52:16 +0000 Received: from mail-pl1-x629.google.com ([2607:f8b0:4864:20::629]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1wCtAc-00000001A6o-0fOT for pgsql-hackers@lists.postgresql.org; Wed, 15 Apr 2026 05:52:16 +0000 Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-2a9296b3926so34620575ad.1 for ; Tue, 14 Apr 2026 22:52:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1776232331; x=1776837131; darn=lists.postgresql.org; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=4Cd8fmGL6EXclC8+QFHYtSCtpFDl/6rAh33UgwYfMBY=; b=AUNlqLmbBDQXbiwGySkzeQP6D1k1g3wWQadHDTyJRQCP1NYUQPRRZKXvI/PyWLmP4c GGMyf04rRoEvaaOKIptsHHIyUTffDtSNhrqeWpg2NIcu/s3qUvw3NTZU9fvaa6APK3AG UW0Yuwr4elZWIQ7QGZxBDtn/QbASYzO8YxUwYLBH1E8fmMUAOo7tIN5QBnlxXi/KDur7 HUodEI1E1VjkyRjEB5lWerVqTlvP2Q2DpAg5Rz43FUgaI5KKkZIJ+6nPKZuaHU8pUXBI /X8hQCUqa4ai1CcrbSBzUltPPH/2U2Wb2IIoAKZBpwZCw2HFXgAAfS9VQEFSn7RyiFRv zu5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1776232331; x=1776837131; h=to:date:message-id:subject:mime-version:from:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4Cd8fmGL6EXclC8+QFHYtSCtpFDl/6rAh33UgwYfMBY=; b=O4PhCztkfO6l3ZCwr0BosVQJHJxkn+uf2w9t6bb5JpvrSTtjakxR1ZpDz1ye9rJxOY kjokRPXFqId4uKZqdGTWvExXe4yFYDTTCQAhSUsixrUxJOYajlgxpoDtCJLWxYWmnY15 gGIgyRG6lPwczDJ2RgmVbmDUlFitHD4Hk6VUDUrhutFga26THSMNhNIlostO7u4tbq7n moGML8GiUX1jWUV+ij2NjDGYIV33dC3mQ9u2qeZ1vd5yPQt8Sda6EQ0jJEE6uPT1Ebls qOopqxOdo6PWyYOx13N6xcNUh9sFTlEKsL8vBgl0AZB1/qERgdYAtkzKLbZXDAbXyOEd qxlw== X-Gm-Message-State: AOJu0YyYPxu7HQJssx23zuGKu16VkTbvo/KD4R7dfkGq2+z7E5A5lziZ qog+SXi8v8mbB760pJMBDYnUn+IcpH/7SZgQU/zwfmMSllTmX0+vOC+xc/+e3czNWJ8= X-Gm-Gg: AeBDieuG8FKHEWF4WtrzR58RL7wVLgm1IPWZ2iwwYAPoBfUcnk3QB55vafvnJk7kGu7 qwtyiVEW0RhlNNMbIg3Xq7t25uNlVR3n05r+b92Ch9ens2hA2MwvDQNDdxj0IhAcBM8QqerbawP BcP80oxqUYMuYvY6TipRsD9pp3obK18kp6Nq58BRAk71jPgBvUZzKDJmrxhVpoG1Zl0c0EUl0cT Yx3pRrTLUwSVXIfb7XWcSQROKTDuVMA6zzLu/PfcwTgecTVc1tvxFlWGtTnjC5ZF+9AnjPFCih1 0OshTq7Q2ISv3rDJQZ3FhLRc1/ykT8jXDTGo94y/D3cNXUid2xqfVUsXr3fFRy8ZuVFv1t2MIXb VZtbluorYOT2W/jWdtxCqxmf5VTn5FT4orz2qAgxKqDiQPTMpyHopNtKJRfmHgvmphm83JGx3r0 jTsABEJpA176A2PrrXuw52zvRssd+8l4Q= X-Received: by 2002:a17:902:d590:b0:2ae:4cb8:484a with SMTP id d9443c01a7336-2b2d599f022mr196880365ad.17.1776232331275; Tue, 14 Apr 2026 22:52:11 -0700 (PDT) Received: from smtpclient.apple ([45.32.121.103]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2b47826e23dsm9759125ad.39.2026.04.14.22.52.09 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 14 Apr 2026 22:52:10 -0700 (PDT) From: Chao Li Content-Type: multipart/mixed; boundary="Apple-Mail=_E8A40D7F-2CD1-48A3-A259-45A52027976A" Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3864.400.21\)) Subject: Fix a server crash problem from pg_get_database_ddl Message-Id: <573E45C1-31A4-4885-A00C-1A2171159A2A@gmail.com> Date: Wed, 15 Apr 2026 13:51:31 +0800 To: Postgres hackers X-Mailer: Apple Mail (2.3864.400.21) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --Apple-Mail=_E8A40D7F-2CD1-48A3-A259-45A52027976A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii Hi, While doing some testing, I hit a server crash: ``` 2026-04-15 11:30:17.377 CST [98179] LOG: client backend (PID 41260) was = terminated by signal 11: Segmentation fault: 11 2026-04-15 11:30:17.377 CST [98179] DETAIL: Failed process was running: = SELECT * FROM pg_get_database_ddl('db1'::regdatabase); 2026-04-15 11:30:17.377 CST [98179] LOG: terminating any other active = server processes 2026-04-15 11:30:17.380 CST [44361] FATAL: the database system is in = recovery mode ``` After debugging it, I found that the crash happened because I had = mistakenly deleted the tablespace entry directly from pg_tablespace, and = pg_get_database_ddl_internal() calls get_tablespace_name() without = checking whether the return value is NULL. So this doesn't seem like a bug a normal user could hit. It is more like = a superuser-only mistake that creates an invalid catalog state. I think = that even in such an edge case, we should raise a proper error instead = of crashing the backend. BTW, I have verified that in this case, ALTER DATABASE ... SET = TABLESPACE can move the database to a valid tablespace and recover from = the issue. This patch fixes that by checking for a NULL result and throwing an = error. Best regards, -- Chao Li (Evan) HighGo Software Co., Ltd. https://www.highgo.com/ --Apple-Mail=_E8A40D7F-2CD1-48A3-A259-45A52027976A Content-Disposition: attachment; filename=v1-0001-ddlutils-error-out-when-pg_get_database_ddl-sees-.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="v1-0001-ddlutils-error-out-when-pg_get_database_ddl-sees-.patch" Content-Transfer-Encoding: quoted-printable =46rom=204529c77c01bd0a2f1718c6dc45a2896191b72001=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20"Chao=20Li=20(Evan)"=20=0A= Date:=20Wed,=2015=20Apr=202026=2013:32:12=20+0800=0ASubject:=20[PATCH=20= v1]=20ddlutils:=20error=20out=20when=20pg_get_database_ddl()=20sees=20a=0A= =20missing=20tablespace=0A=0Apg_get_database_ddl_internal()=20calls=20= get_tablespace_name()=20for=20a=0Adatabase's=20dattablespace,=20and=20= then=20passes=20the=20result=20to=0Apg_strcasecmp()=20without=20checking=20= for=20NULL=20first.=0A=0AFix=20that=20by=20detecting=20the=20missing=20= tablespace=20explicitly=20and=20raising=20an=0AERROR=20with=20= ERRCODE_UNDEFINED_OBJECT.=0A=0AAuthor:=20Chao=20Li=20=0A= Reviewed-by:=0ADiscussion:=20https://postgr.es/m/=0A---=0A=20= src/backend/utils/adt/ddlutils.c=20|=207=20+++++++=0A=201=20file=20= changed,=207=20insertions(+)=0A=0Adiff=20--git=20= a/src/backend/utils/adt/ddlutils.c=20b/src/backend/utils/adt/ddlutils.c=0A= index=20c4f9f86c43e..6ab354e42f6=20100644=0A---=20= a/src/backend/utils/adt/ddlutils.c=0A+++=20= b/src/backend/utils/adt/ddlutils.c=0A@@=20-976,6=20+976,13=20@@=20= pg_get_database_ddl_internal(Oid=20dbid,=20bool=20pretty,=0A=20=09{=0A=20= =09=09char=09=20=20=20*spcname=20=3D=20= get_tablespace_name(dbform->dattablespace);=0A=20=0A+=09=09if=20(spcname=20= =3D=3D=20NULL)=0A+=09=09=09ereport(ERROR,=0A+=09=09=09=09=09= errcode(ERRCODE_UNDEFINED_OBJECT),=0A+=09=09=09=09=09errmsg("tablespace=20= with=20OID=20%u=20does=20not=20exist",=0A+=09=09=09=09=09=09=09= dbform->dattablespace),=0A+=09=09=09=09=09errhint("To=20recover,=20try=20= ALTER=20DATABASE=20...=20SET=20TABLESPACE=20...=20to=20a=20valid=20= tablespace."));=0A+=0A=20=09=09if=20(pg_strcasecmp(spcname,=20= "pg_default")=20!=3D=200)=0A=20=09=09=09append_ddl_option(&buf,=20= pretty,=204,=20"TABLESPACE=20=3D=20%s",=0A=20=09=09=09=09=09=09=09=20=20= quote_identifier(spcname));=0A--=20=0A2.50.1=20(Apple=20Git-155)=0A=0A= --Apple-Mail=_E8A40D7F-2CD1-48A3-A259-45A52027976A--