Re: [HACKERS] New SQL counter statistics view (pg_stat_sql) - Mailing list pgsql-hackers

From Haribabu Kommi
Subject Re: [HACKERS] New SQL counter statistics view (pg_stat_sql)
Date
Msg-id CAJrrPGc+YVymxYRZCUDK8jS4a+=fW0AkQPKkauV58tvm7rk4=A@mail.gmail.com
Whole thread Raw
In response to Re: New SQL counter statistics view (pg_stat_sql)  (Haribabu Kommi <kommi.haribabu@gmail.com>)
List pgsql-hackers


On Thu, Apr 6, 2017 at 5:17 AM, Andres Freund <andres@anarazel.de> wrote:
Hi,


I'm somewhat inclined to think that this really would be better done in
an extension like pg_stat_statements.

Thanks for the review.

 

> +}
> +
> +/*
> + * Count SQL statement for pg_stat_sql view
> + */
> +void
> +pgstat_count_sqlstmt(const char *commandTag)
> +{
> +     PgStat_SqlstmtEntry *htabent;
> +     bool            found;
> +
> +     if (!pgstat_backend_sql)
> +     {
> +             /* First time through - initialize SQL statement stat table */
> +             HASHCTL         hash_ctl;
> +
> +             memset(&hash_ctl, 0, sizeof(hash_ctl));
> +             hash_ctl.keysize = NAMEDATALEN;
> +             hash_ctl.entrysize = sizeof(PgStat_SqlstmtEntry);
> +             pgstat_backend_sql = hash_create("SQL statement stat entries",
> +                                                                              PGSTAT_SQLSTMT_HASH_SIZE,
> +                                                                              &hash_ctl,
> +                                                                              HASH_ELEM | HASH_BLOBS);
> +     }
> +
> +     /* Get the stats entry for this SQL statement, create if necessary */
> +     htabent = hash_search(pgstat_backend_sql, commandTag,
> +                                               HASH_ENTER, &found);
> +     if (!found)
> +             htabent->count = 1;
> +     else
> +             htabent->count++;
> +}


That's not really something in this patch, but a lot of this would be
better if we didn't internally have command tags as strings, but as an
enum.  We should really only convert to a string with needed.  That
we're doing string comparisons on Portal->commandTag is just plain bad.

If so, we could also make this whole patch a lot cheaper - have a fixed
size array that has an entry for every possible tag (possibly even in
shared memory, and then use atomics there).

During the development, I thought of using an array of all command tags
and update the counters using the tag name, but not like the enum values.
Now I have to create a mapping array with enums and tag names for easier
counter updates. The problem in this approach is, whenever any addition
of new commands, this mapping array needs to be updated with both
new enum and new tag name, whereas with hash table approach, it works
future command additions also, but there is some performance overhead
compared to the array approach.

I will modify the patch to use the array approach and provide it to the next
commitfest.

 
> +++ b/src/backend/tcop/postgres.c
> @@ -1109,6 +1109,12 @@ exec_simple_query(const char *query_string)
>
>               PortalDrop(portal, false);
>
> +             /*
> +              * Count SQL statement for pg_stat_sql view
> +              */
> +             if (pgstat_track_sql)
> +                     pgstat_count_sqlstmt(commandTag);
> +

Isn't doing this at the message level quite wrong?  What about
statements executed from functions and such?  Shouldn't this integrate
at the executor level instead?

Currently the patch is designed to find out only the user executed statements
that are successfully finished, (no internal statements that are executed from
functions and etc).

The same question was asked by dilip also in earlier mails, may be now it is
the time that we can decide the approach of statement counts.


Regards,
Hari Babu
Fujitsu Australia

pgsql-hackers by date:

Previous
From: Masahiko Sawada
Date:
Subject: Re: [HACKERS] Interval for launching the table sync worker
Next
From: Noah Misch
Date:
Subject: Re: [HACKERS] SCRAM authentication, take three