update

Changed files
+129
internal
log
utils
+4
internal/log/log.go
··· 41 41 infoLog.Printf("%s [INFO] %s", timestamp(), fmt.Sprintf(format, v...)) 42 42 } 43 43 44 + func Warn(format string, v ...interface{}) { 45 + infoLog.Printf("%s [WARN] %s", timestamp(), fmt.Sprintf(format, v...)) 46 + } 47 + 44 48 func Error(format string, v ...interface{}) { 45 49 errorLog.Printf("%s [ERROR] %s", timestamp(), fmt.Sprintf(format, v...)) 46 50 }
+125
utils/db-sizes.sh
··· 1 + #!/bin/bash 2 + 3 + # === Configuration === 4 + CONFIG_FILE="config.yaml" # Path to your config file 5 + SCHEMA_NAME="public" # Replace if your schema is different 6 + 7 + # Check if config file exists 8 + if [ ! -f "$CONFIG_FILE" ]; then 9 + echo "Error: Config file not found at '$CONFIG_FILE'" 10 + exit 1 11 + fi 12 + 13 + # Check if yq is installed 14 + if ! command -v yq &> /dev/null; then 15 + echo "Error: 'yq' command not found. Please install yq (Go version by Mike Farah)." 16 + echo "See: https://github.com/mikefarah/yq/" 17 + exit 1 18 + fi 19 + 20 + echo "--- Reading connection info from '$CONFIG_FILE' ---" 21 + 22 + # === Extract Database Config using yq === 23 + DB_TYPE=$(yq e '.database.type' "$CONFIG_FILE") 24 + DB_CONN_STRING=$(yq e '.database.path' "$CONFIG_FILE") # This is likely a URI 25 + 26 + if [ -z "$DB_TYPE" ] || [ -z "$DB_CONN_STRING" ]; then 27 + echo "Error: Could not read database type or path from '$CONFIG_FILE'." 28 + exit 1 29 + fi 30 + 31 + # === Parse the Connection String === 32 + DB_USER="" 33 + DB_PASSWORD="" 34 + DB_HOST="localhost" # Default 35 + DB_PORT="5432" # Default 36 + DB_NAME="" 37 + 38 + # Use regex to parse the URI (handles postgres:// or postgresql://, optional password/port, and query parameters) 39 + if [[ "$DB_CONN_STRING" =~ ^(postgres|postgresql)://([^:]+)(:([^@]+))?@([^:/]+)(:([0-9]+))?/([^?]+)(\?.+)?$ ]]; then 40 + DB_USER="${BASH_REMATCH[2]}" 41 + DB_PASSWORD="${BASH_REMATCH[4]}" # Optional group 42 + DB_HOST="${BASH_REMATCH[5]}" 43 + DB_PORT="${BASH_REMATCH[7]:-$DB_PORT}" # Use extracted port or default 44 + DB_NAME="${BASH_REMATCH[8]}" # Database name before the '?' 45 + else 46 + echo "Error: Could not parse database connection string URI: $DB_CONN_STRING" 47 + exit 1 48 + fi 49 + 50 + # Set PGPASSWORD environment variable if password was found 51 + if [ -n "$DB_PASSWORD" ]; then 52 + export PGPASSWORD="$DB_PASSWORD" 53 + else 54 + echo "Warning: No password found in connection string. Relying on ~/.pgpass or password prompt." 55 + unset PGPASSWORD 56 + fi 57 + 58 + echo "--- Database Size Investigation ---" 59 + echo "Database: $DB_NAME" 60 + echo "Schema: $SCHEMA_NAME" 61 + echo "User: $DB_USER" 62 + echo "Host: $DB_HOST:$DB_PORT" 63 + echo "-----------------------------------" 64 + 65 + # === Table Sizes === 66 + echo "" 67 + echo "## Table Sizes (Schema: $SCHEMA_NAME) ##" 68 + # Removed --tuples-only and --no-align, added -P footer=off 69 + psql -U "$DB_USER" -d "$DB_NAME" -h "$DB_HOST" -p "$DB_PORT" -X -q -P footer=off <<EOF 70 + SELECT 71 + c.relname AS "Table Name", 72 + pg_size_pretty(pg_total_relation_size(c.oid)) AS "Total Size", 73 + pg_size_pretty(pg_relation_size(c.oid)) AS "Table Heap Size", 74 + pg_size_pretty(pg_indexes_size(c.oid)) AS "Indexes Size" 75 + FROM 76 + pg_class c 77 + LEFT JOIN 78 + pg_namespace n ON n.oid = c.relnamespace 79 + WHERE 80 + c.relkind = 'r' -- 'r' = ordinary table 81 + AND n.nspname = '$SCHEMA_NAME' 82 + ORDER BY 83 + pg_total_relation_size(c.oid) DESC; 84 + EOF 85 + 86 + if [ $? -ne 0 ]; then 87 + echo "Error querying table sizes. Check connection details, permissions, and password." 88 + unset PGPASSWORD 89 + exit 1 90 + fi 91 + 92 + # === Index Sizes === 93 + echo "" 94 + echo "## Index Sizes (Schema: $SCHEMA_NAME) ##" 95 + # Removed --tuples-only and --no-align, added -P footer=off 96 + psql -U "$DB_USER" -d "$DB_NAME" -h "$DB_HOST" -p "$DB_PORT" -X -q -P footer=off <<EOF 97 + SELECT 98 + c.relname AS "Index Name", 99 + i.indrelid::regclass AS "Table Name", -- Show associated table 100 + pg_size_pretty(pg_relation_size(c.oid)) AS "Index Size" 101 + FROM 102 + pg_class c 103 + LEFT JOIN 104 + pg_index i ON i.indexrelid = c.oid 105 + LEFT JOIN 106 + pg_namespace n ON n.oid = c.relnamespace 107 + WHERE 108 + c.relkind = 'i' -- 'i' = index 109 + AND n.nspname = '$SCHEMA_NAME' 110 + ORDER BY 111 + pg_relation_size(c.oid) DESC; 112 + EOF 113 + 114 + if [ $? -ne 0 ]; then 115 + echo "Error querying index sizes. Check connection details, permissions, and password." 116 + unset PGPASSWORD 117 + exit 1 118 + fi 119 + 120 + echo "" 121 + echo "-----------------------------------" 122 + echo "Investigation complete." 123 + 124 + # Unset the password variable for security 125 + unset PGPASSWORD