I’ve been profiling some part of my application and I’ve found out that
PgAdmin3 is significantly faster than psql at executing the exact same script.
I’m using unix sockets, Ubuntu 12.04 and Postgresql 9.1 and pgadmin3 v1.14. I have a simple script like the following:
BEGIN
INSERT INTO key_value(section,key,value) VALUES('section','key1','value');
....
INSERT INTO key_value(section,key,value) VALUES('section','key10000','value');
COMMIT;
Here is the table:
CREATE TABLE key_value
(
key text NOT NULL,
value text,
CONSTRAINT key_value_pkey PRIMARY KEY (section , key )
)
WITH (
OIDS=FALSE
);
There are 10000 inserts in this script. Executing it in pgadmin3 takes around 0.5 seconds, executing it in psql takes 2.5~3.5 seconds. Both are wrapped in a transaction so there should be really no difference. Results are consistent afer recreating the table, doing a full vacuum etc. Logging in the Postgresql server shows postgres doing a log for every insert statement when being executed in psql but only one log when being executed from pgadmin3.
psql execution is done with the following:
psql -n -t -f p.sql -o/dev/null
and also tested with
psql -n -t -1 -f p.sql -o/dev/null
My question is why is psql so much slower and why us postgresql logging each and every statement in one client but only the whole transaction in the other and wether there is a simple fix to psql.
EDIT Just to clarify, I’m logging the duration not the statement:
In the server I’m getting the following when executing with psql
2012-10-02 12:20:32 CEST LOG: duration: 0.283 ms
....
2012-10-02 12:20:35 CEST LOG: duration: 0.285 ms
2012-10-02 12:20:35 CEST LOG: duration: 0.291 ms
2012-10-02 12:20:35 CEST LOG: duration: 0.279 ms
2012-10-02 12:20:35 CEST LOG: duration: 0.284 ms
2012-10-02 12:20:35 CEST LOG: duration: 0.279 ms
2012-10-02 12:20:35 CEST LOG: duration: 0.299 ms
....
2012-10-02 12:20:36 CEST LOG: duration: 5.779 ms
When executing with pgadmin3:
2012-10-02 12:23:21 CEST LOG: duration: 532.695 ms
User and database are the same in psql and pgadmin3
My tip: pgAdmin execute script as one multistatement – so there is less network (protocol) overhead.