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 1sNdFL-009els-Ku for pgsql-hackers@arkaria.postgresql.org; Sat, 29 Jun 2024 18:56:27 +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 1sNdFK-00AEIK-2r for pgsql-hackers@arkaria.postgresql.org; Sat, 29 Jun 2024 18:56:26 +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 1sNdFJ-00AEIC-Pw for pgsql-hackers@lists.postgresql.org; Sat, 29 Jun 2024 18:56:26 +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 1sNdFD-004Baf-Kl for pgsql-hackers@lists.postgresql.org; Sat, 29 Jun 2024 18:56:25 +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 45TIuFs12232739; Sat, 29 Jun 2024 14:56:15 -0400 From: Tom Lane To: Alvaro Herrera cc: Amit Langote , jian he , Peter Eisentraut , PostgreSQL Hackers Subject: Re: pgsql: Add more SQL/JSON constructor functions In-reply-to: <202406291824.reofujy7xdj3@alvherre.pgsql> References: <202406291824.reofujy7xdj3@alvherre.pgsql> Comments: In-reply-to Alvaro Herrera message dated "Sat, 29 Jun 2024 20:24:27 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2232737.1719687375.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 29 Jun 2024 14:56:15 -0400 Message-ID: <2232738.1719687375@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Alvaro Herrera writes: >> + /* >> + * For domains, consider the base type's typmod to decide whether to = setup >> + * an implicit or explicit cast. >> + */ >> + if (get_typtype(returning->typid) =3D=3D TYPTYPE_DOMAIN) >> + (void) getBaseTypeAndTypmod(returning->typid, &baseTypmod); > TBH I'm not super clear on why we decide on explicit or implicit cast > based on presence of a typmod. Why isn't it better to always use an > implicit one? Hmm ... there are a bunch of existing places that seem to have similar logic, but they are all in new-ish SQL/JSON functionality, and I would not be surprised if they are all wrong. parse_coerce.c is quite opinionated about what a domain's typtypmod means (see comments in coerce_type() for instance); see also the logic in coerce_to_domain: * If the domain applies a typmod to its base type, build the appropri= ate * coercion step. Mark it implicit for display purposes, because we d= on't * want it shown separately by ruleutils.c; but the isExplicit flag pa= ssed * to the conversion function depends on the manner in which the domai= n * coercion is invoked, so that the semantics of implicit and explicit * coercion differ. (Is that really the behavior we want?) I don't think that this SQL/JSON behavior quite matches that. While I'm bitching ... this coding style is bogus anyway: >> + if (get_typtype(returning->typid) =3D=3D TYPTYPE_DOMAIN) >> + (void) getBaseTypeAndTypmod(returning->typid, &baseTypmod); because it results in two syscache lookups not one. You are supposed to apply getBaseTypeAndTypmod unconditionally, as is done everywhere except in the SQL/JSON logic. I am also wondering how it can possibly be sensible to throw away the function result of getBaseTypeAndTypmod in this context. regards, tom lane