Posting this without the SQL/code involved is a bit pointless though... Are you talking about insert statements? Did you split out the parameter assignment as ttomas indicated?
My point is that DB I/O operations are much much faster wher performed thru pure SQL instead doing complicated combinations between the SQL and programing language because it's the programing language that slows it down, specificaly using "for" and "while" loops.
Do a test: create a SQL query and then go thru it with a lazarus/pascal/delphi for loop in order to calculate the total of some fields :
query1.close;
query1.sql.clear;
query1.sql.text:='select id_code, salary from table1';
query1.open;
query1.first;
while not query1.EOF do begin
the_total:=the_total + query1.fieldbyname('salary').asinteger;
query2.close;
query2.sql.clear;
query2.sql.text:='update table2 set total=:thetotal where field_this=that and somtehing_else=the_other_thing and field_with_param=:param_number_one ';
query2.Parambyname('thetotal').asinteger:=the_total;
query2.Parambyname('param_number_one').asinteger:=some_value;
query2.execSQL;
query1.next;
end;
Here we are calling "update" query for each record from query1, you can't notice the speed diference if you have small number of records, so to test the speed you need a table with 200 thousend records in table1. That way you are actualy calling query2.execSQL 200 000 times.
Alternative to this but this time done thru one single call to pure SQL would be :
query1.close;
query1.sql.clear;
query1.sql.add('update table2, table1 set total=sum(salary) where field_this=something and somtehing_else=the_other_thing and field_with_param=:param_number_one ');
query1.Parambyname('param_number_one').asinteger:=some_value;
query1.execSQL;
I believe that this example is quite simple but good enough to see my point.
There is a bond between the programin language and the DB, and in a certain poin the program calls the database passing the contain of the query. My conclusion is that using this bond slows the program, and naturaly slowdown multiplies with repeating this operation.
This is not related only to Postgress but whith calling SQL in generaly.