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 1sRYoK-00GUPV-SW for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Jul 2024 15:00:49 +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 1sRYoJ-0064fx-H7 for pgsql-hackers@arkaria.postgresql.org; Wed, 10 Jul 2024 15:00:47 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sRYoJ-0064dD-1y for pgsql-hackers@lists.postgresql.org; Wed, 10 Jul 2024 15:00:47 +0000 Received: from mail-ed1-x52e.google.com ([2a00:1450:4864:20::52e]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1sRYoB-001O4m-GG for pgsql-hackers@lists.postgresql.org; Wed, 10 Jul 2024 15:00:45 +0000 Received: by mail-ed1-x52e.google.com with SMTP id 4fb4d7f45d1cf-58f9874aeb4so7357640a12.0 for ; Wed, 10 Jul 2024 08:00:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20230601.gappssmtp.com; s=20230601; t=1720623637; x=1721228437; darn=lists.postgresql.org; h=message-id:date:mime-version:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=h2GXbuvOM1rYAImwnB9RVdCU56zp3tGF1ARO8zgF3jo=; b=QgK6cEKfvv0R6f8ut0dvZXKu8sKMD37vL8YGAdGQ6+E/Z5Ib6+baLka9WdE+HVxsBP /1WRgrmt/f6hg/graZN5MpkCASQk6AWuVUNVORxV5QJWIps/702ymwPVC2xGLAGXtYtQ KsbbFaLGbnxXtKx4EPugxWplYMXpBCYhZO3wHBwCW/1Kf6pqYtKTJzv5J5cDNkNK5mwp LgjokQKGs3yUl/RICh8F/HjiUVjYNrl51fDxuo1QHltkbB3Le5qpQ29BN5OWVZy8Zu8A UfizwPvmBrVsP0mB3P831lCCDT3sUWneqtSxB2U13IAL9R1pXF3WsfNfdxwAKSHKym3p pbrQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1720623637; x=1721228437; h=message-id:date:mime-version:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=h2GXbuvOM1rYAImwnB9RVdCU56zp3tGF1ARO8zgF3jo=; b=GyjO0hpt/y6ncM2teMHoYm1YdFbLJHRXtYnJyLkQIPwElIaJTBwoa2ApXanXDXCW6n dktBvAvgVHKL174Vpw8Jj6olVcg/G8/tL3VGiz+YoDC6IcMb3qu6n0WzOwagf83sLdny wg/bAKI8bYZbADQP6r4ajCnHuKDrgN5FibM0dFBRZh7RPFQbDSV3CNQWUycHFXTIlIjp +2k2JCh4BYPeczcJ039eNT32Sn5QgYDID7p2BKCZLNIqFr9BfTvSFGdWiob+bh+D3mjh Jc+OWnOrk7DnNQfWcks9VoXIjMQpCP5JDL1Y2AZb0WNDTfZe73+vX017p07+1D9UBDRJ 8iuw== X-Gm-Message-State: AOJu0Yxohg/A1IXMgSCkoHqD53igGEpOc4A9dKGFwLJONzvdKuaefSuU Pre+C9CofUwwZswHB44/zP1seaxe9aSw5XZ62fkICHhcFO8xOqhy8n6rm+N5fi9QoXA9WZvNJlf S X-Google-Smtp-Source: AGHT+IFBdgFxK/8UQw5dr8kdtgiAgSByyv4lQFlDciXPzpiogapQE3XS1t9kY8Tip5hYtJNQll7ZeA== X-Received: by 2002:a05:6402:2554:b0:58d:10a0:36f1 with SMTP id 4fb4d7f45d1cf-594bb580586mr4563713a12.23.1720623613894; Wed, 10 Jul 2024 08:00:13 -0700 (PDT) Received: from antos (109-81-174-87.rct.o2.cz. [109.81.174.87]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-594bba548e8sm2328510a12.1.2024.07.10.08.00.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 10 Jul 2024 08:00:13 -0700 (PDT) From: Antonin Houska To: pgsql-hackers@lists.postgresql.org Subject: Missed opportunity for bsearch() in TransactionIdIsCurrentTransactionId()? X-Mailer: MH-E 8.6+git; nmh 1.8; GNU Emacs 28.2.50 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Date: Wed, 10 Jul 2024 17:00:13 +0200 Message-ID: <36977.1720623613@antos> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --=-=-= Content-Type: text/plain I don't quite understand why TransactionIdIsCurrentTransactionId() implements binary search in ParallelCurrentXids "from scratch" instead of using bsearch(). If I read the code correctly, the contents of the ParallelCurrentXids is composed in SerializeTransactionState(), which uses xidComparator: qsort(workspace, nxids, sizeof(TransactionId), xidComparator); so it should be o.k. to use bsearch(..., xidComparator). For example, ReorderBufferCopySnap() also uses xidComparator to sort the 'subxip' array, and HeapTupleSatisfiesHistoricMVCC() then uses TransactionIdInArray() (which is effectively bsearch(..., xidComparator)) to search for particular XID in the array. -- Antonin Houska Web: https://www.cybertec-postgresql.com --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=use_bsearch_for_ParallelCurrentXids.diff diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c index d119ab909d..8540e70e70 100644 --- a/src/backend/access/transam/xact.c +++ b/src/backend/access/transam/xact.c @@ -965,29 +965,9 @@ TransactionIdIsCurrentTransactionId(TransactionId xid) * the XIDs in this array are sorted numerically rather than according to * transactionIdPrecedes order. */ - if (nParallelCurrentXids > 0) - { - int low, - high; - - low = 0; - high = nParallelCurrentXids - 1; - while (low <= high) - { - int middle; - TransactionId probe; - - middle = low + (high - low) / 2; - probe = ParallelCurrentXids[middle]; - if (probe == xid) - return true; - else if (probe < xid) - low = middle + 1; - else - high = middle - 1; - } - return false; - } + if (bsearch(&xid, ParallelCurrentXids, nParallelCurrentXids, + sizeof(TransactionId), xidComparator)) + return true; /* * We will return true for the Xid of the current subtransaction, any of --=-=-=--