Commit Graph

448 Commits (fdd984042ee07632e3dfe6f646b8015764a14d52)

Author SHA1 Message Date
Diego Fronza 78c97088cf PG-299: Fix conflicts between devel and master.
Updated sql files (pg_stat_monitor_settings view).

Using right variable name and level checking on pgss_store:
key.toplevel = ((exec_nested_level + plan_nested_level) == 0);
2022-01-03 11:01:01 -03:00
Ibrar Ahmed d3fe5edc80 git push origin develMerge branch 'darkfronza-devel' into devel 2021-12-30 19:33:39 +00:00
Diego Fronza eb4087be4e PG-291: Fix query call count.
The issue with wrong query call count was taking place during transition
to a new bucket, the process is shortly describe bellow:

1. Scan for pending queries in previous bucket.
2. Add pending queries to the new bucket id.
3. Remove pending queries from previous bucket id.

The problem is that when switching to a new bucket, we reset query
statistics for a given entry being processed, so, for example, if the
pending query had a call count of 10 (9 of which were finished, 10th is
the pending one), if we move this query to the new bucket, the entry
will have its stats reseted, clearing the query call count to zero.

To solve the problem, whenever a pending query is detected, if the entry
has a call count > 1, we mark it as finished, and don't remove it from
the previous bucket in order to keep its statistics, then we move just
the pending query (10th in the example) to the new bucket id.

Another issue is that when moving a entry to a new bucket, we missed
copying the query position from the previous entry, which is used to
locate the query text in the query buffer:

hash_entry_dealloc():291
new_entry->query_pos = old_entry->query_pos;
2021-12-30 09:49:32 -03:00
Diego Fronza 57839c7664 PG-295: Fix top_query regression test.
The issue is that between changing GUC "track" from track='top' to
track='all' the queries are executing using previous state of
track='top', to fix that we sleep 1 second after calling
pg_reload_conf() to ensure that queries will run with new settings.
2021-12-30 09:49:32 -03:00
Diego Fronza fd1691626c PG-293: Disable pgsm_track_planning.
This GUC must be disabled by default, it incurss a small performance
penalty in the PostgreSQL TPS, users can enable it at anytime if they
wish to.
2021-12-30 09:49:32 -03:00
Diego Fronza a702f24465 PG-293: Update regression tests (extract_comments).
guc: Add the new GUC variable to the output.

tags: Handle both cases, enable/disable extracting query comments.
2021-12-30 09:49:32 -03:00
Diego Fronza 6042795930 PG-293: Add pg_stat_monitor.extract_comments GUC.
This new GUC allows the user to enable/disable extracting query
comments.
2021-12-30 09:49:32 -03:00
Diego Fronza 30a328f381 PG-293: Update regression tests.
cmd_type: Added missing DROP TABLE t2;

guc: Adjusted to match the updated settings view, which now display
     boolean values as 'yes' and 'no', also added the 'options' column
     to the output.

guc_1: Handle PostgreSQL versions <= 12 which don't have the
           track_planning feature.

rows.out: Added missing DROP TABLE t2. Also removed the line 'ERROR:
          relation "t2" already exists' since we fixed the problem in
	  cmd_type regression.

top_query: Handling both track = 'top' and track = 'all' cases.

top_query_1: On PostgreSQL >= 14 the sub query from the procedure is
             stored as (select $1 + $2), whereas on PG <= 13 it is
	     stored as SELECT (select $1 + $2).
2021-12-30 09:49:30 -03:00
Diego Fronza b6838049b6 PG-293: Add pg_stat_monitor.track GUC.
This new GUC allows users to select which statements are tracked by
pg_stat_monitor:
   - 'top': Default, track only top level queries.
   - 'all': Track top along with sub/nested queries.
   - 'none': Disable query monitoring.

To avoid redudancy, now users disable pg_stat_monitor by setting
pg_stat_monitor.track = 'none', similar to pg_stat_statements.

This new GUC is an enumeration, so the pg_stat_monitor_settings view was
adjusted to add a new column 'options' which lists the possible values
for the field.

