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 1sOHDv-00DPzd-JO for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Jul 2024 13:37:39 +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 1sOHDs-001rDn-AL for pgsql-hackers@arkaria.postgresql.org; Mon, 01 Jul 2024 13:37:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sOHDs-001rDf-0g for pgsql-hackers@lists.postgresql.org; Mon, 01 Jul 2024 13:37:36 +0000 Received: from mail-lj1-x22d.google.com ([2a00:1450:4864:20::22d]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sOHDp-0042R1-IK for pgsql-hackers@postgresql.org; Mon, 01 Jul 2024 13:37:34 +0000 Received: by mail-lj1-x22d.google.com with SMTP id 38308e7fff4ca-2ec002caf3eso48416091fa.1 for ; Mon, 01 Jul 2024 06:37:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1719841052; x=1720445852; darn=postgresql.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=dITwmMxT6NdRY8Bx5pJsb//7pUgUiy5wevKvILVScSU=; b=e1dx/pO31HiQqZkF9hH6kyJ5r9/o2oFBNmWqGvpOjfaM58xOKYTPZ0gvT/CNSee4bH k3uDAShps/gmmX9PGD1xFPOIRMXdG7gmsw1560AMqm/JtMA/uzFMwBn4sQwnUoVw3Kwl hkzxI2uyToo4L46SHgDs09D+qXC/ldqgxN6uZyU/Dk2jd19p30N7JKLzK5GbN4JC9iIE AOB/01uz8uHQGTFEaBFpl6NFfKNQ7800wfUKlPjL+l+KLwyRfIt6JU2SpkzB05E2W4ut 5I/QNWqv6w3PxKGRnCrkuWiPWBbPxTCoAUNEHHdk3HuTJDf5aEbbm4uQM+HpOHMkImS0 OPDw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1719841052; x=1720445852; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dITwmMxT6NdRY8Bx5pJsb//7pUgUiy5wevKvILVScSU=; b=Jrt78uQ37u2OhmxrhYLYR9UILVMHqt93BSKP4N/mNiSLqoNJbmo9MDFHiNP8GY/QLs OiqDVU6Hadv+PAFQXZSOHAbzLedmF859m3gveHDkYkMgGBgeIsgvERvBluBaxqH2hUwX hpsUcjj6aIr2AV3rqXUCYSTo3cd+b+Qob8vgNgg2qHpZC6xomfPOmr+j52aACGyt9WwH z1Qhg+d7Vw16AC89/gtJLDW+GEtoqjcc5NYLMgqVyOFGdX+9CMWicp8WizyQpm61HLuv 5JnzAW7t4PKU4I1EDIpqzi2CreMSPtYqVO9H/IkoFe2rMS8Q7XrpzKOqOohQHcU4HZuv d6nQ== X-Forwarded-Encrypted: i=1; AJvYcCWA9hKvGl86xP4C8TG8AQ4fX2bWiobR9mQv+w8FB4nzJb3YTwBRdRbGqtjU922n1Fi5Wdxpb3SpF4FTLLRaY+TKNf6mJ325umTiF7us X-Gm-Message-State: AOJu0YyeEOF2KJ+0Z4popHnB+ObBMs8uc5lAEyG1199dWFQDHHn6Niox JO9qEcdRtsAaTdf6fNmnF5oWqhQQQgSlCsUg17krYXE0oc0LMa2tsuA/cjsmvlAJylzMS+5g93r Zv6oAO4A30sb5357GwzD3rtsqk1w= X-Google-Smtp-Source: AGHT+IHowsv5DIwvp2QMR8A7HVnQPL6CuYGQDe4Vudx0ccSzXfXG1Ls9GU332niX5uw6C652hy/QB2/bY1+OHfYhUwk= X-Received: by 2002:a05:651c:313:b0:2ed:5af6:e846 with SMTP id 38308e7fff4ca-2ee5e707cfcmr34687411fa.50.1719841051227; Mon, 01 Jul 2024 06:37:31 -0700 (PDT) MIME-Version: 1.0 References: <1362410.1719349169@sss.pgh.pa.us> <1390099.1719357084@sss.pgh.pa.us> <1642803.1719447063@sss.pgh.pa.us> In-Reply-To: From: James Coleman Date: Mon, 1 Jul 2024 09:37:17 -0400 Message-ID: Subject: Re: Should we document how column DEFAULT expressions work? To: "David G. Johnston" Cc: David Rowley , Peter Eisentraut , Tom Lane , pgsql-hackers Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Jun 30, 2024 at 8:16=E2=80=AFPM David G. Johnston wrote: > > On Sun, Jun 30, 2024 at 4:55=E2=80=AFPM David Rowley wrote: >> >> >> I'd like to know what led someone down the path of doing something >> like DEFAULT 'now()'::timestamp in a CREATE TABLE. Could it be a >> faulty migration tool that created these and people copy them thinking >> it's a legitimate syntax? >> > > My thought process on this used to be: Provide a text string of the expr= ession that is then stored within the catalog and eval'd during runtime. I= f the only thing you are providing is a single literal and not some compoun= d expression it isn't that obvious that you are supposed to provide an unqu= oted expression - which feels like it should be immediately evaluated - ver= sus something that is a constant. Kinda like dynamic SQL. I have a similar story to tell: I've honestly never thought about it deeply until I started this thread, but just through experimentation a few things were obvious: - now() as a function call gives you the current timestamp in a query - now() as a function call in a DDL DEFAULT clause sets that as a default function call - Quoting that function call (using the function call syntax is the natural thing to try, I think, if you've already done the first two) -- because some examples online show quoting it -- gives you DDL time evaluation. So I suspect -- though I've been doing this for so long I couldn't tell you for certain -- that I largely intuitive the behavior by observation. And similarly to David J. I'd then assumed -- but never had a need to test it -- that this was generalized. I think DDL is also different conceptually from SQL/DML here in a kind of insidious way: the "bare" function call in DEFAULT is *not* executed as part of the query for DDL like it is with other queries. Hope this helps explain things. James Coleman