public inbox for [email protected]  
help / color / mirror / Atom feed
[PATCH 03/10] Make sure published XIDs are persistent
4+ messages / 4 participants
[nested] [flat]

* [PATCH 03/10] Make sure published XIDs are persistent
@ 2021-03-08 06:43  Kyotaro Horiguchi <[email protected]>
  0 siblings, 0 replies; 4+ messages in thread

From: Kyotaro Horiguchi @ 2021-03-08 06:43 UTC (permalink / raw)

pg_xact_status() premises that XIDs obtained by
pg_current_xact_id(_if_assigned)() are persistent beyond a crash. But
XIDs are not guaranteed to go beyond WAL buffers before commit and
thus XIDs may vanish if server crashes before commit. This patch
guarantees the XID shown by the functions to be flushed out to disk.

Copied from: https://www.postgresql.org/message-id/20210308.173242.463790587797836129.horikyota.ntt%40gmail.com
---
 src/backend/access/transam/xact.c | 55 +++++++++++++++++++++++++------
 src/backend/access/transam/xlog.c |  2 +-
 src/backend/utils/adt/xid8funcs.c | 12 ++++++-
 src/include/access/xact.h         |  3 +-
 4 files changed, 59 insertions(+), 13 deletions(-)

diff --git a/src/backend/access/transam/xact.c b/src/backend/access/transam/xact.c
index 6395a9b240..38e978d238 100644
--- a/src/backend/access/transam/xact.c
+++ b/src/backend/access/transam/xact.c
@@ -201,7 +201,7 @@ typedef struct TransactionStateData
 	int			prevSecContext; /* previous SecurityRestrictionContext */
 	bool		prevXactReadOnly;	/* entry-time xact r/o state */
 	bool		startedInRecovery;	/* did we start in recovery? */
-	bool		didLogXid;		/* has xid been included in WAL record? */
+	XLogRecPtr 	minLSN;			/* LSN needed to reach to record the xid */
 	int			parallelModeLevel;	/* Enter/ExitParallelMode counter */
 	bool		chain;			/* start a new block after this one */
 	bool		assigned;		/* assigned to top-level XID */
@@ -520,14 +520,46 @@ GetCurrentFullTransactionIdIfAny(void)
  *	MarkCurrentTransactionIdLoggedIfAny
  *
  * Remember that the current xid - if it is assigned - now has been wal logged.
+ *
+ * upto is the LSN up to which we need to flush WAL to ensure the current xid
+ * to be persistent. See EnsureCurrentTransactionIdLogged().
  */
 void
-MarkCurrentTransactionIdLoggedIfAny(void)
+MarkCurrentTransactionIdLoggedIfAny(XLogRecPtr upto)
 {
-	if (FullTransactionIdIsValid(CurrentTransactionState->fullTransactionId))
-		CurrentTransactionState->didLogXid = true;
+	if (FullTransactionIdIsValid(CurrentTransactionState->fullTransactionId) &&
+		XLogRecPtrIsInvalid(CurrentTransactionState->minLSN))
+		CurrentTransactionState->minLSN = upto;
 }
 