The "value" and "default_value" columns in the pg_stat_monitor_settings
view was adjusted to be of type text, so we can better display the
enumeration values. Also the boolean types now have their values
displayed as either 'yes' or 'no' to easily distinguish them from the
integer types.
2021-12-30 09:48:27 -03:00
Diego Fronza 6f7f44b744 PG-286: Fix deadlock.
Can't call elog() function from inside the pgsm_log as the pgss_hash
lock could be already acquired in exclusive mode, since elog() triggers
the psmg_emit_log hook, when it calls pgss_store it will try to acquire
the pgss_hash lock again, leading to a deadlock.
2021-12-30 09:48:27 -03:00
Diego Fronza b702145ac3 PG-286: Update regression tests.
As the query normalization and query cleaning is always done in the
right place (pgss_store), no more parsed queries have a trailling comma
';' at the end.

Also, on error regression test, after fixing some problems with utility
related queries, we now have two entries for the RAISE WARNING case, the
first entry is the utility query itself, the second entry is the error
message logged by emit_log_hook.

Some queries have the order adjusted due to the fix introduced by the
previous commits.
2021-12-30 09:48:27 -03:00
Diego Fronza 007445a0d5 PG-286: Several improvements.
This commit introduces serveral improvements:

1. Removal of pgss_store_query and pgss_store_utility functions: To
   store a query, we just use pgss_store(), this makes the code more
   uniform.

2. Always pass the query length to the pgss_store function using parse
   state from PostgreSQL to avoid calculating query length again.

3. Always clean the query (extra spaces, update query location) in
   pgss_store.

4. Normalize queries right before adding them to the query buffer, but
   only if user asked for query normalization.

5. Correctly handle utility queries among different PostgreSQL versions:
   - A word about how utility functions are handled on PG 13 and later
     versions:
      - On PostgreSQL <= 13, we have to compute a query ID, on later
        versions we can call EnableQueryId() to inform Postmaster we
	want to enable query ID computation.

      - On PostgreSQL <= 13, post_parse hook is called after process
        utility hook, on PostgreSQL >= 14, post_parse hook is called
        before process utility functions.

   - Based on that information, on PostgreSQL <= 13 / process utility,
     we pass 0 as queryid to the pgss_store function, then we calculate a
     queryid after cleaning the query (CleanQueryText) using
     pgss_hash_string.

   - On PostgreSQL 14 onward, post_parse() is called before
     pgss_ProcessUtility, we Clear queryId for prepared statements
     related utility, on process utility hook, we save the query ID for
     passing it to the pgss_store function, but mark the query ID with
     zero to avoid instrumenting it again on executor hooks.
2021-12-30 09:48:26 -03:00
Diego Fronza a071516a0f PG-286: Check for NULL return on hash_search before using object.
Check if hash_search() function returns NULL before attempting to
use the object in hash_entry_alloc().
2021-12-30 09:47:06 -03:00
Diego Fronza 59c321ebc5 PG-286: Reduce calls to pgstat_fetch_stat_numbackends().
After couple CPU profiling sessions with perf, it was detected that the
function pgstat_fetch_stat_numbackends() is very expensive, reading the
implementation on PostgreSQL's backend_status.c just confirmed that.

We use that function on pg_stat_monitor to retrieve the application name
and IP address of the client, we now cache the results in order to avoid
calling it for every query being processed.
2021-12-30 09:47:06 -03:00
Diego Fronza d32dea0daa PG-286: Fix query buffer overflow management.
If pgsm_overflow_target is ON (default, 1) and the query buffer
overflows, we now dump the buffer and keep track of how many times
pg_stat_monitor changed bucket since that.

If an overflow happen again before pg_stat_monitor cycle through
pgsm_max_buckets buckets (default 10), then we don't dump the buffer
again, but instead report an error, this ensures that only one dump file
of size pgsm_query_shared_buffer will be in disk at any time, avoiding
slowing down queries to the pg_stat_monitor view.

