0

I have a separate schema called "api" where all my "front-end" functions reside. My API server only have access to this schema. This works great for getting information out of the database in a controlled way.

To insert data in a controlled way I can either create functions in the api schema. But functions with many parameters (i.e. all fields to be inserted) are cumbersome to work with because:

  • It's easy to mix up parameters in calls

  • I don't know of any syntax to call functions with named parameters

So, I'm thinking of creating "proxy-views" in my api schema instead. The views would show no data (i.e. WHERE false). The API server would do ordinary inserts into these views and a trigger would take care of the actual insert logic.

The code for inserting data with INSERT statements are more readable than calling functions with a list of unnamed parameters.

  • Have I missed anything?
  • Is this a bad idea?
  • Maybe there is someway to name the input parameters for functions?

Thanks!

2
  • There are rdbms which allow updateable views. If you put triggers behind it however you have a lot of business logic in the DB... this might or might not be what you want (it’s uncommon for microservices). Same problem with stored procedures or functions, of course.
    – eckes
    Commented Nov 3, 2017 at 10:15
  • I have all access control logic in the database. Other business logic is in the API layer. But I'm actually leaning towards views instead of functions now. It seems that the INSERT statement is better in handling datatypes (i.e. smallint vs int). If I want a smallint as parameter in a function I have to cast it, otherwise Postgres thinks it's an int and won't find my function.
    – Michael
    Commented Nov 3, 2017 at 11:28

2 Answers 2

2

Named parameters should actually be passed using => not = and not :=.

e.g.

select my_function(argument_one => 42, argument_five => 'foobar');

The => is defined by the SQL standard for named parameters, the := was used historically by Postgres, but its use is discouraged in newer versions in favor of the standard compliant =>

Using Named Notation in the manual.

1
  • Thanks! I see now that this is the way to do it (since 9.5)
    – Michael
    Commented Nov 3, 2017 at 10:14
0

I just found the reason for me thinking there where no "named-parameter-calling-concept".

I had tried to name the function parameters with =, but apparently I need to use :=.

This answers my question, but it is unfortunate because = and := should be completely interchangeable in Postgres. Now I know that this is not the case.

EDIT ---

After reading up on this I came to the conclusion that "=" equals ":=" only in plpgsql. If calling a function is done in SQL I suppose that this is not true, hence the ":=" is needed. I'd better start using := everywhere.

5
  • "should be completely interchangeable" - no it shouldn't. Some versions even declared that = as the assignment operator to be deprecated. I don't know why the changed that back to allow both.
    – user1822
    Commented Nov 3, 2017 at 10:05
  • postgresql.org/docs/9.5/static/plpgsql-declarations.html, "Equal (=) can be used instead of PL/SQL-compliant :=."
    – Michael
    Commented Nov 3, 2017 at 10:09
  • I know, but this was not the case in previous versions In postgresql.org/docs/9.3/static/plpgsql-declarations.html that comment is not there.
    – user1822
    Commented Nov 3, 2017 at 10:11
  • But still, you can't use "=" for named parameters.
    – Michael
    Commented Nov 3, 2017 at 10:13
  • No, not when passing parameters to a function, e.g. my_func(argument_one = 10) does not work
    – user1822
    Commented Nov 3, 2017 at 10:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.