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 1rRNDx-006mPo-9E for pgsql-hackers@arkaria.postgresql.org; Sun, 21 Jan 2024 02:06:13 +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 1rRNDw-00Erq8-2C for pgsql-hackers@arkaria.postgresql.org; Sun, 21 Jan 2024 02:06:12 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rRNDv-00Erpz-Ow for pgsql-hackers@lists.postgresql.org; Sun, 21 Jan 2024 02:06:11 +0000 Received: from mail-yw1-x1134.google.com ([2607:f8b0:4864:20::1134]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rRNDq-002sq7-E9 for pgsql-hackers@postgresql.org; Sun, 21 Jan 2024 02:06:10 +0000 Received: by mail-yw1-x1134.google.com with SMTP id 00721157ae682-5f254d1a6daso19326597b3.2 for ; Sat, 20 Jan 2024 18:06:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1705802765; x=1706407565; darn=postgresql.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=gZnj1dWOD6Ez2K65q8LrYz8+eDkiU5XRZUpETGvJzXM=; b=WbIa0UTktNEUpIEKtv3usMzQMmb1UUn1zM4zS09aUvq2NJ3FcX4gVAZmaDmArAJKfV xmZbue8DCcY0K5KqicXd8FDuzAJnPDYULRgYqK1p/wtCkJs+QhqA7H0qa9lPyx7c96gc yOIWkdYIOsRXoLS6QxHisjHYjPqIpkn9CLQnmuCJw0bhOjn2PfbuH1340KR13Jb17qMj 7XXToClAFZyenJE7wJiyDRTFOc0Dua6Van/GuoSqHD97d6qpo4ixb0iva3kzJxgfzMgX Sy8UY20vsLWYrwK6QgX3YNZJ7ocrJma0oOehwogJwq1+MFc9Pkkj2yy8m7/U27owWbO4 SULA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705802765; x=1706407565; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=gZnj1dWOD6Ez2K65q8LrYz8+eDkiU5XRZUpETGvJzXM=; b=tdIMlrk985qQMLxSmWR3sHSA5SX0bHm+S8il1rWcF1Z1zF2nDH5BejgWPFbdbRN2od F/Q7aviQ3iGVpmeV9PqoDJyDZnXO0cHm+GgN9qna1mH4hDwq5eOdo+G+1ZIozrEhCWwn 6yaulUsf0DpszzmqxBF5Gxp0dDQLdA1eMcB1l7gC5sPmwWZcjKwGRHDQSpj3YlO/dqkP 1hHvqvgMVnN3mCcOdzW1IoFZP4t3nibHeo86n0vY/pH+gkg3c4z2l2efta+jeAlZKD8G v30ok/0GegtW47iMkFtZhmP7BCErAkjqSeVZ2CKyEQXGnvD7YjgBmD3k9mNtzhDbFziS plng== X-Gm-Message-State: AOJu0YzfqEMk/C18lEOr/y7ZbycqrfCOzW6lRD/M3udmsuf0hZR89w93 w+mwrXgeZlN7N72+UFsMrFE8C3o3/3hXBDFdUENLcUyge3tTw8XlOcZC548MFf03KKWeQpji7Am vbTzYhLlm6NCLJS+KqwOBkjwQA2s= X-Google-Smtp-Source: AGHT+IH7IBV23A90VFfl/litgao6P91sHXu8klGZFYGiw+QBuVFh3pe4ZwI3gNxbMOE3nPZhnsMCUEswtL5jfrEz2zM= X-Received: by 2002:a0d:e68f:0:b0:5f8:f50e:bcf9 with SMTP id p137-20020a0de68f000000b005f8f50ebcf9mr1513947ywe.20.1705802764494; Sat, 20 Jan 2024 18:06:04 -0800 (PST) MIME-Version: 1.0 References: <0974d29d-d047-36fb-d466-93b933411442@iki.fi> In-Reply-To: <0974d29d-d047-36fb-d466-93b933411442@iki.fi> From: vignesh C Date: Sun, 21 Jan 2024 07:35:53 +0530 Message-ID: Subject: Re: Cross-database SERIALIZABLE safe snapshots To: Heikki Linnakangas Cc: Thomas Munro , pgsql-hackers Content-Type: text/plain; charset="UTF-8" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Mon, 10 Jul 2023 at 14:48, Heikki Linnakangas wrote: > > On 09/03/2023 07:34, Thomas Munro wrote: > > Here is a feature idea that emerged from a pgsql-bugs thread[1] that I > > am kicking into the next commitfest. Example: > > > > s1: \c db1 > > s1: CREATE TABLE t (i int); > > s1: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE; > > s1: INSERT INTO t VALUES (42); > > > > s2: \c db2 > > s2: BEGIN TRANSACTION ISOLATION LEVEL SERIALIZABLE READ ONLY DEFERRABLE; > > s2: SELECT * FROM x; > > > > I don't know of any reason why s2 should have to wait, or > > alternatively, without DEFERRABLE, why it shouldn't immediately drop > > from SSI to SI (that is, opt out of predicate locking and go faster). > > This change relies on the fact that PostgreSQL doesn't allow any kind > > of cross-database access, except for shared catalogs, and all catalogs > > are already exempt from SSI. I have updated this new version of the > > patch to explain that very clearly at the place where that exemption > > happens, so that future hackers would see that we rely on that fact > > elsewhere if reconsidering that. > > Makes sense. > > > @@ -1814,7 +1823,17 @@ GetSerializableTransactionSnapshotInt(Snapshot snapshot, > > { > > othersxact = dlist_container(SERIALIZABLEXACT, xactLink, iter.cur); > > > > - if (!SxactIsCommitted(othersxact) > > + /* > > + * We can't possibly have an unsafe conflict with a transaction in > > + * another database. The only possible overlap is on shared > > + * catalogs, but we don't support SSI for shared catalogs. The > > + * invalid database case covers 2PC, because we don't yet record > > + * database OIDs in the 2PC information. We also filter out doomed > > + * transactions as they can't possibly commit. > > + */ > > + if ((othersxact->database == InvalidOid || > > + othersxact->database == MyDatabaseId) > > + && !SxactIsCommitted(othersxact) > > && !SxactIsDoomed(othersxact) > > && !SxactIsReadOnly(othersxact)) > > { > > Why don't we set the database OID in 2PC transactions? We actually do > set it correctly - or rather we never clear it - when a transaction is > prepared. But you set it to invalid when recovering a prepared > transaction on system startup. So the comment is a bit misleading: the > optimization doesn't apply to 2PC transactions recovered after restart, > other 2PC transactions are fine. > > I'm sure it's not a big deal in practice, but it's also not hard to fix. > We do store the database OID in the twophase state. The caller of > predicatelock_twophase_recover() has it, we just need a little plumbing > to pass it down. > > Attached patches: > > 1. Rebased version of your patch, just trivial pgindent conflict fixes > 2. Some comment typo fixes and improvements > 3. Set the database ID on recovered 2PC transactions > > A test for this would be nice. isolationtester doesn't support > connecting to different databases, restarting the server to test the 2PC > recovery, but a TAP test could do it. @Thomas Munro As this patch is already marked as "Ready for Committer", do you want to take this patch forward based on Heikki's suggestions and get it committed? Regards, Vignesh