Received: from localhost (maia-1.hub.org [200.46.204.191]) by postgresql.org (Postfix) with ESMTP id F34389FB488 for ; Sun, 3 Jun 2007 07:56:05 -0300 (ADT) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.191]) (amavisd-maia, port 10024) with ESMTP id 00486-04 for ; Sun, 3 Jun 2007 07:55:59 -0300 (ADT) X-Greylist: delayed 00:19:03.738739 by SQLgrey-1.7.5 Received: from svr4.postgresql.org (svr4.postgresql.org [66.98.251.159]) by postgresql.org (Postfix) with ESMTP id 5422A9FB482 for ; Sun, 3 Jun 2007 07:55:29 -0300 (ADT) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.3 Received: from mail.eba.si (eba.si [194.249.173.77]) by svr4.postgresql.org (Postfix) with ESMTP id B53915AF67D for ; Sun, 3 Jun 2007 10:36:24 +0000 (GMT) Received: from localhost (localhost [127.0.0.1]) by mail.eba.si (Postfix) with ESMTP id 9769132825D for ; Sun, 3 Jun 2007 12:36:20 +0200 (CEST) Received: from mail.eba.si ([127.0.0.1]) by localhost (mail [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 19604-06 for ; Sun, 3 Jun 2007 12:36:16 +0200 (CEST) Received: from [192.168.0.104] (clj12-62.dial-up.arnes.si [212.235.134.62]) by mail.eba.si (Postfix) with ESMTP id 5AC28328174 for ; Sun, 3 Jun 2007 12:36:16 +0200 (CEST) Message-ID: <466299A7.20403@eba.si> Date: Sun, 03 Jun 2007 12:36:23 +0200 From: "Miha D. Puc" User-Agent: Thunderbird 1.5 (X11/20060111) MIME-Version: 1.0 To: pgsql-interfaces@postgresql.org Subject: Bytea network traffic: binary vs text result format Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at site X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=0.136 tagged_above=0 required=5 tests=BAYES_50=0.001, FORGED_RCVD_HELO=0.135 X-Spam-Level: X-Archive-Number: 200706/1 X-Sequence-Number: 6570 Hi! There was some debate recently about using text or binary format. There's people who would like to use it but have trouble converting binary encoded results into native types and there's people that say there's not much performance difference. I'd like to stress that performance is very different over slow network. The biggest difference is for byte where the text format performance is about 3.5 times worse at inserts and updates and about 2.9 times worse at selects . Here's the reasoning: In text format bytea are escaped using PQescapeBytea. In an average binary stream about 2/3 would be escaped. Each escaped byte becomes of form \\ooo at upload and of form \ooo for download, so the size of the escaped stream is 1/3 + 2/3 * 5 = 11/3 = 3.6 and 1/3 + 2/3 * 4 = 3 respectively. Here are the results of my test. I inserted and selected an OpenOffice document of size 2Mb over a 2M/512K cable. text format: insert: 120.1s select: 24.9s binary format: insert: 33.5s select: 8.6s factor: insert: 3.6 select: 2.9 The difference between the test and the above calculation comes from the estimate that 2/3 of bytes are escaped where in fact 95 out of 256 are escaped (63%). So there is a need (people asking) and reason (performance) to use binary format. But there's a huge drawback - the conversions. It's easy for varchar, not too bad for basic types (int, float, bool), effort is needed for timestamp, date, time and numeric is a pain. So with all the above there should be a utility for conversion between binary format and native types and/or string format in libpq. Regards, Miha Puc