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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:58:30+00:00 2026-05-26T04:58:30+00:00

Given this type: — Just for testing purposes: CREATE TYPE testType as (name text)

  • 0

Given this type:

-- Just for testing purposes:
CREATE TYPE testType as (name text)

I can get the value of a field dynamically with this function:

CREATE OR REPLACE FUNCTION get_field(object anyelement, field text) RETURNS text as
$BODY$
DECLARE
    value text;
BEGIN
    EXECUTE 'SELECT $1."' || field || '"'
      USING object
       INTO value;

    return value;
END;
$BODY$
LANGUAGE plpgsql

Calling get_field('(david)'::testType, 'name') works as expected returning “david”.

But how can I set a value of a field in a composite type? I’ve tried these functions:

CREATE OR REPLACE FUNCTION set_field_try1(object anyelement, field text, value text)
RETURNS anyelement
as
$BODY$
DECLARE
    value text;
BEGIN
    EXECUTE '$1."' || field || '" := $2'
      USING object, value;

    return object;
END;
$BODY$
LANGUAGE plpgsql

CREATE OR REPLACE FUNCTION set_field_try2(object anyelement, field text, value text)
RETURNS anyelement
as
$BODY$
DECLARE
    value text;
BEGIN
    EXECUTE 'SELECT $1 INTO $2."' || field || '"'
      USING value, object;

    return object;
END;
$BODY$
LANGUAGE plpgsql

CREATE OR REPLACE FUNCTION set_field_try3(object anyelement, field text, value text)
RETURNS anyelement
as
$BODY$
DECLARE
    value text;
BEGIN
    EXECUTE 'BEGIN $1."' || field || '" := $2; SELECT $1; END;'
       INTO object
      USING value, object;

    return object;
END;
$BODY$
LANGUAGE plpgsql

and some variations.
Calling set_field_tryX doesn’t work. I always get “ERROR: syntax error at or near…”.
How can I accomplish this?

