Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1c0gaU-0008C4-Gq for pgsql-hackers@arkaria.postgresql.org; Sun, 30 Oct 2016 03:15:10 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1c0gaT-0008Su-2W for pgsql-hackers@arkaria.postgresql.org; Sun, 30 Oct 2016 03:15:09 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1c0gaQ-0008Q9-U3 for pgsql-hackers@postgresql.org; Sun, 30 Oct 2016 03:15:07 +0000 Received: from gauss.credativ.com ([93.94.130.89]) by makus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1c0gaJ-0007gV-MI for pgsql-hackers@postgresql.org; Sun, 30 Oct 2016 03:15:05 +0000 Received: from gauss.credativ.com (localhost [127.0.0.1]) by gauss.credativ.com (Postfix) with ESMTP id 3EE5AAF00E0; Sun, 30 Oct 2016 04:14:56 +0100 (CET) Received: from moll (ip-109-44-2-62.web.vodafone.de [109.44.2.62]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: ase@gauss.credativ.com) by gauss.credativ.com (Postfix) with ESMTPSA id E9374AF002B for ; Sun, 30 Oct 2016 04:14:55 +0100 (CET) From: Andreas Seltenreich To: pgsql-hackers@postgresql.org Subject: [sqlsmith] Missing CHECK_FOR_INTERRUPTS in tsquery_rewrite Date: Sun, 30 Oct 2016 04:14:45 +0100 Message-ID: <8760oasf2y.fsf@credativ.de> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org --=-=-= Content-Type: text/plain Hi, testing with sqlsmith yielded an uncancellable backend hogging CPU time. Gdb showed it was busy in findeq() of tsquery_rewrite.c. This function appears to have exponential complexity wrt. the size of the involved tsqueries. The following query runs for 12s on my machine with no way to cancel it and incrementing the length of the first argument by 1 doubles this time. select ts_rewrite( (select string_agg(i::text, '&')::tsquery from generate_series(1,32) g(i)), (select string_agg(i::text, '&')::tsquery from generate_series(1,19) g(i)), 'foo'); The attached patch adds a CHECK_FOR_INTERRUPTS to make it cancellable. regards, Andreas --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Add-CHECK_FOR_INTERRUPTS-in-tsquery_rewrite-loop.patch From d9910a96c9bd73c16e29ecaa0577945d5e1c091c Mon Sep 17 00:00:00 2001 From: Andreas Seltenreich Date: Sun, 30 Oct 2016 03:25:55 +0100 Subject: [PATCH] Add CHECK_FOR_INTERRUPTS in tsquery_rewrite loop. The loop in findeq() appears to have exponential complexity and runtime becomes excessive for more than about 30 tokens in the tsvectors. Add a CHECK_FOR_INTERRUPTS to make it cancellable. --- src/backend/utils/adt/tsquery_rewrite.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend/utils/adt/tsquery_rewrite.c b/src/backend/utils/adt/tsquery_rewrite.c index 28f328d..ef6444f 100644 --- a/src/backend/utils/adt/tsquery_rewrite.c +++ b/src/backend/utils/adt/tsquery_rewrite.c @@ -95,6 +95,10 @@ findeq(QTNode *node, QTNode *ex, QTNode *subs, bool *isfind) do { + /* This loop is rather heavyweight, it better be + * cancellable. */ + CHECK_FOR_INTERRUPTS(); + tnode->sign = 0; for (i = 0; i < ex->nchild; i++) { -- 2.9.3 --=-=-= Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers --=-=-=--