Liking cljdoc? Tell your friends :D

postgres-extras-clj.core

A namespace of useful functions for inspecting PostgreSQL databases in Clojure.

The SQL query logic is based on

Queries ported to HugSQL syntax to access them as Clojure data structures.

A namespace of useful functions for inspecting
PostgreSQL databases in Clojure.

 The SQL query logic is based on

   * https://github.com/heroku/heroku-pg-extras/tree/main
   * https://github.com/pawurb/ecto_psql_extras/tree/main
   * https://github.com/rustprooflabs/pgdd

 Queries ported to HugSQL syntax to access them
 as Clojure data structures.
raw docstring

all-locksclj

(all-locks db)
(all-locks db params)
(all-locks db params options & command-options)

Queries with active locks

Queries with active locks
sourceraw docstring

autovacuum-disabledclj

(autovacuum-disabled db)
(autovacuum-disabled db params)
(autovacuum-disabled db params options & command-options)

Tables with autovacuum disabled

Tables with autovacuum disabled
sourceraw docstring

bgwriter-statsclj

(bgwriter-stats db)

Checkpoint and background writer activity.

Checkpoint and background writer activity.
sourceraw docstring

bgwriter-stats-legacyclj

(bgwriter-stats-legacy db)
(bgwriter-stats-legacy db params)
(bgwriter-stats-legacy db params options & command-options)

Checkpoint and background writer activity (PostgreSQL < 17)

Checkpoint and background writer activity (PostgreSQL < 17)
sourceraw docstring

bgwriter-stats-pg17clj

(bgwriter-stats-pg17 db)
(bgwriter-stats-pg17 db params)
(bgwriter-stats-pg17 db params options & command-options)

Checkpoint and background writer activity (PostgreSQL >= 17)

Checkpoint and background writer activity (PostgreSQL >= 17)
sourceraw docstring

bloatclj

(bloat db)
(bloat db params)
(bloat db params options & command-options)

Table and index bloat in your database

Table and index bloat in your database
sourceraw docstring

blockingclj

(blocking db)
(blocking db params)
(blocking db params options & command-options)

Queries holding locks other queries are waiting to be released

Queries holding locks other queries are waiting to be released
sourceraw docstring

cache-hitclj

(cache-hit db)
(cache-hit db params)
(cache-hit db params options & command-options)

Index and table hit rate

Index and table hit rate
sourceraw docstring

callsclj

(calls db {:keys [limit] :or {limit 10}})

Queries that have the highest frequency of execution.

Queries that have the highest frequency of execution.
sourceraw docstring

calls-legacyclj

(calls-legacy db)
(calls-legacy db params)
(calls-legacy db params options & command-options)

Queries that have the highest frequency of execution (PostgreSQL < 17)

Queries that have the highest frequency of execution (PostgreSQL < 17)
sourceraw docstring

calls-pg17clj

(calls-pg17 db)
(calls-pg17 db params)
(calls-pg17 db params options & command-options)

Queries that have the highest frequency of execution (PostgreSQL >= 17)

Queries that have the highest frequency of execution (PostgreSQL >= 17)
sourceraw docstring

columnsclj

(columns db)
(columns db params)
(columns db params options & command-options)

List all database column objects

List all database column objects
sourceraw docstring

connection-statesclj

(connection-states db)
(connection-states db params)
(connection-states db params options & command-options)

Connections grouped by state

Connections grouped by state
sourceraw docstring

connectionsclj

(connections db)
(connections db params)
(connections db params options & command-options)

Returns the list of all active database connections

Returns the list of all active database connections
sourceraw docstring

constraintsclj

(constraints db
             &
             {:keys [include-system-objects?]
              :or {include-system-objects? false}})

List all named constraints (primary key, unique, foreign key, check, exclusion, and constraint trigger) on tables in the current database.

