Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1Y1ttn-00020J-58 for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Dec 2014 09:31:03 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1Y1ttm-0007cn-LR for pgsql-hackers@arkaria.postgresql.org; Fri, 19 Dec 2014 09:31:02 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:DHE_RSA_AES_256_CBC_SHA256:256) (Exim 4.80) (envelope-from ) id 1Y1ttl-0007ch-1U for pgsql-hackers@postgresql.org; Fri, 19 Dec 2014 09:31:01 +0000 Received: from mail-wi0-f180.google.com ([209.85.212.180]) by makus.postgresql.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.80) (envelope-from ) id 1Y1tth-0002ZF-CT for pgsql-hackers@postgresql.org; Fri, 19 Dec 2014 09:30:59 +0000 Received: by mail-wi0-f180.google.com with SMTP id n3so1084959wiv.13 for ; Fri, 19 Dec 2014 01:30:54 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:message-id:date:from:user-agent:mime-version:to :cc:subject:references:in-reply-to:content-type; bh=s2cWkV7mxttTkneauRQd0LLOr+JSK3cbs5i9TXBHz8g=; b=RNOU04oqK9nI4JxbZFxZpOAZXQyUKKTCjnIsnwXaGBwYyPyulot21p4L6F5Azxzvzg jVx+escAjol+/tnJoJk3t3q96JIDMX0uSlDFhHJbRcYd1XAf9KIx476ZBsTTzL/fsPQo 5ALXaWy2C8DYksNgfQXLJoz+0m0PogPN9lJGhGd4gyNd4xhDc6V8u0zzArDTwpP2Dq2u yyOJsIf1DJ+91e/peuvrZncveaJw+fVrb69nIAYAX/rs+7KdRc+gSpYI/5zmoWJ1NoTk RP4A8O8nvsrNBk++zpIHclVHPH0wriCGE6SWKHOo31tmzTHLzqY2YR+H/YrA98bQM520 DC6A== X-Gm-Message-State: ALoCoQn8d3goWRE+wORbOIUTcndCBi/HtMdGVDLwMU+v9g3LYix5NA9IoXDhQwF7t3LMqk97nvBB7uni/I+L19eetpBaFNJUkZL//mNrIhZ397YT8YcC5rB6jHp9YAUptMq3zHJoEGlfyJxnOEgYzWWDojbSR2AP/3YtL/S/TY0ixJYjNvRldrI= X-Received: by 10.180.198.52 with SMTP id iz20mr4012477wic.60.1418981454812; Fri, 19 Dec 2014 01:30:54 -0800 (PST) Received: from [192.168.4.174] ([41.33.182.199]) by mx.google.com with ESMTPSA id et4sm12022521wjd.15.2014.12.19.01.30.53 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Dec 2014 01:30:53 -0800 (PST) Message-ID: <5493F04C.6090900@2ndquadrant.com> Date: Fri, 19 Dec 2014 10:30:52 +0100 From: Petr Jelinek User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.3.0 MIME-Version: 1.0 To: Michael Paquier , Heikki Linnakangas CC: Alvaro Herrera , PostgreSQL-development Subject: Re: [COMMITTERS] pgsql: Keep track of transaction commit timestamps References: <54802C7F.90606@vmware.com> <5480427A.1080200@2ndquadrant.com> <548044CF.5010809@vmware.com> <548049D5.1000605@2ndquadrant.com> <54805306.4030100@vmware.com> <548860B6.50705@2ndquadrant.com> In-Reply-To: <548860B6.50705@2ndquadrant.com> Content-Type: multipart/mixed; boundary="------------020705050604010209070201" X-Pg-Spam-Score: -2.6 (--) 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 This is a multi-part message in MIME format. --------------020705050604010209070201 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 7bit On 10/12/14 16:03, Petr Jelinek wrote: > On 10/12/14 04:26, Michael Paquier wrote: >> On Thu, Dec 4, 2014 at 9:26 PM, Heikki Linnakangas >> wrote: >>> Yeah, it was raised. I don't think it was really addressed. There was >>> lengthy discussion on whether to include LSN, node id, and/or some other >>> information, but there was no discussion on: >>> >>> * What is a node ID? >>> * How is it used? >>> * Who assigns it? >>> >>> etc. >>> >>> None of those things are explained in the documentation nor code >>> comments. >>> > >> >> Could it be possible to get a refresh on that before it bitrots too >> much or we'll simply forget about it? In the current state, there is >> no documentation able to explain what is the point of the nodeid >> stuff, how to use it and what use cases it is aimed at. The API in >> committs.c should as well be part of it. If that's not done, I think >> that it would be fair to remove this portion and simply WAL-log the >> commit timestamp its GUC is activated. > > I have this on my list so it's not being forgotten and I don't see it > bitrotting unless we are changing XLog api again. I have bit busy > schedule right now though, can it wait till next week? - I will post > some code documentation patches then. > Hi, as promised I am sending code-comment patch that explains the use of commit timestamps + nodeid C api for the conflict resolution, comments welcome. -- Petr Jelinek http://www.2ndQuadrant.com/ PostgreSQL Development, 24x7 Support, Training & Services --------------020705050604010209070201 Content-Type: text/x-diff; name="commit_ts_code_doc.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="commit_ts_code_doc.patch" diff --git a/src/backend/access/transam/commit_ts.c b/src/backend/access/transam/commit_ts.c index ca074da..fff1fdd 100644 --- a/src/backend/access/transam/commit_ts.c +++ b/src/backend/access/transam/commit_ts.c @@ -6,6 +6,62 @@ * This module is a pg_clog-like system that stores the commit timestamp * for each transaction. * + * When track_commit_timestamp is enabled, this module will keep track of + * commit timestamp for each transaction. It also provides API to for + * optionally storing nodeid (origin) of each transaction. The main purpose of + * this functionality is to help with conflict detection and resolution for + * replication systems. + * + * The following example shows how to use the API provided by this module, to + * handle UPDATE conflicts coming from replication stream: + * void + * update_tuple(Relation relation, HeapTuple remote_tuple, + * TimestampTz remote_commit_ts, CommitTsNodeId remote_node_id) + * { + * bool exists; + * HeapTupleData local_tuple; + * + * // Find existing tuple with same PK/unique index combination. + * exists = find_local_tuple(relation, remote_tuple, &local_tuple); + * + * // The tuple was found. + * if (exists) + * { + * TransactionId xmin; + * TimestampTz local_commit_ts; + * CommitTsNodeId local_node_id; + * + * xmin = HeapTupleHeaderGetXmin(local_tuple.t_data); + * TransactionIdGetCommitTsData(xmin, &local_commit_ts, &nodeid); + * + * // New tuple is coming from different node than the locally saved + * // tuple and the remote commit timestamp is older than local commit + * // timestamp, this is UPDATE/UPDATE conflict (node being UPDATEd on + * // different nodes at the same time. + * if (remote_id != local_node_id && remote_commit_ts <= local_commit_ts) + * { + * if (remote_commit_ts < local_commit_ts) + * return; // Keep the local tuple. + * + * // Handle the conflict in a consistent manner. + * } + * else + * { + * // The new tuple either comes from same node as old tuple and/or + * // is has newer commit timestamp than the local tuple, apply the + * // UPDATE. + * } + * } + * else + * { + * // Tuple not found (possibly UPDATE/DELETE conflict), handle it + * // in a consistent manner. + * } + * } + * + * See default_node_id and CommitTsSetDefaultNodeId for explanation of how to + * set nodeid when applying transactions. + * * XLOG interactions: this module generates an XLOG record whenever a new * CommitTs page is initialized to zeroes. Also, one XLOG record is * generated for setting of values when the caller requests it; this allows @@ -49,6 +105,15 @@ */ /* + * CommitTimestampEntry + * + * Record containing information about the transaction commit timestamp and + * the nodeid. + * + * The nodeid provides IO efficient way for replication systems to store + * information about origin of the transaction. Currently the nodeid is opaque + * value meaning of which is defined by the replication system itself. + * * We need 8+4 bytes per xact. Note that enlarging this struct might mean * the largest possible file name is more than 5 chars long; see * SlruScanDirectory. @@ -93,6 +158,21 @@ CommitTimestampShared *commitTsShared; /* GUC variable */ bool track_commit_timestamp; +/* + * The default_node_id will be written to commit timestamp record for + * every transaction committed by the current backend. + * + * The idea behind having default_node_id is that replication system + * will have connection (backend running) for specific source of replicated + * data to which it assigned the nodeid for identification purposes. + * + * The default_node_id setting stays in effect for duration of a session + * (unless changed again), so the replication system does not have to set + * the nodeid individually for each transaction. + * + * The public access to the default_node_id is provided by + * CommitTsSetDefaultNodeId and CommitTsGetDefaultNodeId interfaces. + */ static CommitTsNodeId default_node_id = InvalidCommitTsNodeId; static void SetXidCommitTsInPage(TransactionId xid, int nsubxids, @@ -112,7 +192,7 @@ static void WriteSetTimestampXlogRec(TransactionId mainxid, int nsubxids, /* * CommitTsSetDefaultNodeId * - * Set default nodeid for current backend. + * Set default nodeid for current session. */ void CommitTsSetDefaultNodeId(CommitTsNodeId nodeid) @@ -123,7 +203,7 @@ CommitTsSetDefaultNodeId(CommitTsNodeId nodeid) /* * CommitTsGetDefaultNodeId * - * Set default nodeid for current backend. + * Get current default nodeid. */ CommitTsNodeId CommitTsGetDefaultNodeId(void) --------------020705050604010209070201 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 --------------020705050604010209070201--