Notes:

  • The parameter is anyelement and the field can be any field in the composite type. I can’t just use object.name.
  • I’m concerned about SQL injection. Any advice in this would be appreciated but it is not my question.
  • 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-26T04:58:31+00:00Added an answer on May 26, 2026 at 4:58 am

    Faster with hstore

    Since Postgres 9.0, with the additional module hstore installed in your database there is a very simple and fast solution with the #= operator that …

    replace[s] fields in record with matching values from hstore.

    To install the module:

    CREATE EXTENSION hstore;
    

    Examples:

    SELECT my_record #= '"field"=>"value"'::hstore;  -- with string literal
    SELECT my_record #= hstore(field, value);        -- with values
    

    Values have to be cast to text and back, obviously.

    Example plpgsql functions with more details:

    • Endless loop in trigger function
    • Assign to NEW by key in a Postgres trigger

    Now works with json / jsonb, too!

    There are similar solutions with json (pg 9.3+) or jsonb (pg 9.4+)

    SELECT json_populate_record (my_record, json_build_object('key', 'new-value');
    

    The functionality was undocumented, but it’s official since Postgres 13. The manual:

    However, if base isn’t NULL then the values it contains will be used for unmatched columns.

    So you can take any existing row and fill arbitrary fields (overwriting what’s in it).

    Major advantages of json vs hstore:

    • works with stock Postgres so you don’t need an additional module.
    • also works for nested array and composite types.

    Minor disadvantage: a bit slower.

    See @Geir’s added answer for details.

    Without hstore and json

    If you are on an older version or cannot install the additional module hstore or cannot assume it’s installed, here is an improved version of what I posted previously. Still slower than the hstore operator, though:

    CREATE OR REPLACE FUNCTION f_setfield(INOUT _comp_val anyelement
                                              , _field text, _val text)
      RETURNS anyelement
      LANGUAGE plpgsql STABLE AS
    $func$
    BEGIN
    
    EXECUTE 'SELECT ' || array_to_string(ARRAY(
          SELECT CASE WHEN attname = _field
                    THEN '$2'
                    ELSE '($1).' || quote_ident(attname)
                 END AS fld
          FROM   pg_catalog.pg_attribute
          WHERE  attrelid = pg_typeof(_comp_val)::text::regclass
          AND    attnum > 0
          AND    attisdropped = FALSE
          ORDER  BY attnum
          ), ',')
    USING  _comp_val, _val
    INTO   _comp_val;
    
    END
    $func$;
    

    Call:

    CREATE TEMP TABLE t( a int, b text);  -- Composite type for testing
    SELECT f_setfield(NULL::t, 'a', '1');
    

    Notes

    • An explicit cast of the value _val to the target data type is not necessary, a string literal in the dynamic query would be coerced automatically, obviating the subquery on pg_type. But I took it one step further:

    • Replace quote_literal(_val) with direct value insertion via the USING clause. Saves one function call and two casts, and is safer anyway. text is coerced to the target type automatically in modern PostgreSQL. (Did not test with versions before 9.1.)

    • array_to_string(ARRAY()) is faster than string_agg().

    • No variables needed, no DECLARE. Fewer assignments.

    • No subquery in the dynamic SQL. ($1).field is faster.

    • pg_typeof(_comp_val)::text::regclass
      does the same as
      (SELECT typrelid FROM pg_catalog.pg_type WHERE oid = pg_typeof($1)::oid)
      for valid composite types, just faster.
      This last modification is built on the assumption that pg_type.typname is always identical to the associated pg_class.relname for registered composite types, and the double cast can replace the subquery. I ran this test in a big database to verify, and it came up empty as expected:

        SELECT *
        FROM   pg_catalog.pg_type t
        JOIN   pg_namespace  n ON n.oid = t.typnamespace
        WHERE  t.typrelid > 0  -- exclude non-composite types
        AND    t.typrelid IS DISTINCT FROM
              (quote_ident(n.nspname ) || '.' || quote_ident(typname))::regclass
    
    • The use of an INOUT parameter obviates the need for an explicit RETURN. This is just a notational shortcut. Pavel won’t like it, he prefers an explicit RETURN statement …

    Everything put together this is twice as fast as the previous version.


    Original (outdated) answer:

    The result is a version that’s ~ 2.25 times faster. But I probably couldn’t have done it without building on Pavel’s second version.

    In addition, this version avoids most of the casting to text and back by doing everything within a single query, so it should be much less error prone.
    Tested with PostgreSQL 9.0 and 9.1.

    CREATE FUNCTION f_setfield(_comp_val anyelement, _field text, _val text)
      RETURNS anyelement
      LANGUAGE plpgsql STABLE AS
    $func$
    DECLARE
       _list text;
    BEGIN
    _list := (
       SELECT string_agg(x.fld, ',')
       FROM  (
          SELECT CASE WHEN a.attname = $2
                  THEN quote_literal($3) || '::'|| (SELECT quote_ident(typname)
                                                    FROM   pg_catalog.pg_type
                                                    WHERE  oid = a.atttypid)
                  ELSE quote_ident(a.attname)
                 END AS fld
          FROM   pg_catalog.pg_attribute a 
          WHERE  a.attrelid = (SELECT typrelid
                               FROM   pg_catalog.pg_type
                               WHERE  oid = pg_typeof($1)::oid)
          AND    a.attnum > 0
          AND    a.attisdropped = false
          ORDER  BY a.attnum
          ) x
       );
    
    EXECUTE 'SELECT ' || _list || ' FROM  (SELECT $1.*) x'
    USING  $1
    INTO   $1;
    
    RETURN $1;
    END
    $func$;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this schema: +-----------------+------------------+------+-----+-------------------+-------+ | Field | Type | Null | Key | Default
Given this table : mysql> describe activity; +---------------------------+-------------+------+-----+---------+-------+ | Field | Type | Null
Given this data set: ID Name City Birthyear 1 Egon Spengler New York 1957
Given this XML ... <ListBucketResult xmlns=http://s3.amazonaws.com/doc/2006-03-01/> <Name>public.rpmware.com</Name> <Prefix></Prefix> <Marker></Marker> <MaxKeys>1000</MaxKeys> <IsTruncated>false</IsTruncated> <Contents> <Key>0.dir</Key> <LastModified>2008-06-25T16:09:49.000Z</LastModified>
Given this: abstract class ViewPresenterPair { type V <: View type P <: Presenter
I have this piece of code: <script language=javascript type=text/jscript> document.write(<img src='http://dm.leadgenesys.com/jpgp.lgt?en=P.........TP_Q=&amp;ur=' + escape(document.referrer) +
By way of explanation, take this value type in C#: struct ObjRef { public
Setup Given this user-defined type: struct T { static int x; int y; T()
Given this text file: Received: from unknown (HELO aws-bacon-delivery-svc-iad-1007.vdc.g.com) ([10.146.157.151]) by na-mm-outgoing-6102-bacon.iad6.g.com with ESMTP;
Given this code: public class Car { public virtual int CarId { get; set;

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.