Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jIwL9-0004Lz-Ha for pgsql-hackers@arkaria.postgresql.org; Mon, 30 Mar 2020 15:28:39 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jIwKc-000384-Pt for pgsql-hackers@arkaria.postgresql.org; Mon, 30 Mar 2020 15:28:06 +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 1jIwKc-00037x-Ax for pgsql-hackers@lists.postgresql.org; Mon, 30 Mar 2020 15:28:06 +0000 Received: from mail-wm1-x344.google.com ([2a00:1450:4864:20::344]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1jIwKZ-0006ip-Ct for pgsql-hackers@postgresql.org; Mon, 30 Mar 2020 15:28:05 +0000 Received: by mail-wm1-x344.google.com with SMTP id c81so20381222wmd.4 for ; Mon, 30 Mar 2020 08:28:03 -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:references:mime-version :content-disposition:in-reply-to; bh=m955RLp6SSBySJROJcbaNx6phpdbdTWqgaKfGN8/nbw=; b=RP+9H+YVoY+ZzPFm6LbrLWuACkpwXDVBUuhOpR5KUPl2R6XQMeXf3am7sUovNsUhIn FrQKsLGzggyoc65TnecVItc6YxPe5KUVwn5PdDaUvC8OTR0SHgrfElaITGT9+tEt/A8d TSIOsvPiu7Sg8TpbysAHacgy/gPaIDOGAR+tD0FwdiP6TCuir9mtI83T+ZpK/Cxt/iOa xYgjTwghQ5zTf339f1fAU2s+zSZeXssJxox3Qw/dZhoKVCWaX4IjE7N7JwYmiupYWJuN RrYAwHZEQwEf8Hgasi3WjU4kgXsIBmL1DW5LXG9PjiMTNKFLisgyveYDobN+BpWOCWfT V9uw== 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:references :mime-version:content-disposition:in-reply-to; bh=m955RLp6SSBySJROJcbaNx6phpdbdTWqgaKfGN8/nbw=; b=s9ynxMExTIoblnLlFtHBwsnai6VMuHABP6XjOHgZwplnMrjLfKzx42CuTxoAoL3d4F NL5CZ/JdMV/MLxP2lwiTV/CiStY8tSx8Cb+1SLoYvbai8nfy0h2dJxdACWl24JED654U v52mA5fyxQS2kRsVqGP3B/LQSrSl8TajDaOtav7DMJq9kKxh0LCPQfyhebrmeuNyREr3 Y2A8p+cV49fJqzZAWHr9Abe4/VvrtDpDIsbCAHIeIQHMjBiclBZuFehsBy2Zjfxp+RAf f9blXw6OFRohuVBrpBFjOM3sxwTvMOkaQ2RCOyx3/a7itspMCptJrtDnRhrMpEL6/9Da hYug== X-Gm-Message-State: ANhLgQ2cwX3yhr4Lm7ErCye+VkAExtH6x52V8kQR2YjVy7AzcbT7rDbe XTdbnQuWmJPvqfNn9cTsTXdR/w== X-Google-Smtp-Source: ADFU+vs0sP+XPgGzQxq1DBtlL3u9nqX967SizYlM1smmHS0jIRDAbXnyoOtsJTHPjXaU8d/UaxhyCg== X-Received: by 2002:a1c:b642:: with SMTP id g63mr12936668wmf.108.1585582080732; Mon, 30 Mar 2020 08:28:00 -0700 (PDT) Received: from localhost (ip-86-49-253-141.net.upcbroadband.cz. [86.49.253.141]) by smtp.gmail.com with ESMTPSA id b26sm14275966wmj.21.2020.03.30.08.27.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Mar 2020 08:27:59 -0700 (PDT) Date: Mon, 30 Mar 2020 17:27:58 +0200 From: Tomas Vondra To: Amit Kapila Cc: Dilip Kumar , Michael Paquier , Peter Eisentraut , PostgreSQL Hackers Subject: Re: PATCH: logical_work_mem and logical streaming of large in-progress transactions Message-ID: <20200330152758.fgp62lwb2ug7cl2a@development> References: <20200303214646.6adway2z5wnapjem@development> <20200329005905.ooxq25qyjwn2mvxz@development> <20200329153105.mdryxzz562tg65pk@development> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="vy6eyainsxhnm65b" Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --vy6eyainsxhnm65b Content-Type: text/plain; charset=us-ascii; format=flowed Content-Disposition: inline On Mon, Mar 30, 2020 at 11:47:57AM +0530, Amit Kapila wrote: >On Sun, Mar 29, 2020 at 9:01 PM Tomas Vondra > wrote: >> >> On Sun, Mar 29, 2020 at 11:19:21AM +0530, Amit Kapila wrote: >> >On Sun, Mar 29, 2020 at 6:29 AM Tomas Vondra >> > wrote: >> >> >> >> Ummm, how is that different from what the patch is doing now? I mean, we >> >> only write the top-level XID for the first WAL record in each subxact, >> >> right? Or what would be the difference with your approach? >> >> >> > >> >We have to do what the patch is currently doing and additionally, we >> >will set this flag after PGPROC_MAX_CACHED_SUBXIDS which would allow >> >us to call ProcArrayApplyXidAssignment during WAL replay only after >> >PGPROC_MAX_CACHED_SUBXIDS number of subxacts. It will help us in >> >clearing the KnownAssignedXids at the same time as we do now, so no >> >additional performance overhead. >> > >> >> Hmmm. So we'd still log assignment twice? Or would we keep just the >> immediate assignments (embedded into xlog records), and cache the >> subxids on the replica somehow? >> > >I think we need to cache the subxids on the replica somehow but I >don't have a very good idea for it. Basically, there are two ways to >do it (a) Change the KnownAssignedXids in some way so that we can >easily find this information without losing on the current benefits of >it. I can't think of a good way to do that and even if we come up >with something, it could easily be a lot of work, (b) Cache the >subxids for a particular transaction in local memory along with >KnownAssignedXids. This is doable but now we have two data-structures >(one in shared memory and other in local memory) managing the same >information in different ways. > >Do you have any other ideas? I don't follow. Why couldn't we have a simple cache on the standby? It could be either a simple array or a hash table (with the top-level xid as hash key)? I think the single array would be sufficient, but the hash table would allow keeping the apply logic more or less as it's today. See the attached patch that adds such cache - I do admit I haven't tested this, but hopefully it's a sufficient illustration of the idea. It does not handle cleanup of the cache, but I think that should not be difficult - we simply need to remove entries for transactions that got committed or rolled back. And do something about transactions without an explicit commit/rollback record, but that can be done by also handling XLOG_RUNNING_XACTS (by removing anything preceding oldestRunningXid). I don't think this is particularly complicated or a lot of code, and I don't see why would it require data structures in shared memory. Only the walreceiver on standby needs to worry about this, no? regards -- Tomas Vondra http://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --vy6eyainsxhnm65b Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="xid-assignment-v13-fix.patch" diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 1951103b26..b85c046b41 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -7300,6 +7300,19 @@ StartupXLOG(void) TransactionIdIsValid(record->xl_xid)) RecordKnownAssignedTransactionIds(record->xl_xid); + /* + * Assign subtransaction xids to the top level xid if the + * record has that information. This is required at most + * once per subtransactions. + */ + if (TransactionIdIsValid(xlogreader->toplevel_xid) && + standbyState >= STANDBY_INITIALIZED) + { + Assert(XLogStandbyInfoActive()); + ProcArrayCacheXidAssignment(xlogreader->toplevel_xid, + record->xl_xid); + } + /* Now apply the WAL record itself */ RmgrTable[record->xl_rmid].rm_redo(xlogreader); diff --git a/src/backend/storage/ipc/procarray.c b/src/backend/storage/ipc/procarray.c index cfb88db4a4..efddd0e1e6 100644 --- a/src/backend/storage/ipc/procarray.c +++ b/src/backend/storage/ipc/procarray.c @@ -958,6 +958,53 @@ ProcArrayApplyXidAssignment(TransactionId topxid, LWLockRelease(ProcArrayLock); } +static HTAB *xidAssignmentsHash = NULL; + +typedef struct XidAssignmentEntry +{ + /* the hash lookup key MUST BE FIRST */ + TransactionId topXid; + + int nsubxids; + TransactionId subxids[PGPROC_MAX_CACHED_SUBXIDS]; +} XidAssignmentEntry; + +void +ProcArrayCacheXidAssignment(TransactionId topXid, TransactionId subXid) +{ + XidAssignmentEntry *entry; + bool found; + + if (xidAssignmentsHash == NULL) + { + /* First time through: initialize the hash table */ + HASHCTL ctl; + + MemSet(&ctl, 0, sizeof(ctl)); + ctl.keysize = sizeof(TransactionId); + ctl.entrysize = sizeof(XidAssignmentEntry); + xidAssignmentsHash = hash_create("XID assignment cache", 256, + &ctl, HASH_ELEM | HASH_BLOBS); + } + + /* Look for an existing entry */ + entry = (XidAssignmentEntry *) hash_search(xidAssignmentsHash, + (void *) &topXid, + HASH_ENTER, &found); + + if (!found) + entry->nsubxids = 0; + + entry->subxids[entry->nsubxids++] = subXid; + + /* after reaching the limit, apply the assignments for this top XID */ + if (entry->nsubxids == PGPROC_MAX_CACHED_SUBXIDS) + { + ProcArrayApplyXidAssignment(topXid, entry->nsubxids, entry->subxids); + entry->nsubxids = 0; + } +} + /* * TransactionIdIsInProgress -- is given transaction running in some backend * diff --git a/src/include/storage/procarray.h b/src/include/storage/procarray.h index a5c7d0c064..17d89bdd7e 100644 --- a/src/include/storage/procarray.h +++ b/src/include/storage/procarray.h @@ -67,6 +67,7 @@ extern void ProcArrayInitRecovery(TransactionId initializedUptoXID); extern void ProcArrayApplyRecoveryInfo(RunningTransactions running); extern void ProcArrayApplyXidAssignment(TransactionId topxid, int nsubxids, TransactionId *subxids); +extern void ProcArrayCacheXidAssignment(TransactionId topxid, TransactionId subxid); extern void RecordKnownAssignedTransactionIds(TransactionId xid); extern void ExpireTreeKnownAssignedTransactionIds(TransactionId xid, --vy6eyainsxhnm65b--