+/*
+ *	EnsureCurrentTransactionIdLogged
+ *
+ * Make sure that the current top XID is WAL-logged.
+ */
+void
+EnsureTopTransactionIdLogged(void)
+{
+	/*
+	 * We need at least one WAL record for the current top transaction to be
+	 * flushed out.  Write one if we don't have one yet.
+	 */
+	if (XLogRecPtrIsInvalid(TopTransactionStateData.minLSN))
+	{
+		xl_xact_assignment xlrec;
+
+		xlrec.xtop = XidFromFullTransactionId(XactTopFullTransactionId);
+		Assert(TransactionIdIsValid(xlrec.xtop));
+		xlrec.nsubxacts = 0;
+
+		XLogBeginInsert();
+		XLogRegisterData((char *) &xlrec, MinSizeOfXactAssignment);
+		TopTransactionStateData.minLSN =
+			XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
+	}
+
+	XLogFlush(TopTransactionStateData.minLSN);
+}
 
 /*
  *	GetStableLatestTransactionId
@@ -616,14 +648,14 @@ AssignTransactionId(TransactionState s)
 	 * When wal_level=logical, guarantee that a subtransaction's xid can only
 	 * be seen in the WAL stream if its toplevel xid has been logged before.
 	 * If necessary we log an xact_assignment record with fewer than
-	 * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if didLogXid isn't set
+	 * PGPROC_MAX_CACHED_SUBXIDS. Note that it is fine if minLSN isn't set
 	 * for a transaction even though it appears in a WAL record, we just might
 	 * superfluously log something. That can happen when an xid is included
 	 * somewhere inside a wal record, but not in XLogRecord->xl_xid, like in
 	 * xl_standby_locks.
 	 */
 	if (isSubXact && XLogLogicalInfoActive() &&
-		!TopTransactionStateData.didLogXid)
+		XLogRecPtrIsInvalid(TopTransactionStateData.minLSN))
 		log_unknown_top = true;
 
 	/*
@@ -693,6 +725,7 @@ AssignTransactionId(TransactionState s)
 			log_unknown_top)
 		{
 			xl_xact_assignment xlrec;
+			XLogRecPtr		   endptr;
 
 			/*
 			 * xtop is always set by now because we recurse up transaction
@@ -707,11 +740,13 @@ AssignTransactionId(TransactionState s)
 			XLogRegisterData((char *) unreportedXids,
 							 nUnreportedXids * sizeof(TransactionId));
 
-			(void) XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
+			endptr = XLogInsert(RM_XACT_ID, XLOG_XACT_ASSIGNMENT);
 
 			nUnreportedXids = 0;
-			/* mark top, not current xact as having been logged */
-			TopTransactionStateData.didLogXid = true;
+
+			/* set minLSN of top, not of current xact if not yet */
+			if (XLogRecPtrIsInvalid(TopTransactionStateData.minLSN))
+				TopTransactionStateData.minLSN = endptr;
 		}
 	}
 }
@@ -2022,7 +2057,7 @@ StartTransaction(void)
 	 * initialize reported xid accounting
 	 */
 	nUnreportedXids = 0;
-	s->didLogXid = false;
+	s->minLSN = InvalidXLogRecPtr;
 
 	/*
 	 * must initialize resource-management stuff first
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 15da91a8dd..6eb46ea8a7 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -1172,7 +1172,7 @@ XLogInsertRecord(XLogRecData *rdata,
 	 */
 	WALInsertLockRelease();
 
-	MarkCurrentTransactionIdLoggedIfAny();
+	MarkCurrentTransactionIdLoggedIfAny(EndPos);
 
 	END_CRIT_SECTION();
 
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index cc2b4ac797..992482f8c8 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -357,6 +357,8 @@ bad_format:
 Datum
 pg_current_xact_id(PG_FUNCTION_ARGS)
 {
+	FullTransactionId xid;
+
 	/*
 	 * Must prevent during recovery because if an xid is not assigned we try
 	 * to assign one, which would fail. Programs already rely on this function
@@ -365,7 +367,12 @@ pg_current_xact_id(PG_FUNCTION_ARGS)
 	 */
 	PreventCommandDuringRecovery("pg_current_xact_id()");
 
-	PG_RETURN_FULLTRANSACTIONID(GetTopFullTransactionId());
+	xid = GetTopFullTransactionId();
+
+	/* the XID is going to be published, make sure it is psersistent */
+	EnsureTopTransactionIdLogged();
+
+	PG_RETURN_FULLTRANSACTIONID(xid);
 }
 
 /*
@@ -380,6 +387,9 @@ pg_current_xact_id_if_assigned(PG_FUNCTION_ARGS)
 	if (!FullTransactionIdIsValid(topfxid))
 		PG_RETURN_NULL();
 
+	/* the XID is going to be published, make sure it is psersistent */
+	EnsureTopTransactionIdLogged();
+
 	PG_RETURN_FULLTRANSACTIONID(topfxid);
 }
 
diff --git a/src/include/access/xact.h b/src/include/access/xact.h
index 34cfaf542c..a61e4d6da5 100644
--- a/src/include/access/xact.h
+++ b/src/include/access/xact.h
@@ -386,7 +386,8 @@ extern FullTransactionId GetTopFullTransactionId(void);
 extern FullTransactionId GetTopFullTransactionIdIfAny(void);
 extern FullTransactionId GetCurrentFullTransactionId(void);
 extern FullTransactionId GetCurrentFullTransactionIdIfAny(void);
