public inbox for [email protected]  
help / color / mirror / Atom feed
From: [email protected]
To: [email protected]
Subject: pg_stat_statements: improve loading and saving routines for  the dump file
Date: Mon, 20 Jan 2025 16:49:45 +0300
Message-ID: <[email protected]> (raw)

Hi!

Currently in pg_stat_statements save/load routines the whole pgssEntry 
entity data are written/read with its last field
slock_t		mutex;
which is actually not used then.
This small patch fixes this issue. Hope, it will be useful.

Respectfully,

Mikhail Litsarev,
Postgres Professional: https://postgrespro.com

Attachments:

  [text/x-diff] v1-0001-Exclude-pgssEntry-mutex-for-rw-dump-file.patch (2.3K, ../[email protected]/2-v1-0001-Exclude-pgssEntry-mutex-for-rw-dump-file.patch)
  download | inline diff:
From 40392a495a91dfc1ee2c62812949eebe54276625 Mon Sep 17 00:00:00 2001
From: Mikhail Litsarev <[email protected]>
Date: Mon, 20 Jan 2025 15:40:12 +0300
Subject: [PATCH] pg_stat_statements: improve loading and saving routines for
 the dump file.

Exclude reading/writing pgssEntry mutex from/into the dump file.
Update the magic number identifying the stats file format.
---
 contrib/pg_stat_statements/pg_stat_statements.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/contrib/pg_stat_statements/pg_stat_statements.c b/contrib/pg_stat_statements/pg_stat_statements.c
index bebf8134eb0..0511a2b7ca1 100644
--- a/contrib/pg_stat_statements/pg_stat_statements.c
+++ b/contrib/pg_stat_statements/pg_stat_statements.c
@@ -82,7 +82,7 @@ PG_MODULE_MAGIC;
 #define PGSS_TEXT_FILE	PG_STAT_TMP_DIR "/pgss_query_texts.stat"
 
 /* Magic number identifying the stats file format */
-static const uint32 PGSS_FILE_HEADER = 0x20220408;
+static const uint32 PGSS_FILE_HEADER = 0x20250120;
 
 /* PostgreSQL major version number, changes in which invalidate all entries */
 static const uint32 PGSS_PG_MAJOR_VERSION = PG_VERSION_NUM / 100;
@@ -233,7 +233,8 @@ typedef struct pgssEntry
 	int			encoding;		/* query text encoding */
 	TimestampTz stats_since;	/* timestamp of entry allocation */
 	TimestampTz minmax_stats_since; /* timestamp of last min/max values reset */
-	slock_t		mutex;			/* protects the counters only */
+	/* protects the counters only. Should be the very last field */
+	slock_t		mutex;
 } pgssEntry;
 
 /*
@@ -625,7 +626,8 @@ pgss_shmem_startup(void)
 		pgssEntry  *entry;
 		Size		query_offset;
 
-		if (fread(&temp, sizeof(pgssEntry), 1, file) != 1)
+		/* Read whole pgssEntry excluding very last mutex field */
+		if (fread(&temp, offsetof(pgssEntry, mutex), 1, file) != 1)
 			goto read_error;
 
 		/* Encoding is the only field we can easily sanity-check */
@@ -782,7 +784,8 @@ pgss_shmem_shutdown(int code, Datum arg)
 		if (qstr == NULL)
 			continue;			/* Ignore any entries with bogus texts */
 
-		if (fwrite(entry, sizeof(pgssEntry), 1, file) != 1 ||
+		/* Write whole pgssEntry excluding very last mutex field */
+		if (fwrite(entry, offsetof(pgssEntry, mutex), 1, file) != 1 ||
 			fwrite(qstr, 1, len + 1, file) != len + 1)
 		{
 			/* note: we assume hash_seq_term won't change errno */
-- 
2.34.1



view thread (7+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: pg_stat_statements: improve loading and saving routines for  the dump file
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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