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 1qaI6z-008m8p-WC for pgsql-novice@arkaria.postgresql.org; Sun, 27 Aug 2023 15:55:38 +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 1qaI6y-0015YX-D4 for pgsql-novice@arkaria.postgresql.org; Sun, 27 Aug 2023 15:55:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qaI6x-0015Y5-Va for pgsql-novice@lists.postgresql.org; Sun, 27 Aug 2023 15:55:35 +0000 Received: from mail-il1-x12b.google.com ([2607:f8b0:4864:20::12b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qaI6m-001MQs-9K for pgsql-novice@lists.postgresql.org; Sun, 27 Aug 2023 15:55:31 +0000 Received: by mail-il1-x12b.google.com with SMTP id e9e14a558f8ab-34cc0ad6f61so9209685ab.0 for ; Sun, 27 Aug 2023 08:55:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=singh-im.20221208.gappssmtp.com; s=20221208; t=1693151722; x=1693756522; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=euq82NTsfNrVHl6dbtmq34K3MJiPjtIrUsgxWF2R/3E=; b=hWhhO62kxKofBYcaIRj0GsF9Rbdd5UXdNC0Ik6ISWDEdIr84ZAFhAUK0fyZCxLw4PM nQnb8/FVwL69ZGydJwfxrp3D+mbKiTvDKiv/Jd+kZGB++aavBo+sYgFkGgrc501hxkSq ns6x0/aWvQ/7ACZosi3rBZlFLseLaJYKA9i+yL9SVh2QDjk34Q455VupYwr4ciGbXQ7F sTOiYzGG5YIFDat5mBIWKS0bvON9N9X6kZ/K8pG3sIm51dDzoC1bXCfECKUr1KgYlKyV DpoAKwYt58EjQDQ2nIcLIOFx5ZQxuG/Jl6j9LowlfVjvtPUyDnXhP6OoQXMYafxDXx2q wUQQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1693151722; x=1693756522; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=euq82NTsfNrVHl6dbtmq34K3MJiPjtIrUsgxWF2R/3E=; b=OlUmPS9vEBB+7kamQcpuvglGSiQgpseR2bSEd7L7yqjqJ29/QjNE8AbI7LSn64Gm+x Ke2bBXzoFVCFPCcMeoC/g4yOiK4jPly23N9RHtyihMEsszUtv9cBWPdhve2wUudnUqiq vwVVGhcs9kp016WDjo87jbAeMgsbdktboyHtFNAHiJqzi4Fs+aEiqJVzQTjleS4IltrJ DDR07KQmMCRkFWP7QChdSCQWwiljyMDOhjGR5A9sEEgXq9YIlIXp57YnsctDex4mSgcL maF1tjkdoj4wrxaxVdIfXf6PBrteyypbpUGh+kUzdmwVBFGAE2z0o5bM2JaiiAhTBX60 9Yjw== X-Gm-Message-State: AOJu0YyknwildSWzlHE+D4BLxRBDwwlNmkIEmxETBzOzLPj2Db2GrAKo Sd1JqH8uFRRJrt/GD/z8XQf8iH6TAkyH9vjIYz4= X-Google-Smtp-Source: AGHT+IGahqi19SkZMqn9jSx0OsBWpj5nHqTZRduVRcCbxt+N+iWMeCEUbGqGmR3Qr/3iRZSTlQUyin9dCjmWK25AO4E= X-Received: by 2002:a05:6e02:eee:b0:349:8dfa:ad64 with SMTP id j14-20020a056e020eee00b003498dfaad64mr13614613ilk.15.1693151722230; Sun, 27 Aug 2023 08:55:22 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Gurjeet Singh Date: Sun, 27 Aug 2023 08:55:06 -0700 Message-ID: Subject: Re: Stored procedures To: Simon Connah Cc: pgsql-novice Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Sun, Aug 27, 2023 at 4:49=E2=80=AFAM Simon Connah wrote: > > I'm building a website in Python using the Django framework and am using = the ORM at the moment but I can see a few situations where I might need to = write raw SQL. My question is do stored procedures execute faster when comp= ared to queries you generate using Python and then send to PostgreSQL? I wo= uld assume they would as there is less communication between the two sides = and also because PostgreSQL knows the stored procedure ahead of time it sho= uld be able to better optimise it. > > Is that true? Does PostgreSQL use a JIT for stored procedures which make = them faster than one-off queries? If your application logic involves many queries for one operation, queries that produce the result that's then massaged and used in subsequent queries, then yes, your application will benefit from bundling that logic in a function/stored procedure and execute purely on database server. This is because, as you noted, it reduces network round-trips. Hence it will lower your latency for that one operation. But also note that since the database is now doing most of the work, and because it's limited by the number of CPUs on the database server, this can lower your application's throughput; hence this is not advisable if you have many more, and always querying client connections than the number of CPUs on the server. If you desire high throughput, and still wish to use functions to keep the single-operation latency low, and if your application's workload has a very high read:write ratio, you can split your read-only workload to use streaming replicas/standby servers, which you can have as many as you want (within reason :-). Instead of, or in addition to functions, you may use CTE (aka WITH clause) to bundle many SQL commands into one. PL/pgSQL is an interpreted language, not a compiled one. It caches and reuses the query plans of the SQL commands in the function, but that's about it, in terms of optimizations. Unlike a compiled language, it's not optimized to eliminate unnecessary operations, etc. As David notes, plpgsql has some overhead, as well, so, for example, wrapping single SQL statements in a plpgsql function will generally make the operation slower than executing that SQL directly. Postgres does have JIT optimizations for expression evaluation, but that benefits all SQL commands, irrespective of whether they are directly coming from the client, or wrapped in a function. Best regards, Gurjeet http://Gurje.et