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 1lvpB9-0004jx-5I for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jun 2021 22:47:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lvpB7-0001OM-Sc for pgsql-hackers@arkaria.postgresql.org; Tue, 22 Jun 2021 22:47:33 +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 1lvofb-0003TS-Es for pgsql-hackers@lists.postgresql.org; Tue, 22 Jun 2021 22:14:59 +0000 Received: from mail-qv1-xf2c.google.com ([2607:f8b0:4864:20::f2c]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lvofY-00005r-Rg for pgsql-hackers@lists.postgresql.org; Tue, 22 Jun 2021 22:14:58 +0000 Received: by mail-qv1-xf2c.google.com with SMTP id g19so385603qvx.12 for ; Tue, 22 Jun 2021 15:14:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:in-reply-to; bh=8MziXQusYjnnMXo33mI9zw6N4AIisFjKnYr2BGwQbbE=; b=OP1W1bNwsIAk3fZlZgVocv60Mzbem5FBrPyHy45wFR4kWEx6/LrAwrvOiicwdJxEos mG3MdNYwDgn6KAgm29gCrJO6uMui3jOy/jTQFfEtoVfDHt0MJgrGQJ8pyDgfYM0D8n6y +WO8I5QyV39ILjuwqi+QAfT7q733Gr5RLHVvL4GATHwaHNtzS5USF5cKPmjnkyrjM5jY VUWBW9N4e64J4rjpeNeDaE066MIXwkZlDx52nFvyjla5Q9lLhPMLWwPVMsmX7BYSS/Ss tGDJ6qcbA5Dkf1fGXyjg1QbVqSsuC5NB7loc5ATx0rI27seBMvQTzy0Vc6c5WBW/Ngvr oDRQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:in-reply-to; bh=8MziXQusYjnnMXo33mI9zw6N4AIisFjKnYr2BGwQbbE=; b=rMnsLCTeHBTiUrlWv17laa86s9wWqVFivn9vrP+l2SnIDSRzh4/8yKUX0D1HaFZpns zq1X2BYeSFmmbrvc9rwiH4r3p12CVayTRJ/OkChnOB4T9HIDPfsovUoq1PFzYkffJS8a mhNTPmDSS8azYp1GwIkfZuIiklPYhZrwv6+k8RSRm0VmNtn/e2TAMANKhjsWjv7jodHg ZFH4wpMiMzjEOXyL7vvLkrgfx0UInZzu8x5OiSV7iWBOwq9XCDhNb0r+M+CzExOFWFDx mW35V0g0B00qVPosPdCXEniXsPX++ty5L4oOGaccY+aw6zl9q31UgpCDMUibbdhmdQju ZF6g== X-Gm-Message-State: AOAM531L4FWtecdVXVXBV8XGIPe7GX2CCG5hkcA+JyYf24fxC3f5ePLM EN3bAss97grfjyYM3t1YYUeNuA== X-Google-Smtp-Source: ABdhPJwdVjjDysRQqxyk6QSxm7u/gEwH3E+wUfWZMiCx4NiVxphe8TcyTvF2xni/J2E20yoMZ+hXbA== X-Received: by 2002:a0c:bf4b:: with SMTP id b11mr1169981qvj.11.1624400095505; Tue, 22 Jun 2021 15:14:55 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.95]) by smtp.gmail.com with ESMTPSA id e24sm2447926qtp.97.2021.06.22.15.14.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 22 Jun 2021 15:14:54 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 955982A0A6E; Tue, 22 Jun 2021 18:14:52 -0400 (-04) Date: Tue, 22 Jun 2021 18:14:52 -0400 From: Alvaro Herrera To: Boris Kolpackov Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Pipeline mode and PQpipelineSync() Message-ID: <202106222214.ptjfmstb23mu@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2021-Jun-21, Boris Kolpackov wrote: > Alvaro Herrera writes: > > > I think I should rephrase this to say that PQpipelineSync() is needed > > where the user needs the server to start executing commands; and > > separately indicate that it is possible (but not promised) that the > > server would start executing commands ahead of time because $reasons. > > I think always requiring PQpipelineSync() is fine since it also serves > as an error recovery boundary. But the fact that the server waits until > the sync message to start executing the pipeline is surprising. To me > this seems to go contrary to the idea of a "pipeline". But does that actually happen? There's a very easy test we can do by sending queries that sleep. If my libpq program sends a "SELECT pg_sleep(2)", then PQflush(), then sleep in the client program two more seconds without sending the sync; and *then* send the sync, I find that the program takes 2 seconds, not four. This shows that both client and server slept in parallel, even though I didn't send the Sync until after the client was done sleeping. In order to see this, I patched libpq_pipeline.c with the attached, and ran it under time: time ./libpq_pipeline simple_pipeline -t simple.trace simple pipeline... sent and flushed the sleep. Sleeping 2s here: client sleep done ok real 0m2,008s user 0m0,000s sys 0m0,003s So I see things happening as you describe in (1): > In fact, I see the following ways the server could behave: > > 1. The server starts executing queries and sending their results before > receiving the sync message. I am completely at a loss on how to explain a server that behaves in any other way, given how the protocol is designed. There is no buffering on the server side. > While it can be tempting to say that this is an implementation detail, > this affects the way one writes a client. For example, I currently have > the following comment in my code: > > // Send queries until we get blocked. This feels like a better > // overall strategy to keep the server busy compared to sending one > // query at a time and then re-checking if there is anything to read > // because the results of INSERT/UPDATE/DELETE are presumably small > // and quite a few of them can get buffered before the server gets > // blocked. > > This would be a good strategy for behavior (1) but not (3) (where it > would make more sense to queue the queries on the client side). Agreed, that's the kind of strategy I would have thought was the most reasonable, given my understanding of how the protocol works. I wonder if your program is being affected by something else. Maybe the socket is nonblocking (though I don't quite understand how that would affect the client behavior in just this way), or your program is buffering elsewhere. I don't do C++ much so I can't help you with that. > So I think it would be useful to clarify the server behavior and > specify it in the documentation. I'll see about improving the docs on these points. > > Do I have it right that other than this documentation problem, you've > > been able to use pipeline mode successfully? > > So far I've only tried it in a simple prototype (single INSERT statement). > But I am busy plugging it into ODB's bulk operation support (that we > already have for Oracle and MSSQL) and once that's done I should be > able to exercise things in more meaningful ways. Fair enough. -- Álvaro Herrera 39°49'30"S 73°17'W