Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6804607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:29:14+00:00 2026-05-26T19:29:14+00:00

Can functions be stored as anonymous functions directly in column as its value? Let’s

  • 0

Can functions be stored as anonymous functions directly in column as its value?

Let’s say I want this function be stored in column.
Example (pseudocode):

Table my_table: pk (int), my_function (func)

func ( x ) { return x * 100 }

And later use it as:

select 
    t.my_function(some_input) AS output
from 
    my_table as t 
where t.pk = 1999

Function may vary for each pk.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:29:15+00:00Added an answer on May 26, 2026 at 7:29 pm

    Your title asks something else than your example.

    1. A function has to be created before you can call it. (title)
    2. An expression has to be evaluated. You would need a meta-function for that. (example)

    Here are solutions for both:

    1. Evaluate expressions dynamically

    You have to take into account that the resulting type can vary. I use polymorphic types for that.

    CREATE OR REPLACE FUNCTION f1(int)
      RETURNS int
      LANGUAGE sql IMMUTABLE AS
    'SELECT $1 * 100;';
    
    CREATE OR REPLACE FUNCTION f2(text)
      RETURNS text
      LANGUAGE sql IMMUTABLE AS
    $$SELECT $1 || '_foo';$$;
    
    CREATE TABLE my_expr (
      expr    text PRIMARY KEY
    , def     text
    , rettype regtype
    );
    
    INSERT INTO my_expr VALUES
      ('x', 'f1(3)'      , 'int')
    , ('y', $$f2('bar')$$, 'text')
    , ('z', 'now()'      , 'timestamptz')
    ;
    
    CREATE OR REPLACE FUNCTION f_eval(text, _type anyelement = 'NULL'::text, OUT _result anyelement)
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       EXECUTE
       'SELECT ' || (SELECT def FROM my_expr WHERE expr = $1)
       INTO _result;
    END
    $func$;
    

    Related:

    • Refactor a PL/pgSQL function to return the output of various SELECT queries

    Call:

    SQL is strictly typed, the same result column can only have one data type. For multiple rows with possibly heterogeneous data types, you might settle for type text, as every data type can be cast to and from text:

    SELECT *, f_eval(expr) AS result  -- default to type text
    FROM   my_expr;
    

    Or return multplce columns like:

    SELECT *
         , CASE WHEN rettype = 'text'::regtype        THEN f_eval(expr) END                    AS text_result  -- default to type text
         , CASE WHEN rettype = 'int'::regtype         THEN f_eval(expr, NULL::int) END         AS int_result
         , CASE WHEN rettype = 'timestamptz'::regtype THEN f_eval(expr, NULL::timestamptz) END AS tstz_result
      -- , more?
    FROM   my_expr;
    

    db<>fiddle here

    2. Create and use functions dynamically

    It is possible to create functions dynamically and then use them. You cannot do that with plain SQL, however. You will have to use another function to do that or at least an anonymous code block (DO statement), introduced in PostgreSQL 9.0.

    It can work like this:

    CREATE TABLE my_func (func text PRIMARY KEY, def text);
    
    INSERT INTO my_func VALUES
      ('f'
     , $$CREATE OR REPLACE FUNCTION f(int)
      RETURNS int
      LANGUAGE sql IMMUTABLE AS
    'SELECT $1 * 100;'$$);
    
    CREATE OR REPLACE FUNCTION f_create_func(text)
      RETURNS void
      LANGUAGE plpgsql AS
    $func$
    BEGIN
       EXECUTE (SELECT def FROM my_func WHERE func = $1);
    END
    $func$;
    

    Call:

    SELECT f_create_func('f');
    SELECT f(3);
    

    db<>fiddle here

    You may want to drop the function afterwards.

    In most cases you should just create the functions instead and be done with it. Use separate schemas if you have problems with multiple versions or privileges.

    For more information on the features I used here, see my related answer on dba.stackexchange.com.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that __stdcall functions can't have ellipses, but I want to be sure
It's possible to call C functions through CREATE FUNCTION but how can .NET functions
I want a std::vector to contain some functions, and that more functions can be
Where are functions stored in a C++ program? For example int abc() { //where
Is it possible to create a stored procedure that can call a function/generate C#
Python doesn't support complicated anonymous functions. What's a good alternative? For example: class Calculation:
How can I see the list of the stored procedures or stored functions in
I know that functions can be stored on html attributes for specific events (e.g.
I made a system that creates a simple string with Function/Response format, example: Check('Value'):ShowImage(@)|Check('Value'):OtherFunction(@)....and
How can I tell closure compiler that an anonymous function should not be removed

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.