pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Joe Conway <mail@joeconway.com>
Cc: Philip Semanchuk <philip@americanefficient.com>
Cc: PostgreSQL General <pgsql-general@lists.postgresql.org>
Subject: Re: IMMUTABLE function to cast enum to/from text?
Date: Thu, 10 Nov 2022 15:39:08 -0500
Message-ID: <229957.1668112748@sss.pgh.pa.us> (raw)
In-Reply-To: <219ec605-315d-fe5f-c0b1-80ece0801946@joeconway.com>
References: <EBFE2A74-B421-4494-8FCC-9F072F128188@americanefficient.com>
	<219ec605-315d-fe5f-c0b1-80ece0801946@joeconway.com>

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





view thread (4+ messages)  latest in thread

Message-ID: <229957.1668112748@sss.pgh.pa.us>
Permalink:  ../229957.1668112748@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/229957.1668112748@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: tgl@sss.pgh.pa.us, mail@joeconway.com, philip@americanefficient.com
  Subject: Re: IMMUTABLE function to cast enum to/from text?
  In-Reply-To: <229957.1668112748@sss.pgh.pa.us>
  References: <EBFE2A74-B421-4494-8FCC-9F072F128188@americanefficient.com> <219ec605-315d-fe5f-c0b1-80ece0801946@joeconway.com> <229957.1668112748@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox