PostgreSQL functions. Invalid INSERT performance versus raw SQL INSERT

I am trying to make PostgreSQL function to insert data. My query is very simple, when I execute SQL query outside of a function, I have great performance, but when inside a function, the performance is terrible.

When I do this:

EXPLAIN ANALYZE INSERT INTO car (name) VALUES ('test') RETURNING *;

      

I have great performance (execution time: 0.054ms), which is usually just a simple insert query.

But if I go to a function created like this:

CREATE OR REPLACE FUNCTION createCar(_name text) 
RETURNS SETOF car AS $$
    INSERT INTO car (name) VALUES (_name) RETURNING *;
$$ LANGUAGE sql;

      

I am executing the function like this:

EXPLAIN ANALYZE SELECT * FROM createCar('test');

      

And the performance is terrible (e.g. 10x slower, ~ Runtime: 1.126ms). And it's just a simple insert!

For a simple SELECT function going into the 'STABLE' function, the problem is solved, but for insert I can just be in VOLATILE mode because I am modifying the database ...

I may be doing something wrong, thanks for your help!

+3
sql postgresql database-performance


source to share


No one has answered this question yet

Check out similar questions:

1587
Insert multiple rows into one SQL query?
1373
How do I execute IF ... THEN in SQL SELECT?
784
Function vs. Stored Procedure in SQL Server
586
Insert, duplicate update in PostgreSQL?
492
SQL Server INSERT or UPDATE Solutions
424
Entity Framework VS LINQ to SQL VS ADO.NET with Stored Procedures?
280
PostgreSQL function for last inserted identifier
3
Is PostgreSQL's cache function being executed?
0
Postgresql performance issue on DigitalOcean VPS
0
PostgreSQL performance - checking if a string exists in a function returning boolean



All Articles
Loading...
X
Show
Funny
Dev
Pics