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 1q6C8g-00053W-3d for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jun 2023 15:28:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q6C8f-00021d-0m for pgsql-hackers@arkaria.postgresql.org; Mon, 05 Jun 2023 15:28:57 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q6C8e-00021U-JQ for pgsql-hackers@lists.postgresql.org; Mon, 05 Jun 2023 15:28:56 +0000 Received: from mail-ot1-x335.google.com ([2607:f8b0:4864:20::335]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1q6C8b-000Oqy-IS for pgsql-hackers@postgresql.org; Mon, 05 Jun 2023 15:28:56 +0000 Received: by mail-ot1-x335.google.com with SMTP id 46e09a7af769-6b0d38ce700so3763620a34.2 for ; Mon, 05 Jun 2023 08:28:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=neon-tech.20221208.gappssmtp.com; s=20221208; t=1685978932; x=1688570932; h=in-reply-to:references:message-id:from:to:subject:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=KbeDe8JAscXs04P80asYA/P2s5w/SvyyoTW/5q6c1is=; b=vft8ha/gUfcmmRTJBsz6P3O535rFjxCXMVa/AFzuoMmRJy6vRaJzMmTGy6iGVWfmTY wpNUhHBEVvZXacFoiu2yo4lRxRUshS/khmj1WCa2sW6Kcy9+SgodKpmwYiHL7TvQ5qbb IBXmQpNsjyrYinYxgS3OXo/ZuEiYtBxN6XQaXRQo8pwXl1wvAvV0IqzmTDKpW1PGc9se p7QgmAl4xzjAOp0CsjWBbSkei1YZ81Py3dJOEFV65B61ahfU+OwtnfVE1qtLmf/zb+Dj XSdPv1aJodHOGyoZLaW/sHQbMWj/cg2rs40xcyuWWE2uEHCgcoYd6AbxOQeLapjCj6MO a+gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685978932; x=1688570932; h=in-reply-to:references:message-id:from:to:subject:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=KbeDe8JAscXs04P80asYA/P2s5w/SvyyoTW/5q6c1is=; b=J4X8IE/mKnEQiaRzPdvgSrOkYbLW3kJmek2LjWdVDeu1BMioZwWP5S64PRRt0A9vjq 1t4TSArbfccBzd1jm6lIqaum6uC/B1YgqX/fjPFgeQRdxOJhrHuKVtaOfOZFUqOR3+EE go01UTsfdR5MDUczjdoDs8UFRVhRoFphmIF5HDPEGi8ljiwhkXmDubKCSqGG38ktI5da ysPOP7jEvwL+7sln3YC6NUUevliDjreOei4FhKi5nOwN2WCRMp2eWX8qxf55PO8M0Sbs xO4SbS+5uiBjx5hEKgMpP0EWrglPF3IKxhmk+bQiRq8iDfBXTnz/zimfRxurAOVixtCB YJdg== X-Gm-Message-State: AC+VfDwzmCbRrmKLL4M4qom4cjjbQPUFl+GcMxlYAWzsnTsj8E2p9hQK gRH7hQVckagQ0n0GssbeemavxP2rIv7D7AolVIo= X-Google-Smtp-Source: ACHHUZ4Qdjwq8MAFodwS3Ss47XJxzw3aZpTnNZxmxXbZ2gJkPmqRaXIJTX2w1dP7MSiKywsZA+n/4Q== X-Received: by 2002:a9d:6952:0:b0:6b1:cc2e:a63 with SMTP id p18-20020a9d6952000000b006b1cc2e0a63mr2406284oto.2.1685978932022; Mon, 05 Jun 2023 08:28:52 -0700 (PDT) Received: from localhost ([2607:fb90:e696:c1dc:3996:e070:71ee:42fb]) by smtp.gmail.com with ESMTPSA id t2-20020a9d7f82000000b006af7ccca526sm3460514otp.20.2023.06.05.08.28.51 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 05 Jun 2023 08:28:51 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 05 Jun 2023 10:28:50 -0500 Subject: Re: Let's make PostgreSQL multi-threaded To: "Heikki Linnakangas" , "pgsql-hackers" From: "Tristan Partin" Message-Id: X-Mailer: aerc 0.15.2-33-gedd4752268b2 References: <31cc6df9-53fe-3cd9-af5b-ac0d801163f4@iki.fi> In-Reply-To: <31cc6df9-53fe-3cd9-af5b-ac0d801163f4@iki.fi> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon Jun 5, 2023 at 9:51 AM CDT, Heikki Linnakangas wrote: > # Global variables > > We have a lot of global and static variables: > > $ objdump -t bin/postgres | grep -e "\.data" -e "\.bss" | grep -v=20 > "data.rel.ro" | wc -l > 1666 > > Some of them are pointers to shared memory structures and can stay as=20 > they are. But many of them are per-connection state. The most=20 > straightforward conversion for those is to turn them into thread-local=20 > variables, like Konstantin did in [0]. > > It might be good to have some kind of a Session context struct that we=20 > pass everywhere, or maybe have a single thread-local variable to hold=20 > it. Many of the global variables would become fields in the Session. But= =20 > that's future work. +1 to the session context idea after the more simple thread_local storage idea. > # Extensions > > A lot of extensions also contain global variables or other things that=20 > break in a multi-threaded environment. We need a way to label extensions= =20 > that support multi-threading. And in the future, also extensions that=20 > *require* a multi-threaded server. > > Let's add flags to the control file to mark if the extension is=20 > thread-safe and/or process-safe. If you try to load an extension that's= =20 > not compatible with the server's mode, throw an error. > > We might need new functions in addition _PG_init, called at connection=20 > startup and shutdown. And background worker API probably needs some chang= es. It would be a good idea to start exposing a variable through pkg-config to tell whether the backend is multi-threaded or multi-process. > # Exposed PIDs > > We expose backend process PIDs to users in a few places.=20 > pg_stat_activity.pid and pg_terminate_backend(), for example. They need= =20 > to be replaced, or we can assign a fake PID to each connection when=20 > running in multi-threaded mode. Would it be possible to just transparently slot in the thread ID instead? > # Thread-safe libraries > > Need to switch to thread-safe versions of library functions, e.g.=20 > uselocale() instead of setlocale(). Seems like a good starting point. > The Python interpreter has a Global Interpreter Lock. It's not possible= =20 > to create two completely independent Python interpreters in the same=20 > process, there will be some lock contention on the GIL. Fortunately, the= =20 > python community just accepted https://peps.python.org/pep-0684/. That's= =20 > exactly what we need: it makes it possible for separate interpreters to= =20 > have their own GILs. It's not clear to me if that's in Python 3.12=20 > already, or under development for some future version, but by the time=20 > we make the switch in Postgres, there probably will be a solution in=20 > cpython. 3.12 is the currently in-development version of Python. 3.12 is planned for release in October of this year. A workaround that some projects seem to do is to use multiple Python interpreters[0], though it seems uncommon. It might be important to note depending on the minimum version of Python Postgres aims to support (not sure on this policy). The C-API of Python also provides mechanisms for releasing the GIL. I am not familiar with how Postgres uses Python, but I have seen huge improvements to performance with well-placed GIL releases in multi-threaded contexts. Surely this API would just become a no-op after the PEP is implemented. [0]: https://peps.python.org/pep-0684/#existing-use-of-multiple-interpreter= s --=20 Tristan Partin Neon (https://neon.tech)