hi, I'd like to create a function that returns a t...
# help
s
hi, I'd like to create a function that returns a tableset + some extra properties, i.e an average of a certain value from that tableset, or perhaps that tableset + a foreign table. Currently I only know how to do
setof <table_name>
, but I can't find any documentation on how to extend the set to include other properties or perhaps other tables
n
Hello @SkullCutter! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
a
If your function output should contain heterogeneous data, I'd suggest to use plv8 and return a json object. e.g.,
Copy code
create or replace function functionName() returns json
language plv8
as
$$
  var somequery = plv8.execute('<your query>');
  var result = {'key': somequery};
  
  result['otherdata'] = [];
  
  var otherdata = plv8.execute('<other query>');
  for (const res of otherdata) {
    result['otherdata'].push(res);
  }

  return result;
$$;
n
SkullCutter (2022-05-30)
s
@SkullCutter You can use:
Copy code
sql
CREATE FUNCTION.. RETURNS TABLE(col1 int, col2 text)
To return columns from different tables. https://www.postgresql.org/docs/current/xfunc-sql.html#XFUNC-SQL-FUNCTIONS-RETURNING-TABLE
s
oic, so every time I change my table, I would also have to change the return type of the function?
a
Every time you change your table, you'd have to update your function in order to return the correct data, regardless of how the returning structure looks like.