├── .checkov.yaml ├── .circleci └── config.yml ├── .editorconfig ├── .envrc ├── .envrc-python ├── .github ├── CODEOWNERS └── workflows │ ├── checkov.yaml │ ├── codeowners.yaml │ ├── fork-sync.yaml │ ├── fork-update-pr.yaml │ ├── grype.yaml │ ├── json.yaml │ ├── kics.yaml │ ├── markdown.yaml │ ├── semgrep-cloud.yaml │ ├── semgrep.yaml │ ├── shellcheck.yaml │ ├── sonarcloud.yaml │ ├── sqlfluff.yaml │ ├── trivy.yaml │ ├── validate.yaml │ ├── xml.yaml │ └── yaml.yaml ├── .mdl.rb ├── .mdlrc ├── .pre-commit-config.yaml ├── .sonarlint └── connectedMode.json ├── .travis.yml ├── LICENSE ├── Makefile ├── README.md ├── analytics ├── README.md ├── bigquery_ecommerce_conversion_rate.sql ├── bigquery_ecommerce_purchasers_on_return_visit.sql ├── bigquery_ecommerce_time_on_site.sql ├── bigquery_ecommerce_top_5_products.sql ├── bigquery_ml_classification_logistic_regression_create_model1.sql ├── bigquery_ml_classification_logistic_regression_create_model2.sql ├── bigquery_ml_classification_logistic_regression_evaluate_model1.sql ├── bigquery_ml_classification_logistic_regression_evaluate_model2.sql └── bigquery_ml_classification_logistic_regression_predict_with_model2.sql ├── aws_athena_cloudtrail_ddl.sql ├── bigquery_billing_commonly_charged_units_of_measure.sql ├── bigquery_billing_latest_100_charges.sql ├── bigquery_billing_products_most_frequently_used.sql ├── bigquery_billing_products_with_highest_aggregate_cost.sql ├── bigquery_billing_products_with_most_billing_records.sql ├── bigquery_info_biggest_public_tables_by_row_count.sql ├── bigquery_info_columns.sql ├── bigquery_info_columns_partitioned_clustered.sql ├── bigquery_info_datasets.sql ├── bigquery_info_tables.sql ├── mysql_databases_by_size.sql ├── mysql_host_summary.sql ├── mysql_indexes_unused.sql ├── mysql_info.sql ├── mysql_memory_by_host.sql ├── mysql_memory_by_user.sql ├── mysql_sessions.sql ├── mysql_statement_latency_by_host.sql ├── mysql_statement_latency_by_user.sql ├── mysql_tables.sql ├── mysql_tables_nonsystem.sql ├── mysql_user.sql ├── mysql_user_summary.sql ├── mysql_users.sql ├── mysql_users_pre56.sql ├── mysql_users_pre8.sql ├── mysql_views.sql ├── mysql_views_system.sql ├── oracle_checkpoints.sql ├── oracle_datafiles_checkpoint_change.sql ├── oracle_datafiles_checkpoint_time.sql ├── oracle_datafiles_lastchanged.sql ├── oracle_datafiles_unrecoverable_change.sql ├── oracle_rds_ddl_tablespace.sql ├── oracle_recent_queries.sql ├── oracle_show_dba_recyclebin.sql ├── oracle_show_sessions_using_temp_tablespace.sql ├── oracle_table_segments_for_schema.sql ├── oracle_table_shrink_candidates.sql ├── oracle_table_space.sql ├── oracle_table_space_for_schema.sql ├── oracle_tablespace_datafiles_sum_vs_max_autoextend.sql ├── oracle_tablespace_datafiles_sum_vs_max_autoextend_temp.sql ├── oracle_tablespace_space.sql ├── oracle_tablespace_space2.sql ├── oracle_tablespace_undo_space.sql ├── oracle_user_sessions.sql ├── postgres_active_query_count.sql ├── postgres_backends_per_database.sql ├── postgres_blocked_queries.sql ├── postgres_columns_null.sql ├── postgres_columns_useless.sql ├── postgres_databases_by_size.sql ├── postgres_databases_by_size_if_accessible.sql ├── postgres_dirs.sql ├── postgres_dirs_pre10.sql ├── postgres_dirs_pre11.9.sql ├── postgres_grant_select_all_tables.sql ├── postgres_idle_sessions.sql ├── postgres_idle_sessions_current_db_kill.sql ├── postgres_idle_sessions_current_db_kill_pre92.sql ├── postgres_idle_sessions_kill.sql ├── postgres_idle_sessions_pre92.sql ├── postgres_index_cardinality.sql ├── postgres_index_cardinality_with_schema_name.sql ├── postgres_indexes_cache_hit_ratio.sql ├── postgres_indexes_unused.sql ├── postgres_info.sql ├── postgres_info_pre10.sql ├── postgres_last_analyze.sql ├── postgres_last_analyze_pre94.sql ├── postgres_last_vacuum.sql ├── postgres_last_vacuum_analyze.sql ├── postgres_last_vacuum_analyze_pre94.sql ├── postgres_last_vacuum_pre91.sql ├── postgres_locks.sql ├── postgres_locks_blocked.sql ├── postgres_locks_blocked_application.sql ├── postgres_locks_query_age.sql ├── postgres_locks_query_age_pre92.sql ├── postgres_queries_slow.sql ├── postgres_queries_slow_pre92.sql ├── postgres_queries_slow_pre96.sql ├── postgres_query_cache_hit_ratio.sql ├── postgres_query_times.sql ├── postgres_query_times_pre13.sql ├── postgres_query_times_pre95.sql ├── postgres_recovery.sql ├── postgres_running_queries.sql ├── postgres_running_queries_pre92.sql ├── postgres_sequences_restart_all.sql ├── postgres_sessions.sql ├── postgres_sessions_pre10.sql ├── postgres_sessions_state_count.sql ├── postgres_settings.sql ├── postgres_settings_auth.sql ├── postgres_settings_autovacuum.sql ├── postgres_settings_client_connection.sql ├── postgres_settings_compatibility.sql ├── postgres_settings_connections.sql ├── postgres_settings_developer.sql ├── postgres_settings_error_handling.sql ├── postgres_settings_files.sql ├── postgres_settings_locking.sql ├── postgres_settings_logging.sql ├── postgres_settings_memory.sql ├── postgres_settings_misc.sql ├── postgres_settings_preset.sql ├── postgres_settings_query_planning.sql ├── postgres_settings_replication.sql ├── postgres_settings_resources.sql ├── postgres_settings_ssl.sql ├── postgres_settings_statistics.sql ├── postgres_settings_wal.sql ├── postgres_tables_by_size.sql ├── postgres_tables_by_size_2.sql ├── postgres_tables_cache_hit_ratio.sql ├── postgres_tables_index_usage.sql ├── postgres_tables_row_estimates.sql ├── setup ├── bootstrap.sh └── ci_bootstrap.sh ├── sonar-project.properties └── test.sh /.checkov.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2022-02-21 16:53:29 +0000 (Mon, 21 Feb 2022) 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # License: see accompanying Hari Sekhon LICENSE file 10 | # 11 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | # ============================================================================ # 17 | # C h e c k o v c o n f i g 18 | # ============================================================================ # 19 | 20 | # https://github.com/bridgecrewio/checkov#configuration-using-a-config-file 21 | # 22 | # This is not well documented but the fields seem to be the same as: 23 | # 24 | # checkov --help 25 | # 26 | # See master template at: 27 | # 28 | # https://github.com/HariSekhon/Templates/blob/master/.checkov.yaml 29 | 30 | --- 31 | compact: true 32 | directory: 33 | - . 34 | skip-path: 35 | - bash-tools 36 | download-external-modules: true # without this gets lots of annoying warning lines such as '2022-02-22 16:14:40,180 [MainThread ] [WARNI] Failed to download module x/y/z:n.n.n' 37 | framework: 38 | - all 39 | no-guide: true 40 | output: cli 41 | quiet: true 42 | repo-id: HariSekhon/SQL-scripts # what to report to Bridgecrew Cloud - without this gets annoying duplicate repos such as 'harisekhon_cli_repo/sql' 43 | skip-suppressions: true 44 | soft-fail: true 45 | -------------------------------------------------------------------------------- /.circleci/config.yml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2020-02-23 23:30:14 +0000 (Sun, 23 Feb 2020) 4 | # Original: H1 2016 (Circle CI 1.x) 5 | # 6 | # vim:ts=2:sts=2:sw=2:et 7 | # 8 | # https://github.com/HariSekhon/SQL-scripts 9 | # 10 | # License: see accompanying Hari Sekhon LICENSE file 11 | # 12 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 13 | # 14 | # https://www.linkedin.com/in/HariSekhon 15 | # 16 | 17 | # https://circleci.com/docs/2.0/configuration-reference 18 | 19 | version: 2.1 20 | jobs: 21 | build: 22 | # technically a docker image is a better choice than machine 23 | # but we want to introduce some native environment variation 24 | # between build systems in order to test the repo's build automation is robust 25 | machine: 26 | image: default 27 | #image: ubuntu-1604:201903-01 28 | # set to an actual docker image when running locally using circle_ci_job.sh 29 | # docker image must have git installed to do the checkout 30 | # so using harisekhon/dev:ubuntu instead of base ubuntu image 31 | #image: harisekhon/dev:ubuntu 32 | steps: 33 | - checkout 34 | - run: setup/ci_bootstrap.sh 35 | - run: make init 36 | - run: make 37 | # to allow docker networking to work 38 | - run: sudo sysctl net.ipv4.ip_forward=1 39 | - run: sudo service docker restart 40 | - run: make test 41 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # vim:ts=4:sts=4:sw=4:et 2 | # 3 | # Author: Hari Sekhon 4 | # Date: 2015-10-31 19:04:34 +0000 (Sat, 31 Oct 2015) 5 | # 6 | # https://github.com/HariSekhon/SQL-scripts 7 | # 8 | # License: see accompanying Hari Sekhon LICENSE file 9 | # 10 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 11 | # to help improve or steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | # http://EditorConfig.org 17 | 18 | # stop recursing upwards for other .editorconfig files 19 | root = true 20 | 21 | # Unix-style newlines with a newline ending every file 22 | [*] 23 | indent_size = 4 24 | indent_style = space 25 | end_of_line = lf 26 | trim_trailing_whitespace = true 27 | insert_final_newline = true 28 | 29 | [*.go] 30 | indent_size = 4 31 | indent_style = tab 32 | end_of_line = lf 33 | trim_trailing_whitespace = true 34 | insert_final_newline = true 35 | 36 | [Makefile] 37 | indent_size = 4 38 | indent_style = tab 39 | end_of_line = lf 40 | trim_trailing_whitespace = true 41 | insert_final_newline = true 42 | 43 | [{*.md,*.hcl,*.tf,*.tfvars}] 44 | indent_size = 2 45 | indent_style = space 46 | end_of_line = lf 47 | trim_trailing_whitespace = true 48 | insert_final_newline = true 49 | 50 | [*.yml,*.yaml] 51 | indent_size = 2 52 | indent_style = space 53 | end_of_line = lf 54 | trim_trailing_whitespace = true 55 | insert_final_newline = true 56 | 57 | [.*] 58 | indent_size = 4 59 | indent_style = space 60 | end_of_line = lf 61 | trim_trailing_whitespace = true 62 | insert_final_newline = true 63 | 64 | # ============================================================================ # 65 | # Older Stuff, don't think I use this any more 66 | # ============================================================================ # 67 | 68 | # Matches multiple files with brace expansion notation 69 | # Set default charset 70 | #[*.{js,py}] 71 | #charset = utf-8 72 | 73 | # Indentation override for all JS under lib directory 74 | #[lib/**.js] 75 | #indent_style = space 76 | #indent_size = 2 77 | 78 | # Matches the exact files either package.json or .travis.yml 79 | #[{package.json,.travis.yml}] 80 | #indent_style = space 81 | #indent_size = 2 82 | 83 | #[*.xml] 84 | #indent_style = space 85 | #indent_size = 2 86 | -------------------------------------------------------------------------------- /.envrc-python: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # vim:ts=4:sts=4:sw=4:et 3 | # 4 | # Author: Hari Sekhon 5 | # Date: Mon Feb 22 17:42:01 2021 +0000 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # License: see accompanying Hari Sekhon LICENSE file 10 | # 11 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | # ============================================================================ # 17 | # P y t h o n D i r E n v 18 | # ============================================================================ # 19 | 20 | # .envrc to auto-load the virtualenv inside the 'venv' directory if present 21 | 22 | # https://direnv.net/man/direnv-stdlib.1.html 23 | 24 | set -euo pipefail 25 | [ -n "${DEBUG:-}" ] && set -x 26 | #srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 27 | 28 | # this is necessary because newer versions of pip no longer allow you to install PyPI packages in system-packages by default 29 | for venv in "$PWD/venv" "$HOME/venv"; do 30 | if [ -f "$venv/bin/activate" ]; then 31 | echo 32 | echo "Virtualenv directory found in: $venv" 33 | echo 34 | echo "Activating Virtualenv inside the directory: $venv" 35 | 36 | # shellcheck disable=SC1091 37 | source "$venv/bin/activate" 38 | break 39 | fi 40 | done 41 | 42 | # read .env too 43 | #dotenv 44 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2021-11-09 15:14:59 +0000 (Tue, 09 Nov 2021) 4 | # 5 | # vim:ts=4:sts=4:sw=4:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # License: see accompanying Hari Sekhon LICENSE file 10 | # 11 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | # https://docs.github.com/en/repositories/managing-your-repositorys-settings-and-features/customizing-your-repository/about-code-owners 17 | 18 | # Good in theory, to alert on PR changes to these code paths, but for public repos which may be forked and run .github/workflows/fork-update.yaml, this will result in a lot of spam 19 | 20 | # Tips: 21 | # 22 | # * includes changes under .github/ 23 | # dir/* only matches first level file changes but doesn't recurse 24 | # dir/ recurses 25 | # 26 | # - CODEOWNERS in base branch of PR determines review request 27 | # - paths are case sensitive 28 | # - last match wins, use * at top for overall owner then override with more specific teams 29 | 30 | #* @harisekhon # username or email address 31 | #* @myorg/platform-engineering # team based is the way to go - team must have Write access to the repo regardless of if individuals have access 32 | #* @myorg/devops 33 | #k8s @myorg/devops @myorg/sre-team 34 | #apps/ @myorg/developers 35 | #apps/dir2 # ignores dir2 as no owner/team specified on this line 36 | #src/ @myorg/developers 37 | #docs/ docs@example.com 38 | #.github/workflows @ci-cd-team 39 | -------------------------------------------------------------------------------- /.github/workflows/checkov.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # C h e c k o v G i t H u b W o r k f l o w 16 | # ============================================================================ # 17 | 18 | # Static analysis of Terraform code - publishes report to GitHub Security tab 19 | 20 | # https://github.com/bridgecrewio/checkov-action 21 | 22 | --- 23 | name: Checkov 24 | 25 | on: # yamllint disable-line rule:truthy 26 | push: 27 | branches: 28 | - master 29 | - main 30 | paths-ignore: 31 | - '**/*.md' 32 | pull_request: 33 | branches: 34 | - master 35 | - main 36 | paths-ignore: 37 | - '**/*.md' 38 | workflow_dispatch: 39 | inputs: 40 | debug: 41 | type: boolean 42 | required: false 43 | default: false 44 | schedule: 45 | - cron: '0 0 * * 1' 46 | 47 | permissions: 48 | actions: read 49 | contents: read 50 | security-events: write 51 | 52 | jobs: 53 | checkov: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: Checkov 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/checkov.yaml@master 59 | with: 60 | debug: ${{ github.event.inputs.debug }} 61 | -------------------------------------------------------------------------------- /.github/workflows/codeowners.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # C o d e O w n e r s 16 | # ============================================================================ # 17 | 18 | --- 19 | name: CodeOwners 20 | 21 | on: # yamllint disable-line rule:truthy 22 | push: 23 | branches: 24 | - master 25 | - main 26 | paths: 27 | - CODEOWNERS 28 | - .github/CODEOWNERS 29 | pull_request: 30 | branches: 31 | - master 32 | - main 33 | paths: 34 | - CODEOWNERS 35 | - .github/CODEOWNERS 36 | workflow_dispatch: 37 | inputs: 38 | debug: 39 | type: boolean 40 | required: false 41 | default: false 42 | schedule: 43 | - cron: '0 0 * * 1' 44 | 45 | permissions: 46 | contents: read 47 | 48 | concurrency: 49 | group: ${{ github.workflow }}-${{ github.ref }} 50 | cancel-in-progress: true 51 | 52 | jobs: 53 | validate: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: Validate CODEOWNERS 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/codeowners.yaml@master 59 | with: 60 | debug: ${{ github.event.inputs.debug }} 61 | -------------------------------------------------------------------------------- /.github/workflows/fork-sync.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # F o r k S y n c 16 | # ============================================================================ # 17 | 18 | # For a fork of the original repo, activate to keep it up to date via straight GitHub sync to the default branch 19 | 20 | --- 21 | name: Fork Sync 22 | 23 | on: # yamllint disable-line rule:truthy 24 | workflow_dispatch: 25 | inputs: 26 | debug: 27 | type: boolean 28 | required: false 29 | default: false 30 | schedule: 31 | - cron: '0 */3 * * *' 32 | 33 | permissions: 34 | contents: write 35 | 36 | concurrency: 37 | group: ${{ github.workflow }}-${{ github.ref }} 38 | cancel-in-progress: false 39 | 40 | jobs: 41 | fork_sync: 42 | # github.event.repository context not available in scheduled workflows 43 | #if: github.event.repository.fork == true 44 | if: github.repository_owner != 'HariSekhon' 45 | name: Fork Sync 46 | uses: HariSekhon/GitHub-Actions/.github/workflows/fork-sync.yaml@master 47 | with: 48 | debug: ${{ github.event.inputs.debug }} 49 | -------------------------------------------------------------------------------- /.github/workflows/fork-update-pr.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # F o r k U p d a t e P R 16 | # ============================================================================ # 17 | 18 | # For a fork of the original repo, activate to keep its branches up to date via Pull Requests 19 | # 20 | # To be used in conjunction with the adjacent fork-sync.yaml which keeps the default branch up to date 21 | 22 | --- 23 | name: Fork Update PR 24 | 25 | on: # yamllint disable-line rule:truthy 26 | workflow_dispatch: 27 | inputs: 28 | debug: 29 | type: boolean 30 | required: false 31 | default: false 32 | schedule: 33 | - cron: '0 10 * * 1' 34 | 35 | permissions: 36 | contents: write 37 | pull-requests: write 38 | 39 | concurrency: 40 | group: ${{ github.workflow }}-${{ github.ref }} 41 | cancel-in-progress: false 42 | 43 | jobs: 44 | fork_update_pr: 45 | # github.event.repository context not available in scheduled workflows 46 | #if: github.event.repository.fork == true 47 | if: github.repository_owner != 'HariSekhon' 48 | name: Fork Update PR 49 | uses: HariSekhon/GitHub-Actions/.github/workflows/fork-update-pr.yaml@master 50 | with: 51 | debug: ${{ github.event.inputs.debug }} 52 | -------------------------------------------------------------------------------- /.github/workflows/grype.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2023-05-13 01:07:56 +0100 (Sat, 13 May 2023) 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # G r y p e 16 | # ============================================================================ # 17 | 18 | --- 19 | name: Grype 20 | 21 | on: # yamllint disable-line rule:truthy 22 | push: 23 | branches: 24 | - master 25 | - main 26 | paths-ignore: 27 | - '**/*.md' 28 | pull_request: 29 | branches: 30 | - master 31 | - main 32 | paths-ignore: 33 | - '**/*.md' 34 | workflow_dispatch: 35 | inputs: 36 | debug: 37 | type: boolean 38 | required: false 39 | default: false 40 | schedule: 41 | - cron: '0 0 * * 1' 42 | 43 | permissions: 44 | actions: read 45 | contents: read 46 | security-events: write 47 | 48 | concurrency: 49 | group: ${{ github.workflow }}-${{ github.ref }} 50 | cancel-in-progress: true 51 | 52 | jobs: 53 | Grype: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: Grype 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/grype.yaml@master 59 | with: 60 | debug: ${{ github.event.inputs.debug }} 61 | -------------------------------------------------------------------------------- /.github/workflows/json.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # J S O N 16 | # ============================================================================ # 17 | 18 | # Validate any JSON files found in the repo 19 | 20 | --- 21 | name: JSON 22 | 23 | on: # yamllint disable-line rule:truthy 24 | push: 25 | branches: 26 | - master 27 | - main 28 | paths: 29 | - '**/*.json' 30 | - .github/workflows/json.yaml 31 | pull_request: 32 | branches: 33 | - master 34 | - main 35 | paths: 36 | - '**/*.json' 37 | - .github/workflows/json.yaml 38 | workflow_dispatch: 39 | inputs: 40 | debug: 41 | type: boolean 42 | required: false 43 | default: false 44 | schedule: 45 | - cron: '0 0 * * 1' 46 | 47 | permissions: 48 | contents: read 49 | 50 | jobs: 51 | check_json: 52 | # github.event.repository context not available in scheduled workflows 53 | #if: github.event.repository.fork == false 54 | if: github.repository_owner == 'HariSekhon' 55 | name: Check JSON 56 | uses: HariSekhon/GitHub-Actions/.github/workflows/json.yaml@master 57 | with: 58 | debug: ${{ github.event.inputs.debug }} 59 | -------------------------------------------------------------------------------- /.github/workflows/kics.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2022-02-01 19:36:08 +0000 (Tue, 01 Feb 2022) 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # K i c s 16 | # ============================================================================ # 17 | 18 | --- 19 | name: Kics 20 | 21 | on: # yamllint disable-line rule:truthy 22 | push: 23 | branches: 24 | - master 25 | - main 26 | paths-ignore: 27 | - '**/*.md' 28 | pull_request: 29 | branches: 30 | - master 31 | - main 32 | paths-ignore: 33 | - '**/*.md' 34 | workflow_dispatch: 35 | inputs: 36 | debug: 37 | type: boolean 38 | required: false 39 | default: false 40 | schedule: 41 | - cron: '0 0 * * 1' 42 | 43 | permissions: 44 | actions: read 45 | contents: read 46 | security-events: write 47 | 48 | concurrency: 49 | group: ${{ github.workflow }}-${{ github.ref }} 50 | cancel-in-progress: true 51 | 52 | jobs: 53 | kics: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: Kics 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/kics.yaml@master 59 | with: 60 | debug: ${{ github.event.inputs.debug }} 61 | -------------------------------------------------------------------------------- /.github/workflows/markdown.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2023-04-14 23:53:43 +0100 (Fri, 14 Apr 2023) 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # M a r k D o w n 16 | # ============================================================================ # 17 | 18 | --- 19 | name: Markdown 20 | 21 | on: # yamllint disable-line rule:truthy 22 | push: 23 | branches: 24 | - master 25 | - main 26 | paths: 27 | - '**/*.md' 28 | - .mdlrc 29 | - .mdl.rb 30 | - .markdownlint.rb 31 | - .github/workflows/markdown.yaml 32 | pull_request: 33 | branches: 34 | - master 35 | - main 36 | paths: 37 | - '**/*.md' 38 | - .mdlrc 39 | - .mdl.rb 40 | - .markdownlint.rb 41 | - .github/workflows/markdown.yaml 42 | workflow_dispatch: 43 | 44 | permissions: 45 | contents: read 46 | pull-requests: read 47 | 48 | jobs: 49 | Markdown: 50 | # github.event.repository context not available in scheduled workflows 51 | #if: github.event.repository.fork == false 52 | if: github.repository_owner == 'HariSekhon' 53 | name: Markdown 54 | uses: HariSekhon/GitHub-Actions/.github/workflows/markdown.yaml@master 55 | -------------------------------------------------------------------------------- /.github/workflows/semgrep-cloud.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # S e m g r e p C l o u d W o r k f l o w 16 | # ============================================================================ # 17 | 18 | # Logs results to https://semgrep.dev/ 19 | 20 | --- 21 | name: Semgrep Cloud 22 | 23 | on: # yamllint disable-line rule:truthy 24 | push: 25 | branches: 26 | - master 27 | - main 28 | paths-ignore: 29 | - '**/*.md' 30 | pull_request: 31 | branches: 32 | - master 33 | - main 34 | paths-ignore: 35 | - '**/*.md' 36 | workflow_dispatch: 37 | inputs: 38 | debug: 39 | type: boolean 40 | required: false 41 | default: false 42 | schedule: 43 | - cron: '0 0 * * 1' 44 | 45 | permissions: 46 | contents: read 47 | 48 | concurrency: 49 | group: ${{ github.workflow }}-${{ github.ref }} 50 | cancel-in-progress: true 51 | 52 | jobs: 53 | semgrep: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: Semgrep Cloud 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/semgrep-cloud.yaml@master 59 | secrets: 60 | SEMGREP_APP_TOKEN: ${{ secrets.SEMGREP_APP_TOKEN }} 61 | with: 62 | debug: ${{ github.event.inputs.debug }} 63 | -------------------------------------------------------------------------------- /.github/workflows/semgrep.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # S e m g r e p G i t H u b W o r k f l o w 16 | # ============================================================================ # 17 | 18 | # Generates code scanning alerts in GitHub's Security tab -> Code scanning alerts 19 | 20 | # https://semgrep.dev/docs/semgrep-ci/sample-ci-configs/#github-actions 21 | 22 | --- 23 | name: Semgrep 24 | 25 | on: # yamllint disable-line rule:truthy 26 | push: 27 | branches: 28 | - master 29 | - main 30 | paths-ignore: 31 | - '**/*.md' 32 | pull_request: 33 | branches: 34 | - master 35 | - main 36 | paths-ignore: 37 | - '**/*.md' 38 | workflow_dispatch: 39 | inputs: 40 | debug: 41 | type: boolean 42 | required: false 43 | default: false 44 | schedule: 45 | - cron: '0 0 * * 1' 46 | 47 | permissions: 48 | actions: read 49 | contents: read 50 | security-events: write 51 | 52 | concurrency: 53 | group: ${{ github.workflow }}-${{ github.ref }} 54 | cancel-in-progress: true 55 | 56 | jobs: 57 | semgrep: 58 | # github.event.repository context not available in scheduled workflows 59 | #if: github.event.repository.fork == false 60 | if: github.repository_owner == 'HariSekhon' 61 | name: Semgrep GitHub Security Tab 62 | uses: HariSekhon/GitHub-Actions/.github/workflows/semgrep.yaml@master 63 | with: 64 | debug: ${{ github.event.inputs.debug }} 65 | -------------------------------------------------------------------------------- /.github/workflows/shellcheck.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # S h e l l C h e c k 16 | # ============================================================================ # 17 | 18 | # Validate any shell scripts found in the repo 19 | 20 | --- 21 | name: ShellCheck 22 | 23 | on: # yamllint disable-line rule:truthy 24 | push: 25 | branches: 26 | - master 27 | - main 28 | paths: 29 | - '**/*.sh' 30 | - .github/workflows/shellcheck.yaml 31 | pull_request: 32 | branches: 33 | - master 34 | - main 35 | paths: 36 | - '**/*.sh' 37 | - .github/workflows/shellcheck.yaml 38 | workflow_dispatch: 39 | inputs: 40 | debug: 41 | type: boolean 42 | required: false 43 | default: false 44 | 45 | permissions: 46 | contents: read 47 | 48 | concurrency: 49 | group: ${{ github.workflow }}-${{ github.ref }} 50 | cancel-in-progress: true 51 | 52 | jobs: 53 | shellcheck: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: ShellCheck 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/shellcheck.yaml@master 59 | with: 60 | debug: ${{ github.event.inputs.debug }} 61 | -------------------------------------------------------------------------------- /.github/workflows/sonarcloud.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2023-04-14 23:53:43 +0100 (Fri, 14 Apr 2023) 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # S o n a r C l o u d 16 | # ============================================================================ # 17 | 18 | --- 19 | name: SonarCloud 20 | 21 | on: # yamllint disable-line rule:truthy 22 | push: 23 | branches: 24 | - master 25 | - main 26 | paths-ignore: 27 | - '**/*.md' 28 | pull_request: 29 | branches: 30 | - master 31 | - main 32 | paths-ignore: 33 | - '**/*.md' 34 | workflow_dispatch: 35 | 36 | permissions: 37 | contents: read 38 | pull-requests: read 39 | 40 | jobs: 41 | SonarCloud: 42 | # github.event.repository context not available in scheduled workflows 43 | #if: github.event.repository.fork == false 44 | if: github.repository_owner == 'HariSekhon' 45 | name: SonarCloud 46 | uses: HariSekhon/GitHub-Actions/.github/workflows/sonarcloud.yaml@master 47 | secrets: 48 | SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} 49 | -------------------------------------------------------------------------------- /.github/workflows/sqlfluff.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # S Q L F l u f f 16 | # ============================================================================ # 17 | 18 | --- 19 | name: SQLFluff 20 | 21 | on: # yamllint disable-line rule:truthy 22 | push: 23 | branches: 24 | - master 25 | - main 26 | paths: 27 | - '**/*.sql' 28 | - .github/workflows/sqlfluff.yaml 29 | pull_request: 30 | branches: 31 | - master 32 | - main 33 | paths: 34 | - '**/*.sql' 35 | - .github/workflows/sqlfluff.yaml 36 | workflow_dispatch: 37 | inputs: 38 | debug: 39 | type: boolean 40 | required: false 41 | default: false 42 | schedule: 43 | - cron: '0 0 * * 1' 44 | 45 | permissions: 46 | contents: read 47 | 48 | concurrency: 49 | group: ${{ github.ref }}-${{ github.workflow }} 50 | cancel-in-progress: true 51 | 52 | jobs: 53 | sqlfluff: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: SQLFluff 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/sqlfluff.yaml@master 59 | with: 60 | debug: ${{ github.event.inputs.debug }} 61 | -------------------------------------------------------------------------------- /.github/workflows/trivy.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2022-02-02 11:27:37 +0000 (Wed, 02 Feb 2022) 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # T r i v y 16 | # ============================================================================ # 17 | 18 | # Scan files in the local repo 19 | 20 | --- 21 | name: Trivy 22 | 23 | on: # yamllint disable-line rule:truthy 24 | push: 25 | branches: 26 | - master 27 | - main 28 | paths-ignore: 29 | - '**/*.md' 30 | pull_request: 31 | branches: 32 | - master 33 | - main 34 | paths-ignore: 35 | - '**/*.md' 36 | workflow_dispatch: 37 | inputs: 38 | debug: 39 | type: boolean 40 | required: false 41 | default: false 42 | schedule: 43 | - cron: '0 0 * * 1' 44 | 45 | permissions: 46 | actions: read 47 | contents: read 48 | security-events: write 49 | 50 | concurrency: 51 | group: ${{ github.workflow }}-${{ github.ref }} 52 | cancel-in-progress: true 53 | 54 | jobs: 55 | trivy: 56 | # github.event.repository context not available in scheduled workflows 57 | #if: github.event.repository.fork == false 58 | if: github.repository_owner == 'HariSekhon' 59 | name: Trivy 60 | uses: HariSekhon/GitHub-Actions/.github/workflows/trivy.yaml@master 61 | with: 62 | debug: ${{ github.event.inputs.debug }} 63 | -------------------------------------------------------------------------------- /.github/workflows/validate.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # V a l i d a t i o n 16 | # ============================================================================ # 17 | 18 | # Run all custom validations against files in the repo 19 | 20 | --- 21 | name: Validation 22 | 23 | on: # yamllint disable-line rule:truthy 24 | push: 25 | branches: 26 | - master 27 | - main 28 | paths-ignore: 29 | - '**/*.md' 30 | pull_request: 31 | branches: 32 | - master 33 | - main 34 | paths-ignore: 35 | - '**/*.md' 36 | workflow_dispatch: 37 | inputs: 38 | debug: 39 | type: boolean 40 | required: false 41 | default: false 42 | #schedule: 43 | # - cron: '0 0 * * 1' 44 | 45 | permissions: 46 | contents: read 47 | 48 | concurrency: 49 | group: ${{ github.workflow }}-${{ github.ref }} 50 | cancel-in-progress: true 51 | 52 | jobs: 53 | validate: 54 | # github.event.repository context not available in scheduled workflows 55 | #if: github.event.repository.fork == false 56 | if: github.repository_owner == 'HariSekhon' 57 | name: Validate 58 | uses: HariSekhon/GitHub-Actions/.github/workflows/validate.yaml@master 59 | with: 60 | debug: ${{ github.event.inputs.debug }} 61 | -------------------------------------------------------------------------------- /.github/workflows/xml.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # X M L 16 | # ============================================================================ # 17 | 18 | # Validate any XML files found in the repo 19 | 20 | --- 21 | name: XML 22 | 23 | on: # yamllint disable-line rule:truthy 24 | push: 25 | branches: 26 | - master 27 | - main 28 | paths: 29 | - '**/*.xml' 30 | - .github/workflows/xml.yaml 31 | pull_request: 32 | branches: 33 | - master 34 | - main 35 | paths: 36 | - '**/*.xml' 37 | - .github/workflows/xml.yaml 38 | workflow_dispatch: 39 | inputs: 40 | debug: 41 | type: boolean 42 | required: false 43 | default: false 44 | schedule: 45 | - cron: '0 0 * * 1' 46 | 47 | permissions: 48 | contents: read 49 | 50 | jobs: 51 | check_xml: 52 | # github.event.repository context not available in scheduled workflows 53 | #if: github.event.repository.fork == false 54 | if: github.repository_owner == 'HariSekhon' 55 | name: Check XML 56 | uses: HariSekhon/GitHub-Actions/.github/workflows/xml.yaml@master 57 | -------------------------------------------------------------------------------- /.github/workflows/yaml.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: Tue Feb 4 09:53:28 2020 +0000 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # ============================================================================ # 15 | # Y A M L 16 | # ============================================================================ # 17 | 18 | # Validate any YAML files found in the repo 19 | 20 | --- 21 | name: YAML 22 | 23 | on: # yamllint disable-line rule:truthy 24 | push: 25 | branches: 26 | - master 27 | - main 28 | paths: 29 | - '**/*.yml' 30 | - '**/*.yaml' 31 | - .github/workflows/yaml.yaml 32 | pull_request: 33 | branches: 34 | - master 35 | - main 36 | paths: 37 | - '**/*.yml' 38 | - '**/*.yaml' 39 | - .github/workflows/yaml.yaml 40 | workflow_dispatch: 41 | inputs: 42 | debug: 43 | type: boolean 44 | required: false 45 | default: false 46 | schedule: 47 | - cron: '0 0 * * 1' 48 | 49 | permissions: 50 | contents: read 51 | 52 | concurrency: 53 | group: ${{ github.workflow }}-${{ github.ref }} 54 | cancel-in-progress: true 55 | 56 | jobs: 57 | check_yaml: 58 | # github.event.repository context not available in scheduled workflows 59 | #if: github.event.repository.fork == false 60 | if: github.repository_owner == 'HariSekhon' 61 | name: Check YAML 62 | uses: HariSekhon/GitHub-Actions/.github/workflows/yaml.yaml@master 63 | with: 64 | debug: ${{ github.event.inputs.debug }} 65 | -------------------------------------------------------------------------------- /.mdl.rb: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # vim:ts=4:sts=4:sw=4:et:filetype=ruby 3 | # 4 | # Author: Hari Sekhon 5 | # Date: 2024-08-22 01:58:12 +0200 (Thu, 22 Aug 2024) 6 | # 7 | # https///github.com/HariSekhon/SQL-scripts 8 | # 9 | # License: see accompanying Hari Sekhon LICENSE file 10 | # 11 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | all 17 | #exclude_rule 'MD001' 18 | #exclude_rule 'MD003' 19 | #exclude_rule 'MD005' 20 | exclude_rule 'MD007' # leave 2 space indentation for lists, 3 space is ugly af 21 | #exclude_rule 'MD012' 22 | exclude_rule 'MD013' # long lines cannot be split if they are URLs 23 | #exclude_rule 'MD022' 24 | #exclude_rule 'MD025' 25 | exclude_rule 'MD026' # Trailing punctuation in header - sometimes I want to do etc. or ... at the end of a heading 26 | #exclude_rule 'MD031' 27 | #exclude_rule 'MD032' 28 | exclude_rule 'MD033' # inline HTML is important for formatting 29 | exclude_rule 'MD036' # emphasis used instead of header for footer Ported from lines 30 | #exclude_rule 'MD039' 31 | #exclude_rule 'MD056' 32 | -------------------------------------------------------------------------------- /.mdlrc: -------------------------------------------------------------------------------- 1 | mdlrc_dir = File.expand_path('..', __FILE__) 2 | 3 | style_file = File.join(mdlrc_dir, '.mdl.rb') 4 | 5 | style style_file 6 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2024-08-08 17:34:56 +0300 (Thu, 08 Aug 2024) 4 | # 5 | # vim:ts=2:sts=2:sw=2:et 6 | # 7 | # https///github.com/HariSekhon/SQL-scripts 8 | # 9 | # License: see accompanying Hari Sekhon LICENSE file 10 | # 11 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | # ============================================================================ # 17 | # P r e - C o m m i t 18 | # ============================================================================ # 19 | 20 | --- 21 | fail_fast: false 22 | #exclude: *.tmp$ 23 | 24 | repos: 25 | 26 | # will accept anything that 'git clone' understands 27 | # this means you can set this to a local git repo to develop your own hook repos interactively 28 | - repo: https://github.com/pre-commit/pre-commit-hooks 29 | rev: v4.6.0 30 | hooks: 31 | - id: check-yaml 32 | # Common errors 33 | #- id: end-of-file-fixer # ruins .gitignore Icon\r 34 | - id: trailing-whitespace 35 | args: [--markdown-linebreak-ext=md] 36 | # Git style 37 | - id: check-added-large-files 38 | - id: check-merge-conflict 39 | - id: check-vcs-permalinks 40 | #- id: forbid-new-submodules 41 | # Cross platform 42 | - id: check-case-conflict 43 | - id: mixed-line-ending 44 | args: [--fix=lf] 45 | # Security 46 | - id: detect-aws-credentials 47 | args: ['--allow-missing-credentials'] 48 | 49 | # rewrites python files with useless changes like changing single quotes to double quotes 50 | #- repo: https://github.com/psf/black 51 | # rev: 24.8.0 52 | # hooks: 53 | # - id: black 54 | 55 | # Git secrets Leaks 56 | - repo: https://github.com/awslabs/git-secrets.git 57 | # the release tags for 1.2.0, 1.2.1 and 1.3.0 are broken with this error: 58 | # 59 | # /Users/hari/.cache/pre-commit/repo......./.pre-commit-hooks.yaml is not a file 60 | # 61 | rev: 5357e18 62 | hooks: 63 | - id: git-secrets 64 | 65 | - repo: https://github.com/markdownlint/markdownlint 66 | rev: v0.12.0 67 | hooks: 68 | - id: markdownlint 69 | name: Markdownlint 70 | description: Run markdownlint on your Markdown files 71 | entry: mdl 72 | args: [-s, .mdl.rb] 73 | language: ruby 74 | files: \.(md|mdown|markdown)$ 75 | -------------------------------------------------------------------------------- /.sonarlint/connectedMode.json: -------------------------------------------------------------------------------- 1 | { 2 | "sonarCloudOrganization": "harisekhon", 3 | "projectKey": "HariSekhon_SQL-scripts" 4 | } 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # vim:ts=2:sts=2:sw=2:et 2 | # 3 | # Author: Hari Sekhon 4 | # Date: 2020-08-12 12:31:25 +0100 (Wed, 12 Aug 2020) 5 | # 6 | # https://github.com/HariSekhon/SQL-scripts 7 | # 8 | # License: see accompanying Hari Sekhon LICENSE file 9 | # 10 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 11 | # to help improve or steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | # ============================================================================ # 17 | # T r a v i s C I 18 | # ============================================================================ # 19 | 20 | # https://docs.travis-ci.com/user/customizing-the-build/ 21 | 22 | --- 23 | version: ~> 1.0 24 | 25 | # ============================================================== 26 | # https://docs.travis-ci.com/user/languages/minimal-and-generic/ 27 | language: bash 28 | os: 29 | - linux 30 | # needs docker and osx on Travis CI doesn't have docker 31 | #- osx 32 | 33 | # ====================================================== 34 | # https://docs.travis-ci.com/user/environment-variables/ 35 | env: 36 | - PYTHONUNBUFFERED=1 37 | 38 | # ============================================== 39 | # https://docs.travis-ci.com/user/notifications/ 40 | notifications: 41 | email: false 42 | 43 | # ================================================================================= 44 | # https://docs.travis-ci.com/user/customizing-the-build/#building-specific-branches 45 | # https://docs.travis-ci.com/user/conditional-builds-stages-jobs 46 | #branches: 47 | # only: 48 | # - master 49 | 50 | # ======================================== 51 | # https://docs.travis-ci.com/user/caching/ 52 | 53 | before_cache: 54 | - rm -f $HOME/.cache/pip/log/debug.log 55 | 56 | cache: 57 | - pip 58 | - directories: 59 | - $HOME/.cache 60 | - $HOME/.cpan 61 | - $HOME/.cpanm 62 | - $HOME/.gem 63 | # bash-tools is not a submodule, but downloaded via Make so ok to cache whole 64 | - bash-tools 65 | 66 | # ============================================== 67 | # https://docs.travis-ci.com/user/job-lifecycle/ 68 | 69 | # avoid package checksum mismatches when installing packages 70 | before_install: 71 | - sudo rm -f "${TRAVIS_ROOT}/etc/apt/apt.conf.d/99-travis-apt-proxy" 72 | - sudo rm -rf "${TRAVIS_ROOT}/var/lib/apt/lists/"* 73 | 74 | install: 75 | - travis_retry make 76 | 77 | script: 78 | # would breach the max log length anyway and just obscure the real error 79 | #- travis_retry make test 80 | - make test 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020 Hari Sekhon 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Author: Hari Sekhon 3 | # Date: 2020-08-12 12:10:41 +0100 (Wed, 12 Aug 2020) 4 | # 5 | # vim:ts=4:sts=4:sw=4:noet 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback 10 | # 11 | # https://www.linkedin.com/in/HariSekhon 12 | # 13 | 14 | # On Ubuntu this fails to pushd otherwise 15 | # fails to bootstrap on Alpine 16 | #SHELL := /usr/bin/env bash 17 | 18 | PATH := "$(PATH):$(PWD)/bash-tools" 19 | 20 | REPO := HariSekhon/SQL-scripts 21 | 22 | CODE_FILES := $(shell find . -type f -name '*.sql') 23 | 24 | # EUID / UID not exported in Make 25 | # USER not populated in Docker 26 | #ifeq '$(shell id -u)' '0' 27 | # SUDO = 28 | # SUDO2 = 29 | #else 30 | # SUDO = sudo 31 | #endif 32 | 33 | .PHONY: build 34 | build: init 35 | @echo "===========" 36 | @echo "SQL Scripts" 37 | @echo "===========" 38 | @echo 39 | @if command -v mysql_test_scripts.sh &>/dev/null && \ 40 | command -v mariadb_test_scripts.sh &>/dev/null && \ 41 | command -v postgres_test_scripts.sh &>/dev/null; then \ 42 | echo 'DevOps Bash Tools appears to be in $$PATH, no further building needed'; \ 43 | else \ 44 | curl https://raw.githubusercontent.com/HariSekhon/DevOps-Bash-tools/master/setup/bootstrap.sh | sh; \ 45 | fi 46 | @echo 47 | 48 | .PHONY: init 49 | init: 50 | git submodule update --init --recursive 51 | 52 | .PHONY: all 53 | all: build test 54 | @: 55 | 56 | .PHONY: test 57 | test: 58 | @./test.sh 59 | 60 | .PHONY: postgres 61 | postgres: 62 | @./test.sh postgres 63 | 64 | .PHONY: mysql 65 | mysql: 66 | @./test.sh mysql 67 | 68 | .PHONY: mariadb 69 | mariadb: 70 | @./test.sh maria 71 | 72 | .PHONY: maria 73 | maria: mariadb 74 | @: 75 | 76 | .PHONY: push 77 | push: 78 | git push --all 79 | 80 | wc: 81 | @wc -l $(CODE_FILES) 82 | @printf "\nSQL Scripts: " 83 | @tr ' ' '\n' <<< "$(CODE_FILES)" | wc -l | sed 's/[[:space:]]//g' 84 | -------------------------------------------------------------------------------- /analytics/README.md: -------------------------------------------------------------------------------- 1 | # BigQuery Analytics Examples 2 | 3 | ## Ecommerce 4 | 5 | Load public dataset into your BigQuery using this link: 6 | 7 | 8 | 9 | Then run the analytics SQL examples from here. 10 | -------------------------------------------------------------------------------- /analytics/bigquery_ecommerce_conversion_rate.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 09:22:11 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | WITH visitors AS( 17 | SELECT 18 | COUNT(DISTINCT fullVisitorId) AS total_visitors 19 | FROM `data-to-insights.ecommerce.web_analytics` 20 | ), 21 | 22 | purchasers AS( 23 | SELECT 24 | COUNT(DISTINCT fullVisitorId) AS total_purchasers 25 | FROM `data-to-insights.ecommerce.web_analytics` 26 | WHERE totals.transactions IS NOT NULL 27 | ) 28 | 29 | SELECT 30 | total_visitors, 31 | total_purchasers, 32 | total_purchasers / total_visitors AS conversion_rate, 33 | ROUND(total_purchasers / total_visitors * 100, 2) AS conversion_percentage 34 | FROM 35 | visitors, 36 | purchasers 37 | ; 38 | -------------------------------------------------------------------------------- /analytics/bigquery_ecommerce_purchasers_on_return_visit.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 09:39:05 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- visitors who bought on a return visit (could have bought on first as well) 17 | WITH all_visitor_stats AS ( 18 | SELECT 19 | fullvisitorid, 20 | IF( 21 | COUNTIF(totals.transactions > 0 22 | AND 23 | totals.newVisits IS NULL) > 0, 1, 0 24 | ) AS will_buy_on_return_visit 25 | FROM 26 | `data-to-insights.ecommerce.web_analytics` 27 | GROUP BY 28 | fullvisitorid 29 | ) 30 | 31 | SELECT 32 | COUNT(DISTINCT fullvisitorid) AS total_visitors, 33 | will_buy_on_return_visit 34 | FROM 35 | all_visitor_stats 36 | GROUP BY 37 | will_buy_on_return_visit; 38 | -------------------------------------------------------------------------------- /analytics/bigquery_ecommerce_time_on_site.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 09:49:38 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- looking at bounces and time on site are not good features for model training and prediction since of the top 10 customers by time on site only 1 purchased! 17 | 18 | SELECT 19 | * EXCEPT(fullVisitorId) 20 | FROM 21 | 22 | # features 23 | (SELECT 24 | fullVisitorId, 25 | IFNULL(totals.bounces, 0) AS bounces, 26 | IFNULL(totals.timeOnSite, 0) AS time_on_site 27 | FROM 28 | `data-to-insights.ecommerce.web_analytics` 29 | WHERE 30 | totals.newVisits = 1) 31 | JOIN 32 | (SELECT 33 | fullvisitorid, 34 | IF( 35 | COUNTIF( 36 | totals.transactions > 0 37 | AND 38 | totals.newVisits IS NULL 39 | ) > 0, 1, 0 40 | ) AS will_buy_on_return_visit 41 | FROM 42 | `data-to-insights.ecommerce.web_analytics` 43 | GROUP BY fullvisitorid) 44 | USING (fullVisitorId) 45 | ORDER BY 46 | time_on_site DESC 47 | LIMIT 10; 48 | -------------------------------------------------------------------------------- /analytics/bigquery_ecommerce_top_5_products.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 09:36:31 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | SELECT 17 | p.v2ProductName, 18 | p.v2ProductCategory, 19 | SUM(p.productQuantity) AS units_sold, 20 | ROUND(SUM(p.localProductRevenue/1000000),2) AS revenue 21 | FROM 22 | `data-to-insights.ecommerce.web_analytics`, 23 | UNNEST(hits) AS h, 24 | UNNEST(h.product) AS p 25 | GROUP BY 1, 2 26 | ORDER BY 27 | revenue DESC 28 | LIMIT 5; 29 | -------------------------------------------------------------------------------- /analytics/bigquery_ml_classification_logistic_regression_create_model1.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 09:54:10 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- ran for 2 minutes to build the trained model 17 | -- 18 | -- This statement created a new model named :ecommerce.classification_model1 19 | -- 20 | -- - click "Go to model" on the far right of the line 21 | 22 | -- this model is weak in feature selection - see model2 for a much better more realistic model 23 | 24 | CREATE OR REPLACE MODEL `ecommerce.classification_model1` 25 | OPTIONS ( 26 | model_type='logistic_reg', 27 | labels = ['will_buy_on_return_visit'] 28 | ) AS 29 | 30 | -- training data provided below via SELECT statement 31 | 32 | SELECT 33 | * EXCEPT(fullVisitorId) 34 | FROM 35 | 36 | -- features 37 | ( 38 | SELECT 39 | fullVisitorId, 40 | IFNULL(totals.bounces, 0) AS bounces, 41 | IFNULL(totals.timeOnSite, 0) AS time_on_site 42 | FROM 43 | `data-to-insights.ecommerce.web_analytics` 44 | WHERE 45 | totals.newVisits = 1 46 | -- train on subset of the data so we can test on the rest of the data 47 | AND date BETWEEN '20160801' AND '20170430' 48 | ) 49 | JOIN 50 | ( 51 | SELECT 52 | fullvisitorid, 53 | -- the label of answers to train on 54 | IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit 55 | FROM 56 | `data-to-insights.ecommerce.web_analytics` 57 | GROUP BY 58 | fullvisitorid 59 | ) 60 | USING 61 | (fullVisitorId) 62 | ; 63 | -------------------------------------------------------------------------------- /analytics/bigquery_ml_classification_logistic_regression_create_model2.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 10:02:02 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- ran in 2 mins 17 | 18 | -- better feature selection than model 1 19 | 20 | CREATE OR REPLACE MODEL `ecommerce.classification_model_2` 21 | OPTIONS ( 22 | model_type='logistic_reg', 23 | labels = ['will_buy_on_return_visit'] 24 | ) AS 25 | 26 | -- training data provided below via SELECT statements 27 | 28 | WITH all_visitor_stats AS ( 29 | SELECT 30 | fullvisitorid, 31 | IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit 32 | FROM `data-to-insights.ecommerce.web_analytics` 33 | GROUP BY fullvisitorid 34 | ) 35 | 36 | -- select features 37 | SELECT * EXCEPT(unique_session_id) FROM ( 38 | 39 | SELECT 40 | CONCAT(fullvisitorid, CAST(visitId AS STRING)) AS unique_session_id, 41 | 42 | -- labels 43 | will_buy_on_return_visit, 44 | 45 | MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress, 46 | 47 | -- behavior on the site 48 | IFNULL(totals.bounces, 0) AS bounces, 49 | IFNULL(totals.timeOnSite, 0) AS time_on_site, 50 | totals.pageviews, 51 | 52 | -- where the visitor came from 53 | trafficSource.source, 54 | trafficSource.medium, 55 | channelGrouping, 56 | 57 | -- mobile or desktop 58 | device.deviceCategory, 59 | 60 | -- geographic 61 | IFNULL(geoNetwork.country, "") AS country 62 | 63 | FROM `data-to-insights.ecommerce.web_analytics`, 64 | UNNEST(hits) AS h 65 | 66 | JOIN 67 | all_visitor_stats 68 | USING 69 | (fullvisitorid) 70 | 71 | WHERE 1=1 72 | -- only predict for new visits 73 | AND totals.newVisits = 1 74 | -- train on subset of the data so we can test on the rest of the data 75 | AND date BETWEEN '20160801' AND '20170430' 76 | 77 | GROUP BY 78 | unique_session_id, 79 | will_buy_on_return_visit, 80 | bounces, 81 | time_on_site, 82 | totals.pageviews, 83 | trafficSource.source, 84 | trafficSource.medium, 85 | channelGrouping, 86 | device.deviceCategory, 87 | country 88 | ); 89 | -------------------------------------------------------------------------------- /analytics/bigquery_ml_classification_logistic_regression_evaluate_model1.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 09:59:49 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- evaluate model1 17 | -- 18 | -- get the score for the ROC area under curve to check the quality of prediction 19 | -- 20 | -- poor ROC auc only ~ 0.7x score - go to model 2 21 | 22 | SELECT 23 | roc_auc, 24 | CASE 25 | WHEN roc_auc > .9 THEN 'good' 26 | WHEN roc_auc > .8 THEN 'fair' 27 | WHEN roc_auc > .7 THEN 'not great' 28 | ELSE 'poor' END AS model_quality, 29 | accuracy, 30 | precision, 31 | recall 32 | FROM 33 | ML.EVALUATE( 34 | MODEL ecommerce.classification_model1, ( 35 | 36 | -- the test data is supplied below via a SELECT query for the model to compare its predictions to (notice this is non-overlapping with the training data) 37 | 38 | SELECT 39 | * EXCEPT(fullVisitorId) 40 | FROM 41 | 42 | -- features 43 | ( 44 | SELECT 45 | fullVisitorId, 46 | IFNULL(totals.bounces, 0) AS bounces, 47 | IFNULL(totals.timeOnSite, 0) AS time_on_site 48 | FROM 49 | `data-to-insights.ecommerce.web_analytics` 50 | WHERE 51 | totals.newVisits = 1 52 | -- notice this time period doesn't overlap with the training data 53 | AND date BETWEEN '20170501' AND '20170630' 54 | ) 55 | JOIN 56 | ( 57 | SELECT 58 | fullvisitorid, 59 | IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit 60 | FROM 61 | `data-to-insights.ecommerce.web_analytics` 62 | GROUP BY 63 | fullvisitorid 64 | ) 65 | USING (fullVisitorId) 66 | ) 67 | ); 68 | -------------------------------------------------------------------------------- /analytics/bigquery_ml_classification_logistic_regression_evaluate_model2.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 10:03:32 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- evaluate model2 with more features 17 | -- 18 | -- get the score for the ROC area under curve to check the quality of prediction 19 | -- 20 | -- good ROC auc > 0.9 - gives much better predictive power than model1 21 | 22 | SELECT 23 | roc_auc, 24 | CASE 25 | WHEN roc_auc > .9 THEN 'good' 26 | WHEN roc_auc > .8 THEN 'fair' 27 | WHEN roc_auc > .7 THEN 'not great' 28 | ELSE 'poor' END AS model_quality, 29 | accuracy, 30 | precision, 31 | recall 32 | FROM 33 | ML.EVALUATE(MODEL ecommerce.classification_model_2, ( 34 | 35 | WITH all_visitor_stats AS ( 36 | SELECT 37 | fullvisitorid, 38 | IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit 39 | FROM `data-to-insights.ecommerce.web_analytics` 40 | GROUP BY fullvisitorid 41 | ) 42 | 43 | # add in new features 44 | SELECT * EXCEPT(unique_session_id) FROM ( 45 | 46 | SELECT 47 | CONCAT(fullvisitorid, CAST(visitId AS STRING)) AS unique_session_id, 48 | 49 | # labels 50 | will_buy_on_return_visit, 51 | 52 | MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress, 53 | 54 | # behavior on the site 55 | IFNULL(totals.bounces, 0) AS bounces, 56 | IFNULL(totals.timeOnSite, 0) AS time_on_site, 57 | totals.pageviews, 58 | 59 | # where the visitor came from 60 | trafficSource.source, 61 | trafficSource.medium, 62 | channelGrouping, 63 | 64 | # mobile or desktop 65 | device.deviceCategory, 66 | 67 | # geographic 68 | IFNULL(geoNetwork.country, "") AS country 69 | 70 | FROM `data-to-insights.ecommerce.web_analytics`, 71 | UNNEST(hits) AS h 72 | 73 | JOIN 74 | all_visitor_stats 75 | 76 | USING(fullvisitorid) 77 | 78 | WHERE 1=1 79 | # only predict for new visits 80 | AND totals.newVisits = 1 81 | AND date BETWEEN '20170501' AND '20170630' # eval 2 months 82 | 83 | GROUP BY 84 | unique_session_id, 85 | will_buy_on_return_visit, 86 | bounces, 87 | time_on_site, 88 | totals.pageviews, 89 | trafficSource.source, 90 | trafficSource.medium, 91 | channelGrouping, 92 | device.deviceCategory, 93 | country 94 | ) 95 | ) 96 | ); 97 | -------------------------------------------------------------------------------- /analytics/bigquery_ml_classification_logistic_regression_predict_with_model2.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-06 10:05:48 +0100 (Sun, 06 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- use the improved model 2 to predict which users will buy on return visit 17 | 18 | SELECT 19 | * 20 | FROM 21 | ml.PREDICT( 22 | MODEL `ecommerce.classification_model_2`, ( 23 | 24 | WITH all_visitor_stats AS ( 25 | SELECT 26 | fullvisitorid, 27 | IF(COUNTIF(totals.transactions > 0 AND totals.newVisits IS NULL) > 0, 1, 0) AS will_buy_on_return_visit 28 | FROM `data-to-insights.ecommerce.web_analytics` 29 | GROUP BY fullvisitorid 30 | ) 31 | 32 | SELECT 33 | CONCAT(fullvisitorid, '-',CAST(visitId AS STRING)) AS unique_session_id, 34 | 35 | -- labels 36 | will_buy_on_return_visit, 37 | 38 | MAX(CAST(h.eCommerceAction.action_type AS INT64)) AS latest_ecommerce_progress, 39 | 40 | -- behavior on the site 41 | IFNULL(totals.bounces, 0) AS bounces, 42 | IFNULL(totals.timeOnSite, 0) AS time_on_site, 43 | totals.pageviews, 44 | 45 | -- where the visitor came from 46 | trafficSource.source, 47 | trafficSource.medium, 48 | channelGrouping, 49 | 50 | -- mobile or desktop 51 | device.deviceCategory, 52 | 53 | -- geographic 54 | IFNULL(geoNetwork.country, "") AS country 55 | 56 | FROM 57 | `data-to-insights.ecommerce.web_analytics`, 58 | UNNEST(hits) AS h 59 | 60 | JOIN 61 | all_visitor_stats 62 | USING 63 | (fullvisitorid) 64 | 65 | WHERE 66 | -- only predict for new visits 67 | totals.newVisits = 1 68 | -- test on a non-overlapping subset to the training data 69 | AND date BETWEEN '20170701' AND '20170801' 70 | 71 | GROUP BY 72 | unique_session_id, 73 | will_buy_on_return_visit, 74 | bounces, 75 | time_on_site, 76 | totals.pageviews, 77 | trafficSource.source, 78 | trafficSource.medium, 79 | channelGrouping, 80 | device.deviceCategory, 81 | country 82 | ) 83 | ) 84 | 85 | ORDER BY 86 | -- your label name will be prefixed with 'predicted_' for the field generated by the model 87 | predicted_will_buy_on_return_visit DESC; 88 | -------------------------------------------------------------------------------- /aws_athena_cloudtrail_ddl.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-01-01 15:38:58 +0000 (Wed, 01 Jan 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn 12 | -- and optionally send me feedback to help improve or steer this or other code I publish 13 | -- 14 | -- https://www.linkedin.com/in/HariSekhon 15 | -- 16 | 17 | -- replace and on last line 18 | 19 | CREATE EXTERNAL TABLE cloudtrail_logs ( 20 | eventVersion STRING, 21 | userIdentity STRUCT< 22 | type: STRING, 23 | principalId: STRING, 24 | arn: STRING, 25 | accountId: STRING, 26 | invokedBy: STRING, 27 | accessKeyId: STRING, 28 | userName: STRING, 29 | sessionContext: STRUCT< 30 | attributes: STRUCT< 31 | mfaAuthenticated: STRING, 32 | creationDate: STRING>, 33 | sessionIssuer: STRUCT< 34 | type: STRING, 35 | principalId: STRING, 36 | arn: STRING, 37 | accountId: STRING, 38 | userName: STRING>>>, 39 | eventTime STRING, 40 | eventSource STRING, 41 | eventName STRING, 42 | awsRegion STRING, 43 | sourceIpAddress STRING, 44 | userAgent STRING, 45 | errorCode STRING, 46 | errorMessage STRING, 47 | requestParameters STRING, 48 | responseElements STRING, 49 | additionalEventData STRING, 50 | requestId STRING, 51 | eventId STRING, 52 | resources ARRAY>, 56 | eventType STRING, 57 | apiVersion STRING, 58 | readOnly STRING, 59 | recipientAccountId STRING, 60 | serviceEventDetails STRING, 61 | sharedEventID STRING, 62 | vpcEndpointId STRING 63 | ) 64 | COMMENT 'CloudTrail table for bucket' 65 | ROW FORMAT SERDE 'com.amazon.emr.hive.serde.CloudTrailSerde' 66 | STORED AS INPUTFORMAT 'com.amazon.emr.cloudtrail.CloudTrailInputFormat' 67 | OUTPUTFORMAT 'org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat' 68 | LOCATION 's3:///AWSLogs//' 69 | TBLPROPERTIES ('classification'='cloudtrail'); 70 | -------------------------------------------------------------------------------- /bigquery_billing_commonly_charged_units_of_measure.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-02-02 00:37:06 +0000 (Sun, 02 Feb 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Billing Query 17 | -- 18 | -- commonly charged units of measure 19 | 20 | SELECT 21 | usage_unit, 22 | COUNT(*) AS billing_records 23 | FROM 24 | `myproject.mydata.imported_billing_data` 25 | WHERE 26 | cost > 0 27 | GROUP BY 28 | usage_unit 29 | ORDER BY 30 | billing_records DESC; 31 | -------------------------------------------------------------------------------- /bigquery_billing_latest_100_charges.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-02-02 00:37:06 +0000 (Sun, 02 Feb 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Billing Query 17 | -- 18 | -- latest 100 charges 19 | 20 | SELECT 21 | product, 22 | resource_type, 23 | start_time, 24 | end_time, 25 | cost, 26 | project_id, 27 | project_name, 28 | project_labels_key, 29 | currency, 30 | currency_conversion_rate, 31 | usage_amount, 32 | usage_unit 33 | FROM 34 | `myproject.mydataset.imported_billing_data` 35 | WHERE 36 | cost > 0 37 | ORDER BY 38 | end_time DESC 39 | LIMIT 100; 40 | -------------------------------------------------------------------------------- /bigquery_billing_products_most_frequently_used.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-02-02 00:37:06 +0000 (Sun, 02 Feb 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Billing Query 17 | -- 18 | -- most frequently used products costing more than 1 dollar 19 | 20 | SELECT 21 | product, 22 | COUNT(*) AS billing_records 23 | FROM 24 | `myproject.mydataset.imported_billing_data` 25 | WHERE 26 | cost > 1 27 | GROUP BY 28 | product 29 | ORDER BY 30 | billing_records DESC; 31 | -------------------------------------------------------------------------------- /bigquery_billing_products_with_highest_aggregate_cost.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-02-02 00:37:06 +0000 (Sun, 02 Feb 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Billing Query 17 | -- 18 | -- products with highest aggregate cost 19 | 20 | SELECT 21 | product, 22 | ROUND(SUM(cost), 2) AS total_cost 23 | FROM 24 | `myproject.mydataset.imported_billing_data` 25 | GROUP BY 26 | product 27 | ORDER BY 28 | total_cost DESC; 29 | -------------------------------------------------------------------------------- /bigquery_billing_products_with_most_billing_records.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-02-02 00:37:06 +0000 (Sun, 02 Feb 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Billing Query 17 | -- 18 | -- products with most billing records 19 | 20 | SELECT 21 | product, 22 | COUNT(*) AS billing_records 23 | FROM 24 | `myproject.mydataset.imported_billing_data` 25 | GROUP BY 26 | product 27 | ORDER BY 28 | billing_records DESC; 29 | -------------------------------------------------------------------------------- /bigquery_info_columns.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-16 08:41:30 +0100 (Wed, 16 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Information Schema 17 | -- 18 | -- Lists columns and their details in a given dataset 19 | -- 20 | -- Processes 10MB of data when run 21 | 22 | -- https://cloud.google.com/bigquery/docs/tables#columns_view 23 | 24 | SELECT 25 | table_catalog, 26 | table_schema, 27 | table_name, 28 | column_name, 29 | ordinal_position, 30 | is_nullable, 31 | data_type, 32 | is_generated, 33 | generation_expression, 34 | is_stored, 35 | is_hidden, 36 | is_updatable, 37 | is_system_defined, 38 | is_partitioning_column, 39 | clustering_ordinal_position 40 | FROM 41 | -- XXX: replace bigquery-public-data.github_repos with myproject.mydataset 42 | `bigquery-public-data.github_repos.INFORMATION_SCHEMA.COLUMNS`; 43 | -------------------------------------------------------------------------------- /bigquery_info_columns_partitioned_clustered.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-16 08:44:33 +0100 (Wed, 16 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Information Schema 17 | -- 18 | -- Lists partitioned and/or clustered columns in a dataset 19 | -- 20 | -- Processes 10MB of data when run 21 | 22 | 23 | SELECT 24 | * 25 | FROM 26 | -- XXX: replace bigquery-public-data.github_repos with myproject.mydataset 27 | `bigquery-public-data.github_repos.INFORMATION_SCHEMA.COLUMNS` 28 | WHERE 29 | is_partitioning_column = 'YES' 30 | OR 31 | clustering_ordinal_position IS NOT NULL; 32 | -------------------------------------------------------------------------------- /bigquery_info_datasets.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-16 08:29:55 +0100 (Wed, 16 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Information Schema 17 | -- 18 | -- Lists datasets in the current project along with their descriptions, sorted by last modified time 19 | -- 20 | -- Processes 20MB 21 | 22 | SELECT 23 | s.*, 24 | TIMESTAMP_DIFF(CURRENT_TIMESTAMP(), creation_time, DAY) AS days_live, 25 | option_value AS dataset_description 26 | FROM 27 | -- XXX: can prefix INFORMATION_SCHEMA with your '.' to select from another project, but defaulting to the current project is more convenient than editing this 28 | `INFORMATION_SCHEMA.SCHEMATA` AS s 29 | LEFT JOIN `INFORMATION_SCHEMA.SCHEMATA_OPTIONS` AS so 30 | USING (schema_name) 31 | -- will miss all the datasets without descriptions like this 32 | --WHERE 33 | -- so.option_name = 'description' 34 | ORDER BY 35 | last_modified_time DESC 36 | LIMIT 15; 37 | -------------------------------------------------------------------------------- /bigquery_info_tables.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-09-16 08:29:55 +0100 (Wed, 16 Sep 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- BigQuery Information Schema 17 | -- 18 | -- Lists tables and their details in a given dataset 19 | -- 20 | -- Processes zero bytes for billing purposes since this is just using the Information Schema 21 | 22 | -- https://cloud.google.com/bigquery/docs/information-schema-tables 23 | -- 24 | -- https://cloud.google.com/bigquery/docs/tables#getting_information_about_tables 25 | 26 | -- 27 | -- Returns: 28 | -- 29 | -- dataset id 30 | -- table id 31 | -- size in GB 32 | -- row counts 33 | -- created human readable timestamp 34 | -- last modified human readable timestamp 35 | -- type - table or view 36 | -- number of rows is the number of tables in the dataset 37 | -- 38 | 39 | SELECT 40 | dataset_id, 41 | table_id, 42 | -- convert bytes to GB 43 | ROUND(size_bytes/pow(10,9),2) as size_gb, 44 | row_count, 45 | -- Convert UNIX EPOCH to a timestamp. 46 | TIMESTAMP_MILLIS(creation_time) AS creation_time, 47 | TIMESTAMP_MILLIS(last_modified_time) as last_modified_time, 48 | CASE 49 | WHEN type = 1 THEN 'table' 50 | WHEN type = 2 THEN 'view' 51 | ELSE NULL 52 | END AS type 53 | FROM 54 | -- XXX: replace bigquery-public-data.github_repos with myproject.mydataset 55 | `bigquery-public-data.github_repos.__TABLES__` 56 | ORDER BY 57 | size_gb DESC; 58 | -------------------------------------------------------------------------------- /mysql_databases_by_size.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-07 01:00:25 +0100 (Fri, 07 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL databases by size descending 17 | -- 18 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 19 | 20 | SELECT 21 | table_schema, 22 | ROUND(SUM(data_length + index_length) / 1024 / 1024, 3) AS 'Database Size (MB)', 23 | ROUND(SUM(data_free) / 1024 / 1024, 3) AS 'Free Space (MB)' 24 | FROM 25 | information_schema.tables 26 | GROUP BY 27 | table_schema 28 | ORDER BY 29 | 2 DESC; 30 | -------------------------------------------------------------------------------- /mysql_host_summary.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 01:51:14 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL show activity per host 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | SELECT 26 | * 27 | FROM 28 | sys.host_summary 29 | ORDER BY 30 | statement_latency DESC; 31 | -------------------------------------------------------------------------------- /mysql_indexes_unused.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 02:41:02 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL unused indexes 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | -- requires Performance Schema to be enabled 26 | -- 27 | -- in my.cnf: 28 | -- 29 | -- [mysqld] 30 | -- performance_schema=ON 31 | -- 32 | -- 1 = true, 0 = false 33 | SELECT IF (@@performance_schema, 'TRUE', 'FALSE') AS 'Performance Schema enabled'; 34 | 35 | \! echo "Unused Indexes since startup:"; 36 | 37 | SELECT 38 | * 39 | FROM 40 | sys.schema_unused_indexes 41 | ORDER BY 42 | object_schema, 43 | object_name, 44 | index_name; 45 | -------------------------------------------------------------------------------- /mysql_info.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 10:08:00 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Useful MySQL funcs & info 17 | -- 18 | -- Useful for debugging intended vs actual user auth to fix mysql.user table 19 | -- 20 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 21 | 22 | 23 | -- https://dev.mysql.com/doc/refman/8.0/en/sql-function-reference.html 24 | 25 | 26 | -- some of these allow not using the brackets but use them for portability between MySQL 5.x and 8.x 27 | SELECT 28 | VERSION(), 29 | USER(), -- client's reported user + host, same as SESSION_USER(), SYSTEM_USER() 30 | CURRENT_USER(), -- authenticated user name + host name - this is the one you want for debugging your mysql.user table configuration 31 | DATABASE(), -- SCHEMA() 32 | NOW(), 33 | CURDATE(), 34 | CURTIME(), 35 | UTC_DATE(), 36 | UTC_TIME(), 37 | UTC_TIMESTAMP(), 38 | SYSDATE(), -- returns date of function completion 39 | -- PS_CURRENT_THREAD_ID(), -- MySQL 8.0.16+ 40 | UUID_SHORT(), -- integer 41 | UUID() -- alnum 42 | ; 43 | -------------------------------------------------------------------------------- /mysql_memory_by_host.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 02:13:54 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL memory used per host 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | SELECT 26 | * 27 | FROM 28 | sys.memory_by_host_by_current_bytes 29 | WHERE 30 | host <> 'background' 31 | ORDER BY 32 | total_allocated DESC; 33 | -------------------------------------------------------------------------------- /mysql_memory_by_user.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 02:13:54 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL memory used per user 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | SELECT 26 | * 27 | FROM 28 | sys.memory_by_user_by_current_bytes 29 | WHERE 30 | user <> 'background' 31 | ORDER BY 32 | total_allocated DESC; 33 | -------------------------------------------------------------------------------- /mysql_sessions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 02:13:54 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL sessions 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | SELECT 26 | user, 27 | db, 28 | program_name, 29 | command, 30 | state, 31 | time, 32 | current_statement, 33 | current_memory, 34 | progress, 35 | lock_latency, 36 | rows_examined, 37 | rows_sent, 38 | rows_affected, 39 | tmp_tables, 40 | tmp_disk_tables, 41 | full_scan, 42 | last_statement, 43 | last_statement_latency, 44 | last_wait, 45 | last_wait_latency, 46 | trx_latency, 47 | trx_state 48 | FROM 49 | sys.session 50 | WHERE 51 | user <> 'sql/event_scheduler' 52 | ORDER BY 53 | current_memory DESC; 54 | -------------------------------------------------------------------------------- /mysql_statement_latency_by_host.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 01:51:14 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL statement latency stats per host 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | SELECT 26 | * 27 | FROM 28 | sys.host_summary_by_statement_latency 29 | ORDER BY 30 | total_latency DESC; 31 | -------------------------------------------------------------------------------- /mysql_statement_latency_by_user.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 01:51:14 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL statement latency stats per user 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | SELECT 26 | * 27 | FROM 28 | sys.user_summary_by_statement_latency 29 | ORDER BY 30 | total_latency DESC; 31 | -------------------------------------------------------------------------------- /mysql_tables.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 02:45:44 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL tables across schemas (useful for finding tables) 17 | -- 18 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 19 | 20 | SELECT 21 | table_schema, 22 | table_name, 23 | table_type, 24 | engine, 25 | table_rows, 26 | avg_row_length, 27 | data_length, 28 | max_data_length, 29 | index_length, 30 | data_free, 31 | auto_increment, 32 | create_time, 33 | update_time, 34 | table_comment 35 | FROM 36 | information_schema.tables 37 | WHERE 38 | table_type NOT LIKE '%VIEW%' 39 | ORDER BY 40 | table_schema, 41 | table_name; 42 | -------------------------------------------------------------------------------- /mysql_tables_nonsystem.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 02:45:44 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL tables across schemas, excluding system tables such as information_schema, performance_schema, mysql and sys 17 | -- 18 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 19 | 20 | SELECT 21 | table_schema, 22 | table_name, 23 | table_type, 24 | engine, 25 | table_rows, 26 | avg_row_length, 27 | data_length, 28 | max_data_length, 29 | index_length, 30 | data_free, 31 | auto_increment, 32 | create_time, 33 | update_time, 34 | table_comment 35 | FROM 36 | information_schema.tables 37 | WHERE 38 | table_type NOT LIKE '%VIEW%' 39 | AND 40 | table_schema NOT IN ('mysql', 'sys', 'information_schema', 'performance_schema') 41 | ORDER BY 42 | table_schema, 43 | table_name; 44 | -------------------------------------------------------------------------------- /mysql_user.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 10:08:00 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL Requested vs Actual User 17 | -- 18 | -- Useful for debugging intended vs actual user auth to fix mysql.user table 19 | 20 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 21 | 22 | -- https://dev.mysql.com/doc/refman/8.0/en/sql-function-reference.html 23 | 24 | SELECT 25 | USER() AS 'Your Requested USER()', -- client's reported user + host, same as SESSION_USER(), SYSTEM_USER() 26 | CURRENT_USER() AS 'Your Actual CURRENT_USER()' -- authenticated user name + host name - this is the one you want for debugging your mysql.user table configuration 27 | ; 28 | -------------------------------------------------------------------------------- /mysql_user_summary.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 01:51:14 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL show activity per user 17 | -- 18 | -- Requires MySQL 5.7+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 5.7, 8.0 22 | -- 23 | -- sys doesn't exist in MySQL <= 5.6 or MariaDB 10.5 24 | 25 | SELECT 26 | * 27 | FROM 28 | sys.user_summary 29 | ORDER BY 30 | statement_latency DESC; 31 | -------------------------------------------------------------------------------- /mysql_users.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 09:51:00 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List MySQL users ordered by host, user similar to auth resolution 17 | -- 18 | -- Requires MySQL 8.0+ 19 | -- Requires MariaDB N/A 20 | -- 21 | -- Tested on MySQL 8.0 22 | -- 23 | -- some fields don't exist on MySQL < 8.0 and MariaDB 10.0 - 10.5, must use mysql_users_pre8.sql instead 24 | 25 | SELECT 26 | host, 27 | user, 28 | max_connections, 29 | max_user_connections, 30 | password_expired, 31 | password_last_changed, 32 | password_lifetime, 33 | account_locked, 34 | password_reuse_time, 35 | password_require_current 36 | FROM 37 | mysql.user 38 | ORDER BY 39 | host, 40 | user; 41 | -------------------------------------------------------------------------------- /mysql_users_pre56.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 09:51:00 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List MySQL users ordered by host, user similar to auth resolution 17 | -- 18 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 19 | 20 | SELECT 21 | host, 22 | user, 23 | max_connections, 24 | max_user_connections 25 | -- this field doesn't exist in MySQL < 5.6 26 | -- password_expired 27 | -- these fields don't exist in MySQL < 8.0 / MariaDB 10.5 28 | -- password_last_changed, 29 | -- password_lifetime, 30 | -- account_locked, 31 | -- password_reuse_time, 32 | -- password_require_current 33 | FROM 34 | mysql.user 35 | ORDER BY 36 | host, 37 | user; 38 | -------------------------------------------------------------------------------- /mysql_users_pre8.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 09:51:00 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List MySQL users ordered by host, user similar to auth resolution 17 | -- 18 | -- Requires MySQL 5.6+ 19 | -- Requires MariaDB 10 20 | -- 21 | -- Tested on MySQL 5.6, 5.7, 8.0 and MariaDB 10.0 - 10.5 22 | 23 | SELECT 24 | host, 25 | user, 26 | max_connections, 27 | max_user_connections, 28 | -- not available on MySQL 5.5 29 | password_expired 30 | -- these fields don't exist in MySQL < 8.0 / MariaDB 10 31 | -- password_last_changed, 32 | -- password_lifetime, 33 | -- account_locked, 34 | -- password_reuse_time, 35 | -- password_require_current 36 | FROM 37 | mysql.user 38 | ORDER BY 39 | host, 40 | user; 41 | -------------------------------------------------------------------------------- /mysql_views.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-08 09:54:19 +0100 (Sat, 08 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL views across schemas (useful for finding views) 17 | -- 18 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 19 | 20 | SELECT 21 | -- table_catalog, 22 | table_schema, 23 | table_name, 24 | IS_UPDATABLE, 25 | DEFINER, 26 | SECURITY_TYPE 27 | FROM 28 | information_schema.views 29 | ORDER BY 30 | table_schema, 31 | table_name; 32 | -------------------------------------------------------------------------------- /mysql_views_system.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 02:45:44 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- MySQL system views 17 | -- 18 | -- Tested on MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 5.5, 10.0 - 10.5 19 | 20 | SELECT 21 | table_schema, 22 | table_name, 23 | table_type, 24 | engine, 25 | table_rows, 26 | avg_row_length, 27 | data_length, 28 | max_data_length, 29 | index_length, 30 | data_free, 31 | auto_increment, 32 | create_time, 33 | update_time, 34 | table_comment 35 | FROM 36 | information_schema.tables 37 | WHERE 38 | table_type = 'SYSTEM VIEW' 39 | ORDER BY 40 | table_schema, 41 | table_name; 42 | -------------------------------------------------------------------------------- /oracle_checkpoints.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2008-04-28 21:36:48 +0100 (Mon, 28 Apr 2008) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Checkpoints on DBF data files (relevant for DBA recoveries) 17 | -- 18 | -- Tested on Oracle 9i, 10g, 11g, 19c 19 | 20 | SELECT 21 | NAME, 22 | checkpoint_time 23 | FROM 24 | v$datafile 25 | ORDER BY 26 | checkpoint_time; 27 | -------------------------------------------------------------------------------- /oracle_datafiles_checkpoint_change.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2008-04-28 21:36:48 +0100 (Mon, 28 Apr 2008) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle Show the Checkpoints on the DBF data files (important for DBA recoveries) 17 | -- 18 | -- Tested on Oracle 9i, 10g, 11g, 19c 19 | 20 | SELECT 21 | checkpoint_change#, 22 | file# 23 | FROM 24 | v$datafile 25 | ORDER BY 26 | checkpoint_change#; 27 | -------------------------------------------------------------------------------- /oracle_datafiles_checkpoint_time.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2008-04-28 21:36:48 +0100 (Mon, 28 Apr 2008) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle Show Checkpoint Dates on DBF data files (relevant for DBA recoveries) 17 | -- 18 | -- Tested on Oracle 9i, 10g, 11g, 19c 19 | 20 | 21 | SELECT 22 | checkpoint_time, 23 | file# 24 | FROM 25 | v$datafile 26 | ORDER BY 27 | checkpoint_time; 28 | -------------------------------------------------------------------------------- /oracle_datafiles_lastchanged.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2008-04-28 21:36:48 +0100 (Mon, 28 Apr 2008) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle Show the system change number (SCN) 17 | -- 18 | -- Tested on Oracle 9i, 10g, 11g, 19c 19 | 20 | SELECT 21 | last_change#, 22 | file# 23 | FROM 24 | v$datafile 25 | ORDER BY 26 | last_change#; 27 | -------------------------------------------------------------------------------- /oracle_datafiles_unrecoverable_change.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2008-04-28 21:36:48 +0100 (Mon, 28 Apr 2008) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle 17 | -- 18 | -- Tested on Oracle 9i, 10g, 11g, 19c 19 | 20 | SELECT 21 | unrecoverable_change#, 22 | file# 23 | FROM 24 | v$datafile 25 | ORDER BY 26 | unrecoverable_change#; 27 | -------------------------------------------------------------------------------- /oracle_rds_ddl_tablespace.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-18 01:25:03 +0400 (Fri, 18 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - get the Tablespace DDL 17 | 18 | SELECT 19 | -- USERS tablespace was case sensitive 20 | dbms_metadata.get_ddl('TABLESPACE','USERS') 21 | FROM dual; 22 | -------------------------------------------------------------------------------- /oracle_recent_queries.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-12 06:17:50 +0300 (Sat, 12 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Recent Queries 17 | -- 18 | -- Tested on Oracle 19c 19 | 20 | SELECT 21 | sql_text, 22 | executions 23 | FROM 24 | v$sql 25 | ORDER BY 26 | last_active_time 27 | DESC; 28 | -------------------------------------------------------------------------------- /oracle_show_dba_recyclebin.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-11 03:50:00 +0300 (Fri, 11 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show All Users Recyclebins via DBA Recyclebin 17 | -- 18 | -- Tested on Oracle 19c 19 | 20 | SELECT 21 | owner, 22 | object_name, 23 | original_name, 24 | type, 25 | droptime, 26 | space 27 | FROM 28 | dba_recyclebin 29 | ORDER BY 30 | owner, 31 | droptime 32 | DESC; 33 | -------------------------------------------------------------------------------- /oracle_show_sessions_using_temp_tablespace.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-17 23:06:57 +0400 (Thu, 17 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show User Sessions Using Temporary Tablespace 17 | -- 18 | -- Check there are none before dropping an old temp tablespace file to avoid disruptions 19 | -- 20 | -- Tested on Oracle 19c 21 | 22 | SELECT 23 | s.sid, 24 | s.username, 25 | t.tablespace, 26 | t.blocks, 27 | t.segfile#, 28 | t.segblk#, 29 | t.contents, 30 | t.sql_id 31 | FROM 32 | v$sort_usage t, 33 | v$session s 34 | WHERE 35 | t.session_addr = s.saddr; 36 | -------------------------------------------------------------------------------- /oracle_table_segments_for_schema.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-11 03:24:48 +0300 (Fri, 11 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Tables' Segments Size in a given Tablespace 17 | -- 18 | -- Tested on Oracle 19c 19 | 20 | SELECT 21 | segment_name, 22 | segment_type, 23 | tablespace_name, 24 | bytes/1024/1024/1024 AS size_gb, 25 | blocks 26 | FROM 27 | dba_segments 28 | WHERE 29 | owner = 'USERS' -- XXX: Edit this 30 | AND 31 | segment_type = 'TABLE' 32 | AND 33 | blocks > 8 34 | -- to look at only specific tables 35 | -- AND 36 | --segment_name IN 37 | --('MY_TABLE_1', 38 | -- 'MY_TABLE_2', 39 | -- 'MY_TABLE_3', 40 | -- 'MY_TABLE_4') 41 | ORDER BY 42 | size_gb DESC; 43 | -------------------------------------------------------------------------------- /oracle_table_shrink_candidates.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-11 03:24:48 +0300 (Fri, 11 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Table Candidates to Move / Shrink in a given Tablespace 'USERS' 17 | -- 18 | -- where the tables are over 20% utilized 19 | -- 20 | -- Tested on Oracle 19c 21 | 22 | SELECT 23 | segment_name, 24 | segment_type, 25 | ROUND(SUM(bytes)/1024/1024/1024, 2) AS size_gb 26 | FROM 27 | dba_segments 28 | WHERE 29 | tablespace_name = 'USERS' -- XXX: Edit 30 | GROUP BY 31 | segment_name, 32 | segment_type 33 | HAVING 34 | SUM(bytes)/1024/1024/1024 > 1 35 | ORDER BY 36 | size_gb DESC; 37 | -------------------------------------------------------------------------------- /oracle_table_space.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-11 03:24:48 +0300 (Fri, 11 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Tables' Space Used vs Free and Free Percentage 17 | -- 18 | -- where the tables are over 20% utilized 19 | -- 20 | -- Calculations assume an 8KB block size, which you should verify like this: 21 | -- 22 | -- SELECT value FROM v$parameter WHERE name = 'db_block_size'; 23 | -- 24 | -- Tested on Oracle 19c 25 | 26 | SELECT 27 | owner, 28 | table_name, 29 | -- each block is 8KB, multiply it to GB, round to two decimal places 30 | ROUND(blocks * 8 / 1024 / 1024, 2) AS total_gb, 31 | -- estimate data size from rows vs average row size, round to two decimal places 32 | ROUND(num_rows * avg_row_len / 1024 / 1024 / 1024, 2) AS actual_data_gb, 33 | -- estimate free space by subtracting the two above calculations 34 | ROUND((blocks * 8 / 1024 / 1024) - (num_rows * avg_row_len / 1024 / 1024 / 1024), 2) AS free_space_gb, 35 | -- calculate free space percentage from the above three calculations 36 | ROUND( 37 | ( (blocks * 8 / 1024 / 1024) - (num_rows * avg_row_len / 1024 / 1024 / 1024) ) / 38 | (blocks * 8 / 1024 / 1024) * 100, 2) AS free_space_pct 39 | FROM 40 | dba_tables 41 | WHERE 42 | blocks > 0 43 | AND 44 | num_rows > 0 45 | AND 46 | ((blocks * 8 / 1024 / 1024) - (num_rows * avg_row_len / 1024 / 1024 / 1024)) / 47 | (blocks * 8 / 1024 / 1024) > 0.2 -- TUNE: currently only showing tables over 20% utilized 48 | AND 49 | owner NOT IN 50 | ('SYS', 'SYSTEM', 'SYSAUX', 'RDSADMIN') 51 | ORDER BY 52 | free_space_gb DESC, 53 | total_gb DESC; 54 | -------------------------------------------------------------------------------- /oracle_table_space_for_schema.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-11 03:24:48 +0300 (Fri, 11 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Tables' Space Used vs Free and Free Percentage in a given Tablespace 17 | -- 18 | -- for tables over 20% utilized 19 | -- 20 | -- Calculations assume an 8KB block size, which you should verify like this: 21 | -- 22 | -- SELECT value FROM v$parameter WHERE name = 'db_block_size'; 23 | -- 24 | -- Tested on Oracle 19c 25 | 26 | SELECT 27 | t.owner, 28 | t.table_name, 29 | -- each block is 8KB, multiply it to GB, round to two decimal places 30 | ROUND(t.blocks * 8 / 1024 / 1024, 2) AS total_gb_from_tables, 31 | -- estimate from segments 32 | ROUND(s.bytes / 1024 / 1024 / 1024, 2) AS total_gb_from_segments, 33 | -- estimate data size from rows vs average row size, round to two decimal places 34 | ROUND(t.num_rows * t.avg_row_len / 1024 / 1024 / 1024, 2) AS actual_data_gb, 35 | -- estimate free space by subtracting the two above calculations 36 | ROUND(((t.blocks * 8 / 1024) - (t.num_rows * t.avg_row_len / 1024 / 1024)) / 1024, 2) AS free_space_gb, 37 | -- calculate free space percentage from the above three calculations 38 | ROUND( 39 | ( (t.blocks * 8) - (t.num_rows * t.avg_row_len / 1024) ) / 40 | (t.blocks * 8) * 100, 2) AS free_space_pct, 41 | t.last_analyzed 42 | FROM 43 | dba_tables t 44 | JOIN 45 | dba_segments s 46 | ON 47 | t.owner = s.owner 48 | AND 49 | t.table_name = s.segment_name 50 | WHERE 51 | t.blocks > 0 52 | AND 53 | t.num_rows > 0 54 | AND 55 | -- TUNE: currently only showing tables over 20% utilized 56 | ((t.blocks * 8 / 1024) - (t.num_rows * t.avg_row_len / 1024 / 1024)) / (t.blocks * 8 / 1024) > 0.2 57 | AND 58 | t.owner = 'USERS' -- XXX: Change this to your owner schema 59 | ORDER BY 60 | free_space_gb DESC, 61 | total_gb_from_segments DESC; 62 | -------------------------------------------------------------------------------- /oracle_tablespace_datafiles_sum_vs_max_autoextend.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-18 01:18:50 +0400 (Fri, 18 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Tablespace Datafiles Sum vs Max and AutoExtend 17 | 18 | 19 | SELECT 20 | tablespace_name, 21 | ROUND(SUM(BYTES)/(1024*1024*1024),2) Total_GB, 22 | ROUND(MAXBYTES/(1024*1024*1024),2) Max_GB, 23 | autoextensible 24 | FROM 25 | dba_data_files 26 | GROUP BY 27 | tablespace_name, 28 | maxbytes, 29 | autoextensible; 30 | -------------------------------------------------------------------------------- /oracle_tablespace_datafiles_sum_vs_max_autoextend_temp.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-18 01:18:50 +0400 (Fri, 18 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Temp Tablespace Datafiles Sum vs Max and AutoExtend 17 | 18 | 19 | SELECT 20 | tablespace_name, 21 | ROUND(SUM(BYTES)/(1024*1024*1024),2) Total_GB, 22 | ROUND(MAXBYTES/(1024*1024*1024),2) Max_GB, 23 | autoextensible 24 | FROM 25 | dba_temp_files 26 | GROUP BY 27 | tablespace_name, 28 | maxbytes, 29 | autoextensible; 30 | -------------------------------------------------------------------------------- /oracle_tablespace_space.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-11 03:24:48 +0300 (Fri, 11 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Tablespace Size, Space Used vs Free in GB and as a Percentage 17 | -- 18 | -- Tested on Oracle 19c 19 | 20 | SELECT 21 | df.tablespace_name "Tablespace", 22 | df.bytes / (1024 * 1024 * 1024) "Size (GB)", 23 | (df.bytes - SUM(fs.bytes)) / (1024 * 1024 * 1024) "Used Space (GB)", 24 | ROUND(SUM(fs.bytes) / (1024 * 1024 * 1024), 2) "Free Space (GB)", 25 | ROUND(SUM(fs.bytes) / df.bytes * 100, 2) "Free Space %" 26 | FROM 27 | dba_free_space fs, 28 | (SELECT 29 | tablespace_name, 30 | SUM(bytes) bytes 31 | FROM 32 | dba_data_files 33 | GROUP BY 34 | tablespace_name) df 35 | WHERE 36 | fs.tablespace_name (+) = df.tablespace_name 37 | GROUP BY 38 | df.tablespace_name, 39 | df.bytes 40 | ORDER BY 41 | "Free Space (GB)" DESC, 42 | "Used Space (GB)" DESC; 43 | -------------------------------------------------------------------------------- /oracle_tablespace_space2.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-11 03:24:48 +0300 (Fri, 11 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Tablespace Size, Space Used GB and Percentage Used 17 | -- 18 | -- Calculations assume an 8KB block size, which you should verify like this: 19 | -- 20 | -- SELECT value FROM v$parameter WHERE name = 'db_block_size'; 21 | -- 22 | -- Tested on Oracle 19c 23 | 24 | SELECT 25 | tablespace_name "Tablespace", 26 | -- convert used_space in blocks to GB as each block is 8KB 27 | ROUND(used_space * 8 / 1024 / 1024, 2) AS "Used Space (GB)", 28 | -- convert tablespace_size in blocks to GB as each block is 8KB 29 | ROUND(tablespace_size * 8 / 1024 / 1024, 2) AS "Total Space (GB)", 30 | ROUND(used_percent, 2) AS "Used Space %" 31 | FROM 32 | dba_tablespace_usage_metrics 33 | ORDER BY 34 | "Used Space %" DESC; 35 | -------------------------------------------------------------------------------- /oracle_tablespace_undo_space.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2024-10-18 04:24:34 +0400 (Fri, 18 Oct 2024) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https///github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show Tablespace size of the Undo Tablespace 17 | -- 18 | -- Tested on Oracle 19c 19 | 20 | SELECT 21 | df.tablespace_name "Tablespace", 22 | df.bytes / (1024 * 1024 * 1024) "Size (GB)", 23 | (df.bytes - SUM(fs.bytes)) / (1024 * 1024 * 1024) "Used Space (GB)", 24 | ROUND(SUM(fs.bytes) / (1024 * 1024 * 1024), 2) "Free Space (GB)", 25 | ROUND(SUM(fs.bytes) / df.bytes * 100, 2) "Free Space %" 26 | FROM 27 | dba_free_space fs, 28 | (SELECT 29 | tablespace_name, 30 | SUM(bytes) bytes 31 | FROM 32 | dba_data_files 33 | GROUP BY 34 | tablespace_name) df 35 | WHERE 36 | fs.tablespace_name (+) = df.tablespace_name 37 | AND 38 | UPPER(fs.tablespace_name) LIKE '%UNDO%' 39 | GROUP BY 40 | df.tablespace_name, 41 | df.bytes 42 | ORDER BY 43 | "Free Space (GB)" DESC, 44 | "Used Space (GB)" DESC; 45 | -------------------------------------------------------------------------------- /oracle_user_sessions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2008-04-28 21:36:48 +0100 (Mon, 28 Apr 2008) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Oracle - Show User Sessions Status 17 | -- 18 | -- Tested on Oracle 9i, 10g, 11g, 19c 19 | 20 | SELECT 21 | username, 22 | sid, 23 | serial#, 24 | status 25 | FROM 26 | v$session; 27 | -------------------------------------------------------------------------------- /postgres_active_query_count.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 15:33:36 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL number of active queries 17 | -- 18 | -- If COUNT consistently > CPU Cores, then upgrade/scale 19 | -- 20 | -- Requires PostgreSQL >= 9.2 for state column in pg_stat_activity 21 | -- 22 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 23 | 24 | SELECT 25 | COUNT(*) as active_query_count 26 | FROM 27 | pg_stat_activity 28 | WHERE 29 | state='active'; 30 | -------------------------------------------------------------------------------- /postgres_backends_per_database.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-17 20:17:18 +0100 (Mon, 17 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL number of backends connected to each database 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | datname, 22 | numbackends 23 | FROM 24 | pg_catalog.pg_stat_database 25 | ORDER BY 26 | numbackends DESC, 27 | datname ASC; 28 | -------------------------------------------------------------------------------- /postgres_blocked_queries.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 18:53:54 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Lists PostgreSQL queries blocked along with the pids of those holding the locks blocking them 17 | -- 18 | -- Requires PostgreSQL >= 9.6 19 | -- 20 | -- Tested on PostgreSQL 9.6+, 10.x 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | pid, 24 | usename, 25 | pg_blocking_pids(pid) AS blocked_by_pids, 26 | query AS blocked_query 27 | FROM 28 | pg_stat_activity 29 | WHERE 30 | cardinality(pg_blocking_pids(pid)) > 0; 31 | -------------------------------------------------------------------------------- /postgres_columns_null.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-10-20 11:01:44 +0100 (Tue, 20 Oct 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Finds PostgreSQL columns that contain only NULLs 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | 21 | SELECT 22 | nspname, 23 | relname, 24 | attname, 25 | typname, 26 | (stanullfrac*100)::int AS null_percent 27 | FROM 28 | pg_class c 29 | JOIN 30 | pg_namespace ns 31 | ON 32 | (ns.oid=relnamespace) 33 | JOIN 34 | pg_attribute 35 | ON 36 | (c.oid=attrelid) 37 | JOIN 38 | pg_type t 39 | ON 40 | (t.oid=atttypid) 41 | JOIN 42 | pg_statistic 43 | ON 44 | (c.oid=starelid AND staattnum=attnum) 45 | WHERE 46 | (stanullfrac*100)::int = 100 47 | AND 48 | nspname NOT LIKE E'pg\\_%' 49 | AND 50 | nspname != 'information_schema' 51 | AND 52 | relkind = 'r' 53 | AND 54 | NOT attisdropped 55 | AND 56 | attstattarget != 0 57 | -- AND 58 | --reltuples >= 100 59 | ORDER BY 60 | nspname, 61 | relname, 62 | attname; 63 | -------------------------------------------------------------------------------- /postgres_columns_useless.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-10-20 11:01:44 +0100 (Tue, 20 Oct 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Finds useless PostgreSQL columns containing only a single value 17 | -- 18 | -- eg. all NULLs or a redundant non-distinguishing field 19 | -- 20 | -- Only returns columns with over 100 rows 21 | -- 22 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 23 | 24 | 25 | SELECT 26 | nspname, 27 | relname, 28 | attname, 29 | typname, 30 | (stanullfrac*100)::int AS null_percent, 31 | case 32 | when stadistinct >= 0 33 | then stadistinct 34 | else 35 | abs(stadistinct)*reltuples 36 | end AS "distinct", 37 | case 1 38 | when stakind1 39 | then stavalues1 40 | when stakind2 41 | then stavalues2 42 | end AS "values" 43 | FROM 44 | pg_class c 45 | JOIN 46 | pg_namespace ns 47 | ON 48 | (ns.oid=relnamespace) 49 | JOIN 50 | pg_attribute 51 | ON 52 | (c.oid=attrelid) 53 | JOIN 54 | pg_type t 55 | ON 56 | (t.oid=atttypid) 57 | JOIN 58 | pg_statistic 59 | ON 60 | (c.oid=starelid AND staattnum=attnum) 61 | WHERE 62 | nspname NOT LIKE E'pg\\_%' 63 | AND 64 | nspname != 'information_schema' 65 | AND 66 | relkind = 'r' 67 | AND 68 | NOT attisdropped 69 | AND 70 | attstattarget != 0 71 | AND 72 | reltuples >= 100 73 | AND 74 | stadistinct BETWEEN 0 AND 1 75 | ORDER BY 76 | nspname, 77 | relname, 78 | attname; 79 | -------------------------------------------------------------------------------- /postgres_databases_by_size.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 14:54:12 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL databases by size descending 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | datname, 22 | pg_size_pretty(pg_database_size(datname)) 23 | FROM 24 | pg_database 25 | ORDER 26 | BY pg_database_size(datname) DESC; 27 | -------------------------------------------------------------------------------- /postgres_databases_by_size_if_accessible.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:49:13 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL databases by size descending (only ones you can access) 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | d.datname AS Name, 22 | pg_catalog.pg_get_userbyid(d.datdba) AS Owner, 23 | CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') 24 | THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname)) 25 | ELSE 'No Access' 26 | END AS SIZE 27 | FROM 28 | pg_catalog.pg_database d 29 | ORDER BY 30 | CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT') 31 | THEN pg_catalog.pg_database_size(d.datname) 32 | ELSE NULL 33 | END 34 | DESC; 35 | -------------------------------------------------------------------------------- /postgres_dirs.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 10:34:23 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Useful PostgreSQL dir info 17 | -- 18 | -- Requires PostgreSQL >= 11.9 19 | -- 20 | -- Tested on PostgreSQL 11.9, 12.x, 13.0 21 | 22 | SELECT 23 | current_setting('config_file') AS "config_file", 24 | current_setting('hba_file') AS "hba_file", 25 | current_setting('ident_file') AS "ident_file"; 26 | 27 | SELECT 28 | current_setting('data_directory') AS "data_directory", 29 | current_setting('external_pid_file') AS "external_pid_file"; 30 | 31 | SELECT 32 | current_setting('unix_socket_directories') AS "unix_socket_directories", 33 | current_setting('unix_socket_permissions') AS "unix_socket_permissions", 34 | current_setting('unix_socket_group') AS "unix_socket_group"; 35 | 36 | SELECT 37 | -- not available on PostgreSQL < 10 38 | pg_current_logfile(), 39 | current_setting('log_directory') AS "log_directory", -- log 40 | current_setting('log_filename') AS "log_filename"; -- postgresql-%Y-%m-%d_%H%M%S.log 41 | 42 | SELECT 43 | -- CASE WHEN pg_current_logfile() IS NOT NULL THEN pg_ls_logdir() END AS pg_ls_logdir, 44 | pg_ls_waldir(), 45 | -- not available on PostgreSQL <= 11.8 46 | pg_ls_archive_statusdir(), 47 | pg_ls_tmpdir(); 48 | -------------------------------------------------------------------------------- /postgres_dirs_pre10.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 10:34:23 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Useful PostgreSQL dir info 17 | -- 18 | -- Requires PostgreSQL >= 10 19 | -- 20 | -- Tested on PostgreSQL 9.x, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | current_setting('config_file') AS "config_file", 24 | current_setting('hba_file') AS "hba_file", 25 | current_setting('ident_file') AS "ident_file"; 26 | 27 | SELECT 28 | current_setting('data_directory') AS "data_directory", 29 | current_setting('external_pid_file') AS "external_pid_file"; 30 | 31 | SELECT 32 | -- not available on PostgreSQL < 9.3 33 | --current_setting('unix_socket_directories') AS "unix_socket_directories", 34 | current_setting('unix_socket_permissions') AS "unix_socket_permissions", 35 | current_setting('unix_socket_group') AS "unix_socket_group"; 36 | 37 | SELECT 38 | -- not available on PostgreSQL < 10 39 | --pg_current_logfile(), 40 | current_setting('log_directory') AS "log_directory", -- log 41 | current_setting('log_filename') AS "log_filename"; -- postgresql-%Y-%m-%d_%H%M%S.log 42 | 43 | --SELECT 44 | -- CASE WHEN pg_current_logfile() IS NOT NULL THEN pg_ls_logdir() END AS pg_ls_logdir, 45 | -- not available on PostgreSQL < 10 46 | --pg_ls_waldir() 47 | -- not available on PostgreSQL <= 11.8 48 | --pg_ls_archive_statusdir(), 49 | --pg_ls_tmpdir(); 50 | ; 51 | -------------------------------------------------------------------------------- /postgres_dirs_pre11.9.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 10:34:23 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Useful PostgreSQL dir info 17 | -- 18 | -- Requires PostgreSQL >= 10 19 | -- 20 | -- Tested on PostgreSQL 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | current_setting('config_file') AS "config_file", 24 | current_setting('hba_file') AS "hba_file", 25 | current_setting('ident_file') AS "ident_file"; 26 | 27 | SELECT 28 | current_setting('data_directory') AS "data_directory", 29 | current_setting('external_pid_file') AS "external_pid_file"; 30 | 31 | SELECT 32 | current_setting('unix_socket_directories') AS "unix_socket_directories", 33 | current_setting('unix_socket_permissions') AS "unix_socket_permissions", 34 | current_setting('unix_socket_group') AS "unix_socket_group"; 35 | 36 | SELECT 37 | -- not available on PostgreSQL < 10 38 | pg_current_logfile(), 39 | current_setting('log_directory') AS "log_directory", -- log 40 | current_setting('log_filename') AS "log_filename"; -- postgresql-%Y-%m-%d_%H%M%S.log 41 | 42 | SELECT 43 | -- CASE WHEN pg_current_logfile() IS NOT NULL THEN pg_ls_logdir() END AS pg_ls_logdir, 44 | pg_ls_waldir() 45 | -- not available on PostgreSQL <= 11.8 46 | --pg_ls_archive_statusdir(), 47 | --pg_ls_tmpdir(); 48 | ; 49 | -------------------------------------------------------------------------------- /postgres_grant_select_all_tables.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2021-02-01 17:33:17 +0000 (Mon, 01 Feb 2021) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- 3rd party ETL tools often need a postgres user account with SELECT on all tables in a database, and this also handles granting it to future tables 17 | -- 18 | -- XXX: replace 'fivetran' with the postgres user account used by the 3rd party ETL tool 19 | -- 20 | -- https://fivetran.com/docs/databases/postgresql/gcs-setup-guide?embed=true#grantreadonlyaccess 21 | -- 22 | -- Requires PostgreSQL 9.0+ 23 | -- 24 | -- Tested on PostgreSQL 9.x, 10.x, 11.x, 12.x, 13.0 25 | 26 | CREATE USER fivetran; 27 | 28 | GRANT USAGE ON SCHEMA "public" TO fivetran; 29 | GRANT SELECT ON ALL TABLES IN SCHEMA "public" TO fivetran; 30 | ALTER DEFAULT PRIVILEGES IN SCHEMA "public" GRANT SELECT ON TABLES TO fivetran; 31 | -------------------------------------------------------------------------------- /postgres_idle_sessions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 18:34:44 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List PostgreSQL stale connections that have been idle > 10 mins 17 | -- 18 | -- Can be used to kill connections using the pids returned, eg. 19 | -- 20 | -- SELECT pg_terminate_backend(pid) 21 | -- 22 | -- Requires PostgreSQL 9.2+ 23 | -- 24 | -- Tested on PostgreSQL 9.2, 10.x, 11.x, 12.x, 13.0 25 | 26 | SELECT 27 | rank() over (partition by client_addr order by backend_start ASC) as rank, 28 | pid, 29 | backend_start, 30 | query_start, 31 | state_change, 32 | datname, 33 | usename, 34 | client_addr 35 | FROM 36 | pg_stat_activity 37 | WHERE 38 | -- don't kill yourself 39 | pid <> pg_backend_pid() 40 | -- AND 41 | -- don't kill your admin tools 42 | --application_name !~ '(?:psql)|(?:pgAdmin.+)' 43 | -- AND 44 | --usename not in ('postgres') 45 | AND 46 | query in ('') 47 | AND 48 | ( 49 | (current_timestamp - query_start) > interval '10 minutes' 50 | OR 51 | (query_start IS NULL AND (current_timestamp - backend_start) > interval '10 minutes') 52 | ) 53 | ; 54 | -------------------------------------------------------------------------------- /postgres_idle_sessions_current_db_kill.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:00:35 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Kills idle PostgreSQL sessions for the currently select DB that haven't been used in > 15 minutes 17 | -- 18 | -- Requires PostgreSQL 9.2+ 19 | -- 20 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | pg_terminate_backend(pid) 24 | FROM 25 | pg_stat_activity 26 | WHERE 27 | -- don't kill yourself 28 | pid <> pg_backend_pid() 29 | -- AND 30 | -- don't kill your admin tools 31 | --application_name !~ '(?:psql)|(?:pgAdmin.+)' 32 | -- AND 33 | --usename not in ('postgres') 34 | AND 35 | datname = current_database() 36 | AND 37 | query in ('') 38 | AND 39 | state in ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled') 40 | AND 41 | --state_change < current_timestamp - INTERVAL '15' MINUTE; 42 | ( 43 | (current_timestamp - query_start) > interval '15 minutes' 44 | OR 45 | (query_start IS NULL AND (current_timestamp - backend_start) > interval '15 minutes') 46 | ) 47 | ; 48 | -------------------------------------------------------------------------------- /postgres_idle_sessions_current_db_kill_pre92.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:00:35 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Kills idle PostgreSQL sessions for the currently select DB that haven't been used in > 15 minutes 17 | -- 18 | -- Requires PostgreSQL <= 9.1 19 | -- 20 | -- Tested on PostgreSQL 8.4, 9.0, 9.1 21 | 22 | SELECT 23 | pg_terminate_backend(procpid) 24 | FROM 25 | pg_stat_activity 26 | WHERE 27 | -- don't kill yourself 28 | procpid <> pg_backend_pid() 29 | -- AND 30 | -- don't kill your admin tools 31 | --application_name !~ '(?:psql)|(?:pgAdmin.+)' 32 | -- AND 33 | --usename not in ('postgres') 34 | AND 35 | datname = current_database() 36 | AND 37 | current_query in ('') 38 | AND 39 | waiting 40 | --state in ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled') 41 | AND 42 | --state_change < current_timestamp - INTERVAL '15' MINUTE; 43 | ( 44 | (current_timestamp - query_start) > interval '15 minutes' 45 | OR 46 | (query_start IS NULL AND (current_timestamp - backend_start) > interval '15 minutes') 47 | ) 48 | ; 49 | -------------------------------------------------------------------------------- /postgres_idle_sessions_kill.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:00:35 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Kills idle PostgreSQL sessions that haven't been used in > 15 minutes 17 | -- 18 | -- Requires PostgreSQL >= 9.2 19 | -- 20 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | pg_terminate_backend(pid) 24 | FROM 25 | pg_stat_activity 26 | WHERE 27 | -- don't kill yourself 28 | pid <> pg_backend_pid() 29 | -- AND 30 | -- don't kill your admin tools 31 | --application_name !~ '(?:psql)|(?:pgAdmin.+)' 32 | -- AND 33 | --usename not in ('postgres') 34 | AND 35 | query in ('') 36 | AND 37 | state in ('idle', 'idle in transaction', 'idle in transaction (aborted)', 'disabled') 38 | AND 39 | --state_change < current_timestamp - INTERVAL '15' MINUTE; 40 | ( 41 | (current_timestamp - query_start) > interval '15 minutes' 42 | OR 43 | (query_start IS NULL AND (current_timestamp - backend_start) > interval '15 minutes') 44 | ) 45 | ; 46 | -------------------------------------------------------------------------------- /postgres_idle_sessions_pre92.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 18:34:44 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List PostgreSQL stale connections that have been idle > 10 mins 17 | -- 18 | -- Can be used to kill connections using the procpids returned, eg. 19 | -- 20 | -- SELECT pg_terminate_backend(procpid) 21 | -- 22 | -- Requires PostgreSQL <= 9.1 23 | -- 24 | -- Tested on PostgreSQL 8.4, 9.0, 9.1 25 | 26 | SELECT 27 | rank() over (partition by client_addr order by backend_start ASC) as rank, 28 | procpid, 29 | backend_start, 30 | query_start, 31 | waiting, 32 | datname, 33 | usename, 34 | client_addr 35 | FROM 36 | pg_stat_activity 37 | WHERE 38 | -- don't kill yourself 39 | procpid <> pg_backend_pid() 40 | -- AND 41 | -- don't kill your admin tools 42 | --application_name !~ '(?:psql)|(?:pgAdmin.+)' 43 | -- AND 44 | --usename not in ('postgres') 45 | AND 46 | current_query in ('') 47 | AND 48 | ( 49 | (current_timestamp - query_start) > interval '10 minutes' 50 | OR 51 | (query_start IS NULL AND (current_timestamp - backend_start) > interval '10 minutes') 52 | ) 53 | ; 54 | -------------------------------------------------------------------------------- /postgres_index_cardinality.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:18:01 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL index cardinality 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | relname, 22 | --relkind, 23 | reltuples AS "cardinality (reltuples)", 24 | relpages 25 | FROM 26 | pg_class 27 | WHERE 28 | relkind = 'i' 29 | -- AND 30 | --relname LIKE 'someprefix%'; 31 | AND 32 | relname NOT ILIKE 'pg_%' 33 | ORDER BY 34 | 2 DESC; 35 | -------------------------------------------------------------------------------- /postgres_index_cardinality_with_schema_name.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:18:01 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL index cardinality with schema name 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | schema_name, 22 | relname, 23 | reltuples AS "cardinality (reltuples)", 24 | relpages 25 | FROM ( 26 | SELECT 27 | pg_catalog.pg_namespace.nspname AS schema_name, 28 | relname, 29 | reltuples, 30 | relpages 31 | FROM 32 | pg_class 33 | JOIN 34 | pg_namespace ON relnamespace = pg_namespace.oid 35 | WHERE 36 | relkind = 'i' 37 | ) t 38 | WHERE 39 | schema_name NOT ILIKE 'pg_%' 40 | AND 41 | schema_name <> 'information_schema' 42 | -- AND 43 | -- relname LIKE 'someprefix%'; 44 | AND 45 | relname NOT ILIKE 'pg_%' 46 | ORDER BY 47 | 3 DESC, 48 | schema_name, 49 | relname; 50 | -------------------------------------------------------------------------------- /postgres_indexes_cache_hit_ratio.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 15:28:12 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Indexes Cache-hit Ratio 17 | -- 18 | -- should be closer to 1, eg. 0.99 19 | -- 20 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | SUM(idx_blks_read) AS idx_blks_read, 24 | SUM(idx_blks_hit) AS idx_blks_hit, 25 | SUM(idx_blks_hit) / 26 | GREATEST(SUM(idx_blks_hit) + SUM(idx_blks_read), 1)::float 27 | AS ratio 28 | FROM 29 | pg_statio_user_indexes; 30 | -------------------------------------------------------------------------------- /postgres_indexes_unused.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 23:48:47 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL unused indexes 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | relname AS table, 22 | indexrelname AS index, 23 | idx_scan, 24 | idx_tup_read, 25 | idx_tup_fetch, 26 | pg_size_pretty(pg_relation_size(indexrelname::regclass)) 27 | FROM 28 | pg_stat_all_indexes 29 | WHERE 30 | schemaname = 'public' 31 | AND 32 | idx_scan = 0 33 | AND 34 | idx_tup_read = 0 35 | AND 36 | idx_tup_fetch = 0 37 | ORDER BY 38 | pg_relation_size(indexrelname::regclass) DESC; 39 | -------------------------------------------------------------------------------- /postgres_last_analyze.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:30:04 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Analyze info for tables with rows modified since last analyze 17 | -- 18 | -- Requires PostgreSQL 9.4+ (see postgres_last_analyze_pre94.sql for earlier versions) 19 | -- 20 | -- Tested on PostgreSQL 9.4+, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | schemaname, 24 | relname, 25 | -- not available on PostgreSQL <= 9.3 26 | n_mod_since_analyze, 27 | last_analyze, 28 | last_autoanalyze, 29 | -- not available on PostgreSQL <= 9.0 30 | analyze_count, 31 | autoanalyze_count 32 | FROM pg_stat_user_tables 33 | ORDER BY 34 | n_mod_since_analyze DESC, 35 | last_analyze DESC, 36 | last_autoanalyze DESC; 37 | -------------------------------------------------------------------------------- /postgres_last_analyze_pre94.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:30:04 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Analyze info for tables with rows modified since last analyze 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | schemaname, 22 | relname, 23 | -- not available on PostgreSQL <= 9.3 24 | --n_mod_since_analyze, 25 | last_analyze, 26 | last_autoanalyze 27 | -- not available on PostgreSQL <= 9.0 28 | --analyze_count, 29 | --autoanalyze_count 30 | FROM pg_stat_user_tables 31 | ORDER BY 32 | --n_mod_since_analyze DESC; 33 | last_analyze DESC, 34 | last_autoanalyze DESC; 35 | -------------------------------------------------------------------------------- /postgres_last_vacuum.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:30:04 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Vacuum info for tables with deleted rows 17 | -- 18 | -- Requires PostgreSQL 9.1+ (see postgres_last_vacuum_pre91.sql for earlier versions) 19 | -- 20 | -- Tested on PostgreSQL 9.1+, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | schemaname, 24 | relname, 25 | n_live_tup, 26 | n_dead_tup, 27 | n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1)::float * 100 AS dead_percentage, 28 | last_vacuum, 29 | last_autovacuum, 30 | -- not available on PostgreSQL <= 9.0 31 | vacuum_count, 32 | autovacuum_count 33 | FROM pg_stat_user_tables 34 | WHERE 35 | n_dead_tup > 0 36 | ORDER BY 37 | n_dead_tup DESC, 38 | last_vacuum DESC, 39 | last_autovacuum DESC; 40 | -------------------------------------------------------------------------------- /postgres_last_vacuum_analyze.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:30:04 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Vacuum and Analyze info 17 | -- 18 | -- Requires PostgreSQL 9.4+ (see postgres_last_vacuum_analyze_pre94.sql for earlier versions) 19 | -- 20 | -- Tested on PostgreSQL 9.4+, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | schemaname, 24 | relname, 25 | n_live_tup, 26 | n_dead_tup, 27 | n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1)::float * 100 AS dead_percentage, 28 | n_mod_since_analyze, 29 | last_vacuum, 30 | last_autovacuum, 31 | last_analyze, 32 | last_autoanalyze, 33 | vacuum_count, 34 | autovacuum_count, 35 | analyze_count, 36 | autoanalyze_count 37 | FROM 38 | pg_stat_user_tables 39 | ORDER BY 40 | n_dead_tup DESC, 41 | n_mod_since_analyze DESC, 42 | last_vacuum DESC, 43 | last_analyze DESC, 44 | last_autovacuum DESC, 45 | last_autoanalyze DESC; 46 | -------------------------------------------------------------------------------- /postgres_last_vacuum_analyze_pre94.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:30:04 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Vacuum and Analyze info 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | schemaname, 22 | relname, 23 | n_live_tup, 24 | n_dead_tup, 25 | n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1)::float * 100 AS dead_percentage, 26 | -- not available on PostgreSQL <= 9.3 27 | --n_mod_since_analyze, 28 | last_vacuum, 29 | last_autovacuum, 30 | last_analyze, 31 | last_autoanalyze 32 | -- not available on PostgreSQL <= 9.0 33 | --vacuum_count, 34 | --autovacuum_count, 35 | -- not available on PostgreSQL <= 9.0 36 | --analyze_count, 37 | --autoanalyze_count 38 | FROM 39 | pg_stat_user_tables 40 | ORDER BY 41 | n_dead_tup DESC, 42 | last_vacuum DESC, 43 | last_analyze DESC, 44 | last_autovacuum DESC, 45 | last_autoanalyze DESC; 46 | -------------------------------------------------------------------------------- /postgres_last_vacuum_pre91.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:30:04 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Vacuum info for tables with deleted rows 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | schemaname, 22 | relname, 23 | n_live_tup, 24 | n_dead_tup, 25 | n_dead_tup / GREATEST(n_live_tup + n_dead_tup, 1)::float * 100 AS dead_percentage, 26 | last_vacuum, 27 | last_autovacuum 28 | -- not available on PostgreSQL <= 9.0 29 | --vacuum_count, 30 | --autovacuum_count 31 | FROM pg_stat_user_tables 32 | WHERE 33 | n_dead_tup > 0 34 | ORDER BY 35 | n_dead_tup DESC, 36 | last_vacuum DESC, 37 | last_autovacuum DESC; 38 | -------------------------------------------------------------------------------- /postgres_locks.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 18:16:55 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List PostgreSQL Locks 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | -- https://wiki.postgresql.org/wiki/Lock_Monitoring 21 | 22 | SELECT 23 | t.schemaname, 24 | t.relname, 25 | -- l.database, -- id number is less useful, take schemaname from join instead 26 | l.locktype, 27 | page, 28 | virtualtransaction, 29 | pid, 30 | mode, 31 | granted 32 | FROM 33 | pg_locks l, 34 | --pg_stat_user_tables t 35 | pg_stat_all_tables t 36 | WHERE 37 | l.relation = t.relid 38 | ORDER BY 39 | relation ASC; 40 | 41 | SELECT 42 | relation::regclass AS relation_regclass, 43 | * 44 | FROM 45 | pg_locks 46 | WHERE 47 | NOT granted; 48 | -------------------------------------------------------------------------------- /postgres_locks_blocked.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 18:16:55 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List PostgreSQL locks blocked 17 | -- 18 | -- Requires PostgreSQL 9.2+ 19 | -- 20 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 21 | 22 | -- https://wiki.postgresql.org/wiki/Lock_Monitoring 23 | 24 | SELECT 25 | blocked_locks.pid AS blocked_pid, 26 | blocked_activity.usename AS blocked_user, 27 | blocking_locks.pid AS blocking_pid, 28 | blocking_activity.usename AS blocking_user, 29 | blocked_activity.query AS blocked_statement, 30 | blocking_activity.query AS current_statement_in_blocking_process 31 | FROM 32 | pg_catalog.pg_locks AS blocked_locks 33 | JOIN 34 | pg_catalog.pg_stat_activity AS blocked_activity 35 | ON 36 | blocked_activity.pid = blocked_locks.pid 37 | JOIN 38 | pg_catalog.pg_locks AS blocking_locks 39 | ON 40 | blocking_locks.locktype = blocked_locks.locktype 41 | AND blocking_locks.database IS NOT DISTINCT FROM blocked_locks.database 42 | AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation 43 | AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page 44 | AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple 45 | AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid 46 | AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid 47 | AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid 48 | AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid 49 | AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid 50 | AND blocking_locks.pid != blocked_locks.pid 51 | JOIN 52 | pg_catalog.pg_stat_activity blocking_activity 53 | ON 54 | blocking_activity.pid = blocking_locks.pid 55 | WHERE 56 | NOT blocked_locks.granted; 57 | -------------------------------------------------------------------------------- /postgres_locks_blocked_application.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-07 00:43:13 +0100 (Fri, 07 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- List PostgreSQL locks blocked with application name 17 | -- 18 | -- Requires PostgreSQL 9.2+ 19 | -- 20 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 21 | 22 | -- https://wiki.postgresql.org/wiki/Lock_Monitoring 23 | 24 | SELECT 25 | blocked_locks.pid AS blocked_pid, 26 | blocked_activity.usename AS blocked_user, 27 | blocking_locks.pid AS blocking_pid, 28 | blocking_activity.usename AS blocking_user, 29 | blocked_activity.query AS blocked_statement, 30 | blocking_activity.query AS current_statement_in_blocking_process, 31 | blocked_activity.application_name AS blocked_application, 32 | blocking_activity.application_name AS blocking_application 33 | FROM 34 | pg_catalog.pg_locks blocked_locks 35 | JOIN 36 | pg_catalog.pg_stat_activity blocked_activity 37 | ON 38 | blocked_activity.pid = blocked_locks.pid 39 | JOIN 40 | pg_catalog.pg_locks blocking_locks 41 | ON 42 | blocking_locks.locktype = blocked_locks.locktype 43 | AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE 44 | AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation 45 | AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page 46 | AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple 47 | AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid 48 | AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid 49 | AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid 50 | AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid 51 | AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid 52 | AND blocking_locks.pid != blocked_locks.pid 53 | JOIN 54 | pg_catalog.pg_stat_activity blocking_activity 55 | ON 56 | blocking_activity.pid = blocking_locks.pid 57 | WHERE 58 | NOT blocked_locks.granted; 59 | -------------------------------------------------------------------------------- /postgres_locks_query_age.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-07 00:47:45 +0100 (Fri, 07 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL locks with query and age 17 | -- 18 | -- Requires PostgreSQL 9.2+ 19 | -- 20 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 21 | 22 | -- https://wiki.postgresql.org/wiki/Lock_Monitoring 23 | 24 | SELECT 25 | a.datname, 26 | l.relation::regclass, 27 | l.transactionid, 28 | l.mode, 29 | l.GRANTED, 30 | a.usename, 31 | a.query, 32 | a.query_start, 33 | age(now(), a.query_start) AS "age", 34 | a.pid 35 | FROM 36 | pg_stat_activity a 37 | JOIN 38 | pg_locks l 39 | ON 40 | l.pid = a.pid 41 | ORDER BY 42 | a.query_start; 43 | -------------------------------------------------------------------------------- /postgres_locks_query_age_pre92.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-07 00:47:45 +0100 (Fri, 07 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL locks with query and age for PostgreSQL <= 9.1 17 | -- 18 | -- Requires PostgreSQL <= 9.1 (for newer versions see postgres_locks_query_age.sql) 19 | -- 20 | -- Tested on PostgreSQL 8.4, 9.0, 9.1 21 | 22 | -- https://wiki.postgresql.org/wiki/Lock_Monitoring 23 | 24 | SELECT 25 | a.datname, 26 | c.relname, 27 | l.transactionid, 28 | l.mode, 29 | l.granted, 30 | a.usename, 31 | a.current_query, 32 | a.query_start, 33 | age(now(), a.query_start) as "age", 34 | a.procpid 35 | FROM 36 | pg_stat_activity a 37 | JOIN 38 | pg_locks l 39 | ON 40 | l.pid = a.procpid 41 | JOIN 42 | pg_class c 43 | ON 44 | c.oid = l.relation 45 | ORDER BY 46 | a.query_start; 47 | -------------------------------------------------------------------------------- /postgres_queries_slow.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:24:59 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL queries currently executing that have taken over 30 secs 17 | -- 18 | -- a current snapshot alternative to slow query log 19 | -- 20 | -- Requires PostgreSQL 9.6+ (see postgres_queries_slow_pre96.sql and postgres_queries_slow_pre92.sql) 21 | -- 22 | -- Tested on PostgreSQL 9.6+, 10x, 11.x, 12.x, 13.0 23 | 24 | SELECT 25 | now() - query_start as "runtime", 26 | usename, 27 | datname, 28 | -- not available on PostgreSQL < 9.6 29 | wait_event, 30 | -- not available on PostgreSQL < 9.2 31 | state, 32 | -- current_query on PostgreSQL < 9.2 33 | query 34 | FROM 35 | pg_stat_activity 36 | WHERE 37 | -- can't use 'runtime' here 38 | now() - query_start > '30 seconds'::interval 39 | ORDER BY 40 | runtime DESC; 41 | -------------------------------------------------------------------------------- /postgres_queries_slow_pre92.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:24:59 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL queries currently executing that have taken over 30 secs 17 | -- 18 | -- a current snapshot alternative to slow query log 19 | -- 20 | -- Requires PostgreSQL <= 9.1 21 | -- 22 | -- Tested on PostgreSQL 8.4, 9.0, 9.1 23 | 24 | SELECT 25 | now() - query_start as "runtime", 26 | usename, 27 | datname, 28 | -- not available on PostgreSQL < 9.6 29 | -- wait_event, 30 | waiting, 31 | -- not available on PostgreSQL < 9.2 32 | --state, 33 | current_query 34 | FROM 35 | pg_stat_activity 36 | WHERE 37 | -- can't use 'runtime' here 38 | now() - query_start > '30 seconds'::interval 39 | ORDER BY 40 | runtime DESC; 41 | -------------------------------------------------------------------------------- /postgres_queries_slow_pre96.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:24:59 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL queries currently executing that have taken over 30 secs 17 | -- 18 | -- a current snapshot alternative to slow query log 19 | -- 20 | -- Requires 9.2 <= PostgreSQL <= 9.5 21 | -- 22 | -- Tested on PostgreSQL 9.2, 9.3, 9.4, 9.5 23 | 24 | SELECT 25 | now() - query_start as "runtime", 26 | usename, 27 | datname, 28 | -- not available on PostgreSQL < 9.6 29 | -- wait_event, 30 | waiting, 31 | -- not available on PostgreSQL < 9.2 32 | state, 33 | query 34 | FROM 35 | pg_stat_activity 36 | WHERE 37 | -- can't use 'runtime' here 38 | now() - query_start > '30 seconds'::interval 39 | ORDER BY 40 | runtime DESC; 41 | -------------------------------------------------------------------------------- /postgres_query_cache_hit_ratio.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 16:22:42 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL queries cache-hit ratio from pg_stat_statements 17 | -- 18 | -- Requires PostgreSQL 9.1+ 19 | -- 20 | -- Tested on PostgreSQL 9.1+, 10.x, 11.x, 12.x, 13.0 21 | 22 | -- postgresql.conf needs before start: 23 | -- shared_preload_libraries = 'pg_stat_statements' 24 | -- 25 | CREATE EXTENSION IF NOT EXISTS pg_stat_statements; 26 | 27 | SELECT 28 | calls, 29 | rows, 30 | shared_blks_hit, 31 | shared_blks_read, 32 | -- using greatest() to avoid divide by zero error, by ensuring we divide by at least 1 33 | shared_blks_hit / 34 | GREATEST(shared_blks_hit + shared_blks_read, 1)::float AS shared_blks_hit_ratio, 35 | -- casting divisor to float to avoid getting integer maths returning zeros instead of fractional ratios 36 | local_blks_hit, 37 | local_blks_read, 38 | local_blks_hit / 39 | GREATEST(local_blks_hit + local_blks_read, 1)::float AS local_blks_hit_ratio, 40 | query 41 | FROM 42 | pg_stat_statements 43 | --ORDER BY rows DESC 44 | ORDER BY 45 | shared_blks_hit_ratio DESC, 46 | local_blks_hit_ratio DESC, 47 | rows DESC 48 | LIMIT 100; 49 | -------------------------------------------------------------------------------- /postgres_query_times.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 16:16:21 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL query times from pg_stat_statements 17 | -- 18 | -- Requires PostgreSQL 13+ 19 | -- 20 | -- For PostgreSQL 9.5 - 12.x use postgres_query_times_pre13.sql 21 | -- For PostgreSQL <= 9.4 use postgres_query_times_pre95.sql 22 | -- 23 | -- Tested on PostgreSQL 13.0 24 | 25 | -- postgresql.conf needs before start: 26 | -- shared_preload_libraries = 'pg_stat_statements' 27 | -- 28 | CREATE EXTENSION IF NOT EXISTS pg_stat_statements; 29 | 30 | SELECT 31 | calls, 32 | rows, 33 | ROUND((total_exec_time::numeric / 1000), 4) AS total_secs, 34 | -- newer versions of PostgreSQL have mean_exec_time field, don't need to calculate 35 | --ROUND((total_exec_time / 1000 / calls)::numeric, 4) AS average_secs, 36 | ROUND(mean_exec_time::numeric / 1000, 4) AS average_secs, 37 | ROUND(min_exec_time::numeric / 1000, 4) AS min_secs, 38 | ROUND(max_exec_time::numeric / 1000, 4) AS max_secs, 39 | ROUND(stddev_exec_time::numeric / 1000, 4) AS stddev_secs, 40 | query 41 | FROM 42 | pg_stat_statements 43 | ORDER BY 44 | average_secs DESC, 45 | calls DESC, 46 | rows DESC 47 | LIMIT 100; 48 | -------------------------------------------------------------------------------- /postgres_query_times_pre13.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 16:16:21 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL query times from pg_stat_statements 17 | -- 18 | -- Requires 9.5 <= PostgreSQL <= 12 19 | -- 20 | -- Tested on PostgreSQL 9.5+, 10.x, 11.x, 12.x 21 | 22 | -- postgresql.conf needs before start: 23 | -- shared_preload_libraries = 'pg_stat_statements' 24 | -- 25 | CREATE EXTENSION IF NOT EXISTS pg_stat_statements; 26 | 27 | SELECT 28 | calls, 29 | rows, 30 | ROUND((total_time::numeric / 1000), 4) AS total_secs, 31 | -- newer versions of PostgreSQL have mean_time field, don't need to calculate 32 | --ROUND((total_time / 1000 / calls)::numeric, 4) AS average_secs, 33 | ROUND(mean_time::numeric / 1000, 4) AS average_secs, 34 | ROUND(min_time::numeric / 1000, 4) AS min_secs, 35 | ROUND(max_time::numeric / 1000, 4) AS max_secs, 36 | ROUND(stddev_time::numeric / 1000, 4) AS stddev_secs, 37 | query 38 | FROM 39 | pg_stat_statements 40 | ORDER BY 41 | average_secs DESC, 42 | calls DESC, 43 | rows DESC 44 | LIMIT 100; 45 | -------------------------------------------------------------------------------- /postgres_query_times_pre95.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 16:16:21 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL query times from pg_stat_statements for PostgreSQL <= 9.4 17 | -- 18 | -- Also works on later versions up to PostgreSQL 12, but primarily written to be able to be able to query earlier PostgreSQL versions 19 | -- 20 | -- For PostgreSQL >= 9.5 recommend postgres_query_times_pre13.sql instead which returns more available information 21 | -- 22 | -- Requires 9.1 <= PostgreSQL <= 12 23 | -- 24 | -- Tested on PostgreSQL 9.1+, 10.x, 11.x, 12.x, 13.0 25 | 26 | -- postgresql.conf needs before start: 27 | -- shared_preload_libraries = 'pg_stat_statements' 28 | -- 29 | CREATE EXTENSION IF NOT EXISTS pg_stat_statements; 30 | 31 | SELECT 32 | calls, 33 | rows, 34 | ROUND(total_time::numeric / 1000, 4) AS total_secs, 35 | -- newer versions of PostgreSQL have mean_time field, but we have to calculate on PostgreSQL <= 9.4 36 | ROUND(total_time::numeric / 1000 / GREATEST(calls, 1), 4) AS average_secs, 37 | query 38 | FROM 39 | pg_stat_statements 40 | ORDER BY 41 | average_secs DESC, 42 | calls DESC, 43 | rows DESC 44 | LIMIT 100; 45 | -------------------------------------------------------------------------------- /postgres_recovery.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 11:36:42 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL recovery info 17 | -- 18 | -- Requires PostgreSQL 10+ 19 | -- 20 | -- Tested on PostgreSQL 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | pg_is_in_backup(), 24 | pg_is_in_recovery(), 25 | pg_backup_start_time(), 26 | -- the following recovery control functions can only be executed during recovery - to get just the above use postgres_funcs.sql 27 | ( CASE WHEN pg_is_in_recovery() THEN pg_is_wal_replay_paused() END) AS "pg_is_wal_replay_paused()", 28 | ( CASE WHEN pg_is_in_recovery() THEN pg_last_wal_receive_lsn() END) AS "pg_last_wal_receive_lsn()", 29 | ( CASE WHEN pg_is_in_recovery() THEN pg_last_wal_replay_lsn() END) AS "pg_last_wal_replay_lsn()", 30 | ( CASE WHEN pg_is_in_recovery() THEN pg_last_xact_replay_timestamp() END) AS "pg_last_xact_replay_timestamp()" 31 | ; 32 | -------------------------------------------------------------------------------- /postgres_running_queries.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 12:54:34 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Running queries for PostgreSQL >= 9.2 17 | -- 18 | -- Requires PostgreSQL 9.2+ 19 | -- 20 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | pid, 24 | age(clock_timestamp(), query_start), 25 | usename, 26 | application_name, 27 | query 28 | FROM 29 | pg_stat_activity 30 | WHERE 31 | state != 'idle' 32 | AND 33 | query NOT ILIKE '%pg_stat_activity%' 34 | ORDER BY 35 | query_start DESC; 36 | -------------------------------------------------------------------------------- /postgres_running_queries_pre92.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 12:52:58 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Running queries for PostgreSQL <= 9.1 17 | -- 18 | -- Requires PostgreSQL <= 9.1 19 | -- 20 | -- Tested on PostgreSQL 8.4, 9.0, 9.1 21 | 22 | SELECT 23 | procpid, 24 | age(clock_timestamp(), query_start), 25 | usename, 26 | current_query 27 | FROM 28 | pg_stat_activity 29 | WHERE 30 | current_query != '' 31 | AND 32 | current_query NOT ILIKE '%pg_stat_activity%' 33 | ORDER BY 34 | query_start DESC; 35 | -------------------------------------------------------------------------------- /postgres_sequences_restart_all.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:32:45 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Restarts all PostgreSQL sequences 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | 'ALTER SEQUENCE ' || relname || ' RESTART;' 22 | FROM 23 | pg_class 24 | WHERE 25 | relkind = 'S'; 26 | -------------------------------------------------------------------------------- /postgres_sessions.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:23:40 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL count of sessions in each state 17 | -- 18 | -- Requires PostgreSQL 10+ 19 | -- 20 | -- Tested on PostgreSQL 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | pid, 24 | usename, 25 | client_addr, 26 | client_hostname, 27 | client_port, 28 | backend_start, 29 | query_start, 30 | state, 31 | -- not available on PostgreSQL < 10 32 | backend_type 33 | FROM 34 | pg_stat_activity 35 | ORDER BY 36 | -- not available on PostgreSQL < 10 37 | backend_type; 38 | -------------------------------------------------------------------------------- /postgres_sessions_pre10.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:23:40 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL count of sessions in each state 17 | -- 18 | -- Requires 9.2 <= PostgreSQL < 10 19 | -- 20 | -- Tested on PostgreSQL 9.2 - 9.6, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | pid, 24 | usename, 25 | client_addr, 26 | client_hostname, 27 | client_port, 28 | backend_start, 29 | query_start, 30 | state 31 | FROM 32 | pg_stat_activity; 33 | -------------------------------------------------------------------------------- /postgres_sessions_state_count.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 00:23:40 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL count of sessions in each state 17 | -- 18 | -- Requires PostgreSQL 9.2+ 19 | -- 20 | -- Tested on PostgreSQL 9.2+, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | count(1), 24 | state 25 | FROM 26 | pg_stat_activity 27 | GROUP BY 28 | state 29 | ORDER BY 30 | 1 DESC; 31 | -------------------------------------------------------------------------------- /postgres_settings.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-12 10:36:55 +0100 (Wed, 12 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL running settings and those set in postgres.conf 17 | -- 18 | -- Requires PostgreSQL 9.5+ 19 | -- 20 | -- Tested on PostgreSQL 9.5, 9.6, 10.x, 11.x, 12.x, 13.0 21 | 22 | --\pset title 'pg_settings' 23 | \echo pg_settings: 24 | SELECT 25 | name, 26 | setting, 27 | unit, 28 | context 29 | FROM 30 | pg_settings; 31 | 32 | \echo 33 | 34 | --\pset title 'pg_file_settings' 35 | \echo pg_file_settings: 36 | SELECT 37 | * 38 | FROM 39 | pg_file_settings; 40 | -------------------------------------------------------------------------------- /postgres_settings_auth.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL authentication settings 17 | -- 18 | -- eg. password_encryption, krb_server_keyfile 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-AUTHENTICATION 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Connections and Authentication / Authentication 44 | category ILIKE '% / Authentication%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_autovacuum.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL auto vacuum settings 17 | -- 18 | -- eg. autovacuum, log_autovacuum_min_duration, autovacuum_naptime, autovacuum_max_workers, autovacuum_vacuum_threshold, autovacuum_analyze_threshold 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-autovacuum.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Autovacuum 44 | category ILIKE '%Autovacuum%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_client_connection.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL client connection default settings 17 | -- 18 | -- eg. client_min_messages, transaction_isolation / default_transaction_isolation, statement_timeout, lock_timeout, TimeZone, lc_ctype, server_encoding, search_path 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-client.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Client Connection Defaults / Statement Behavior 44 | -- Client Connection Defaults / Locale and Formatting 45 | -- Client Connection Defaults / Other Defaults 46 | -- Client Connection Defaults / Shared Library Preloading 47 | category ILIKE '%Client Connection Defaults%'; 48 | -------------------------------------------------------------------------------- /postgres_settings_compatibility.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL version and platform compatibility settings 17 | -- 18 | -- eg. backslash_quote, escape_string_warning, transform_null_equals 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-compatible.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Version and Platform Compatibility / Previous PostgreSQL Versions 44 | -- Version and Platform Compatibility / Other Platforms and Clients 45 | category ILIKE '%Compatibility%'; 46 | -------------------------------------------------------------------------------- /postgres_settings_connections.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL connection settings 17 | -- 18 | -- eg. listen_addresses, port, max_connections, unix sockets 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SETTINGS 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Connections and Authentication / Connection Settings 44 | category ILIKE '% / Connection Settings%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_developer.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL developer options settings 17 | -- 18 | -- Requires PostgreSQL 9.5+ 19 | -- 20 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 21 | 22 | -- https://www.postgresql.org/docs/12/runtime-config-developer.html 23 | 24 | SELECT 25 | name, 26 | setting, 27 | -- category, 28 | vartype, 29 | short_desc, 30 | -- enumvals, 31 | source, 32 | min_val, 33 | max_val, 34 | boot_val, 35 | reset_val, 36 | -- not available on PostgreSQL < 9.5 37 | pending_restart 38 | FROM 39 | pg_settings 40 | WHERE 41 | -- Developer Options 42 | category ILIKE '%Developer%'; 43 | -------------------------------------------------------------------------------- /postgres_settings_error_handling.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL error handling settings 17 | -- 18 | -- eg. exit_on_error, restart_after_crash 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-error-handling.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Error Handling 44 | category ILIKE '%Error%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_files.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL file location settings 17 | -- 18 | -- eg. config_file, hba_file, data_directory 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-file-locations.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- File Locations 44 | category ILIKE '%File Locations%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_locking.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL lock settings 17 | -- 18 | -- eg. deadlock_timeout, max_locks_per_transaction 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-locks.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Lock Management 44 | category ILIKE '%Lock%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_logging.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL logging settings 17 | -- 18 | -- eg. log_destination, logging_collector, log_rotation_age, log_filename (format), log_connections, log_hostname, log_disconnections, log_checkpoints, log_lock_waits etc. 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-logging.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Reporting and Logging / What to Log 44 | -- broader '%Log%' pulls in WAL settings which we don't want here 45 | category ILIKE '%Logging%'; 46 | -------------------------------------------------------------------------------- /postgres_settings_memory.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL memory resource settings 17 | -- 18 | -- eg. shared_buffers, work_mem, huge_pages, temp_buffers, maintenance_work_mem 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-resource.html#RUNTIME-CONFIG-RESOURCE-MEMORY 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Resource Usage / Memory 44 | category ILIKE '%Memory%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_preset.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL preset options settings 17 | -- 18 | -- eg. block_size, wal_block_size, data_checksums, server_version, server_version_num, ssl_library 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-preset.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Preset Options 44 | category ILIKE '%Preset Options%'; 45 | -------------------------------------------------------------------------------- /postgres_settings_query_planning.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL Query Tuning & Query Statistics Collection settings 17 | -- 18 | -- eg. enable_indexscan, enable_hashjoin, jit, track_activities, track_counts 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-query.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Query Tuning / Planner Method Configuration 44 | -- Query Tuning / Planner Cost Constants 45 | -- Query Tuning / Other Planner Options 46 | -- Query Tuning / Genetic Query Optimizer 47 | -- Statistics / Query and Index Statistics Collector 48 | -- 49 | -- more general to pull in 'track_activities' from category 'Statistics / Query and Index Statistics Collector' 50 | --category ILIKE '%Query Tuning%'; 51 | category ILIKE '%Query%'; 52 | -------------------------------------------------------------------------------- /postgres_settings_replication.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL Replication settings 17 | -- 18 | -- eg. hot_standby, max_replication_slots, max_wal_senders etc. 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-replication.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Replication / Standby Servers 44 | -- Replication / Sending Servers 45 | -- Replication / Master Server 46 | -- Replication / Subscribers 47 | -- Replication 48 | category ILIKE '%Replication%'; 49 | -------------------------------------------------------------------------------- /postgres_settings_resources.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL resource settings 17 | -- 18 | -- eg. shared_buffers, work_mem, max_files_per_process 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-resource.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Resource Usage / Memory 44 | -- Resource Usage / Asynchronous Behavior 45 | -- Resource Usage / Cost-Based Vacuum Delay 46 | -- Resource Usage / Background Writer 47 | -- Resource Usage / Disk 48 | -- Resource Usage / Kernel Resources 49 | category ILIKE '%Resource%'; 50 | -------------------------------------------------------------------------------- /postgres_settings_ssl.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL SSL settings 17 | -- 18 | -- eg. ssl on/off, ssl_cert_file, ssl_ciphers etc. 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-connection.html#RUNTIME-CONFIG-CONNECTION-SSL 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category 30 | vartype, 31 | short_desc, 32 | enumvals, 33 | boot_val, 34 | reset_val, 35 | -- not available on PostgreSQL < 9.5 36 | pending_restart 37 | FROM 38 | pg_settings 39 | WHERE 40 | name ILIKE '%ssl%' 41 | OR 42 | -- Connections and Authentication / SSL 43 | category ILIKE '%SSL%' 44 | OR 45 | short_desc ILIKE '%SSL%'; 46 | -------------------------------------------------------------------------------- /postgres_settings_statistics.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL statistics collection settings 17 | -- 18 | -- eg. track_activities, track_counts, log_planner_stats, log_statement_stats 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-statistics.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Statistics / Query and Index Statistics Collector 44 | -- Statistics / Monitoring 45 | category ILIKE '%Statistics%'; 46 | -------------------------------------------------------------------------------- /postgres_settings_wal.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-06 18:07:25 +0100 (Thu, 06 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- Show PostgreSQL WAL settings 17 | -- 18 | -- eg. fsync, wal_level, wal_buffers, wal_sync_method, synchronous_commit, checkpoint_timeout, archive_mode etc. 19 | -- 20 | -- Requires PostgreSQL 9.5+ 21 | -- 22 | -- Tested on PostgreSQL 9.5, 9.6, 10.x 11.x, 12.x, 13.0 23 | 24 | -- https://www.postgresql.org/docs/12/runtime-config-wal.html 25 | 26 | SELECT 27 | name, 28 | setting, 29 | -- category, 30 | vartype, 31 | short_desc, 32 | -- enumvals, 33 | source, 34 | min_val, 35 | max_val, 36 | boot_val, 37 | reset_val, 38 | -- not available on PostgreSQL < 9.5 39 | pending_restart 40 | FROM 41 | pg_settings 42 | WHERE 43 | -- Write-Ahead Log / Settings 44 | -- Write-Ahead Log / Recovery Target 45 | -- Write-Ahead Log / Checkpoints 46 | -- Write-Ahead Log / Archive Recovery 47 | -- Write-Ahead Log / Archiving 48 | category ILIKE '%Write-Ahead Log%'; 49 | -------------------------------------------------------------------------------- /postgres_tables_by_size.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 17:54:19 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL tables by size, excluding catalog and information schema 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | --nspname || '.' || relname AS relation, 22 | nspname, 23 | relname, 24 | pg_size_pretty(pg_total_relation_size(C.oid)) AS total_size 25 | FROM 26 | pg_class C 27 | LEFT JOIN 28 | pg_namespace N ON (N.oid = C.relnamespace) 29 | WHERE 30 | nspname NOT IN ('pg_catalog', 'information_schema') 31 | AND 32 | C.relkind <> 'i' 33 | AND 34 | nspname !~ '^pg_toast' 35 | ORDER BY 36 | pg_total_relation_size(C.oid) DESC; 37 | -------------------------------------------------------------------------------- /postgres_tables_by_size_2.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2022-05-18 18:06:53 +0100 (Wed, 18 May 2022) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL tables by size 17 | -- 18 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 19 | 20 | SELECT 21 | schemaname AS table_schema, 22 | relname AS TABLE_NAME, 23 | pg_size_pretty(pg_total_relation_size(relid)) AS total_size, 24 | pg_size_pretty(pg_relation_size(relid)) AS data_size, 25 | pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid)) AS external_size 26 | FROM 27 | pg_catalog.pg_statio_user_tables 28 | ORDER BY 29 | pg_total_relation_size(relid) DESC, 30 | pg_relation_size(relid) DESC; 31 | -------------------------------------------------------------------------------- /postgres_tables_cache_hit_ratio.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 14:57:37 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL Tables Cache-hit Ratio 17 | -- 18 | -- should be closer to 1, eg. 0.99, at least 0.90 19 | -- 20 | -- otherwise increase 'shared_buffer' size (should be at least 25% of total RAM for a dedicated DB server) 21 | -- 22 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 23 | 24 | SELECT 25 | SUM(heap_blks_read) AS heap_blks_read, 26 | SUM(heap_blks_hit) AS heap_blks_hit, 27 | SUM(heap_blks_hit) / 28 | GREATEST(SUM(heap_blks_hit) + SUM(heap_blks_read), 1)::float 29 | AS ratio 30 | FROM 31 | pg_statio_user_tables; 32 | -------------------------------------------------------------------------------- /postgres_tables_index_usage.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 15:08:23 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL % of times indexes are used on tables by table size descending (might want to tweak to list by % descending) 17 | -- 18 | -- If not near 99% index usage on tables > 10,000 rows, consider adding an index to match your query patterns (CREATE INDEX CONCURRENTLY in prod to avoid locking table) 19 | -- 20 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 21 | 22 | SELECT 23 | relname AS table, 24 | 100 * idx_scan / GREATEST(seq_scan + idx_scan, 1) AS percent_of_times_index_used, 25 | n_live_tup AS rows_in_table 26 | FROM 27 | pg_stat_user_tables 28 | WHERE 29 | seq_scan + idx_scan > 0 30 | ORDER BY 31 | rows_in_table DESC, 32 | percent_of_times_index_used DESC; 33 | -------------------------------------------------------------------------------- /postgres_tables_row_estimates.sql: -------------------------------------------------------------------------------- 1 | -- 2 | -- Author: Hari Sekhon 3 | -- Date: 2020-08-05 18:21:15 +0100 (Wed, 05 Aug 2020) 4 | -- 5 | -- vim:ts=4:sts=4:sw=4:et:filetype=sql 6 | -- 7 | -- https://github.com/HariSekhon/SQL-scripts 8 | -- 9 | -- License: see accompanying Hari Sekhon LICENSE file 10 | -- 11 | -- If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | -- 13 | -- https://www.linkedin.com/in/HariSekhon 14 | -- 15 | 16 | -- PostgreSQL table row count estimates in one place 17 | -- 18 | -- Useful when comparing tables or when a table becomes so large you don't want the expense 19 | -- of 'SELECT COUNT(*)' and are happy to use the internal statistics instead 20 | -- 21 | -- Tested on PostgreSQL 8.4, 9.x, 10.x, 11.x, 12.x, 13.0 22 | 23 | SELECT 24 | schemaname, 25 | relname, 26 | n_live_tup 27 | FROM 28 | --pg_stat_all_tables 29 | pg_stat_user_tables 30 | ORDER BY 31 | n_live_tup DESC; 32 | -------------------------------------------------------------------------------- /setup/bootstrap.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | # vim:ts=4:sts=4:sw=4:et 3 | # 4 | # Author: Hari Sekhon 5 | # Date: 2019-10-16 10:33:03 +0100 (Wed, 16 Oct 2019) 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # License: see accompanying Hari Sekhon LICENSE file 10 | # 11 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | # Alpine / Wget: 17 | # 18 | # wget -O- https://raw.githubusercontent.com/HariSekhon/SQL-scripts/master/setup/bootstrap.sh | sh 19 | # 20 | # Curl: 21 | # 22 | # curl https://raw.githubusercontent.com/HariSekhon/SQL-scripts/master/setup/bootstrap.sh | sh 23 | 24 | set -eu 25 | [ -n "${DEBUG:-}" ] && set -x 26 | srcdir="$(dirname "$0")" 27 | 28 | repo="https://github.com/HariSekhon/SQL-scripts" 29 | 30 | directory="sql" 31 | 32 | sudo="" 33 | [ "$(whoami)" = "root" ] || sudo=sudo 34 | 35 | if [ "$(uname -s)" = Darwin ]; then 36 | echo "Bootstrapping on Mac OS X: $repo" 37 | if ! type brew >/dev/null 2>&1; then 38 | curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install | $sudo ruby 39 | fi 40 | elif [ "$(uname -s)" = Linux ]; then 41 | echo "Bootstrapping on Linux: $repo" 42 | if type apk >/dev/null 2>&1; then 43 | $sudo apk --no-cache add bash git make curl wget 44 | elif type apt-get >/dev/null 2>&1; then 45 | if [ -n "${CI:-}" ]; then 46 | export DEBIAN_FRONTEND=noninteractive 47 | fi 48 | opts="" 49 | if [ -z "${PS1:-}" ]; then 50 | opts="-qq" 51 | fi 52 | $sudo apt-get update $opts 53 | $sudo apt-get install $opts -y git make curl wget --no-install-recommends 54 | elif type yum >/dev/null 2>&1; then 55 | if grep -qi 'NAME=.*CentOS' /etc/*release; then 56 | echo "CentOS EOL detected, replacing yum base URL to vault to re-enable package installs" 57 | $sudo sed -i 's/^[[:space:]]*mirrorlist/#mirrorlist/' /etc/yum.repos.d/CentOS-Linux-* 58 | $sudo sed -i 's|^#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|' /etc/yum.repos.d/CentOS-Linux-* 59 | fi 60 | $sudo yum install -y git make curl wget 61 | else 62 | echo "Package Manager not found on Linux, cannot bootstrap" 63 | exit 1 64 | fi 65 | else 66 | echo "Only Mac & Linux are supported for conveniently bootstrapping all install scripts at this time" 67 | exit 1 68 | fi 69 | 70 | if [ "${srcdir##*/}" = setup ]; then 71 | cd "$srcdir/.." 72 | else 73 | # if this is an empty directory eg. a cache mount, then remove it to get a proper checkout 74 | rmdir "$directory" 2>/dev/null || : 75 | if [ -d "$directory" ]; then 76 | cd "$directory" 77 | git pull 78 | else 79 | git clone "$repo" "$directory" 80 | cd "$directory" 81 | fi 82 | fi 83 | 84 | if [ -z "${NO_MAKE:-}" ]; then 85 | make 86 | fi 87 | -------------------------------------------------------------------------------- /sonar-project.properties: -------------------------------------------------------------------------------- 1 | # vim:ts=4:sts=4:sw=4:et 2 | # 3 | # Author: Hari Sekhon 4 | # Date: 2016-07-19 18:31:17 +0100 (Tue, 19 Jul 2016) 5 | # 6 | # https://github.com/HariSekhon/SQL-scripts 7 | # 8 | # License: see accompanying Hari Sekhon LICENSE file 9 | # 10 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 11 | # 12 | # https://www.linkedin.com/in/HariSekhon 13 | # 14 | 15 | # ============================================================================ # 16 | # S o n a r Q u b e 17 | # ============================================================================ # 18 | 19 | sonar.host.url=https://sonarcloud.io 20 | 21 | # Required metadata 22 | sonar.organization=harisekhon 23 | sonar.projectName=SQL-scripts 24 | sonar.projectKey=HariSekhon_SQL-scripts 25 | sonar.projectVersion=1.0 26 | 27 | sonar.projectDescription=SQL-scripts 28 | 29 | sonar.links.homepage=https://github.com/HariSekhon/SQL-scripts 30 | sonar.links.scm=https://github.com/HariSekhon/SQL-scripts 31 | sonar.links.issue=https://github.com/HariSekhon/SQL-scripts/issues 32 | sonar.links.ci=https://github.com/HariSekhon/SQL-scripts/actions 33 | 34 | # directories to scan (defaults to sonar-project.properties dir otherwise) 35 | sonar.sources=. 36 | 37 | #sonar.language=py 38 | 39 | sonar.sourceEncoding=UTF-8 40 | 41 | #sonar.exclusions=**/tests/** 42 | #sonar.exclusions=**/zookeeper-*/**/* 43 | -------------------------------------------------------------------------------- /test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # vim:ts=4:sts=4:sw=4:et 3 | # 4 | # Author: Hari Sekhon 5 | # Date: 2020-08-12 12:13:30 +0100 (Wed, 12 Aug 2020) 6 | # 7 | # https://github.com/HariSekhon/SQL-scripts 8 | # 9 | # License: see accompanying Hari Sekhon LICENSE file 10 | # 11 | # If you're using my code you're welcome to connect with me on LinkedIn and optionally send me feedback to help steer this or other code I publish 12 | # 13 | # https://www.linkedin.com/in/HariSekhon 14 | # 15 | 16 | set -euo pipefail 17 | [ -n "${DEBUG:-}" ] && set -x 18 | srcdir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" 19 | 20 | cd "$srcdir" 21 | 22 | if ! type -P mysql_test_scripts.sh &>/dev/null || 23 | ! type -P mariadb_test_scripts.sh &>/dev/null || 24 | ! type -P postgres_test_scripts.sh &>/dev/null; then 25 | # downloads and builds DevOps-Bash-tools repo in $PWD/bash-tools - this is where the postgres/mysql/mariadb boot + test scripts are 26 | curl https://raw.githubusercontent.com/HariSekhon/DevOps-Bash-tools/master/setup/bootstrap.sh | sh 27 | fi 28 | 29 | export PATH="$PATH:$srcdir/bash-tools" 30 | 31 | # if this exists it means we bootstrapped above because the tools weren't in the $PATH 32 | if [ -d "bash-tools" ]; then 33 | pushd bash-tools 34 | make update2 35 | popd 36 | echo 37 | fi 38 | 39 | hr(){ 40 | echo 41 | echo "# ============================================================================ #" 42 | echo 43 | } 44 | 45 | echo 46 | if [ $# -eq 0 ] || [[ "$*" =~ mysql ]]; then 47 | mysql_test_scripts.sh 48 | hr 49 | fi 50 | if [ $# -eq 0 ] || [[ "$*" =~ postgres ]]; then 51 | postgres_test_scripts.sh 52 | hr 53 | fi 54 | if [ $# -eq 0 ] || [[ "$*" =~ maria ]]; then 55 | mariadb_test_scripts.sh 56 | hr 57 | fi 58 | --------------------------------------------------------------------------------