Re: IMMUTABLE function to cast enum to/from text? - Mailing list pgsql-general

From Tom Lane
Subject Re: IMMUTABLE function to cast enum to/from text?
Date
Msg-id 229957.1668112748@sss.pgh.pa.us
Whole thread Raw
In response to Re: IMMUTABLE function to cast enum to/from text?  (Joe Conway <mail@joeconway.com>)
Responses Re: IMMUTABLE function to cast enum to/from text?  (Philip Semanchuk <philip@americanefficient.com>)
List pgsql-general
Joe Conway <mail@joeconway.com> 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



pgsql-general by date:

Previous
From: Joe Conway
Date:
Subject: Re: IMMUTABLE function to cast enum to/from text?
Next
From: Philip Semanchuk
Date:
Subject: Re: IMMUTABLE function to cast enum to/from text?