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 1m1BNy-0006IH-Lg for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jul 2021 17:30: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 1m1BNv-0003wW-MC for pgsql-hackers@arkaria.postgresql.org; Wed, 07 Jul 2021 17:30:55 +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 1m1BNv-0003wO-9w for pgsql-hackers@lists.postgresql.org; Wed, 07 Jul 2021 17:30:55 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1m1BNq-00054C-6t for pgsql-hackers@lists.postgresql.org; Wed, 07 Jul 2021 17:30:54 +0000 Received: by mail-pl1-x630.google.com with SMTP id x16so582177plg.3 for ; Wed, 07 Jul 2021 10:30:49 -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=HVb28Wuycqye7I6J4C8YUqxz8L5KVeOgYWqKf38vnGo=; b=zrrX+CWHH6bYiqDoEyCFY0Fcvj8pnZiFV3dAUQtF5WM3CAKUmp7Ttugz16nqmuD30b wlBM//rnTNyjnl3wg8n7PgIYkJWk0ExUwV9ttg37Twpf4kCp/ehcx57PgXwP4dL+yCF3 EDtv7POwIOlZbDK8v0Ldc+N7NbNsHZaSn/0UwTTM8Pr7Cv8hfl53EyN4yurhUubQe56W niHuJP1WuKWekXOq6Fw0PKCnnHz5GkP9Yj069MSXtmfNs4hBKbJ3m+5c8wCHCyHw0lFp 1egkj2C8mpDY392YL9mHco5IfH2R+MytukmqGA6SWJLBQRAOn38fAqobuYlfwphrKuP2 vN7g== 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=HVb28Wuycqye7I6J4C8YUqxz8L5KVeOgYWqKf38vnGo=; b=Y/L3xuR6TAZ/YJ19SH5izo5FZsxMecnLw2ddX/8TA2ZrOfQ2NZ7AMvOJfZkuEXyMHY 3rfY79X2su4phOmkTjMJxNADdT+rrQE8EeSe2Xit1J8ZNKpNlYxCX4p6qmyw4RqbTSC+ xK5HUmAXNrm7LCjSUV1zs5uogOq18HHq5cycEqWM1z4poEJERR4WCaasVqwN6QL9Ie6q ZfhQpdsgi8S6AJB7t5o9xOmWNLhXxNKHMapuedShheFIZqus6Dg8Q5xhKrhlk7z0ukTP nj//xaSMl1fFvl0JLeCOxy7KCbAC90bMB6IjUjEGQF9538TA29zu7vpTt6GYoHAn2dXy tssQ== X-Gm-Message-State: AOAM532N/rsCClKoG96Td01WLWg+hyPJ7eh5xkywWJiuZ9YJu/jezp9u h+KwHRkAOpNM7PqIxXHmsoqrA4lVY/ZO9Q== X-Google-Smtp-Source: ABdhPJyJ7uwsMuLK16JWNSOqvE64L12GCDN2Fi788d1aBspHGOtTsR0gpmGZNSPgg3xjixWmfPJpiA== X-Received: by 2002:a17:90a:aa8a:: with SMTP id l10mr27340675pjq.227.1625679048759; Wed, 07 Jul 2021 10:30:48 -0700 (PDT) Received: from perhan.alvh.no-ip.org ([190.95.19.95]) by smtp.gmail.com with ESMTPSA id j205sm21707260pfd.4.2021.07.07.10.30.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Jul 2021 10:30:47 -0700 (PDT) Received: by perhan.alvh.no-ip.org (Postfix, from userid 1000) id 0BB982A66A0; Wed, 7 Jul 2021 13:30:46 -0400 (-04) Date: Wed, 7 Jul 2021 13:30:46 -0400 From: Alvaro Herrera To: Boris Kolpackov Cc: pgsql-hackers@lists.postgresql.org Subject: Re: Pipeline mode and PQpipelineSync() Message-ID: <202107071730.vkyej2goquhp@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 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-Jul-07, Boris Kolpackov wrote: > // Try to minimize the chance of blocking the server by first processing > // the result and then sending more queries. > // > if (FD_ISSET (sock, &rds)) > { > if (PQconsumeInput (conn) == 0) > assert (false); > > while (PQisBusy (conn) == 0) > { > //fprintf (stderr, "PQgetResult %zu\n", rn); > > PGresult* res = PQgetResult (conn); > assert (res != NULL); > ExecStatusType stat = PQresultStatus (res); Hmm ... aren't you trying to read more results than you sent queries? I think there should be a break out of that block when that happens (which means the read of the PGRES_PIPELINE_SYNC needs to be out of there too). With this patch, the program seems to work well for me. *************** *** 94,112 **** while (PQisBusy (conn) == 0) { //fprintf (stderr, "PQgetResult %zu\n", rn); PGresult* res = PQgetResult (conn); assert (res != NULL); ExecStatusType stat = PQresultStatus (res); - if (stat == PGRES_PIPELINE_SYNC) - { - assert (wdone && rn == n); - PQclear (res); - rdone = true; - break; - } - if (stat == PGRES_FATAL_ERROR) { const char* s = PQresultErrorField (res, PG_DIAG_SQLSTATE); --- 94,110 ---- while (PQisBusy (conn) == 0) { //fprintf (stderr, "PQgetResult %zu\n", rn); + if (rn >= wn) + { + if (wdone) + rdone = true; + break; + } PGresult* res = PQgetResult (conn); assert (res != NULL); ExecStatusType stat = PQresultStatus (res); if (stat == PGRES_FATAL_ERROR) { const char* s = PQresultErrorField (res, PG_DIAG_SQLSTATE); *************** *** 190,195 **** --- 188,201 ---- break; // Blocked or done. } } + + if (rdone) + { + PGresult *res = PQgetResult(conn); + assert(PQresultStatus(res) == PGRES_PIPELINE_SYNC); + PQclear(res); + break; + } } if (PQexitPipelineMode (conn) == 0 || *************** *** 246,248 **** --- 252,269 ---- PQclear (res); } } + + int main(int argc, char **argv) + { + PGconn *conn = PQconnectdb(""); + if (PQstatus(conn) != CONNECTION_OK) + { + fprintf(stderr, "connection failed: %s\n", + PQerrorMessage(conn)); + return 1; + } + + test(conn); + } + + -- Álvaro Herrera Valdivia, Chile — https://www.EnterpriseDB.com/