Skip to content

Instantly share code, notes, and snippets.

View shadyrudy's full-sized avatar
🍃
Doing the MongoDB thing

Rudy Rodarte shadyrudy

🍃
Doing the MongoDB thing
View GitHub Profile
@shadyrudy
shadyrudy / mongodbCollectionSizes.js
Last active April 19, 2024 15:13
MongoDB - Get collections, collection count, and size in MB
// Get the current timestamp in ISO 8601 format and adjust it to the desired format
var timestamp = new Date().toISOString().replace(/T/, ' ').replace(/\..+/, '');
// Print the header line for the CSV, including database name and timestamp
print("databaseName,timestamp,collectionName,documentCount,collectionSizeMB");
// Get the name of the current database
var dbName = db.getName();
// Get the list of all collection names in the current database
@shadyrudy
shadyrudy / PostgreSQL_vacuum_tables_modified.sql
Created February 27, 2024 21:03
PostgreSQL - Tables modified since last vacuum.
SELECT n.nspname AS schema_name
,c.relname AS table_or_index_name
,c.relkind AS table_or_index
,c.reltuples AS row_count
,s.last_vacuum
,s.last_autovacuum
,s.last_analyze
,s.last_autoanalyze
FROM pg_class c
JOIN pg_namespace n ON n.oid = c.relnamespace
@shadyrudy
shadyrudy / PostgreSQL_vacuum_statistics.sql
Created February 27, 2024 20:48
PostgreSQL Vacuum Statistics
-- Display vacuum information for PostgreSQL Tables
SELECT n.nspname AS schema_name
, c.relname AS table_name
, c.reltuples AS row_count
, c.relpages AS page_count
, s.n_dead_tup AS dead_row_count
, s.last_vacuum
, s.last_autovacuum
, s.last_analyze
, s.last_autoanalyze
@shadyrudy
shadyrudy / pg_du_plus.sql
Created February 1, 2024 17:23
PostgreSQL \du+ in query form
-- The following script emulates the PostgreSQL commmand
-- \du+ using SQL instead of the command
SELECT r.rolname
,r.rolsuper
,r.rolinherit
,r.rolcreaterole
,r.rolcreatedb
,r.rolcanlogin
,r.rolreplication
,r.rolconnlimit
@shadyrudy
shadyrudy / postgresql_grants.sql
Created January 22, 2024 21:51
CTE version of postgresql grants query
with results as (
SELECT rug.grantor, rug.grantee, rug.object_catalog, rug.object_schema, rug.object_name, rug.object_type, rug.privilege_type, rug.is_grantable, null::text AS with_hierarchy
FROM information_schema.role_usage_grants rug
WHERE rug.object_schema NOT IN ('pg_catalog', 'information_schema')
AND grantor <> grantee
UNION
SELECT rtg.grantor, rtg.grantee, rtg.table_catalog, rtg.table_schema, rtg.table_name, tab.table_type, rtg.privilege_type, rtg.is_grantable, rtg.with_hierarchy
FROM information_schema.role_table_grants rtg
LEFT JOIN information_schema.tables tab
ON (tab.table_catalog = rtg.table_catalog AND tab.table_schema = rtg.table_schema AND tab.table_name = rtg.table_name)
@shadyrudy
shadyrudy / pgbouncer-logroate.sh
Created November 21, 2023 21:26
The logroate.d entry for pgbouncer. Rotates the log once a day.
# /etc/logrotate.d/pgbouncer
/var/log/postgresql/pgbouncer.log {
daily
missingok
rotate 7
compress
delaycompress
notifempty
create 640 postgres adm
sharedscripts
@shadyrudy
shadyrudy / drop-pguser.sql
Last active November 21, 2023 17:21
Dropping a user in postgres. WIP
-- Delete permissions and drop a user (ROLE) from a
-- postgres database.
-- Be sure to update the following:
-- <username_to_delete> - The target user name
-- <your_database_name> - The target database name
-- <schema_name> - The target schema.
DO $$
DECLARE
r RECORD;
BEGIN
@shadyrudy
shadyrudy / postgres-permissions.sql
Last active November 9, 2023 22:52
PostgreSQL grant information. View postgres permission information
-- A Quick query to view postgresql grant information
-- Work in progress
SELECT
'TABLE' AS object_type,
grantee,
table_schema,
table_name,
privilege_type
FROM
information_schema.role_table_grants
@shadyrudy
shadyrudy / AGListener.ps1
Created June 14, 2023 21:20
Get SQL Server AG Listener Name via Powershell
# Import SQL Server module
Import-Module "SQLPS" -DisableNameChecking
# Variables
$ServerName = "YourServerName" # Enter your server name here
$Server = New-Object Microsoft.SqlServer.Management.Smo.Server $ServerName
# Get the availability groups
$AvailabilityGroups = $Server.AvailabilityGroups
@shadyrudy
shadyrudy / updateStats.sql
Created March 2, 2023 16:00
Update SQL Server Statistics
-- The following command will generate the
-- Statistics Rebuild command for all of the
-- statistics on a the targeted schema and table
select 'update statistics ' + sc.name + '.' + so.name + '(' + stat.name + ') with fullscan;' as Script
, stats_date(stat.object_id, stat.stats_id) as last_updated
, stuff((
select ', ' + cols.name
from sys.stats_columns as statcols
join sys.columns as cols
on statcols.column_id = cols.column_id