As soon as pg_stat_monitor cycles through all buckets, we remove the
dump file and reset the counter (pgss->n_bucket_cycles).
2021-12-30 09:47:06 -03:00
Diego Fronza 1b51defc68 PG-286: Small performance improvements.
pgss_ExecutorEnd: Avoid unnecessary memset(plan_info, 0, ...).
We only use this object if the condition below is true, in which case we
already initialize all the fields in the object, also we now store the
plan string length (plan_info.plan_len) to avoid calling strlen on it
again later:
if (queryDesc->operation == CMD_SELECT && PGSM_QUERY_PLAN) {
... here we initialize plan_info

If the condition is false, then we pass a NULL PlanInfo* to the
pgss_store to avoid more unnecessary processing.

pgss_planner_hook: Similar, avoid memset(plan_info, 0, ...) this object
is not used here, so we pass NULL to pgss_store.

pg_get_application_name: Remove call to strlen, snprintf already give us
the calculated string length, so we just return it.

pg_get_client_addr: Cache localhost, avoid calling
ntohl(inet_addr("127.0.0.1")) all the time.

pgss_update_entry: Make use of PlanInfo->plan_len, avoiding a call to
strlen again.

intarray_get_datum: Init the string by setting the first byte to '\0'.
2021-12-30 09:47:06 -03:00
Diego Fronza ad1187b9da PG-286: Avoid duplicate queries in text buffer.
The memory area reserved for query text (pgsm_query_shared_buffer) was
divided evenly for each bucket, this allowed to have the same query,
e.g. "SELECT 1", duplicated in different buckets, thus wasting space.

This commit fix the query text duplication by adding a new hash table
whose only purpose is to verify if a given query is already added to the
buffer (by using the queryID).

This allows different buckets that share the same query to point to a
unique entry in the query buffer (pgss_qbuf).

When pg_stat_monitor moves to a new bucket id, by avoiding adding a
query that already exists in the buffer it can also save some CPU time.
2021-12-30 09:47:04 -03:00
Diego Fronza 8ea02b0f2a PG-228: Fix hash table creation flags on PG <= 13.
Before PostgreSQL 14, HASH_STRINGS flag was not available when creating
a hash table with ShmemInitHash().

Use HASH_BLOBS for previous PostgreSQL versions.
2021-12-30 09:46:15 -03:00
Diego Fronza 6f353a5596 PG-228: Add severity to the internal message logging API.
Add support to include the severity of messages added to the
pg_stat_monitor_errors view.
2021-12-30 09:46:15 -03:00
Diego Fronza c37713b9d5 PG-228: Add new view, pg_stat_monitor_errors.
The pg_stat_monitor_errors view was created in order to help inspecting
internal errors that may occur in pg_stat_monitor module itself, it
contains the error message, its severity, the last time the error occurred
and the number of times that any given error ocurred.

Implementation details:
 - A new lwlock was added to the pgssSharedState structure in order to
   control access to the errors hash table.
 - Increased RequestNamedLWLockTranche() no. of locks requested to 2.
 - The function GetNamedLWLockTranche() returns an array of locks, we
   use the first lock for controlling access to the usual buckets hash
   table, and the second one to control access to the errors hash table.
 - During module initialization (_PG_init) we increased the amount of
   shared memory space requested to include space to the new errors hash
   table: RequestAddinShmemSpace(... + pgsm_errors_size())
 - The implementation in pgsm_errors.c simple uses a hash table to track
   error messages, the message is also used as the key.
2021-12-30 09:46:12 -03:00
Ibrar Ahmed 10f305b4b1
Merge pull request #156 from umairshahid/patch-1
Update README.md
2021-12-28 20:53:19 +05:00
Umair Shahid 150df586ee
Update README.md
Proposed changes for https://jira.percona.com/browse/PG-155
2021-12-28 17:58:36 +04:00
Ibrar Ahmed 91073aad78
Merge pull request #150 from darkfronza/PG-290_fix_crash_higher_debug_level_master
PG-290: Fix crash when enabling debugging log level on PostgreSQL.
2021-12-07 15:17:39 -05:00
Vadim Yalovets 9b0b5c398f DISTPG-349 fix conflicts 2021-12-07 10:49:18 +02:00
Vadim Yalovets b7bcf60b2d
Merge branch 'percona:master' into master 2021-12-07 10:11:28 +02:00
Diego Fronza 3433c77d9d PG-290: Fix crash when enabling debugging log level on PostgreSQL.
There were couple issues to handle, the main one was that our log hook
(pgsm_emit_log_hook) was being called after the shared memory hook
completed (pgss_shmem_startup) but before PostgreSQL boostraping code
finished, thus triggering the following assertion during a call to
LWLockAcquire():
Assert(!(proc == NULL && IsUnderPostmaster));

proc is a pointer to MyProc, a PostgreSQL's shared global variable that
was not yet initalized by PostgreSQL.

We must also check for a NULL pointer return in pg_get_backend_status()
the pgstat_fetch_stat_local_beentry() function may return a NULL pointer
during initialization, in which case we use "127.0.0.1" for the client
address, and "postmaster" for application name.
2021-12-06 15:39:34 -03:00
EvgeniyPatlan fb7220cdf1
Merge pull request #149 from percona/fix_packaging
Fix rpm packaging
2021-12-06 16:34:15 +02:00
Evgeniy Patlan 5d526fbbd4 Fix rpm packaging 2021-12-06 14:43:51 +02:00
Vadim Yalovets 752eb71ce6 DISTPG-349 restored spec file 2021-12-06 12:33:06 +02:00
Vadim Yalovets 61c9a621ae DISTPG-349 modify build script 2021-12-06 11:55:21 +02:00
Vadim Yalovets 0ead47c868 DISTPG-349 modify build script 2021-12-06 10:06:55 +02:00
Vadim Yalovets 95bee8648d DISTPG-349 modify control.in 2021-12-03 10:45:38 +02:00
Vadim Yalovets c9da6ddc5c DISTPG-349 modify control.in 2021-12-02 19:02:41 +02:00
Vadim Yalovets 3e7445efc0 DISTPG-349 Change Copyright 2021-12-02 12:54:08 +02:00
Vadim Yalovets d034b9f668 DISTPG-349 Modify Description 2021-12-01 21:43:41 +02:00
Vadim Yalovets e009f13fed DISTPG-349 modified build script 2021-12-01 21:30:14 +02:00
Vadim Yalovets 71d36a1578 DISTPG-349 modified build script 2021-12-01 19:22:29 +02:00
Vadim Yalovets f36b9c3e92 DISTPG-349 modified build script 2021-12-01 12:12:39 +02:00
Vadim Yalovets 2507619134 DISTPG-349 modified build script 2021-11-30 21:43:54 +02:00
Vadim Yalovets 851da52750 DISTPG-349 modified build script 2021-11-30 21:39:52 +02:00
Vadim Yalovets d293be2ac4 DISTPG-349 modified build script 2021-11-30 21:04:43 +02:00
Vadim Yalovets 74fcbbca5b DISTPG-349 modified build script 2021-11-30 15:13:04 +02:00
Vadim Yalovets 09aa10d140 DISTPG-353 PG Debian Requirement: the Description is overflowing 2021-11-30 14:38:45 +02:00
Vadim Yalovets d5c9c94d4f DISTPG-349 PG Debian Requirement: Remove dependency on Percona PostgreSQL 2021-11-30 14:10:37 +02:00
Vadim Yalovets 5b26a92644 DISTPG-348-pg_stat_monitor-Update-directory-structure-for-Debian-packaging-files 2021-11-30 14:03:13 +02:00
Oleksandr Miroshnychenko d05f6d8e3b
Merge pull request #143 from vorsel/1.0.0-2
DISTPG-7 update debian rules for 1.0.0-rc.1 version
2021-11-29 10:51:54 +02:00
Oleksandr Miroshnychenko 3b155bd643 DISTPG-7 fir debian rules for 1.0.0-rc.1 version 2021-11-26 16:47:00 +02:00
Ibrar Ahmed 8fe7676923 PG-284: Bump version to 1.0.0-rc.1. 2021-11-24 18:55:13 +00:00
Ibrar Ahmed 269c8bc62c
Merge pull request #142 from nastena1606/PG-285-Doc-order-of-extensions
PG-285 Doc: Order of modules
2021-11-24 16:52:31 +05:00
Anastasia Alexadrova f7275071cd PG-285 Doc: Order of modules
Added a note about strict order of modules for PG 13 and earlier versions

modified:   README.md
2021-11-24 12:47:06 +02:00