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 1lwXth-0002j6-FZ for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Jun 2021 22:32:33 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lwXtg-0007wj-FO for pgsql-hackers@arkaria.postgresql.org; Thu, 24 Jun 2021 22:32:32 +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 1lwXtg-0007wV-2D for pgsql-hackers@lists.postgresql.org; Thu, 24 Jun 2021 22:32:32 +0000 Received: from mail-qv1-xf2e.google.com ([2607:f8b0:4864:20::f2e]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1lwXtZ-0002ej-Hi for pgsql-hackers@lists.postgresql.org; Thu, 24 Jun 2021 22:32:31 +0000 Received: by mail-qv1-xf2e.google.com with SMTP id g19so4184259qvx.12 for ; Thu, 24 Jun 2021 15:32:25 -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=mYXYDnnbl7hdyEL8eVGkMCn+4K1+f8ORrJdcyie4BXY=; b=olWEXXuNjVlBedpsS/WAgfac5n1hEvGahqJBGT2T0RKQHbL14G+6w/hS15FgsNC63W GobaYaWPgH4VyNo565ep4gmHY0dxRyWesuq4VxawOu0zDO1Q7CU72jtt4ImcZxyomoUR ADDBb5VQhGx34MjUo6eUY/jB8q2pa5yKbJk9YSnnYsnS0MyqJj15jeXkxucgm/k192Ok xEnq0GNSW5oUPsJKX21gzPaMCAxDeI9tdUQdYA9q5cV/2hI1liQ06wYBJ1mAxH0AdFA0 dfYczFpPaWR8ZOkxGtE+OXMau35TSANLqVueyoWA4hwoTVcpW8hyLZTm672hrRXmzxb7 5c9g== 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=mYXYDnnbl7hdyEL8eVGkMCn+4K1+f8ORrJdcyie4BXY=; b=nVo8ckoZh8tdKBApL6ZAdhQPIF4g/lZJxAtfrTaUwqFz0w7ADXGp/iLuAxIVhDNTSg aUXIjZQgKMKAKJeQCF2Wp3hcvjyrdAZ5d7oj5+htqx+t1oXC5qBKegukKjG4Rofn7wDs BDI+w/UGV6OFyDRKz9q98v8OoLtpETpKoeH0awLRt6ipzH8kSPn1dSTX19j9vAbKa52a v+oJ/ljk9H4VAiGRafe77BMQUaxp2jy8gbyk/8nTy6EIRVXAiwl3npJSTalVm1o6Hmsy 1lq1Jw4uyq0d44yxSQpdk9f+YXf264Isa13Q8x7rIiNHuvGxKmisR7FEV5LEYCKK7Jc7 +vtw== X-Gm-Message-State: AOAM532RrCFUfpQr9LIg7Te6LpJ8NPKyiCH87aWi1sA0T+H9bfN++RvP VCJb82DS2If3tJ9CYvcmgWrDsQ== X-Google-Smtp-Source: ABdhPJwBrSYs8qGYGzm7qEJ4eSc/Z9yWJU4NwZk6CrO076dH4dgYJY6js8qXt8eVCGwr4Et7teoXgg== X-Received: by 2002:a0c:83e1:: with SMTP id k88mr7963588qva.40.1624573943961; Thu, 24 Jun 2021 15:32:23 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.95]) by smtp.gmail.com with ESMTPSA id t139sm3413197qka.85.2021.06.24.15.32.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Jun 2021 15:32:22 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id E6AE72A0BBB; Thu, 24 Jun 2021 18:32:20 -0400 (-04) Date: Thu, 24 Jun 2021 18:32:20 -0400 From: Alvaro Herrera To: Boris Kolpackov Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Pipeline mode and PQpipelineSync() Message-ID: <202106242232.vpewlschvxfv@alvherre.pgsql> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="iq42jpxio57kw75o" 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 --iq42jpxio57kw75o Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit On 2021-Jun-23, Boris Kolpackov wrote: > If, however, I execute it by checking for results before sending the > next INSERT, I get the following call sequence: > > PQsendQueryPrepared() # INSERT #1 > PQflush() > PQsendQueryPrepared() # INSERT #2 > PQflush() > ... > PQsendQueryPrepared() # INSERT #~400 > PQflush() > PQconsumeInput() # At this point select() indicates we can read. > PQgetResult() # NULL (???) > PQgetResult() # INSERT #1 > PQgetResult() # NULL > PQgetResult() # INSERT #2 > PQgetResult() # NULL IIUC the problem is that PQgetResult is indeed not prepared to deal with a result the first time until after the queue has been "prepared", and this happens on calling PQpipelineSync. But I think the formulation in the attached patch works too, and the resulting code is less surprising. I wrote a test case that works as you describe, and indeed with the original code it gets a NULL initially; that disappears with the attached patch. Can you give it a try? Thanks -- Álvaro Herrera Valdivia, Chile "Escucha y olvidarás; ve y recordarás; haz y entenderás" (Confucio) --iq42jpxio57kw75o Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="0001-Fix-libpq-state-machine.patch" From 4073abc0ef8b4404915b949b3058ba47b7c51ba9 Mon Sep 17 00:00:00 2001 From: Alvaro Herrera Date: Thu, 24 Jun 2021 18:24:04 -0400 Subject: [PATCH] Fix libpq state machine --- src/interfaces/libpq/fe-exec.c | 19 +++++-------------- 1 file changed, 5 insertions(+), 14 deletions(-) diff --git a/src/interfaces/libpq/fe-exec.c b/src/interfaces/libpq/fe-exec.c index 7bd5b3a7b9..8403a9bbca 100644 --- a/src/interfaces/libpq/fe-exec.c +++ b/src/interfaces/libpq/fe-exec.c @@ -1375,8 +1375,7 @@ PQsendQueryInternal(PGconn *conn, const char *query, bool newQuery) /* OK, it's launched! */ pqAppendCmdQueueEntry(conn, entry); - if (conn->pipelineStatus == PQ_PIPELINE_OFF) - conn->asyncStatus = PGASYNC_BUSY; + conn->asyncStatus = PGASYNC_BUSY; return 1; sendFailed: @@ -1513,8 +1512,7 @@ PQsendPrepare(PGconn *conn, pqAppendCmdQueueEntry(conn, entry); - if (conn->pipelineStatus == PQ_PIPELINE_OFF) - conn->asyncStatus = PGASYNC_BUSY; + conn->asyncStatus = PGASYNC_BUSY; /* * Give the data a push (in pipeline mode, only if we're past the size @@ -1817,8 +1815,7 @@ PQsendQueryGuts(PGconn *conn, /* OK, it's launched! */ pqAppendCmdQueueEntry(conn, entry); - if (conn->pipelineStatus == PQ_PIPELINE_OFF) - conn->asyncStatus = PGASYNC_BUSY; + conn->asyncStatus = PGASYNC_BUSY; return 1; sendFailed: @@ -2448,8 +2445,7 @@ PQsendDescribe(PGconn *conn, char desc_type, const char *desc_target) /* OK, it's launched! */ pqAppendCmdQueueEntry(conn, entry); - if (conn->pipelineStatus == PQ_PIPELINE_OFF) - conn->asyncStatus = PGASYNC_BUSY; + conn->asyncStatus = PGASYNC_BUSY; return 1; sendFailed: @@ -3084,12 +3080,7 @@ PQpipelineSync(PGconn *conn) */ if (PQflush(conn) < 0) goto sendFailed; - - /* - * Call pqPipelineProcessQueue so the user can call start calling - * PQgetResult. - */ - pqPipelineProcessQueue(conn); + conn->asyncStatus = PGASYNC_BUSY; return 1; -- 2.30.2 --iq42jpxio57kw75o--