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.94.2) (envelope-from ) id 1tHnGK-009sO3-Lg for pgsql-hackers@arkaria.postgresql.org; Sun, 01 Dec 2024 16:57:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tHnGG-008mR0-7z for pgsql-hackers@arkaria.postgresql.org; Sun, 01 Dec 2024 16:57:33 +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.94.2) (envelope-from ) id 1tHnGF-008mPE-Ti for pgsql-hackers@lists.postgresql.org; Sun, 01 Dec 2024 16:57:33 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tHnGD-000Vdz-1c for pgsql-hackers@lists.postgresql.org; Sun, 01 Dec 2024 16:57:32 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 4B1GvNHq1353044; Sun, 1 Dec 2024 11:57:23 -0500 From: Tom Lane To: jian he cc: Kirill Reshke , Peter Eisentraut , PostgreSQL Hackers Subject: Re: CREATE SCHEMA ... CREATE DOMAIN support In-reply-to: References: <1898722.1732732780@sss.pgh.pa.us> <2007224.1732773174@sss.pgh.pa.us> <1075425.1732993688@sss.pgh.pa.us> <1220935.1733009604@sss.pgh.pa.us> <1273964.1733032384@sss.pgh.pa.us> Comments: In-reply-to jian he message dated "Sun, 01 Dec 2024 17:59:08 +0800" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <1353042.1733072243.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 01 Dec 2024 11:57:23 -0500 Message-ID: <1353043.1733072243@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk jian he writes: > On Sun, Dec 1, 2024 at 1:53=E2=80=AFPM Tom Lane wrot= e: >> (I'd be curious to know how other major implementations handle >> this. Are we the only implementation that ever read the spec >> that way?) > quote from https://learn.microsoft.com/en-us/sql/t-sql/statements/creat= e-schema-transact-sql?view=3Dsql-server-ver16 > <<>> > CREATE SCHEMA can create a schema, the tables and views it contains, and= GRANT, > REVOKE, or DENY permissions on any securable in a single statement. This > statement must be executed as a separate batch. Objects created by the C= REATE > SCHEMA statement are created inside the schema that is being created. > Securables to be created by CREATE SCHEMA can be listed in any order, ex= cept for > views that reference other views. In that case, the referenced view must= be > created before the view that references it. > Therefore, a GRANT statement can grant permission on an object before th= e object > itself is created, or a CREATE VIEW statement can appear before the CREA= TE TABLE > statements that create the tables referenced by the view. Also, CREATE T= ABLE > statements can declare foreign keys to tables that are defined later in = the > CREATE SCHEMA statement. > <<>> Interesting. But I suspect this tells us more about SQL Server's internal implementation of DDL actions than about spec requirements. I looked at DB2's reference page: https://www.ibm.com/docs/en/db2/11.5?topic=3Dstatements-create-schema It doesn't have much of anything explicit on this topic, but they do give an example showing that you can create two tables with mutually referencing foreign keys, which means they postpone FK constraint creation till the end. There's also this interesting tidbit: "Unqualified object names in any SQL statement within the CREATE SCHEMA statement are implicitly qualified by the name of the created schema." which eliminates some of the is-that-an-external-reference-or-a- forward-reference ambiguities I was concerned about yesterday. That ship sailed decades ago for us, however. I'm also interested to note that like SQL Server, DB2 has strict limits on the types of objects that can be created, much narrower than what the spec suggests. For DB2 it's: CREATE TABLE statement, excluding typed tables and materialized query tabl= es CREATE VIEW statement, excluding typed views CREATE INDEX statement COMMENT statement GRANT statement That suggests, even though they don't say so, that they're trying to do forward-reference removal; there'd be little reason for the restriction otherwise. MySQL doesn't have CREATE SCHEMA (it's a synonym for CREATE DATABASE), so nothing to be learned there. Whether or not the standard has an opinion on this topic, it's pretty clear that real implementations are all over the place and have plenty of ad-hoc restrictions. I'm still thinking that "let's forget all that and do the subcommands in order" is a win for sanity and explainability. regards, tom lane