Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgasM-00012e-1J for pgsql-novice@arkaria.postgresql.org; Mon, 27 Mar 2023 00:38:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pgasK-0006Ja-Mz for pgsql-novice@arkaria.postgresql.org; Mon, 27 Mar 2023 00:38:16 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pgasK-0006JR-Bh for pgsql-novice@lists.postgresql.org; Mon, 27 Mar 2023 00:38:16 +0000 Received: from mail-lf1-x12d.google.com ([2a00:1450:4864:20::12d]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1pgasH-0003tW-JA for pgsql-novice@lists.postgresql.org; Mon, 27 Mar 2023 00:38:16 +0000 Received: by mail-lf1-x12d.google.com with SMTP id h11so2203490lfu.8 for ; Sun, 26 Mar 2023 17:38:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679877491; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=3T2THgrOa1hOyPclZuLRerHzZ3U4iTDfCmKw4kATh0Y=; b=AFzbJcstEcM560XKkQ2hfRzvs07pvE34Hukz1WFDcID3hykPIJw14wMtD7cyKGHSyu QtZu4BRSRIhi/tkvgtKpqCjaBRAfzcKdH6foynH0gtVT5B5UIBKcRwZZGTSLlBH4R/1O hiIIqc8IpKSoYkuKOE2mEc/h11pyFOVd+33SO055J0kxcQegYJimF/T9NVDEGIfXTRf0 PO0Iyha7nlhDGeANVuvOYNr3+gztCh903vEkfe8N8dFUkIYRJ4tdpTptb7dFyH2dVAi5 cPAh91s081VBTCOU56X+qVMNCYKp94hX7RZhm4qql2+5UGvZjzdPYSPrbwvzyecQfJfo 07OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679877491; h=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=3T2THgrOa1hOyPclZuLRerHzZ3U4iTDfCmKw4kATh0Y=; b=2n7gzkjO5hH/JWUH46H+u/EZHmCPeHmKKldeqSUTDopMraP4AmxJwoYDP2BwxukKqu gAJLsTHyaxXddMbOHPr+wr823uYHv2Xtx98twENHcbuCI3qo+ov5XNN7GwE7i60tl7EL 2tmss2n/oSpt4UPuAZf1i3P5YwTA+XiLc2wRNjbGdtJBTjW38L01jEBejE01qAgiNVkh 53+ZFFUrwYXyVUxiFkYlmpBJTN9xkNUCulxO8EGy6qIp+f5edFckF4E6yuYkFy07sxQm pNGxYj/POys8Sc7SnbBF3bJfpk1yOmASyi7SMx3hhRMnq0FJIu++zI8WNVGQCsRORijK +yDw== X-Gm-Message-State: AAQBX9dEK5RC6sxPPaLYXEGl86ZHMcrH5nCn8AHlwciHZUcLEAO3zD8N yZq3HrCqLwVVHxaI0fWCHa1cFaTgWJRiBXT7+K4= X-Google-Smtp-Source: AKy350aD/z4nNWXCXczobuzYhkdYzQf6wukx9BiCbsObwD8v/Svp8MjT16lgh9terNHCKTwirfb8FqZ2YZHnV2yk35A= X-Received: by 2002:ac2:4a9d:0:b0:4e8:9988:f8ab with SMTP id l29-20020ac24a9d000000b004e89988f8abmr2764974lfp.11.1679877491087; Sun, 26 Mar 2023 17:38:11 -0700 (PDT) MIME-Version: 1.0 References: <2911010.1679844383@sss.pgh.pa.us> <2914598.1679846405@sss.pgh.pa.us> In-Reply-To: <2914598.1679846405@sss.pgh.pa.us> From: Valerio Battaglia Date: Mon, 27 Mar 2023 02:37:59 +0200 Message-ID: Subject: Re: How to get column, table or parameter name reporting when violating DOMAIN type constraint To: Tom Lane Cc: "David G. Johnston" , "pgsql-novice@lists.postgresql.org" Content-Type: multipart/alternative; boundary="0000000000001f5fc105f7d6f579" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --0000000000001f5fc105f7d6f579 Content-Type: text/plain; charset="UTF-8" Thanks for the answer and explanation. My reasoning behind the use of domains, particularly in function calls, is to have a useful approach for validating input before it enters a function. By doing so, it eliminates the need to manually verify input for public / exposed functions, and would effectively make domains provide an efficient way to encapsulate and enforce business rules. Considering the aforementioned example, having a message reporting with the position name would make the consumer life much easier, program or human alike SELECT my_function(100, -100); -- ERROR: value "second_parameter" for domain my_domain violates check constraint "value_min" SELECT (-1)::my_domain; -- ERROR: value "unnamed" for domain my_domain violates check constraint "value_min" Valerio On Sun, 26 Mar 2023 at 18:00, Tom Lane wrote: > "David G. Johnston" writes: > > On Sunday, March 26, 2023, Tom Lane wrote: > >> More to the point, you have the wrong mental model: a domain constraint > >> violation might not be associated with a table column at all. > > > I disagree that the mental model is wrong. > > The OP is asking for action-at-a-distance. There are probably cases > where the distance is short enough that we could associate the runtime > error with a particular insertion target column, but I don't think it > could possibly be made to work for every sort of insert/select query. > In any case, the possibility of a hypothetical future feature (which > nobody is actively working on AFAIK) doesn't seem like a very useful > answer here. > > > There are existing threads that I may hunt later. IIRC, you (Tom) even > > suggested a possible way forward on this last time it came up. > > I recall proposing that we try to produce syntax-error-like error > cursors for runtime errors, but it's not apparent to me that that'd > be tremendously applicable to the OP's problem. The output would > look something like > > ERROR: value for domain my_domain violates check constraint "value_min" > LINE 1: SELECT my_function(100, -100); > ^ > > which might be helpful for a human, but probably not very much so > for a program. (BTW, this illustrates another issue: I'm pretty > sure that in the given case, the error is thrown while evaluating > my_function's arguments, long before we get within hailing distance > of any INSERT at all.) > > regards, tom lane > --0000000000001f5fc105f7d6f579 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Thanks for the answer and explanation. My reason= ing behind the use of domains, particularly in function calls, is to have a useful=20 approach for validating=20 input before it enters a function. By doing so, it eliminates the need=20 to manually verify input for public / exposed functions, and would effectiv= ely make domains provide an=20 efficient way to encapsulate and enforce business rules.

= Considering the aforementioned example, having a message reporting with the=20 position name would make the consumer life much easier, program or human alike
SELECT my_function(100, -100);
-- ERROR: val= ue "second_parameter" for domain my_domain violates = check constraint "value_min"

SEL= ECT (-1)::my_domain;
-- ERROR: value "unnamed" for domain my_domain violates check constrain= t "value_min"

Valerio
=

On Sun, 26 Mar 2023 at 18:00, Tom Lane <tgl@sss.pgh.pa.us> wrote:
"David G. Johnston" <david.g.johnston@gmail.c= om> writes:
> On Sunday, March 26, 2023, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> More to the point, you have the wrong mental model: a domain const= raint
>> violation might not be associated with a table column at all.

> I disagree that the mental model is wrong.

The OP is asking for action-at-a-distance.=C2=A0 There are probably cases where the distance is short enough that we could associate the runtime
error with a particular insertion target column, but I don't think it could possibly be made to work for every sort of insert/select query.
In any case, the possibility of a hypothetical future feature (which
nobody is actively working on AFAIK) doesn't seem like a very useful answer here.

> There are existing threads that I may hunt later.=C2=A0 IIRC, you (Tom= ) even
> suggested a possible way forward on this last time it came up.

I recall proposing that we try to produce syntax-error-like error
cursors for runtime errors, but it's not apparent to me that that'd=
be tremendously applicable to the OP's problem.=C2=A0 The output would<= br> look something like

ERROR: value for domain my_domain violates check constraint "value_min= "
LINE 1: SELECT my_function(100, -100);
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ^

which might be helpful for a human, but probably not very much so
for a program.=C2=A0 (BTW, this illustrates another issue: I'm pretty sure that in the given case, the error is thrown while evaluating
my_function's arguments, long before we get within hailing distance
of any INSERT at all.)

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 regards, tom lane
--0000000000001f5fc105f7d6f579--