Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9hA5-0005os-JG for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 07:12:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q9hA4-0008OR-Hn for pgsql-hackers@arkaria.postgresql.org; Thu, 15 Jun 2023 07:12:52 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q9hA4-0007zJ-02 for pgsql-hackers@lists.postgresql.org; Thu, 15 Jun 2023 07:12:52 +0000 Received: from mail-lf1-x129.google.com ([2a00:1450:4864:20::129]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q9hA0-002ItP-2G for pgsql-hackers@postgresql.org; Thu, 15 Jun 2023 07:12:49 +0000 Received: by mail-lf1-x129.google.com with SMTP id 2adb3069b0e04-4f764e9295dso2680432e87.0 for ; Thu, 15 Jun 2023 00:12:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=garret-ru.20221208.gappssmtp.com; s=20221208; t=1686813166; x=1689405166; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:from:to:cc:subject:date :message-id:reply-to; bh=QoBfdf76RsQ5Ji+eqXqip0Pqc7fBznXJsOuIlAEl/2E=; b=wrvg1fuDO3fqemq3feugo5a+Ztoj3R0ucy7RwOvw/IVRVTEW3k5/3/blSE+Dsa1evb lll3vYDApYAn0dR7Bzu2vn5nwQ8CEPAFi3MdhYoWhyhGVhE1SOXXvC+DwMcZv1eJm7cE /I2k+W/Ndzye9NtCXSH0jBRUdw+C6oNs60pbw7IBvP1QDEOv+gwHrqws3JCbx2getUJo 9eTb3XMQa2Kl8kQ9P2AM23dCAT3ieegvoFlxoenHHY6UyZTkpBS6nTtKQlf9c9IzC/bU y8KlrXNnsKSjDDiLzDc2Czzmh9b9guFi/GcwtEp9DWRVqjDHrtlkMXSLKWwlOrlu+dYT wRMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1686813166; x=1689405166; h=in-reply-to:from:references:cc:to:content-language:subject :user-agent:mime-version:date:message-id:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=QoBfdf76RsQ5Ji+eqXqip0Pqc7fBznXJsOuIlAEl/2E=; b=jhSKpvhigHhkJ6NGcUshEqt6CYf8clTuAkYFGKWbMeQQT3CYKsk0rxlyx2CSNd2Wz5 NRiSb34yV9vKn3j/adQ+SFjd7Q1D9so3jXNQ9Su9LjyGQ7WznEcTS5aOGJM2DkIYkgO3 P/8qrYHwg6gb0m/GckJn6MQpAqwB22KMUOWu2kfXqlYMr7avWqXGORu5BLnZmpDrdOUL TNHDUkzXJKKRVslBzb65o/1cw5PaKroiSlZVzvgsWZvM+hQTEQK6+AedYkx1NKJU0HaF 0lRAW3rKycjB/psJrFlJ0+NjT2/3mf1I1l4Sd8Ymly3QwrWHIC2i9o4N+gKSon4XJ8rp bN/Q== X-Gm-Message-State: AC+VfDzz/koY+K/IQ1o2FeUmU4XNeAw5p7K9zclwL2+TZ7EZ1f/3WNSF pT3bEYZ57y7xOT5AXkyB2XJy6g== X-Google-Smtp-Source: ACHHUZ4Fi63yxMmVWeSpBIoSXy4SBn1Ue2wNtJECeYtu2t3OFijIU/zKwYrSN6RFcbSBPCDv/V+7/g== X-Received: by 2002:ac2:464d:0:b0:4f0:af5:5560 with SMTP id s13-20020ac2464d000000b004f00af55560mr10995220lfo.19.1686813165515; Thu, 15 Jun 2023 00:12:45 -0700 (PDT) Received: from [192.168.2.28] ([217.175.216.56]) by smtp.gmail.com with ESMTPSA id k6-20020adfd846000000b003111025ec67sm2307244wrl.25.2023.06.15.00.12.40 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 15 Jun 2023 00:12:45 -0700 (PDT) Content-Type: multipart/alternative; boundary="------------0NoE0R15bAEWUv6nqRL0wEL4" Message-ID: <36f61a71-3bbb-b7b0-0d99-db5e69715af7@garret.ru> Date: Thu, 15 Jun 2023 10:12:32 +0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Subject: Re: Let's make PostgreSQL multi-threaded Content-Language: en-US To: James Addison Cc: Pavel Borisov , Dilip Kumar , Hannu Krosing , Heikki Linnakangas , pgsql-hackers References: <31cc6df9-53fe-3cd9-af5b-ac0d801163f4@iki.fi> <2c2665d2-c513-c12e-9097-9b1805bc2471@garret.ru> From: Konstantin Knizhnik In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --------------0NoE0R15bAEWUv6nqRL0wEL4 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 15.06.2023 1:23 AM, James Addison wrote: > On Tue, 13 Jun 2023 at 07:55, Konstantin Knizhnik wrote: >> >> >> On 12.06.2023 3:23 PM, Pavel Borisov wrote: >>> Is the following true or not? >>> >>> 1. If we switch processes to threads but leave the amount of session >>> local variables unchanged, there would be hardly any performance gain. >>> 2. If we move some backend's local variables into shared memory then >>> the performance gain would be very near to what we get with threads >>> having equal amount of session-local variables. >>> >>> In other words, the overall goal in principle is to gain from less >>> memory copying wherever it doesn't add the burden of locks for >>> concurrent variables access? >>> >>> Regards, >>> Pavel Borisov, >>> Supabase >>> >>> >> IMHO both statements are not true. >> Switching to threads will cause less context switch overhead (because >> all threads are sharing the same memory space and so preserve TLB. >> How big will be this advantage? In my prototype I got ~10%. But may be >> it is possible to fin workloads when it is larger. > Hi Konstantin - do you have code/links that you can share for the > prototype and benchmarks used to gather those results? Sorry, I have already shared the link: https://github.com/postgrespro/postgresql.pthreads/ As you can see last commit was 6 years ago when I stopped work on this project. Why?  I already tried to explain it: - benefits from switching to threads were not so large. May be I just failed to fid proper workload, but is was more or less expected result, because most of the code was not changed - it uses the same sync primitives, the same local catalog/relation caches,.. To take all advantage of multithreadig model it is necessary to rewrite many components, especially related with interprocess communication. But maintaining such fork of Postgres and synchronize it with mainstream requires too much efforts and I was not able to do it myself. There are three different but related directions of improving current Postgres: 1. Replacing processes with threads 2. Builtin connection pooler 3. Lightweight backends (shared catalog/relation/prepared statements caches) The motivation for such changes are also similar: 1. Increase Postgres scalability 2. Reduce memory consumption 3. Make Postgres better fir cloud and serverless requirements I am not sure now which one should be addressed first or them can be done together. Replacing static variables with thread-local is the first and may be the easiest step. It requires more or less mechanical changes. More challenging thing is replacing private per-backend data structures with shared ones (caches, file descriptors,...) --------------0NoE0R15bAEWUv6nqRL0wEL4 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 8bit

On 15.06.2023 1:23 AM, James Addison wrote:
On Tue, 13 Jun 2023 at 07:55, Konstantin Knizhnik <knizhnik@garret.ru> wrote:


On 12.06.2023 3:23 PM, Pavel Borisov wrote:
Is the following true or not?

1. If we switch processes to threads but leave the amount of session
local variables unchanged, there would be hardly any performance gain.
2. If we move some backend's local variables into shared memory then
the performance gain would be very near to what we get with threads
having equal amount of session-local variables.

In other words, the overall goal in principle is to gain from less
memory copying wherever it doesn't add the burden of locks for
concurrent variables access?

Regards,
Pavel Borisov,
Supabase


IMHO both statements are not true.
Switching to threads will cause less context switch overhead (because
all threads are sharing the same memory space and so preserve TLB.
How big will be this advantage? In my prototype I got ~10%. But may be
it is possible to fin workloads when it is larger.
Hi Konstantin - do you have code/links that you can share for the
prototype and benchmarks used to gather those results?


Sorry, I have already shared the link:
https://github.com/postgrespro/postgresql.pthreads/

As you can see last commit was 6 years ago when I stopped work on this project.
Why?  I already tried to explain it:
- benefits from switching to threads were not so large. May be I just failed to fid proper workload, but is was more or less expected result,
because most of the code was not changed - it uses the same sync primitives, the same local catalog/relation caches,..
To take all advantage of multithreadig model it is necessary to rewrite many components, especially related with interprocess communication.
But maintaining such fork of Postgres and synchronize it with mainstream requires too much efforts and I was not able to do it myself.

There are three different but related directions of improving current Postgres:
1. Replacing processes with threads
2. Builtin connection pooler
3. Lightweight backends (shared catalog/relation/prepared statements caches)

The motivation for such changes are also similar:
1. Increase Postgres scalability
2. Reduce memory consumption
3. Make Postgres better fir cloud and serverless requirements

I am not sure now which one should be addressed first or them can be done together.

Replacing static variables with thread-local is the first and may be the easiest step.
It requires more or less mechanical changes. More challenging thing is replacing private per-backend data structures
with shared ones (caches, file descriptors,...)

--------------0NoE0R15bAEWUv6nqRL0wEL4--