-extern void MarkCurrentTransactionIdLoggedIfAny(void);
+extern void MarkCurrentTransactionIdLoggedIfAny(XLogRecPtr upto);
+extern void EnsureTopTransactionIdLogged(void);
 extern bool SubTransactionIsActive(SubTransactionId subxid);
 extern CommandId GetCurrentCommandId(bool used);
 extern void SetParallelStartTimestamps(TimestampTz xact_ts, TimestampTz stmt_ts);
-- 
2.17.0


--jozmn01XJZjDjM3N
Content-Type: text/x-diff; charset=us-ascii
Content-Disposition: attachment;
 filename="0004-wal_compression_method-default-to-zlib.patch"



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Avoid use deprecated Windows Memory API
@ 2023-03-17 13:57  Aleksander Alekseev <[email protected]>
  0 siblings, 1 reply; 4+ messages in thread

From: Aleksander Alekseev @ 2023-03-17 13:57 UTC (permalink / raw)
  To: pgsql-hackers; +Cc: Ranier Vilela <[email protected]>; Daniel Gustafsson <[email protected]>; Alvaro Herrera <[email protected]>

Hi,

> Again, MSDN claims to use a new API.

TWIMC the patch rotted a bit and now needs to be updated [1].
I changed its status to "Waiting on Author" [2].

[1]: http://cfbot.cputube.org/
[2]: https://commitfest.postgresql.org/42/3893/

-- 
Best regards,
Aleksander Alekseev






^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Avoid use deprecated Windows Memory API
@ 2023-03-17 15:19  Ranier Vilela <[email protected]>
  parent: Aleksander Alekseev <[email protected]>
  0 siblings, 1 reply; 4+ messages in thread

From: Ranier Vilela @ 2023-03-17 15:19 UTC (permalink / raw)
  To: Aleksander Alekseev <[email protected]>; +Cc: pgsql-hackers; Daniel Gustafsson <[email protected]>; Alvaro Herrera <[email protected]>

Em sex., 17 de mar. de 2023 às 10:58, Aleksander Alekseev <
[email protected]> escreveu:

> Hi,
>
> > Again, MSDN claims to use a new API.
>
> TWIMC the patch rotted a bit and now needs to be updated [1].
> I changed its status to "Waiting on Author" [2].
>
Rebased to latest Head.

best regards,
Ranier Vilela


Attachments:

  [application/octet-stream] v2-use-heapalloc-instead-deprecated-localalloc.patch (3.2K, ../../CAEudQAoAjCodLnyjg4piyLJyB_7BjKTrkGOq_zE-8TMhNoH1uQ@mail.gmail.com/3-v2-use-heapalloc-instead-deprecated-localalloc.patch)
  download | inline diff:
diff --git a/src/common/exec.c b/src/common/exec.c
index 93b2faf2c4..ff59e0bf66 100644
--- a/src/common/exec.c
+++ b/src/common/exec.c
@@ -561,13 +561,23 @@ AddUserToTokenDacl(HANDLE hToken)
 	TOKEN_DEFAULT_DACL *ptdd = NULL;
 	TOKEN_INFORMATION_CLASS tic = TokenDefaultDacl;
 	BOOL		ret = FALSE;
