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 1otE0L-0003pJ-VG for pgsql-general@arkaria.postgresql.org; Thu, 10 Nov 2022 20:18:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1otE0K-0001bN-H1 for pgsql-general@arkaria.postgresql.org; Thu, 10 Nov 2022 20:18:28 +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 1otE0J-0001bD-Pp for pgsql-general@lists.postgresql.org; Thu, 10 Nov 2022 20:18:28 +0000 Received: from mail-qt1-x830.google.com ([2607:f8b0:4864:20::830]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1otE0E-0007oR-Gd for pgsql-general@lists.postgresql.org; Thu, 10 Nov 2022 20:18:27 +0000 Received: by mail-qt1-x830.google.com with SMTP id h21so1643107qtu.2 for ; Thu, 10 Nov 2022 12:18:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=joeconway.com; s=google; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=UBUx4Wh0B7mzPKTaJkV/hVCfiyKlNsQxyubg7B97ldg=; b=hIjcQdiEr9peZmTsQ4f49cw9Xq89regfAIh+9nJE/pnGFf5ygo9Aoqka77bcr7vO4m ExXRSwaC3b3iBjvFvjjQx2NVQ/fS+HzA3jNME4OSXk8PLX4xKr+JS6PzgnN71GKhQSrl cgVj5+E6BpAgUVvO2kIqvRudwGqP7m8kmfiVlyyUEVf/2J8H6VHFR7BDnfchdBvGlfmD ekAwYjq67gZ703fd39jDHscsLLtqBaDDzZIK+6lolN2a+9MNdnBrE8c28hFO+iMPSMn6 Kl6as1MGKdhFE0/NF0t7Zv3u1L8Kl8dnSeD87Jf5t/skzW61ldjOKyQ3d3EJXUuFoKRl l5Fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:in-reply-to:from:references:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UBUx4Wh0B7mzPKTaJkV/hVCfiyKlNsQxyubg7B97ldg=; b=Ad6AnxZA+aMKiOtGt9zRMGQBq9FJdDGFKu9Fw9Lr6CBsZV6jKptVjRude3iJdtKmdZ LDkCwV1LqktM6zsk61wh19KKoiMwvHJxoiFQyHNoIFCE0HcSIhO1CJ7tLnCWxzeaH40a gl6GKzqHrAFBNpp+9hK7a6w0BUFsfH5KXv+1F5z+iBMHYDAnXh+/66T/wmRXRYtga/yP IEB3MKJEWsrZqCgPTAMAVu41d4aHgULsq0Xc5fWqFrH238YEkg3iUEc/Y8hiIHQhvHXs W6BWgT5zkXIBNCsebE3BLrrwpdO3VC1BaUG2qKWcJAjRruR+g76cllBDRfa7nzb5w5Gl n7Aw== X-Gm-Message-State: ACrzQf1yoe1s63DxQoyoiIgCFq5pXXnKW5MOGzwbCMKb1w6V+Tk8lm1F 0qDv/nK+j+d0RwhlGuYAzkXeIhDUy4bwjQ== X-Google-Smtp-Source: AMsMyM4ZpEA0tPfULlskBN+bqzkmy4T7KZVK9tfpze7Oyl1KDOku0QUml0PVUiLCuh6YAc2R31YjXQ== X-Received: by 2002:a05:622a:316:b0:3a4:ea22:d19e with SMTP id q22-20020a05622a031600b003a4ea22d19emr54036399qtw.467.1668111500250; Thu, 10 Nov 2022 12:18:20 -0800 (PST) Received: from [192.168.4.41] (162-239-31-113.lightspeed.dybhfl.sbcglobal.net. [162.239.31.113]) by smtp.gmail.com with ESMTPSA id j184-20020a3755c1000000b006f9ddaaf01esm133525qkb.102.2022.11.10.12.18.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 10 Nov 2022 12:18:19 -0800 (PST) Message-ID: <219ec605-315d-fe5f-c0b1-80ece0801946@joeconway.com> Date: Thu, 10 Nov 2022 15:18:19 -0500 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: IMMUTABLE function to cast enum to/from text? Content-Language: en-US To: Philip Semanchuk , PostgreSQL General References: From: Joe Conway In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/10/22 14:52, Philip Semanchuk wrote: > Hi all, > I know that Postgres' enum_in()/enum_out() functions have a > volatility class of STABLE, and STABLE is required because enum > elements can be renamed. We have an enum in our database used in a > number of custom functions, all of which require casting the enum > to/from text. Since enum_in() and enum_out() are STABLE, that means > our functions that rely on those casts must also be STABLE, and as a > result we can't use them in generated columns. > > 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? When asking for help here, it always helps us to help you if you provide a self-contained set of SQL that illustrates what you are looking for. That said, I think you are looking for something like this: CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy'); CREATE TABLE person (name text, current_mood mood); INSERT INTO person VALUES ('Moe', 'happy'); CREATE OR REPLACE FUNCTION mood2text(mood) RETURNS text AS $$ select $1 $$ STRICT IMMUTABLE LANGUAGE sql; SELECT name, mood2text(current_mood) FROM person; name | mood2text ------+----------- Moe | happy (1 row) HTH, -- Joe Conway PostgreSQL Contributors Team RDS Open Source Databases Amazon Web Services: https://aws.amazon.com