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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:06:56+00:00 2026-06-12T06:06:56+00:00

I’m trying to create new aggregate function in PostgreSQL to use instead of the

  • 0

I’m trying to create new aggregate function in PostgreSQL to use instead of the sum() function

I started my journey in the manual here.

Since I wanted to create a function that takes an array of double precision values, sums them and then does some additional calculations I first created that final function:

takes double precision as input and gives double precision as output    
DECLARE
      v double precision;
BEGIN
      IF tax > 256 THEN
            v := 256;
      ELSE
            v := tax;
      END IF;
      RETURN v*0.21/0.79;
END;

Then I wanted to create the aggregate function that takes an array of double precision values and puts out a single double precision value for my previous function to handle.

CREATE AGGREGATE aggregate_ee_income_tax (float8[]) (
  sfunc = array_agg
 ,stype = float8
 ,initcond = '{}'
 ,finalfunc = eeincometax);

What I get when I run that command is:

ERROR: function array_agg(double precision, double precision[]) does
not exist

I’m somewhat stuck here, because the manual lists array_agg() as existing function. What am I doing wrong?

Also, when I run:

\da
                     List of aggregate functions
 Schema | Name | Result data type | Argument data types | Description 
--------+------+------------------+---------------------+-------------
(0 rows)

My installation has no aggregate functions at all? Or does only list user defined functions?

Basically what I’m trying to understand:

1) Can I use an existing functions to sum up my array values?

2) How can I find out about input and ouptut data types of functions? Docs claim that array_agg() takes any kind of input.

3) What is wrong with my own aggregate function?

Edit 1

To give more information and clearer picture of what I’m trying to achieve:

I have one huge query over several tables which goes something like this:

SELECT sum(tax) ... from (SUBQUERY) as foo group by id

I want to replace that sum function with my own aggregate function so I don’t have to do additional calculations on backend – since they can all be done on database level.

Edit 2

Accepted Ants’s answer. Since final solution comes from comments I post it here for reference:

CREATE AGGREGATE aggregate_ee_income_tax (float8)
(
 sfunc = float8pl
,stype = float8
,initcond = '0.0'
,finalfunc = eeincometax
);
  • 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-12T06:06:58+00:00Added an answer on June 12, 2026 at 6:06 am

    Array agg is an aggregate function not a regular function, so it can’t be used as a state transition function for a new aggregate. What you want to do is to create an aggregate function which has a state transition function that is identical to array_agg and a custom final func.

    Unfortunately the state transition function of array_agg is defined in terms of an internal datatype so it can’t be reused. Fortunately there is an existing function in core that already does what you want.

    CREATE AGGREGATE aggregate_ee_income_tax (float8)(
        sfunc = array_append,
        stype = float8[],
        initcond = '{}',
        finalfunc = eeincometax);
    

    Also note that you had your types mixed up, you probably want aggregate a set of floats to an array, not a set of arrays to a float.

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

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group

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.