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 1otEKR-0004r6-C7 for pgsql-general@arkaria.postgresql.org; Thu, 10 Nov 2022 20:39:15 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1otEKQ-0001VN-Ag for pgsql-general@arkaria.postgresql.org; Thu, 10 Nov 2022 20:39:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1otEKP-0001VD-VU for pgsql-general@lists.postgresql.org; Thu, 10 Nov 2022 20:39:13 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1otEKN-0005a0-Q7 for pgsql-general@lists.postgresql.org; Thu, 10 Nov 2022 20:39:12 +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 2AAKd8Dr229958; Thu, 10 Nov 2022 15:39:08 -0500 From: Tom Lane To: Joe Conway cc: Philip Semanchuk , PostgreSQL General Subject: Re: IMMUTABLE function to cast enum to/from text? In-reply-to: <219ec605-315d-fe5f-c0b1-80ece0801946@joeconway.com> References: <219ec605-315d-fe5f-c0b1-80ece0801946@joeconway.com> Comments: In-reply-to Joe Conway message dated "Thu, 10 Nov 2022 15:18:19 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <229956.1668112748.1@sss.pgh.pa.us> Date: Thu, 10 Nov 2022 15:39:08 -0500 Message-ID: <229957.1668112748@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Joe Conway writes: > On 11/10/22 14:52, Philip Semanchuk wrote: >> I have seen conversations that suggest creating a custom IMMUTABLE >> function to perform the cast, but I can't figure out how to do that >> except with a CASE statement that enumerates every possible value. Is >> there a more elegant approach? > CREATE OR REPLACE FUNCTION mood2text(mood) > RETURNS text AS > $$ > select $1 > $$ STRICT IMMUTABLE LANGUAGE sql; Of course, what this is doing is using a SQL-function wrapper to lie about the mutability of the expression. Whether you consider that elegant is up to you ;-) ... but it should work, as long as you don't break things by renaming the enum's values. regards, tom lane