By default, system objects (constraints on tables in pg_catalog, information_schema, pg_, and TimescaleDB's timescaledb, timescaledb_information, and timescaledb_experimental schemas) are excluded. Pass :include-system-objects? true to include them.

List all named constraints (primary key, unique, foreign key,
check, exclusion, and constraint trigger) on tables in the
current database.

By default, system objects (constraints on tables in pg_catalog,
information_schema, pg_*, and TimescaleDB's _timescaledb_*,
timescaledb_information, and timescaledb_experimental schemas)
are excluded. Pass :include-system-objects? true to include them.
sourceraw docstring

constraints-rawclj

(constraints-raw db)
(constraints-raw db params)
(constraints-raw db params options & command-options)

List all constraint objects in current database

List all constraint objects in current database
sourceraw docstring

database-statsclj

(database-stats db)
(database-stats db params)
(database-stats db params options & command-options)

Cumulative database-level counters

Cumulative database-level counters
sourceraw docstring

databasesclj

(databases db)
(databases db params)
(databases db params options & command-options)

List all databases

List all databases
sourceraw docstring

db-settingsclj

(db-settings db)
(db-settings db params)
(db-settings db params options & command-options)

Values of selected PostgreSQL settings

Values of selected PostgreSQL settings
sourceraw docstring

ddl-commentsclj

(ddl-comments db)
(ddl-comments db params)
(ddl-comments db params options & command-options)

Table/view/column comments for COMMENT ON statements

Table/view/column comments for COMMENT ON statements
sourceraw docstring

ddl-function-defsclj

(ddl-function-defs db)
(ddl-function-defs db params)
(ddl-function-defs db params options & command-options)

Function and procedure CREATE OR REPLACE definitions

Function and procedure CREATE OR REPLACE definitions
sourceraw docstring

ddl-index-defsclj

(ddl-index-defs db)
(ddl-index-defs db params)
(ddl-index-defs db params options & command-options)

Index definitions, excluding primary-key and constraint-owned indexes

Index definitions, excluding primary-key and constraint-owned indexes
sourceraw docstring

ddl-rule-defsclj

(ddl-rule-defs db)
(ddl-rule-defs db params)
(ddl-rule-defs db params options & command-options)

Rule definitions, excluding the internal _RETURN rules of views

Rule definitions, excluding the internal _RETURN rules of views
sourceraw docstring

ddl-table-columnsclj

(ddl-table-columns db)
(ddl-table-columns db params)
(ddl-table-columns db params options & command-options)

Columns of tables (relkind r/p/f), with rendered type/default/identity

Columns of tables (relkind r/p/f), with rendered type/default/identity
sourceraw docstring

ddl-table-constraintsclj

(ddl-table-constraints db)
(ddl-table-constraints db params)
(ddl-table-constraints db params options & command-options)

Table constraints (PK/unique/check/FK), excluding NOT NULL (contype n)

Table constraints (PK/unique/check/FK), excluding NOT NULL (contype n)
sourceraw docstring

ddl-table-optionsclj

(ddl-table-options db)
(ddl-table-options db params)
(ddl-table-options db params options & command-options)

Persistence, storage params, tablespace and partition key of tables

Persistence, storage params, tablespace and partition key of tables
sourceraw docstring

ddl-trigger-defsclj

(ddl-trigger-defs db)
(ddl-trigger-defs db params)
(ddl-trigger-defs db params options & command-options)

Trigger definitions, excluding internal triggers

Trigger definitions, excluding internal triggers
sourceraw docstring

ddl-view-defsclj

(ddl-view-defs db)
(ddl-view-defs db params)
(ddl-view-defs db params options & command-options)

View and materialized view SELECT definitions

View and materialized view SELECT definitions
sourceraw docstring

default-diagnostic-fnsclj

Defines the critical queries and their cutoffs for the diagnose function. The value is a map containing a predicate fn (true = pass) to test against each result, and a description of the problem if false.

Defines the critical queries and their cutoffs for the `diagnose`
function. The value is a map containing a predicate fn (true = pass)
to test against each result, and a description of the problem if false.
sourceraw docstring

diagnoseclj

(diagnose stats
          &
          {:keys [diagnostic-fns] :or {diagnostic-fns default-diagnostic-fns}})

Run assertions on stats and return a seq of result maps. Iterate through the diagnostic functions, run them, then iterate through each record, applying the predicate. Returns a flat list of result maps.

Run assertions on stats and return a seq of result maps.
Iterate through the diagnostic functions, run them,
then iterate through each record, applying the predicate.
Returns a flat list of result maps.
sourceraw docstring

diagnose-warningsclj

(diagnose-warnings stats
                   &
                   {:keys [diagnostic-fns]
                    :or {diagnostic-fns default-diagnostic-fns}})

Run assertions on stats and return a seq of result maps. Iterate through the diagnostic functions, then through the results, applying the predicate. Returns a flat list of result maps, filtered to contain only actionable warnings. An empty return sequence indicates all your dianostics passed.

Run assertions on stats and return a seq of result maps.
Iterate through the diagnostic functions,
then through the results, applying the predicate.
Returns a flat list of result maps, filtered to contain
only actionable warnings. An empty return sequence indicates
all your dianostics passed.
sourceraw docstring

duplicate-indexesclj

(duplicate-indexes db)
(duplicate-indexes db params)
(duplicate-indexes db params options & command-options)

Multiple indexes that have the same set of columns, same opclass, expression and predicate

Multiple indexes that have the same set of columns, same opclass, expression and predicate
sourceraw docstring

extensionsclj

(extensions db)
(extensions db params)
(extensions db params options & command-options)

Available and installed extensions

Available and installed extensions
sourceraw docstring

functionsclj

(functions db)
(functions db params)
(functions db params options & command-options)

List all function objects in current database

List all function objects in current database
sourceraw docstring

health-checkclj

(health-check db)
(health-check db params)
(health-check db params options & command-options)

Checks the db for liveliness

Checks the db for liveliness
sourceraw docstring

idle-in-transactionclj

(idle-in-transaction db)
(idle-in-transaction db params)
(idle-in-transaction db params options & command-options)

Sessions idle in transaction (vacuum blockers)

Sessions idle in transaction (vacuum blockers)
sourceraw docstring

index-cache-hitclj

(index-cache-hit db)
(index-cache-hit db params)
(index-cache-hit db params options & command-options)

Calculates your cache hit rate for reading indexes

Calculates your cache hit rate for reading indexes
sourceraw docstring

index-sizeclj

(index-size db)
(index-size db params)
(index-size db params options & command-options)

The size of indexes

The size of indexes
sourceraw docstring

index-usageclj

(index-usage db)
(index-usage db params)
(index-usage db params options & command-options)

Index hit rate (effective databases are at 99% and up)

Index hit rate (effective databases are at 99% and up)
sourceraw docstring

indexesclj

(indexes db)
(indexes db params)
(indexes db params options & command-options)

List all index objects in current database

List all index objects in current database
sourceraw docstring

invalid-constraintsclj

(invalid-constraints db)
(invalid-constraints db params)
(invalid-constraints db params options & command-options)

Constraints that are NOT VALID

Constraints that are NOT VALID
sourceraw docstring

invalid-indexesclj

(invalid-indexes db)
(invalid-indexes db params)
(invalid-indexes db params options & command-options)

Indexes that are invalid and need REINDEX

Indexes that are invalid and need REINDEX
sourceraw docstring

key-relationshipsclj

(key-relationships db
                   &
                   {:keys [include-system-objects?]
                    :or {include-system-objects? false}})

List all foreign key relationships as ERD-ready edges.

Each map describes one foreign key constraint: :constraint — name of the foreign key constraint :schema, :table — referencing side :columns — referencing columns, in constraint order :foreign_schema, :foreign_table — referenced side :foreign_columns — referenced columns, parallel to :columns :delete_action — ON DELETE action (NO ACTION, RESTRICT, CASCADE, SET NULL, SET DEFAULT) :update_action — ON UPDATE action :match_type — MATCH type (SIMPLE, FULL, PARTIAL) :deferrable, :initially_deferred, :validated

Composite foreign keys produce a single relationship map with parallel :columns and :foreign_columns vectors.

By default, system objects are excluded. Pass :include-system-objects? true to include them.

List all foreign key relationships as ERD-ready edges.

Each map describes one foreign key constraint:
  :constraint          — name of the foreign key constraint
  :schema, :table      — referencing side
  :columns             — referencing columns, in constraint order
  :foreign_schema,
  :foreign_table       — referenced side
  :foreign_columns     — referenced columns, parallel to :columns
  :delete_action       — ON DELETE action (NO ACTION, RESTRICT,
                         CASCADE, SET NULL, SET DEFAULT)
  :update_action       — ON UPDATE action
  :match_type          — MATCH type (SIMPLE, FULL, PARTIAL)
  :deferrable,
  :initially_deferred,
  :validated

Composite foreign keys produce a single relationship map with
parallel :columns and :foreign_columns vectors.

By default, system objects are excluded. Pass :include-system-objects?
true to include them.
sourceraw docstring

key-relationships-rawclj

(key-relationships-raw db)
(key-relationships-raw db params)
(key-relationships-raw db params options & command-options)

List all foreign key relationships, one row per column pair

List all foreign key relationships, one row per column pair
sourceraw docstring

kill-all!clj

(kill-all! db)
(kill-all! db params)
(kill-all! db params options & command-options)

Kill all the active database connections

Kill all the active database connections
sourceraw docstring

locksclj

(locks db)
(locks db params)
(locks db params options & command-options)

Queries with active exclusive locks

Queries with active exclusive locks
sourceraw docstring

long-running-queriesclj

(long-running-queries db)
(long-running-queries db params)
(long-running-queries db params options & command-options)

All queries longer than the threshold

All queries longer than the threshold
sourceraw docstring

maintenanceclj

(maintenance db)
(maintenance db params)
(maintenance db params options & command-options)

Last vacuum/analyze and their counts per table

Last vacuum/analyze and their counts per table
sourceraw docstring

mandelbrotclj

(mandelbrot db)
(mandelbrot db params)
(mandelbrot db params options & command-options)

The mandelbrot set

The mandelbrot set
sourceraw docstring

missing-indexesclj

(missing-indexes db)
(missing-indexes db params)
(missing-indexes db params options & command-options)

Tables with low index usage and many rows

Tables with low index usage and many rows
sourceraw docstring

null-indexesclj

(null-indexes db)
(null-indexes db params)
(null-indexes db params options & command-options)

Find indexes with a high ratio of NULL values

Find indexes with a high ratio of NULL values
sourceraw docstring

outliersclj

(outliers db {:keys [limit] :or {limit 10}})

Queries that have longest execution time in aggregate.

Queries that have longest execution time in aggregate.
sourceraw docstring

outliers-legacyclj

(outliers-legacy db)
(outliers-legacy db params)
(outliers-legacy db params options & command-options)

Queries that have longest execution time in aggregate (PostgreSQL < 17)

Queries that have longest execution time in aggregate (PostgreSQL < 17)
sourceraw docstring

outliers-pg17clj

(outliers-pg17 db)
(outliers-pg17 db params)
(outliers-pg17 db params options & command-options)

Queries that have longest execution time in aggregate (PostgreSQL >= 17)

Queries that have longest execution time in aggregate (PostgreSQL >= 17)
sourceraw docstring

partition-childrenclj

(partition-children db)
(partition-children db params)
(partition-children db params options & command-options)

List all child partitions in current database

List all child partitions in current database
sourceraw docstring

partition-parentsclj

(partition-parents db)
(partition-parents db params)
(partition-parents db params options & command-options)

List all parent partitions in current database

List all parent partitions in current database
sourceraw docstring

read-data-dictionaryclj

(read-data-dictionary db
                      &
                      {:keys [include-system-objects?]
                       :or {include-system-objects? false}})

Create a data dictionary summarizing all major objects in your PostgreSQL database. Respects SQL COMMENTS, thus serves as a human-readable description of your data model.

By default, system objects (schemas in pg_catalog, information_schema, pg_, and TimescaleDB's timescaledb, timescaledb_information, and timescaledb_experimental schemas) are excluded. Pass :include-system-objects? true to include them.

Create a data dictionary summarizing all major objects
in your PostgreSQL database. Respects SQL COMMENTS,
thus serves as a human-readable description of your data model.

By default, system objects (schemas in pg_catalog,
information_schema, pg_*, and TimescaleDB's _timescaledb_*,
timescaledb_information, and timescaledb_experimental schemas)
are excluded. Pass :include-system-objects? true to include them.
sourceraw docstring

read-statsclj

(read-stats db
            &
            {:keys [limit include-system-objects?]
             :or {limit 10 include-system-objects? false}})

Query postgres instance for all available diagnostic information. This will invoke all of the public, read-only diagnostic queries in the namespace.

The :outliers and :calls stats require enabling the pg-stat-statements extension, otherwise they will log a warning and return nil.

By default, :stats-collector-drift excludes system objects. Pass :include-system-objects? true to include them.

Query postgres instance for all available diagnostic information.
This will invoke all of the public, read-only diagnostic queries in the namespace.

The :outliers and :calls stats require enabling the pg-stat-statements extension,
otherwise they will log a warning and return nil.

By default, :stats-collector-drift excludes system objects. Pass
:include-system-objects? true to include them.
sourceraw docstring

records-rankclj

(records-rank db)
(records-rank db params)
(records-rank db params options & command-options)

All tables and their row counts from two sources: the query planner (pg_class.reltuples) and the statistics collector (pg_stat_user_tables.n_live_tup)

All tables and their row counts from two sources: the query planner (pg_class.reltuples) and the statistics collector (pg_stat_user_tables.n_live_tup)
sourceraw docstring

replication-lagclj

(replication-lag db)
(replication-lag db params)
(replication-lag db params options & command-options)

Standby replication lag in seconds

Standby replication lag in seconds
sourceraw docstring

replication-slotsclj

(replication-slots db)
(replication-slots db params)
(replication-slots db params options & command-options)

Replication slots with lag and retention

Replication slots with lag and retention
sourceraw docstring

replication-statsclj

(replication-stats db)
(replication-stats db params)
(replication-stats db params options & command-options)

Standby connections and per-standby lag from the primary

Standby connections and per-standby lag from the primary
sourceraw docstring

rolesclj

(roles db)
(roles db params)
(roles db params options & command-options)

Roles and their privilege/expiry attributes

Roles and their privilege/expiry attributes
sourceraw docstring

schemasclj

(schemas db)
(schemas db params)
(schemas db params options & command-options)

List all shemas in current database

List all shemas in current database
sourceraw docstring

seq-scansclj

(seq-scans db)
(seq-scans db params)
(seq-scans db params options & command-options)

Count of sequential scans by table

Count of sequential scans by table
sourceraw docstring

sequence-originclj

(sequence-origin db)
(sequence-origin db params)
(sequence-origin db params options & command-options)

How each sequence was created: serial, identity, or explicit CREATE SEQUENCE

How each sequence was created: serial, identity, or explicit CREATE SEQUENCE
sourceraw docstring

sequencesclj

(sequences db)
(sequences db params)
(sequences db params options & command-options)

Sequences near exhaustion or on small integer types

Sequences near exhaustion or on small integer types
sourceraw docstring

server-version-numclj

(server-version-num db)
(server-version-num db params)
(server-version-num db params options & command-options)

Numeric PostgreSQL server version (e.g. 170005 for 17.5)

Numeric PostgreSQL server version (e.g. 170005 for 17.5)
sourceraw docstring

stats-collector-driftclj

(stats-collector-drift db
                       &
                       {:keys [include-system-objects?]
                        :or {include-system-objects? false}})

Row-count drift between the planner estimate (reltuples) and the statistics collector (n_live_tup). Includes a :drift_probability from 0..1 (0=perfect agreement, 1=worst case drift). Higher values indicate a manual ANALYZE may be necessary.

Row-count drift between the planner estimate (reltuples)
and the statistics collector (n_live_tup).
Includes a :drift_probability from 0..1
(0=perfect agreement, 1=worst case drift).
Higher values indicate a manual ANALYZE may be necessary.
sourceraw docstring

stats-collector-drift-rawclj

(stats-collector-drift-raw db)
(stats-collector-drift-raw db params)
(stats-collector-drift-raw db params options & command-options)

Probability of drift between the planner estimates and the statistics collector. 0 = perfect agreement, 1 = total disagreement.

Probability of drift between the planner estimates and the statistics collector. 0 = perfect agreement, 1 = total disagreement.
sourceraw docstring

table-cache-hitclj

(table-cache-hit db)
(table-cache-hit db params)
(table-cache-hit db params options & command-options)

Calculates your cache hit rate for reading tables

Calculates your cache hit rate for reading tables
sourceraw docstring

table-indexes-sizeclj

(table-indexes-size db)
(table-indexes-size db params)
(table-indexes-size db params options & command-options)

Total size of all the indexes on each table

Total size of all the indexes on each table
sourceraw docstring

table-sizeclj

(table-size db)
(table-size db params)
(table-size db params options & command-options)

Size of the tables (excluding indexes)

Size of the tables (excluding indexes)
sourceraw docstring

tablesclj

(tables db)
(tables db params)
(tables db params options & command-options)

List all table objects in current database

List all table objects in current database
sourceraw docstring

tables-without-primary-keyclj

(tables-without-primary-key db)
(tables-without-primary-key db params)
(tables-without-primary-key db params options & command-options)

Tables without a primary key

Tables without a primary key
sourceraw docstring

total-index-sizeclj

(total-index-size db)
(total-index-size db params)
(total-index-size db params options & command-options)

Total size of all indexes in MB

Total size of all indexes in MB
sourceraw docstring

total-table-sizeclj

(total-table-size db)
(total-table-size db params)
(total-table-size db params options & command-options)

Size of the tables (including indexes)

Size of the tables (including indexes)
sourceraw docstring

transaction-idclj

(transaction-id db)
(transaction-id db params)
(transaction-id db params options & command-options)

Transaction ID wraparound risk per database

Transaction ID wraparound risk per database
sourceraw docstring

triggersclj

(triggers db)
(triggers db params)
(triggers db params options & command-options)

List all trigger objects in current database

List all trigger objects in current database
sourceraw docstring

unindexed-foreign-keysclj

(unindexed-foreign-keys db)
(unindexed-foreign-keys db params)
(unindexed-foreign-keys db params options & command-options)

Foreign keys without a leading index on the referencing columns

Foreign keys without a leading index on the referencing columns
sourceraw docstring

unlogged-tablesclj

(unlogged-tables db)
(unlogged-tables db params)
(unlogged-tables db params options & command-options)

Unlogged tables (data lost on crash)

Unlogged tables (data lost on crash)
sourceraw docstring

unused-indexesclj

(unused-indexes db)
(unused-indexes db params)
(unused-indexes db params options & command-options)

Unused and almost unused indexes

Unused and almost unused indexes
sourceraw docstring

unused-tablesclj

(unused-tables db)
(unused-tables db params)
(unused-tables db params options & command-options)

Tables never accessed via an index

Tables never accessed via an index
sourceraw docstring

vacuum-statsclj

(vacuum-stats db)
(vacuum-stats db params)
(vacuum-stats db params options & command-options)

Dead rows and whether an automatic vacuum is expected to be triggered

Dead rows and whether an automatic vacuum is expected to be triggered
sourceraw docstring

viewsclj

(views db)
(views db params)
(views db params options & command-options)

List all view objects in current database

List all view objects in current database
sourceraw docstring

wal-archiverclj

(wal-archiver db)
(wal-archiver db params)
(wal-archiver db params options & command-options)

WAL archiving health

WAL archiving health
sourceraw docstring

wal-statsclj

(wal-stats db)
(wal-stats db params)
(wal-stats db params options & command-options)

WAL activity (PostgreSQL 14+)

WAL activity (PostgreSQL 14+)
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
←Move to previous article
→Move to next article
Ctrl+/Jump to the search field
× close