+	HANDLE 	hDefaultProcessHeap;	
+
+	hDefaultProcessHeap = GetProcessHeap();
+	if (hDefaultProcessHeap == NULL)
+	{
+		log_error(errcode(ERRCODE_SYSTEM_ERROR),
+				"could not get default process heap: error code %lu",
+				GetLastError());
+		return FALSE;
+    }
 
 	/* Figure out the buffer size for the DACL info */
 	if (!GetTokenInformation(hToken, tic, (LPVOID) NULL, dwTokenInfoLength, &dwSize))
 	{
 		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
 		{
-			ptdd = (TOKEN_DEFAULT_DACL *) LocalAlloc(LPTR, dwSize);
+			ptdd = (TOKEN_DEFAULT_DACL *) HeapAlloc(hDefaultProcessHeap, 0, dwSize);
 			if (ptdd == NULL)
 			{
 				log_error(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -612,7 +622,7 @@ AddUserToTokenDacl(HANDLE hToken)
 		GetLengthSid(pTokenUser->User.Sid) - sizeof(DWORD);
 
 	/* Allocate the ACL buffer & initialize it */
-	pacl = (PACL) LocalAlloc(LPTR, dwNewAclSize);
+	pacl = (PACL) HeapAlloc(hDefaultProcessHeap, 0, dwNewAclSize);
 	if (pacl == NULL)
 	{
 		log_error(errcode(ERRCODE_OUT_OF_MEMORY),
@@ -669,13 +679,13 @@ AddUserToTokenDacl(HANDLE hToken)
 
 cleanup:
 	if (pTokenUser)
-		LocalFree((HLOCAL) pTokenUser);
+		HeapFree(hDefaultProcessHeap, 0, pTokenUser);
 
 	if (pacl)
-		LocalFree((HLOCAL) pacl);
+		HeapFree(hDefaultProcessHeap, 0, pacl);
 
 	if (ptdd)
-		LocalFree((HLOCAL) ptdd);
+		HeapFree(hDefaultProcessHeap, 0, ptdd);
 
 	return ret;
 }
@@ -685,16 +695,26 @@ cleanup:
  *
  * Get the users token information from a process token.
  *
- * The caller of this function is responsible for calling LocalFree() on the
+ * The caller of this function is responsible for calling HeapFree() on the
  * returned TOKEN_USER memory.
  */
 static BOOL
 GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
 {
 	DWORD		dwLength;
+	HANDLE 	hDefaultProcessHeap;	
 
 	*ppTokenUser = NULL;
 
+	hDefaultProcessHeap = GetProcessHeap();
+    if (hDefaultProcessHeap == NULL)
+	{
+		log_error(errcode(ERRCODE_SYSTEM_ERROR),
+				"could not get default process heap: error code %lu",
+				GetLastError());
+		return FALSE;
+    }
+
 	if (!GetTokenInformation(hToken,
 							 TokenUser,
 							 NULL,
@@ -703,7 +723,7 @@ GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
 	{
 		if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
 		{
-			*ppTokenUser = (PTOKEN_USER) LocalAlloc(LPTR, dwLength);
+			*ppTokenUser = (PTOKEN_USER) HeapAlloc(hDefaultProcessHeap, 0, dwLength);
 
 			if (*ppTokenUser == NULL)
 			{
@@ -727,7 +747,7 @@ GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
 							 dwLength,
 							 &dwLength))
 	{
-		LocalFree(*ppTokenUser);
+		HeapFree(hDefaultProcessHeap, 0, *ppTokenUser);
 		*ppTokenUser = NULL;
 
 		log_error(errcode(ERRCODE_SYSTEM_ERROR),
@@ -736,7 +756,7 @@ GetTokenUser(HANDLE hToken, PTOKEN_USER *ppTokenUser)
 		return FALSE;
 	}
 
-	/* Memory in *ppTokenUser is LocalFree():d by the caller */
+	/* Memory in *ppTokenUser is HeapFree():d by the caller */
 	return TRUE;
 }
 


^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: Avoid use deprecated Windows Memory API
@ 2023-03-19 22:41  Michael Paquier <[email protected]>
  parent: Ranier Vilela <[email protected]>
  0 siblings, 0 replies; 4+ messages in thread

From: Michael Paquier @ 2023-03-19 22:41 UTC (permalink / raw)
  To: Ranier Vilela <[email protected]>; +Cc: Aleksander Alekseev <[email protected]>; pgsql-hackers; Daniel Gustafsson <[email protected]>; Alvaro Herrera <[email protected]>

On Fri, Mar 17, 2023 at 12:19:56PM -0300, Ranier Vilela wrote:
> Rebased to latest Head.

I was looking at this thread, and echoing Daniel's and Alvaro's
arguments, I don't quite see why this patch is something we need.  I
would recommend to mark it as rejected and move on.
--
Michael


Attachments:

  [application/pgp-signature] signature.asc (833B, ../../[email protected]/2-signature.asc)
  download

^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2023-03-19 22:41 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-08 06:43 [PATCH 03/10] Make sure published XIDs are persistent Kyotaro Horiguchi <[email protected]>
2023-03-17 13:57 Re: Avoid use deprecated Windows Memory API Aleksander Alekseev <[email protected]>
2023-03-17 15:19 ` Re: Avoid use deprecated Windows Memory API Ranier Vilela <[email protected]>
2023-03-19 22:41   ` Re: Avoid use deprecated Windows Memory API Michael Paquier <[email protected]>

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox