Skip to content

Commit

Permalink
new queroes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhupesh-V committed Jan 23, 2025
1 parent 2c3ea90 commit 8bcf4fb
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .vitepress/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ export const sidebar = [
"link": "/databases/postgres-internals-data-organisation"
},
{
"text": "Postgres Tips & Tricks",
"text": "Postgres Tips & Tricks - Megalist of Secret SQL Queries",
"link": "/databases/postgres-tips-megalist"
},
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/inserting-null-everywhere.md">Inserting NULL wherever possible on Postgres</a></li>
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/list-all-values-for-custom-enum-postgres.md">List all values of a custom enum in Postgres</a></li>
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/postgres-internals-data-organisation.md">Postgres 14 Internals: Data Organisation</a></li>
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/postgres-tips-megalist.md">Postgres Tips & Tricks</a></li>
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/postgres-tips-megalist.md">Postgres Tips & Tricks - Megalist of Secret SQL Queries</a></li>
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/sqlite-space-optimization-with-rowd-id.md">SQLite db optimization with ROWID</a></li>
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/slowly-changing-dimensions.md">Slowly Changing Dimensions (SCD)</a></li>
<li><a target="_blank" href="https://github.com/Bhupesh-V/til/blob/master/databases/oath-for-3-normal-forms.md">The 3NF Oath ✋🏼</a></li>
Expand Down
34 changes: 27 additions & 7 deletions databases/postgres-tips-megalist.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Postgres Tips & Tricks
<!-- omit from toc -->
# Postgres Tips & Tricks - Megalist of Secret SQL Queries

- [List all table indexes](#list-all-table-indexes)
- [List all column types across the schema](#list-all-column-types-across-the-schema)
- [List history of sequential \& index scans across all tables](#list-history-of-sequential--index-scans-across-all-tables)
- [Show table size](#show-table-size)

## List all table indexes

Expand All @@ -16,12 +21,6 @@ where c.relname = 'TABLE_NAME'

```

List all indexes for a table

```sql
select * from pg_indexes where tablename = 'TABLE_NAME'
```

## List all column types across the schema

```sql
Expand All @@ -34,3 +33,24 @@ JOIN pg_namespace n ON t.typnamespace = n.oid
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
AND t.typtype IN ('e', 'c'); -- 'e' for ENUM, 'c' for composite types
```

## List history of sequential & index scans across all tables

```sql
SELECT
relname AS table_name,
seq_scan,
last_seq_scan,
idx_scan,
last_idx_scan,
seq_scan + idx_scan AS total_accesses
FROM pg_stat_all_tables
WHERE schemaname = 'public'
ORDER BY total_accesses DESC;
```

## Show table size

```sql
SELECT pg_size_pretty(pg_relation_size('table'));
```
4 changes: 2 additions & 2 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<td><a href="https://til.bhupesh.me/management/the-art-and-science-mess-management">The art and science of mess management</a></td>
</tr>
<tr>
<td><a href="https://til.bhupesh.me/databases/postgres-tips-megalist">Postgres Tips & Tricks</a></td>
<td><a href="https://til.bhupesh.me/databases/postgres-tips-megalist">Postgres Tips & Tricks - Megalist of Secret SQL Queries</a></td>
</tr>
<tr>
<td><a href="https://til.bhupesh.me/psychology/eustress">Eustress: The Good Stress</a></td>
Expand Down Expand Up @@ -204,7 +204,7 @@
<li><a href="/databases/inserting-null-everywhere">Inserting NULL wherever possible on Postgres</a></li>
<li><a href="/databases/list-all-values-for-custom-enum-postgres">List all values of a custom enum in Postgres</a></li>
<li><a href="/databases/postgres-internals-data-organisation">Postgres 14 Internals: Data Organisation</a></li>
<li><a href="/databases/postgres-tips-megalist">Postgres Tips & Tricks</a></li>
<li><a href="/databases/postgres-tips-megalist">Postgres Tips & Tricks - Megalist of Secret SQL Queries</a></li>
<li><a href="/databases/sqlite-space-optimization-with-rowd-id">SQLite db optimization with ROWID</a></li>
<li><a href="/databases/slowly-changing-dimensions">Slowly Changing Dimensions (SCD)</a></li>
<li><a href="/databases/oath-for-3-normal-forms">The 3NF Oath ✋🏼</a></li>
Expand Down
2 changes: 1 addition & 1 deletion recent_tils.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
"url": "https://til.bhupesh.me/management/the-art-and-science-mess-management"
},
{
"title": "Postgres Tips & Tricks",
"title": "Postgres Tips & Tricks - Megalist of Secret SQL Queries",
"url": "https://til.bhupesh.me/databases/postgres-tips-megalist"
},
{
Expand Down

0 comments on commit 8bcf4fb

Please sign in to comment.