Logging slow queries on Google Cloud SQL PostgreSQL instancesHow to log PostgreSQL queries?Improve INSERT-per-second performance of SQLite?Poor Google Cloud SQL performanceGoogle Cloud SQL monitoringGoogle Cloud SQL CPU MonitoringGoogle Cloud: MySQL queries very slowSetup VPN for database instance on Google Cloud SQLGoogle Cloud SQL Postgres - randomly slow queries from Google Compute / KubernetesHow do I terminate a session in Google Cloud SQL for PostgreSQL?How to increase the connection limit for the Google Cloud SQL Postgres database?

How do you respond to a colleague from another team when they're wrongly expecting that you'll help them?

Should I stop contributing to retirement accounts?

What does routing an IP address mean?

"Spoil" vs "Ruin"

What was the exact wording from Ivanhoe of this advice on how to free yourself from slavery?

How do I color the graph in datavisualization?

Not using 's' for he/she/it

Yosemite Fire Rings - What to Expect?

On a tidally locked planet, would time be quantized?

Offered money to buy a house, seller is asking for more to cover gap between their listing and mortgage owed

Multiplicative persistence

When were female captains banned from Starfleet?

Problem with TransformedDistribution

It grows, but water kills it

How should I respond when I lied about my education and the company finds out through background check?

How can Trident be so inexpensive? Will it orbit Triton or just do a (slow) flyby?

Why does the Sun have different day lengths, but not the gas giants?

A social experiment. What is the worst that can happen?

Freedom of speech and where it applies

What are the purposes of autoencoders?

Is there a name for this algorithm to calculate the concentration of a mixture of two solutions containing the same solute?

The screen of my macbook suddenly broken down how can I do to recover

Non-trope happy ending?

Electoral considerations aside, what are potential benefits, for the US, of policy changes proposed by the tweet recognizing Golan annexation?



Logging slow queries on Google Cloud SQL PostgreSQL instances


How to log PostgreSQL queries?Improve INSERT-per-second performance of SQLite?Poor Google Cloud SQL performanceGoogle Cloud SQL monitoringGoogle Cloud SQL CPU MonitoringGoogle Cloud: MySQL queries very slowSetup VPN for database instance on Google Cloud SQLGoogle Cloud SQL Postgres - randomly slow queries from Google Compute / KubernetesHow do I terminate a session in Google Cloud SQL for PostgreSQL?How to increase the connection limit for the Google Cloud SQL Postgres database?













7















The company I work for uses Google Cloud SQL to manage their SQL databases in production.



We're having performance issues and I thought it'd be a good idea (among other things) to see/monitor all queries above a specific threshold (e.g. 250ms).



By looking at the PostgreSQL documentation I think log_min_duration_statement seems like the flag I need.




log_min_duration_statement (integer)



Causes the duration of each completed statement to be logged if the statement ran for at least the specified number of milliseconds. Setting this to zero prints all statement durations.




But judging from the Cloud SQL documentation I see that is only possible to set a narrow set of database flags (as in for each DB instance) but as you can see from here log_min_duration_statement is not among those supported flags.



So here comes the question. How do I log/monitor my slow PostgreSQL queries with Google Cloud SQL? If not possible then what kind of tool/methodologies do you suggest I use to achieve a similar result?










