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 1w5gZx-003WIv-1r for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 09:00:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1w5gZw-001lpe-05 for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Mar 2026 09:00: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.96) (envelope-from ) id 1w5gZv-001lpU-20 for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 09:00:36 +0000 Received: from mail-vs1-xe29.google.com ([2607:f8b0:4864:20::e29]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.98.2) (envelope-from ) id 1w5gZu-0000000178M-1Nbl for pgsql-hackers@lists.postgresql.org; Thu, 26 Mar 2026 09:00:35 +0000 Received: by mail-vs1-xe29.google.com with SMTP id ada2fe7eead31-604c237486dso197500137.2 for ; Thu, 26 Mar 2026 02:00:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1774515633; cv=none; d=google.com; s=arc-20240605; b=G69UDhlLsVT+Tg6ph+4Ac8Bi5DvcC640OzC7SBZhDfgtzWYZPUTVx0MWqcQmR2e4NC 9hiTrMCIPAJdcrxKKSgLpC4Q7nO38S1KO9wAXnOz64B2NVskPNgiq31EGDTHugDHX3Ki DUuv4KJSDlb9aUFJVFNxzEpWcQnwWO/Ugh4INHqj7OdontIgRaIiaCM0KlrB565qwngi IEoCXqeiy4PjpkfXVbCxs0XZcgvBFPVefpQo5lvThVlxdW0VFhSObT+4+7SuCqdi151Z MjGTX0lde3ExLHCE93+E4fqpgEMSu8/7kzjIwMGqavq7uXVPELiaMnLi2pOqUS9PmXRx 0IVg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:dkim-signature; bh=obl36IWY51KV9YIBWr9jriRGBHNnHB3ULRnW4IZYDqI=; fh=x2OuhkPyoekK+R+HGOb9MlZhUYf1powAZRTCbeay00A=; b=AoCWlqjn+ahLpgCwe/eOVrAcSZP2vkvCT6svV9+OCt2GO+gl2hl6rn9sZ1DsYUWyqb 7veo27B3weuy0d4NnaCcWY8ksXe5a1kjOi0t3aNNmycpM5hsRwAhmSQEVADjL6k7SsDT 7mdYLOBWTSG7lmjRW0R+XO5XJ4wJa5lgOOCHsAvBg3FjrGNtz4PtLg8/rbTAXbzlu238 yRN6tfrol0K8NChrLQQBatWXBW/ZEOSNmOp7cetHUqCn9kcE/WH13ZOAzwAhyI5HVWRf 8Z6Q3EksIwqRUgbx3M+8XiFnA9jxC7jBCPm+aLsQRKdBI4ZM8HwwkOzIu7NkmmYEdj0k mcQw==; darn=lists.postgresql.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1774515633; x=1775120433; darn=lists.postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=obl36IWY51KV9YIBWr9jriRGBHNnHB3ULRnW4IZYDqI=; b=B9WKaU3jrfVshWJKnabIPpz7ggTcj3KYGLVx2qHwv9QvCylXVLevIBXj1lUydv+V8y LPmAijIxlBoZBa/Z472pE5yk/xRAKwkpucZ5LpimNlUWdzgsCuupEDz3eKzHFfEKN+90 cerTpKicxqbJnBGXtrVvPmA0sEOWFIdVGPCeVsi/MMWSlc4VrhqwIzQOoX0fjLAdykJO NikZnGb/qig1mxS6rN1vgdHzlLa59OkeePC0V6/PlXskQYg7hnUFbs09G+UxxLqLaTTz 8m1PfE5ZB5OU14KyqUeKLq3HMEGlxqQboXpTYStdLMIR5I46wUONLhY0QfELHGixfdJr NhQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1774515633; x=1775120433; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=obl36IWY51KV9YIBWr9jriRGBHNnHB3ULRnW4IZYDqI=; b=qF1GtQ9oSmdWm356anUNUAOeEla28H8+VX+KX0qqmuODUbeKHWj/G5SlNKZlBN5Yjo abNKul0OYAw4i72IoncmwVbwj4t4AjKpQnJxgPKsU6UEtcVrAYN4XovM0p/pHJvRkCad Y/gmMDTAoM91qX7Jrj5uow/K5DmwJn2Uh1mld2CGa9fhMhSQwA/oMxLtjngBWzRQl599 ioz7pEDhOfvOy49HwG1DYiohBnG19ogSL+UYHRUaSaZVl44l77hFknbOIsFBMLh2n7XT e9hMXLPhjSegBtElwjnAOX4WR4gJE3rKWpxczZpzx4XnpFwDYjvkHvfAG+OKeFMUqMxH I8Mg== X-Forwarded-Encrypted: i=1; AJvYcCXnfXpBs08z65GbrKMDYWMopBg7arr0/XcMeDrUx0HFKqJ0EbgkDc67g59a7rODyzH90xZE4aJr5/VbnZcK@lists.postgresql.org X-Gm-Message-State: AOJu0YxyasJOFnvodrygLgZMhPAexPAWCwmUBUmsjLXMKad4//lxQcW3 57fODAenAEsXjiasKC6XjK4EHJFQx7mpX3NTIHRx4FNxlfq5ggNeipQPAh7l1TKriJ9RWG2SZAC 7HHTWZF9oV6JVRs49Ud+LIBCQrUhNKgk= X-Gm-Gg: ATEYQzz11PqV8Amk5qvgjti2I1rh47FR7ccIx9uOgTWo1T73W9ZXwx025IRD/thsyrB tji6sMLLSnUbxebDi7ngiAbWf+ycZYkCyeCFVWlpEC1W9tkoXkaAT4KNXYuShoObbwcTkI7yxEr lYjxtMdGR0bbfRttVUO6qJgMsauvx7yOcQZctd1BCuFS9xg2DOd0eeB1xN30MplSE2EmRFU62Xh kujn2DqYmGgMyj+CjOpYd/XYcJcONfR8iygQZ9ZtIGQLx0rlA7i7961CJrUUeFnugwZtJ1LfIdv f2oWa5APD6FdZ2TGaH3OQRhg+3HQABcE5iDOqPzQWSN2m6WirxN4A8ENaE2rmqgxLWgcedIlf17 IHQcKQEcWysLbL9tKc0AQW2a/enchnpjhNp1PmX3+vpU2Um3s+2E2i5nzthhCqWaBEIsnV6RDrD XhFahpeqo6onbzJ1ZuO7i4iQ== X-Received: by 2002:a05:6102:370d:b0:5f5:4055:4558 with SMTP id ada2fe7eead31-60378ff856bmr3567784137.2.1774515632936; Thu, 26 Mar 2026 02:00:32 -0700 (PDT) MIME-Version: 1.0 References: <5ae9578e-f25e-49c5-97ab-ad27bc2050b5@eisentraut.org> In-Reply-To: From: jian he Date: Thu, 26 Mar 2026 16:59:56 +0800 X-Gm-Features: AQROBzB9FZ_Ma2Y1nZLw4LFEU1GgvdKpz8gZ3pMLhLZe6_RNJppsK9keuEdO9yg Message-ID: Subject: Re: CAST(... ON DEFAULT) - WIP build on top of Error-Safe User Functions To: Peter Eisentraut Cc: Corey Huinker , Amul Sul , Kirill Reshke , Vik Fearing , Isaac Morland , pgsql-hackers@lists.postgresql.org Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Currently, when a valid ErrorSaveContext is passed to state->escontext (ExprState->escontext), ExecInitExprRec is designed to compile the entire expression tree using soft errors. Consider the following example: create table t1(a text, b text, c int, d int8); create domain d2 as text check (value <> 'a'); insert into t1(a, d) values('a', 2147483648); select cast (cast(d as int) as text default null on conversion error) from t1; -- queryA select cast (cast(a as d2) as text default null on conversion error) from t1; -- queryB select cast (cast(a as int) as text default null on conversion error) from t1; -- queryC With V24, for queryC, ExecInitExprRec will compile the inner ``cast(a as int)`` error-safe way. This is just a subexpression of the main outer cast(... as text default ...) node, we may want the inner cast to be evaluated not error-safe. To resolve this, we need to differentiate whether the specific nodes produced by coerce_to_target_type->build_coercion_expression/coerce_to_domain should be error-safe. This means the resulting nodes (FuncExpr, ArrayCoerceExpr, CoerceViaIO, CoerceToDomain) need a boolean flag. We have already solved this for FuncExpr in [1] by adding an errorsafe field. We now need to add a similar errorsafe boolean to CoerceViaIO and CoerceToDomain. Therefore, for safe error evaluation: 1. ExecInitExprRec, ExprState->escontext must be a valid ErrorSaveContext 2. The expression node (need a booleaan) to tell ExecInitExprRec that compile this in an error safe manner. [1]: https://postgr.es/m/CACJufxGbw9iNT8QVm4QD9cPFKnDnvDBQp7AGxkoqDa-JjzVXmg@mail.gmail.com Summary: Making the above queryA, queryB, and queryC return NULL is straightforward; however, forcing all of them to throw an error is non-trivial because it involves compiling/evaluating some subexpressions in an error-safe manner while others are not. What do you think? -- jian https://www.enterprisedb.com/