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 7925577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:21:15+00:00 2026-06-03T18:21:15+00:00

Is there any way to have a function in PostgreSQL return an arbitrary type?

  • 0

Is there any way to have a function in PostgreSQL return an arbitrary type?

I am trying to use PLV8 to grab and compare fields from a JSON type in PostgreSQL 9.2.

The following works:

CREATE or replace FUNCTION jn (j json, key any ) RETURNS numeric 
LANGUAGE plv8   
IMMUTABLE 
AS $function$  
  var ej = JSON.parse(j);   
  if (typeof ej != 'object') return NULL;   
  return JSON.stringify(ej[key]);  
$function$;

SELECT * FROM things WHERE jn(data,'count') <= '10';

Returning the where the count field is <= 10. BUT, it fails if the field is not numeric.
I don’t want to create a specific function for every type of data my json might hold, is there a way to have the data type inferred somehow and cast implicitly?

See also How do I query using fields inside the new PostgreSQL JSON datatype?

  • 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-06-03T18:21:18+00:00Added an answer on June 3, 2026 at 6:21 pm

    You seem to be looking for functions with Polymorphic types.
    Read the above section on types and the last 2 examples in this section.

    Note, that types of the key parameter and function’s output should match. If this is not your case, then you should probably return text and then cast it to the right type outside the function.

    I think this will do the job for you:

    CREATE or replace FUNCTION jn (j json, key anyelement ) RETURNS anyelement
    LANGUAGE plv8   
    IMMUTABLE 
    AS $function$  
      var ej = JSON.parse(j);   
      if (typeof ej != 'object') return NULL;   
      return JSON.stringify(ej[key]);  
    $function$;
    

    Sorry, I don’t have 9.2 server around so I cannot test this.


    EDIT:

    Things is — before you will be able to perform any casts within the function, it must be created first. At creation time function’s input parameters’ types and return type is fixed. This means you have only 2 possibilities:

    • use anyelement type, which will force you to have key parameter having the same type you want to be returned from the function;
    • use text type and cast results outside the function.

    As PLV8 doesn’t support anyelement types yet, you can actually create a wrapper function on PL/pgSQL on top of it to do the job for you:

    CREATE FUNCTION jn_wrap(jn varchar, key anyelement, OUT ret anyelement)
        AS $js_wrap$
    BEGIN
        EXECUTE 'SELECT CAST(jn($1, $2) AS '||pg_typeof(key)||')'
            USING jn, key INTO ret;
    
        RETURN ;
    END;
    $js_wrap$ LANGUAGE plpgsql;
    

    Though I think it might be quite typical, that key‘s type will differ from the desired return type.

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

Sidebar

Related Questions

Is there any way to have a query return say 8 fields but only
I have a javascript function that runs every 3 seconds, is there any way
Is there any way to have Python operators line == and > return ints
Is there any way to know the type of the FileStream . I have
Is there any way to have a JavaScript function make a <BR/> tag active
Is there any way to have PHP automatically call a function, before a script
is there any way to have a tail-recursive function inside CPS not throwing a
Is there any way (hack) to push Python function (def) to return results by
Is there any way to have a label wordwrap text as needed? I have
Is there any way to have to tabcontrol take the size of the largest

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.