PostgreSQL performance worse when adding CPUs I ran into a strange behaviour when benchmarking PostgreSQL. The setup is like this:


Virtual box VM, with 3 GB of RAM. The host has 16GB of RAM and 4 CPUs.
Ubuntu 14.04, standard PostgreSQL 9.3 installation with the default configuration
a single database with a single table having ~4k rows


I wrote a program that runs 10k simple selects (select * from table_name where id = 1) and times how long it takes to run them all.

When I assign a single CPU to the VM, it takes ~2.5 seconds to execute the queries.

With 2 CPUs, it takes more than 4 seconds.

With 3 CPUs, it takes more than 7 seconds.

With 2 (or 3) CPUs, if I also run at the same time another program which uses 100% CPU (but doesn't use the database), performance improves.

What's going on here?
I often notice that PostgreSQL benchmarks run faster once more than half the CPUs are busy.  I think that what happens is Linux tries to keep the client and the server on different CPUs, which slows down the intense back-and-forth between them (which is the main thing your benchmark is benchmarking).  Once Linux is forced to run them on the same CPU, it gets faster.  

Although I haven't seen the effect to be nearly as large as you report here.