Commit Graph

196 Commits (639bf6f1581cc19fb9017a5d885ff8f1837ab114)

Author SHA1 Message Date
Hamid Akhtar 639bf6f158 Version bumped for the 2.0.1 release. 2023-05-24 12:28:16 +05:00
Hamid Akhtar 39d9419bd0 PG-624: pg_stat_monitor: Possible server crash when running pgbench with pg_stat_monitor loaded (#396)
PG-624: pg_stat_monitor: Possible server crash when running pgbench
with pg_stat_monitor loaded

It appears that this issue was being caused by improper handling of
dynamic number of buckets. This commit resolves the issue.

Also, as part of a larger cleanup, memory context has been moved to
local space from shared storage. Also, some unwanted
and no-longer-needed variables are removed.

Co-authored-by: Muhammad Usama <muhammad.usama@percona.com>
2023-05-24 12:27:40 +05:00
Hamid Akhtar 2ceb47e3cd PG-613: Postgresql crashes with Segmentation fault when query plan is enabled on large queries
The return value for snprintf was incorrectly being recorded as plan
length. That's been resolved.

As part of this fix, we've also elminated the possibility of a potential
memory leak when plan text was being saved.

Co-authored-by: Hamid Akhtar <hamid.akhtar@percona.com>
Co-authored-by: Muhammad Usama <muhammad.usama@percona.com>
2023-05-24 12:26:30 +05:00
Hamid Akhtar cf5be6dd5d Adding pgsm_query_id for pgsm_store_error function 2023-03-01 19:40:24 +05:00
Hamid Akhtar 9ecd2ccbb7 Updating formatting of source code 2023-03-01 19:40:24 +05:00
Hamid Akhtar faa938b8f1 Fixing code indentation with pgindent 2023-02-27 14:47:27 +05:00
Muhammad Usama c31d3ba332
PG-609: Bump version to 2.0.0 for the release. (#385)
Version bumped for the 2.0.0 release.
2023-02-23 23:02:18 +05:00
Muhammad Usama fe23d31bf9
PG-607: Allow histogram to track queries in sub-ms time brackets (#384)
* PG-607: Allow histogram to track queries in sub-ms time brackets

Updated the GUC configuration and the relevant histogram functionality
to track queries in lower cardinality than ms. This is done by saving
the GUC values for histogram min and max values in real (double) type.

All test cases except for the 030 tap test are passing. The test case
needs an update.

* Fixing regression issues for v12 and below because of histogram changes.
2023-02-23 21:24:40 +05:00
Muhammad Usama 05ffcac2fa
PG-606: New GUC required for enabling/disabling of pgsm_query_id calculation… (#383)
* PG-606: New GUC required for enabling/disabling of pgsm_query_id calculation

Adds a new GUC pg_stat_monitor.pgsm_enable_pgsm_query_id to enable/disable
pgsm query id calculation. Apart from that patch also refactors the GUC-related
code to match PostgreSQL conventions.

Moreover, the commit also changes the pgsm_enable_overflow GUC to boolean
instead of enum.
2023-02-23 19:08:09 +05:00
Hamid Akhtar 9d2efb8913 PG-542: Performance improvement of pg_stat_monitor.
Refining the code for storing ip locally.
2023-02-23 13:18:38 +05:00
Hamid Akhtar ccaa910c35 PG-542: Performance improvement of pg_stat_monitor.
Saving the client IP address once per the lifetime of a backend. This avoid
the expensive operation multiple times, and hence improving performance
significantly.
2023-02-23 02:41:39 +05:00
Hamid Akhtar 8482bcc347 Revert "PG-542: Performance improvement of pg_stat_monitor."
This reverts commit 7b0e603bcf.
2023-02-23 02:41:39 +05:00
Hamid Akhtar 5f6425a6f8 PG-542: Performance improvement of pg_stat_monitor.
Performance related changes where some calculations are moved out
of the spinlock in the pgsm_update_entry function. This should
improve the performance a bit.

Also, moved the histogram calculation function to init. The update
function now only searches an array rather than recalculatiing the
histogram bucket timings.

Updated conditional statement to update parent query only when
required.
2023-02-23 01:53:30 +05:00
Muhammad Usama 7b0e603bcf PG-542: Performance improvement of pg_stat_monitor.
Saving the client IP address once per the lifetime of a backend. This avoid
the expensive operation multiple times, and hence improving performance
significantly.
2023-02-23 01:33:23 +05:00
Hamid Akhtar de66ef0fce PG-588: Some queries are not being normalised.
This bug uncovered serious issues with how the data was being stored by PSGM.
So it require a complete redesign.

pg_stat_monitor now stores the data locally within the backend process's local
memory. The data is only stored when the query completes. This reduces the
number of lock acquisitions that were previously needed during various stages
of the execution. Also, this avoids data loss in case the current bucket
changes during execution. Also, the unavailability of jumble state during later
stages of executions was causing pg_stat_monitor to save non-normalized query.
This was a major problem as well.

pg_stat_monitor specific memory context is implemented. It is used for saving
data locally. The context memory callback helps us clear the locally saved data
so that we do not store it multiple times in the shared hash.

As part of this major rewrite, pgss reference in function and variable names
is changed to pgsm. Memory footprint for the entries is reduced, data types
are corrected where needed, and we've removed unused variables, functions and
macros.

This patch was mutually created by:
Co-authored-by: Hamid Akhtar <hamid.akhtar@percona.com>
Co-authored-by: Muhammad Usama <muhammad.usama@percona.com>
2023-02-22 19:31:52 +05:00
Muhammad Usama 8193e527da
PG-587: pg_stat_monitor: Validate the upgrade from 1.x to 2.0 version (#370)
Disallow V1 API to be used with V2.0 lib and remove pg_stat_monitor--1.0.sql
as part of that. A few adjustments to 1.x to 2.0 upgrade script are also
part of the commit
2023-02-01 01:38:02 +05:00
Hamid Akhtar 3b6fc3846c PG-601: Histogram ranges are not correct
Resolved the issue with histogram outlier buckets. Also updated
the printing of bucket ranges to be in correct set notation with
reference to brackets. The lower bounds of buckets always have an
exclusive range except for the first bucket, and the upper bounds
always have an inclusive value.
( or ) => exclusive
{ or } => inclusive

The entire range is enclosed within the {} brackets.
2023-01-25 20:31:14 +05:00
Hamid Akhtar 3487e70cc6 PG-599: PGSM build failure on PG-11
Resolving the compilation issue caused by ereport statement.
2023-01-25 12:51:57 +05:00
Hamid Akhtar 9327c864d3 PG-586: pg_stat_monitor: CPU and user timing should be captured
for utility statements as well

Setting user and sys time to 0 in case there is a problem getting
rusage details.
2023-01-25 12:50:29 +05:00
Hamid Akhtar ee18c16149 PG-586: pg_stat_monitor: CPU and user timing should be captured
for utility statements as well

Added the necessary capture of resource usage  in the process
utility function. We are now storing CPU and user timings for a
utility statement.
2023-01-25 12:50:29 +05:00
Muhammad Usama ac8800a637
PG-597: pg_stat_monitor: Remove rounding off for floating point values (#364)
As an observability tool that serves data to other tools, data must be output without any loss. So rounding off causes data loss and rounding off errors when comparing different columns.

Therefore, it was decided to eliminate rounding off when outputting values. Any consumer of this data should round off data to whatever precision it prefers.

This behaviour is also consistent with pg_stat_statements.
2023-01-24 16:54:13 +05:00
Muhammad Usama 5648b99eee
PG-585: pg_stat_monitor: Add code comments to the DSA related funcs.. (#360)
Adding code comments for the DSA related functionality.
2023-01-23 14:36:23 +05:00
Hamid Akhtar dfd41519cf PG-588: Some queries are not being normalised.
There is no specific test case where I can either reproduce or validate
the fix. Though, one of the suspects is this condition in pgss_store.
Therefore removed, and it requires verification.
2023-01-23 12:39:18 +05:00
Hamid Akhtar 1662e9efa1 PG-562: Histogram Ranges/Buckets are not correct.
Replaced the error on server start with a warning. The functionality
now handles "pgsm_histogram_buckets" as the maximum number of histogram
buckets to be created. On init, pg_stat_monitor calculates the max
number of buckets that can be created within the given min/max time
range. If the number is below the user configuration, it emits a
warning in the log file stating the number of max buckets set.
2023-01-23 12:37:51 +05:00
Hamid Akhtar 209f370cef PG-562: Histogram Ranges/Buckets are not correct.
Added buckets for queries that take less than minimum histogram time
and one for the ones taking more than the max value specified.

Also, in case the buckets end up overlapping, on server start, an
error will be thrown informing the user of this issue and requesting
a rectification.

Refactored the code to consolidate the calculations in a single
function.
2023-01-23 12:37:51 +05:00
Hamid Akhtar 1286427445
PG-543: pg_stat_monitor: PostgreSQL's pg_stat_statements compatible view. (#352)
* PG-543: pg_stat_monitor: PostgreSQL's pg_stat_statements compatible view.

The view now carries all the columns as pg_stat_statements. This required fixing
data types of some of the columns, renaming a few, as well inclusion of new
columns to make the view fully compatible with pg_stat_statements.

* PG-543: pg_stat_monitor: PostgreSQL's pg_stat_statements compatible view.

Updating the upgrade sql file from 1.0 to 2.0 version linked with this issue
changes.

* PG-543: pg_stat_monitor: PostgreSQL's pg_stat_statements compatible view.

Updating datum calls to use UInt64 rather than Int64.
2023-01-19 01:55:20 +05:00
Hamid Akhtar 7dece7cf1d
PG-582: blk_read_time and blk_write_time are not being rounded. (#353)
* PG-582: blk_read_time and blk_write_time are not being rounded.

Added the round off within the internal function so that values for
blk_read_time, blk_write_time are rounded off to 4 decimal places.

Additionally, added rounding off for the PG15+ columns of
temp_blk_read_time and temp_blk_write_time.

* PG-582: blk_read_time and blk_write_time are not being rounded.

Added rounding off for four JIT related columns introduced for PG15.
2023-01-18 17:17:23 +05:00
Muhammad Usama a75e47add9 PG-400: pg_stat_monitor: Timezone in msgtime column...
The bucket start time reported by pg_stat_monitor does not match the PG time and
timezone. The fix is to use TimestampTz for recording the bucket start time.
2023-01-18 16:38:23 +05:00
Muhammad Usama caeb5f5e73
PG-579: Querying pg_stat_monitor crashes the server ... (#351)
pgsm_get_ss() must only be called when pg_stat_monitor.so is loaded.
Fix is to move the pgsm_get_ss() call after checking if the pg_stat_monotor
library is loaded or not.
2023-01-17 15:49:38 +05:00
Muhammad Usama 2c5e12af0a
PG-488: pg_stat_monitor: Overflow management. (#342)
* PG-488: pg_stat_monitor: Overflow management.

Reimplement the storage mechanism of buckets (for PG-15 onward) and query texts
using Dynamic shared memory. Since the dynamic shared memory can grow into a
swap area, so we get the overflow out of the box.

As PostgreSQL versions prior to V15 does not support sequence scan on dynamic
shared memory hashes, so older versions has to live with the classic shared
memory hash for storing the buckets.

Another noteworthy change with the new design is: it saves the query pointer
inside the bucket, and eventually, the query text gets evicted with the bucket
recycle.

Finally, the dynamic shared memory hash has a built-in locking mechanism, so we
can revisit the whole locking in pg_stat_monitor has the potential for lots of
performance improvements.

* Fixing tap test reported issues and also disabling dynamic hash for all versions

* Updating the expected out file for top_query test case

Co-authored-by: Hamid Akhtar <hamid.akhtar@percona.com>
2023-01-10 17:54:17 +05:00
Hamid Akhtar f170322f38 PG-576 - Segmentation fault caused by pg_stat_monitor unique
queryid creation mechanism.

Resolving the crash identified by regression and reported by Naeem.
This fix resolves the issue with incorrect query length in case of
normalized query when the query length exceeds PGSM_QUERY_MAX_LEN.

Resolving the crash identify by regression and reported by Naeem.
2023-01-03 17:55:44 +05:00
Hamid Akhtar b20eda7066 PG-545: pg_stat_monitor: Same query text should generate same queryid
Regardless of the database or the user, the same query will yield the
same query ID. As part of this, a new column, 'pgsm_query_id', is added.

* pgsm_query_id:
pgsm_query_id has the same data type of int8 as the queryid column. If
the incoming SQL command includes any constants, it internally normalizes
the query to remove those constant values with placeholders. Otherwise,
it uses the query directly to generate the query hash.

Since we no longer depend on the server's parse tree mechanism, we can
generate the same hash for the same query text for all server versions.

Also, it is important to note that the hash being calculated is a database,
schema and user independent. So same query text in different databases
will generate the same hash.

This column is not part of the key; rather, for observability purposes only.

* Regression
SQL test case pgsm_query_id.sql is added to the SQL regression.
2022-12-28 14:24:19 +05:00
Ibrar Ahmed 802774a2a7
PG-488: Revert pg_stat_monitor: Overflow management. (#338)
PG-488: Revert pg_stat_monitor: Overflow management.

This patch does not work for  < PostgreSQL - 15. More work required.
2022-12-22 19:15:14 +05:00
Ibrar Ahmed 7c5ad48276
Merge pull request #334 from EngineeredVirus/main
PG-354: pg_stat_monitor: Remove pg_stat_monitor_settings view
2022-12-21 20:34:00 +05:00
Muhammad Usama df0580b741 PG-488: pg_stat_monitor: Overflow management.
Reimplement the storage mechanism of buckets and query texts
using Dynamic shared memory. Since the dynamic shared memory
can grow into a swap area, so we get the overflow out of the box.

oreover the new design saves the query pointer inside the bucket
and eventually, the query text gets evicted with the bucket recycle.

Finally, the dynamic shared memory hash has a built-in locking
mechanism so we can revisit the whole locking in pg_stat_monitor
has potential for lots of performance improvements
2022-12-20 17:29:15 +05:00
Hamid Akhtar 2917ae6805 PG-354: pg_stat_monitor: Remove pg_stat_monitor_settings view
Removing the view for 2.0. Updating the required SQL files to manage
the upgrade. Downgrade from 2.x to 1.x is not supported.

Also part of this fix is the SQL regression. This does not update the
tap test cases.
2022-12-13 17:05:46 +05:00
Muhammad Usama 913064b68d
PG-435: Adding new counters that are available in PG15 (#329)
In line with pg_stat_statments for PG15, This commit adds eight new cumulative
counters for jit operations, making it easier to diagnose how JIT is used in an
installation. And two new columns, temp_blk_read_time, and temp_blk_write_time,
respectively, show the time spent reading and writing temporary file blocks
on disk.
Moreover, The commit also contains a few indentations and API adjustments.
2022-12-07 15:40:13 +05:00
Ibrar Ahmed f7860b472f
PG-310: Bucket is “Done” vs still being current/last. (#321)
A new column is added to mention that bucket is active or done. there is    
some timing based adjustment was required with that too.   

Co-authored-by: Hamid Akhtar <hamid.akhtar@percona.com>
2022-11-23 02:23:28 +05:00
Muhammad Usama 2f2c40ed22
PG-555 :Infrastructure to allow multiple SQL APIs (#320)
Creating the infrastructure that'll allow using newer versions
of the loadable module with old SQL declarations.
Also updating the build version to 2.0.0-dev
2022-11-21 18:27:21 +05:00
Ibrar Ahmed 710103cd0d PG-320: Removing the query state code from the view. 2022-11-16 19:37:15 +00:00
Ibrar Ahmed bc19c99c0b PG-306: The bucket start time should be timestamp instead of TEXT. 2022-11-15 18:11:10 +00:00
Ibrar Ahmed 40afdce2eb PG-552: Remove unnecessary columns from PostgreSQL 11 and 12 views.
There was a typo while checking the PostgreSQL version in the SQL file. This commit
will fix the typo, and only the necessary columns will be visible in the view.
2022-11-15 17:10:42 +00:00
Ibrar Ahmed db5a6aa30e PG-320: Removing the query state code from the view.
The query status monitoring code was used to track the current query state, for example,     
parsing, executing and finishing. After careful review, we have figured out that  
it does not make sense while a lot of time same query is running. Therefore it  
is also consuming resources. This commit will remove that feature. The upgrade
SQL from 1.0 - 2.0 is also updated.
2022-11-15 16:31:37 +00:00
Kai Wagner 2cef796e92 PG-526: bump version to 1.1.1 and adding release notes
Signed-off-by: Kai Wagner <kai.wagner@percona.com>
2022-11-08 23:31:01 +05:00
Muhammad Usama 0fe9908d5f PG-520 pg_stat_monitor does not work with PG15
PG 15 requires additional shared memory and LWLocks requests to be made from the
newly introduced shmem_request_hook and disallows the requests initiated
from outside the hook.
The commit makes moves the additional shared memory and LWLocks requests
from _PG_init to shmem_request_hook for PG15
2022-11-08 23:30:29 +05:00
Ibrar Ahmed 634f0ce580
PG-174: Code cleanup. (#310)
pg_stat_monitor is a bit longer; therefore, it requires some code cleanup.
Therefore I decided to turn these tasks into multiple commits and PR to avoid
various changes in one PR. This will ease the review and Q/A process.
In this commit, I have done these tasks.

1 - Fixing compilation issue, cause by previous commit, where we removed
the benchmarking code. It was causing problem for PostgreSQL-12.
2022-10-25 01:04:23 +05:00
Ibrar Ahmed 0af6295513 PG-174: Code cleanup.
pg_stat_monitor is a bit longer; therefore, it requires some code cleanup.
Therefore I decided to turn these tasks into multiple commits and PR to avoid
various changes in one PR. This will ease the review and Q/A process.
In this commit, I have done these tasks.

1 - Remove all benchmarking and debugging code.
2022-10-24 17:42:38 +00:00
Hamid Akhtar b920224e0f
Merging the 1.1.0 branch back to main branch (#303)
* PG-475: Inconsistent behaviour of PGSM

Reverting the bucket locking mechanism to previous behavior. This has
a lot of room for improvement that needs to be part of a major refactoring
in the 2.x release.

* PG-481 Release notes 1.1.0 (#294)

modified:   RELEASE_NOTES.md

* PG-500: Bump the version of pg_stat_monitor to 1.1.0 (#297)

* PG-501: Missing Buckets and incorrect calls count. (#298)

prev_bucket_sec holds the actual time at which the previous bucket was created
and it is used to compute if the previous bucket time has elapsed and when is
the time to create a new one. But since the bucket start time is rounded down
to logical time window start, that makes the prev_bucket_sec and bucket start
time out of sync with each other, and depending on the query arrival time there
is a high probability that a bucket gets missed especially when the last bucket
was created around the end of the bucket time window.

Solution is to keep the prev_bucket_sec and bucket start time in-sync.

Moreover, we are using the unint64 for storing the prev_bucket_sec which is kind
of an overkill and a simple uint should be good enough for the purpose. But that
change can be taken up as part of the create-bucket function refactoring task.

* PG-501: Missing Buckets and incorrect calls count.

Ensuring the outer bound for the bucket is an exclusive boundary and it
as it belongs to the next bucket. To explain the point further, a set of
five second bucket would be:
    Bucket 1: 00:00:00.00 -> 00:00:04.99...
    Bucket 2: 00:00:00.05 -> 00:00:09.99...
    Bucket 3: 00:00:00.10 -> 00:00:14.99...
    ...

Co-authored-by: Ibrar Ahmed <ibrar.ahmed@percona.com>
Co-authored-by: Anastasia Alexandrova <anastasia.alexandrova@percona.com>
Co-authored-by: Muhammad Usama <m.usama@gmail.com>
2022-09-13 15:59:39 +05:00
Hamid Akhtar a3ad4033e0 PG-476: pg_stat_monitor causing errors : could not read file /tmp/pg_stat_monitor_query: No such file or directory
This only fixes the basic the path issue. It moves the query overflow file
from /tmp to PGSTAT folder data directory.
2022-08-04 13:47:36 +05:00
Ibrar Ahmed a9187117f9
PG-456: Running pgindent to make source PostgreSQL compatible. (#269)
PG-456: Running pgindent to make source indentation/spacing PostgreSQLCompatible.

PostgreSQL uses pgindent from time to time to make source code PostgreSQL
style guide compatible, it is a very long time since we have not done that.
Commit fixes a lot of indentation and spacing issues.

Co-authored-by: Hamid Akhtar <hamid.akhtar@gmail.com>
2022-06-29 00:42:40 +05:00