Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sjbdo-002DHj-Gz for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Aug 2024 09:40:32 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sjbdm-0007pn-JS for pgsql-hackers@arkaria.postgresql.org; Thu, 29 Aug 2024 09:40:31 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sjbdl-0007pO-Kl for pgsql-hackers@lists.postgresql.org; Thu, 29 Aug 2024 09:40:30 +0000 Received: from m16.mail.163.com ([220.197.31.2]) by makus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sjbdf-001zc2-Ud for pgsql-hackers@postgresql.org; Thu, 29 Aug 2024 09:40:28 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=Aftm1UvGIgZ502QZsFtmxrD9ILFb+j5cpH+3gL27oAM=; b=jNnouJiWl0Iy1iWHI4w+zBEZa74y3HiLPOZrnfafWkXSOEKdaJ8rIAEAjjCMS9 5lWabOb6kjSvx6Juk1nUyKKfWv152JwrGC5AeEJuidA6el0APGcn60jdKo5ENBMw GnE5A5dusqTkMo8flrI/HuWCa7N9zUa43CS+ahtT+tIXo= Received: from lovely-coding (unknown [101.227.46.166]) by gzga-smtp-mta-g3-0 (Coremail) with SMTP id _____wA336P+QdBmx8oVBA--.28570S3; Thu, 29 Aug 2024 17:40:15 +0800 (CST) From: Andy Fan To: pgsql-hackers Subject: Make printtup a bit faster Date: Thu, 29 Aug 2024 17:40:14 +0800 Message-ID: <87wmjzfz0h.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wA336P+QdBmx8oVBA--.28570S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7Ar1DCFyfuFW5uryrJr4UCFg_yoW5Jr43pa 9Ika4YyrWkCrn7Jrs3Zr4F9r93AF48Jw45Cr1UCrWDuw15Jan7KFZ8Kr1jv3ZrGr1Ivw1Y vF4jkryIq3WDua7anT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UF_MfUUUUU= X-Originating-IP: [101.227.46.166] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/xtbBhQJKU2WXwsVRLQAAsX List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Usually I see printtup in the perf-report with a noticeable ratio. Take "SELECT * FROM pg_class" for example, we can see: 85.65% 3.25% postgres postgres [.] printtup The high level design of printtup is: 1. Used a pre-allocated StringInfo DR_printtup.buf to store data for each tuples. 2. for each datum in the tuple, it calls the type-specific out function and get a cstring. 3. after get the cstring, we figure out the "len" and add both len and 'data' into DR_printtup.buf. 4. after all the datums are handled, socket_putmessage copies them into PqSendBuffer. 5. When the usage of PgSendBuffer is up to PqSendBufferSize, using send syscall to sent them into client (by copying the data from userspace to kernel space again). Part of the slowness is caused by "memcpy", "strlen" and palloc in outfunction. 8.35% 8.35% postgres libc.so.6 [.] __strlen_avx2 4.27% 0.00% postgres libc.so.6 [.] __memcpy_avx_unaligned_erms 3.93% 3.93% postgres postgres [.] palloc (part of them caused by out function) 5.70% 5.70% postgres postgres [.] AllocSetAlloc (part of them caused by printtup.) My high level proposal is define a type specific print function like: oidprint(Datum datum, StringInfo buf) textprint(Datum datum, StringInfo buf) This function should append both data and len into buf directly. for the oidprint case, we can avoid: 5. the dedicate palloc in oid function. 6. the memcpy from the above memory into DR_printtup.buf for the textprint case, we can avoid 7. strlen, since we can figure out the length from varlena.vl_len int2/4/8/timestamp/date/time are similar with oid. and numeric, varchar are similar with text. This almost covers all the common used type. Hard coding the relationship between common used type and {type}print function OID looks not cool, Adding a new attribute in pg_type looks too aggressive however. Anyway this is the next topic to talk about. If a type's print function is not defined, we can still using the out function (and PrinttupAttrInfo caches FmgrInfo rather than FunctionCallInfo, so there is some optimization in this step as well). This proposal covers the step 2 & 3. If we can do something more aggressively, we can let the xxxprint print to PqSendBuffer directly, but this is more complex and need some infrastructure changes. the memcpy in step 4 is: "1.27% __memcpy_avx_unaligned_erms" in my above case. What do you think? -- Best Regards Andy Fan