So, what about the performance of PostgreSQL binaries themselves when built with each compiler? I had heard contradictory reports of the performance of binaries built with Clang. In Belgium, Chris Lattner said that Clang built binaries could perform better, but a number of independent benchmarks suggested that Clang was generally behind, with some notable exceptions. I asked 2ndQuadrant colleague and PostgreSQL performance expert Greg Smith to suggest a useful benchmark to serve as a good starting point for comparing Postgres performance when built with Clang to performance when built with GCC. He suggested that I apply Jeff Janes' recent patch for pgbench that he'd reviewed. It stresses the executor, and therefore the CPU quite effectively, rather than table locks or IPC mechanisms. The results of this benchmark were very interesting.

Greg provided me with shell access to a beefy server, the same server that he used in his review of Jeff’s patch, which added the -P option: http://archives.postgresql.org/message-id/4DFE788F.5020704@2ndQuadrant.com . I hacked together a shell script to run pgbench for this purpose. Binaries were built using GCC and Clang, each with exactly the same flags - Clang accepts the same flags as GCC. To smooth the results out, and to get a conclusive outcome, I decided on 16 10 minute -P runs with 4 connections, that alternated between using each set of binaries, lasting a total of 3 hours. Here’s a summary of the results:

The total transactions per second with the Clang binaries was marginally ahead of the GCC binaries. While further analysis is certainly needed, it is a remarkable achievement for Clang to have been able to hold its own against, or even slightly outperform a compiler as mature and popular as GCC here.

So, is it ready for prime-time? Well, not quite. Even my bleeding edge Fedora 15 system only comes with Clang 2.8, and only Clang 2.9 is listed as supported in 9.1. These build time figures are only obtainable on very recent revisions of Clang. While I’m extremely encouraged by our pgbench benchmark results, I hesitate to recommend the use of Clang for building production PostgreSQL binaries just yet. The benchmark is quite synthetic, and may not be a great proxy for general performance, although it certainly wasn’t cherry-picked. I encourage others to independently reproduce my work here, and to suggest alernative benchmarks.

I can heartily recommend Clang for hacking on PostgreSQL today. I was also impressed with how accessible the Clang developers are if you have a problem. Sometimes you have to be a bit persistent, but in general the Clang community are quite responsive to end-users concerns.

Just to clarify the transaction rate info here: the new "-P" mode proposed for pgbench, and used by this benchmark, executes many loops of a server-side function for each "transaction" reported. Peter is showing that raw rate here; the results I posted to pgsql-hackers (list archive link above) decode that into a SELECT/s rate.

Since this is a completely new mode to pgbench, you can't compare any of these numbers (raw or expressed as a SELECT rate) usefully to traditional SELECT/s rates from pgbench. It is an extremely CPU bound workload as executed here though, which makes it quite sensitive to the compiler optimizations used. When the results here being so close, it would have been difficult to even measure a difference above the run-to-run variation using any of the older, less CPU bound pgbench modes.

So which compiler-options did you use exactly? The results would be somewhat uninteresting if you compared the compilers at something like -O0. In the tests I did so far, gcc tended to outperform clang when compiled with all the fancy optimisations on (loop unrolling/hoisting, SSE fpu, etc), but otherwise there was fairly little difference.It would also be interesting to see some build-time statistics.

If you want to compare seed, efficiency of code generated by clang vs gcc, take out the disk of the loop, so don't benchmark a big SQL database, I've read your post but "stresses the executor and the CPU quite effectively" means NOTHING, not worth a blog post about it.

use CPU bound tasks, database do actually stores some datas on disk I've been told.

Sorry I may sound a tad harsh and oldtimer, but... You don't even talk about the write cache setting of your PG setup. Secondly, for measuring perf. of code generated by 2 compilers, never use a piece of software which does disk IO. Clang is a tremendous piece of software, but on it's website, the perf. of generated code is not even discussed. Why? it's young, a lot of apple developers work on it ok, but it has no past, no years of tuning. Apple talks on speed are about the time to compile a piece of C or C++ code...

I've recently looked at quality of SIMD code generated by both GCC and Clang: both are raw pieces of shit. Things are not as bad in FP87 insns and integers, for sure, just to gives some abilities and limits of those compilers.

Also, I'm astonished to see peoples writing a full compiler, in 2011, solely in C++, only companies with huge pile of money can afford it. I'm thinking about ML family of language which ease compilers writing, but yeah, every compiler writers want to be self hosting at some point.

Download the shootout benchmark and build them with both compilers, just the C/C++ one, that would be a nice post.

Then explain in full details how the benchmarsk was set to not touch the disk but only the cache (which is a rather singular use of an SQL DB).

I've seen variations in the range of 15-20% slower than GCC, about Clang generated code, I can't find back the URL of the guy having done the benchmark, your numbers are so much grouped around 34 TPS, it smell a benchmark bound by IO.

Overall, I would not use SQL as a compiler benchmark, SQL databases are specifically tailored to store on the disk as fast as possible for reliability.