share|improve this question




























    7















    The company I work for uses Google Cloud SQL to manage their SQL databases in production.



    We're having performance issues and I thought it'd be a good idea (among other things) to see/monitor all queries above a specific threshold (e.g. 250ms).



    By looking at the PostgreSQL documentation I think log_min_duration_statement seems like the flag I need.




    log_min_duration_statement (integer)



    Causes the duration of each completed statement to be logged if the statement ran for at least the specified number of milliseconds. Setting this to zero prints all statement durations.




    But judging from the Cloud SQL documentation I see that is only possible to set a narrow set of database flags (as in for each DB instance) but as you can see from here log_min_duration_statement is not among those supported flags.



    So here comes the question. How do I log/monitor my slow PostgreSQL queries with Google Cloud SQL? If not possible then what kind of tool/methodologies do you suggest I use to achieve a similar result?










    share|improve this question


























      7












      7








      7


      2






      The company I work for uses Google Cloud SQL to manage their SQL databases in production.



      We're having performance issues and I thought it'd be a good idea (among other things) to see/monitor all queries above a specific threshold (e.g. 250ms).



      By looking at the PostgreSQL documentation I think log_min_duration_statement seems like the flag I need.




      log_min_duration_statement (integer)



      Causes the duration of each completed statement to be logged if the statement ran for at least the specified number of milliseconds. Setting this to zero prints all statement durations.




      But judging from the Cloud SQL documentation I see that is only possible to set a narrow set of database flags (as in for each DB instance) but as you can see from here log_min_duration_statement is not among those supported flags.



      So here comes the question. How do I log/monitor my slow PostgreSQL queries with Google Cloud SQL? If not possible then what kind of tool/methodologies do you suggest I use to achieve a similar result?










      share|improve this question
















      The company I work for uses Google Cloud SQL to manage their SQL databases in production.



      We're having performance issues and I thought it'd be a good idea (among other things) to see/monitor all queries above a specific threshold (e.g. 250ms).



      By looking at the PostgreSQL documentation I think log_min_duration_statement seems like the flag I need.




      log_min_duration_statement (integer)



      Causes the duration of each completed statement to be logged if the statement ran for at least the specified number of milliseconds. Setting this to zero prints all statement durations.




      But judging from the Cloud SQL documentation I see that is only possible to set a narrow set of database flags (as in for each DB instance) but as you can see from here log_min_duration_statement is not among those supported flags.



      So here comes the question. How do I log/monitor my slow PostgreSQL queries with Google Cloud SQL? If not possible then what kind of tool/methodologies do you suggest I use to achieve a similar result?







      postgresql performance monitoring google-cloud-sql






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 3 '18 at 10:08









      Rodrigo C.

      767212




      767212










      asked May 2 '18 at 15:30









      Francesco CasulaFrancesco Casula

      15.2k792111




      15.2k792111






















          3 Answers
          3






          active

          oldest

          votes


















          8














          The possibility of monitoring slow PostgreSQL queries for Cloud SQL instances is currently not available. As you comment, the log_min_duration_statement flag is currently not supported by Cloud SQL.



          Right now, work is being made on adding this feature to Cloud SQL, and you can keep track on the progress made through this link. You can click on the star icon on the top left corner to get email notifications whenever any significant progress has been achieved.






          share|improve this answer




















          • 1





            Wow. Such a basic feature and it's not available.

            – mattes
            Sep 7 '18 at 21:05


















          2














          Not ideal by any measure, but what we do is run something like this on a cron once a minute and log out the result:



          SELECT EXTRACT(EPOCH FROM now() - query_start) AS seconds, query
          FROM pg_stat_activity
          WHERE state = 'active' AND now() - query_start > interval '1 seconds' AND query NOT LIKE '%pg_stat_activity%'
          ORDER BY seconds DESC LIMIT 20


          You'd need to fiddle with the query to get millisecond granularity, and even then it'll only catch queries that overlap with your cron frequency, but probably better than nothing?






          share|improve this answer






























            0














            There is a way to log slow queries through the pg_stat_statements extension which is supported by Cloud SQL.



            Since Cloud SQL doesn't grant superuser right to any of the users you need to use some workaround.
            First, you need to enable the extension with



            CREATE EXTENSION IF NOT EXISTS pg_stat_statements;


            then you can check slow queries with a query like



            SELECT pd.datname,
            us.usename,
            pss.userid,
            pss.query AS SQLQuery,
            pss.rows AS TotalRowCount,
            (pss.total_time / 1000) AS TotalSecond,
            ((pss.total_time / 1000) / calls) as TotalAverageSecond
            FROM pg_stat_statements AS pss
            INNER JOIN pg_database AS pd
            ON pss.dbid = pd.oid
            INNER JOIN pg_user AS us
            ON pss.userid = us.usesysid
            ORDER BY TotalAverageSecond DESC
            LIMIT 10;


            As postgres user you can have a look on all slow queries, but since the user is not superuser you will see <insufficient privilege> on all other users' queries.
            To get around this limitation you can install the extension on other databases too (normally only postgres user has rigths to install extensions) and you can check the query texts with the owner of the db.






            share|improve this answer






















              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50138337%2flogging-slow-queries-on-google-cloud-sql-postgresql-instances%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              8














              The possibility of monitoring slow PostgreSQL queries for Cloud SQL instances is currently not available. As you comment, the log_min_duration_statement flag is currently not supported by Cloud SQL.



              Right now, work is being made on adding this feature to Cloud SQL, and you can keep track on the progress made through this link. You can click on the star icon on the top left corner to get email notifications whenever any significant progress has been achieved.






              share|improve this answer




















              • 1





                Wow. Such a basic feature and it's not available.

                – mattes
                Sep 7 '18 at 21:05















              8














              The possibility of monitoring slow PostgreSQL queries for Cloud SQL instances is currently not available. As you comment, the log_min_duration_statement flag is currently not supported by Cloud SQL.



              Right now, work is being made on adding this feature to Cloud SQL, and you can keep track on the progress made through this link. You can click on the star icon on the top left corner to get email notifications whenever any significant progress has been achieved.






              share|improve this answer




















              • 1





                Wow. Such a basic feature and it's not available.

                – mattes
                Sep 7 '18 at 21:05













              8












              8








              8







              The possibility of monitoring slow PostgreSQL queries for Cloud SQL instances is currently not available. As you comment, the log_min_duration_statement flag is currently not supported by Cloud SQL.



              Right now, work is being made on adding this feature to Cloud SQL, and you can keep track on the progress made through this link. You can click on the star icon on the top left corner to get email notifications whenever any significant progress has been achieved.






              share|improve this answer















              The possibility of monitoring slow PostgreSQL queries for Cloud SQL instances is currently not available. As you comment, the log_min_duration_statement flag is currently not supported by Cloud SQL.



              Right now, work is being made on adding this feature to Cloud SQL, and you can keep track on the progress made through this link. You can click on the star icon on the top left corner to get email notifications whenever any significant progress has been achieved.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited May 3 '18 at 10:10

























              answered May 3 '18 at 9:55









              Rodrigo C.Rodrigo C.

              767212




              767212







              • 1





                Wow. Such a basic feature and it's not available.

                – mattes
                Sep 7 '18 at 21:05












              • 1





                Wow. Such a basic feature and it's not available.

                – mattes
                Sep 7 '18 at 21:05







              1




              1





              Wow. Such a basic feature and it's not available.

              – mattes
              Sep 7 '18 at 21:05





              Wow. Such a basic feature and it's not available.

              – mattes
              Sep 7 '18 at 21:05













              2














              Not ideal by any measure, but what we do is run something like this on a cron once a minute and log out the result:



              SELECT EXTRACT(EPOCH FROM now() - query_start) AS seconds, query
              FROM pg_stat_activity
              WHERE state = 'active' AND now() - query_start > interval '1 seconds' AND query NOT LIKE '%pg_stat_activity%'
              ORDER BY seconds DESC LIMIT 20


              You'd need to fiddle with the query to get millisecond granularity, and even then it'll only catch queries that overlap with your cron frequency, but probably better than nothing?






              share|improve this answer



























                2














                Not ideal by any measure, but what we do is run something like this on a cron once a minute and log out the result:



                SELECT EXTRACT(EPOCH FROM now() - query_start) AS seconds, query
                FROM pg_stat_activity
                WHERE state = 'active' AND now() - query_start > interval '1 seconds' AND query NOT LIKE '%pg_stat_activity%'
                ORDER BY seconds DESC LIMIT 20


                You'd need to fiddle with the query to get millisecond granularity, and even then it'll only catch queries that overlap with your cron frequency, but probably better than nothing?






                share|improve this answer

























                  2












                  2








                  2







                  Not ideal by any measure, but what we do is run something like this on a cron once a minute and log out the result:



                  SELECT EXTRACT(EPOCH FROM now() - query_start) AS seconds, query
                  FROM pg_stat_activity
                  WHERE state = 'active' AND now() - query_start > interval '1 seconds' AND query NOT LIKE '%pg_stat_activity%'
                  ORDER BY seconds DESC LIMIT 20


                  You'd need to fiddle with the query to get millisecond granularity, and even then it'll only catch queries that overlap with your cron frequency, but probably better than nothing?






                  share|improve this answer













                  Not ideal by any measure, but what we do is run something like this on a cron once a minute and log out the result:



                  SELECT EXTRACT(EPOCH FROM now() - query_start) AS seconds, query
                  FROM pg_stat_activity
                  WHERE state = 'active' AND now() - query_start > interval '1 seconds' AND query NOT LIKE '%pg_stat_activity%'
                  ORDER BY seconds DESC LIMIT 20


                  You'd need to fiddle with the query to get millisecond granularity, and even then it'll only catch queries that overlap with your cron frequency, but probably better than nothing?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Aug 5 '18 at 14:42









                  depsypherdepsypher

                  753616




                  753616





















                      0














                      There is a way to log slow queries through the pg_stat_statements extension which is supported by Cloud SQL.



                      Since Cloud SQL doesn't grant superuser right to any of the users you need to use some workaround.
                      First, you need to enable the extension with



                      CREATE EXTENSION IF NOT EXISTS pg_stat_statements;


                      then you can check slow queries with a query like



                      SELECT pd.datname,
                      us.usename,
                      pss.userid,
                      pss.query AS SQLQuery,
                      pss.rows AS TotalRowCount,
                      (pss.total_time / 1000) AS TotalSecond,
                      ((pss.total_time / 1000) / calls) as TotalAverageSecond
                      FROM pg_stat_statements AS pss
                      INNER JOIN pg_database AS pd
                      ON pss.dbid = pd.oid
                      INNER JOIN pg_user AS us
                      ON pss.userid = us.usesysid
                      ORDER BY TotalAverageSecond DESC
                      LIMIT 10;


                      As postgres user you can have a look on all slow queries, but since the user is not superuser you will see <insufficient privilege> on all other users' queries.
                      To get around this limitation you can install the extension on other databases too (normally only postgres user has rigths to install extensions) and you can check the query texts with the owner of the db.






                      share|improve this answer



























                        0














                        There is a way to log slow queries through the pg_stat_statements extension which is supported by Cloud SQL.



                        Since Cloud SQL doesn't grant superuser right to any of the users you need to use some workaround.
                        First, you need to enable the extension with



                        CREATE EXTENSION IF NOT EXISTS pg_stat_statements;


                        then you can check slow queries with a query like



                        SELECT pd.datname,
                        us.usename,
                        pss.userid,
                        pss.query AS SQLQuery,
                        pss.rows AS TotalRowCount,
                        (pss.total_time / 1000) AS TotalSecond,
                        ((pss.total_time / 1000) / calls) as TotalAverageSecond
                        FROM pg_stat_statements AS pss
                        INNER JOIN pg_database AS pd
                        ON pss.dbid = pd.oid
                        INNER JOIN pg_user AS us
                        ON pss.userid = us.usesysid
                        ORDER BY TotalAverageSecond DESC
                        LIMIT 10;


                        As postgres user you can have a look on all slow queries, but since the user is not superuser you will see <insufficient privilege> on all other users' queries.
                        To get around this limitation you can install the extension on other databases too (normally only postgres user has rigths to install extensions) and you can check the query texts with the owner of the db.






                        share|improve this answer

























                          0












                          0








                          0







                          There is a way to log slow queries through the pg_stat_statements extension which is supported by Cloud SQL.



                          Since Cloud SQL doesn't grant superuser right to any of the users you need to use some workaround.
                          First, you need to enable the extension with



                          CREATE EXTENSION IF NOT EXISTS pg_stat_statements;


                          then you can check slow queries with a query like



                          SELECT pd.datname,
                          us.usename,
                          pss.userid,
                          pss.query AS SQLQuery,
                          pss.rows AS TotalRowCount,
                          (pss.total_time / 1000) AS TotalSecond,
                          ((pss.total_time / 1000) / calls) as TotalAverageSecond
                          FROM pg_stat_statements AS pss
                          INNER JOIN pg_database AS pd
                          ON pss.dbid = pd.oid
                          INNER JOIN pg_user AS us
                          ON pss.userid = us.usesysid
                          ORDER BY TotalAverageSecond DESC
                          LIMIT 10;


                          As postgres user you can have a look on all slow queries, but since the user is not superuser you will see <insufficient privilege> on all other users' queries.
                          To get around this limitation you can install the extension on other databases too (normally only postgres user has rigths to install extensions) and you can check the query texts with the owner of the db.






                          share|improve this answer













                          There is a way to log slow queries through the pg_stat_statements extension which is supported by Cloud SQL.



                          Since Cloud SQL doesn't grant superuser right to any of the users you need to use some workaround.
                          First, you need to enable the extension with



                          CREATE EXTENSION IF NOT EXISTS pg_stat_statements;


                          then you can check slow queries with a query like



                          SELECT pd.datname,
                          us.usename,
                          pss.userid,
                          pss.query AS SQLQuery,
                          pss.rows AS TotalRowCount,
                          (pss.total_time / 1000) AS TotalSecond,
                          ((pss.total_time / 1000) / calls) as TotalAverageSecond
                          FROM pg_stat_statements AS pss
                          INNER JOIN pg_database AS pd
                          ON pss.dbid = pd.oid
                          INNER JOIN pg_user AS us
                          ON pss.userid = us.usesysid
                          ORDER BY TotalAverageSecond DESC
                          LIMIT 10;


                          As postgres user you can have a look on all slow queries, but since the user is not superuser you will see <insufficient privilege> on all other users' queries.
                          To get around this limitation you can install the extension on other databases too (normally only postgres user has rigths to install extensions) and you can check the query texts with the owner of the db.







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 days ago









                          materemiasmateremias

                          71127




                          71127



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f50138337%2flogging-slow-queries-on-google-cloud-sql-postgresql-instances%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript