Skip to content

Instantly share code, notes, and snippets.

@krisiye
krisiye / pg_stat_database.sql
Created August 18, 2022 18:42
Useful queries to evaluate database level stats
-- Cache hit ratio.
-- Sweet spot here are values close to 100 which also means all necessary data were read from shared buffers.
-- values near 90 or lower show that postgres read from disk time to time.
SELECT
round(100 * sum(blks_hit) / sum(blks_hit + blks_read), 3) as cache_hit_ratio
FROM pg_stat_database;
-- estimate commit ratio and detect errors
-- values that are closer to 100 mean that you database has very few errors.
SELECT
@krisiye
krisiye / postgresql_cpu_troubleshooting.sql
Last active January 12, 2023 20:43
Useful queries in troubleshooting CPU utilization on PostgreSQL
-- tested with postgres13. Lower versions may need tweaks.
-- Top 20 cpu heavy queries
SELECT
query AS short_query,
round((total_plan_time + total_exec_time)::numeric, 2),
calls,
rows,
round((total_plan_time + total_exec_time)::numeric / calls, 2) AS avg_time,
round((100 * (total_plan_time + total_exec_time) / sum((total_plan_time + total_exec_time)::numeric) OVER ())::numeric, 2) AS percentage_cpu
@krisiye
krisiye / postgresql_cheat_sheet.sql
Last active November 21, 2023 01:10
Useful queries for postgresql for monitoring queries and disk usage
-- Temporary file usage by database
SELECT datname AS "database", temp_files AS "Temporary files", temp_bytes
AS "Size of temporary files"
FROM pg_stat_database;
-- Cache Hit Ratio. Anything greater than 90% is always good
SELECT sum(blks_hit)*100/sum(blks_hit+blks_read) AS hit_ratio FROM pg_stat_database;
-- Top Queries
SELECT substr(query, 0, 250), calls,
@krisiye
krisiye / VaultAWSConfiguration.java
Created March 26, 2021 13:49
VaultAWSConfiguration and Lease Listener
package example.springboot.config;
import javax.annotation.PostConstruct;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
@krisiye
krisiye / AWSConfiguration.java
Created March 25, 2021 21:15
AWSConfiguration Example
package example.springboot.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.amazonaws.auth.AWSCredentials;
@krisiye
krisiye / AwsConfigurationProperties.java
Created March 25, 2021 21:07
AwsConfigurationProperties Example
package example.springboot.config;
import static example.springboot.config.AwsConfigurationProperties.PREFIX;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Sample ConfigurationProperties for AWS STS
*
@krisiye
krisiye / gist:b3a597b5e9a8e72904cc444eb1522979
Created October 4, 2017 21:55
Hasmap rehash code example
// example with a hashmap to demonstrate how we could end up missing hashmap entries
// under concurrent modification.
package test;
import java.util.HashMap;
public class MapTestTask implements Runnable {
private static HashMap<Object, Object> hashMap;