How to use the @heroku-cli/plugin-pg-v5.psql function in @heroku-cli/plugin-pg-v5

To help you get started, we’ve selected a few @heroku-cli/plugin-pg-v5 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github heroku / heroku-pg-extras / commands / fdwsql.js View on Github external
function * run (context, heroku) {
  const app = context.app
  const {prefix, database} = context.args

  let db = yield pg.fetcher(heroku).database(app, database)
  let addon = yield pg.fetcher(heroku).addon(app, database)
  yield util.ensureNonStarterPlan(addon)
  cli.log('CREATE EXTENSION IF NOT EXISTS postgres_fdw;')
  cli.log(`DROP SERVER IF EXISTS ${prefix}_db;`)
  cli.log(`CREATE SERVER ${prefix}_db
  FOREIGN DATA WRAPPER postgres_fdw
  OPTIONS (dbname '${db.database}', host '${db.host}');`)
  cli.log(`CREATE USER MAPPING FOR CURRENT_USER
  SERVER ${prefix}_db
  OPTIONS (user '${db.user}', password '${db.password}');`)
  let output = yield pg.psql.exec(db, query(prefix))
  output = output.split('\n').filter(l => /CREATE/.test(l)).join('\n')
  process.stdout.write(output)
  cli.log()
}
github heroku / heroku-pg-extras / commands / mandelbrot.js View on Github external
FROM Z
            WHERE X * X + Y * Y < 16::float
            AND I < 100
      )
SELECT array_to_string(array_agg(SUBSTRING(' .,,,-----++++%%%%@@@@#### ', LEAST(GREATEST(I,1),27), 1)),'')
FROM (
      SELECT IX, IY, MAX(I) AS I
      FROM Z
      GROUP BY IY, IX
      ORDER BY IY, IX
     ) AS ZT
GROUP BY IY
ORDER BY IY
  `

  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / commands / unused_indexes.js View on Github external
let db = yield pg.fetcher(heroku).database(context.app, context.args.database)

  let query = `
SELECT
  schemaname || '.' || relname AS table,
  indexrelname AS index,
  pg_size_pretty(pg_relation_size(i.indexrelid)) AS index_size,
  idx_scan as index_scans
FROM pg_stat_user_indexes ui
JOIN pg_index i ON ui.indexrelid = i.indexrelid
WHERE NOT indisunique AND idx_scan < 50 AND pg_relation_size(relid) > 5 * 8192
ORDER BY pg_relation_size(i.indexrelid) / nullif(idx_scan, 0) DESC NULLS FIRST,
pg_relation_size(i.indexrelid) DESC;
`

  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / commands / blocking.js View on Github external
function * run (context, heroku) {
  let db = yield pg.fetcher(heroku).database(context.app, context.args.database)
  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / commands / table_indexes_size.js View on Github external
function * run (context, heroku) {
  let db = yield pg.fetcher(heroku).database(context.app, context.args.database)

  let query = `
SELECT c.relname AS table,
  pg_size_pretty(pg_indexes_size(c.oid)) AS index_size
FROM pg_class c
LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
AND n.nspname !~ '^pg_toast'
AND c.relkind='r'
ORDER BY pg_indexes_size(c.oid) DESC;
`

  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / commands / cache_hit.js View on Github external
function * run (context, heroku) {
  let db = yield pg.fetcher(heroku).database(context.app, context.args.database)
  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / commands / vacuum_stats.js View on Github external
to_char(psut.last_autovacuum, 'YYYY-MM-DD HH24:MI') AS last_autovacuum,
  to_char(pg_class.reltuples, '9G999G999G999') AS rowcount,
  to_char(psut.n_dead_tup, '9G999G999G999') AS dead_rowcount,
  to_char(autovacuum_vacuum_threshold
       + (autovacuum_vacuum_scale_factor::numeric * pg_class.reltuples), '9G999G999G999') AS autovacuum_threshold,
  CASE
    WHEN autovacuum_vacuum_threshold + (autovacuum_vacuum_scale_factor::numeric * pg_class.reltuples) < psut.n_dead_tup
    THEN 'yes'
  END AS expect_autovacuum
FROM
  pg_stat_user_tables psut INNER JOIN pg_class ON psut.relid = pg_class.oid
    INNER JOIN vacuum_settings ON pg_class.oid = vacuum_settings.oid
ORDER BY 1
`

  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / commands / table_size.js View on Github external
function * run (context, heroku) {
  let db = yield pg.fetcher(heroku).database(context.app, context.args.database)

  let query = `
SELECT c.relname AS name,
  pg_size_pretty(pg_table_size(c.oid)) AS size
FROM pg_class c
LEFT JOIN pg_namespace n ON (n.oid = c.relnamespace)
WHERE n.nspname NOT IN ('pg_catalog', 'information_schema')
AND n.nspname !~ '^pg_toast'
AND c.relkind='r'
ORDER BY pg_table_size(c.oid) DESC;
`

  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / commands / bloat.js View on Github external
CASE WHEN relpages < otta THEN '0' ELSE (bs*(table_bloat.relpages-otta)::bigint)::bigint END AS raw_waste
FROM
  table_bloat
    UNION
SELECT
  'index' as type,
  schemaname,
  tablename || '::' || iname as object_name,
  ROUND(CASE WHEN iotta=0 OR ipages=0 THEN 0.0 ELSE ipages/iotta::numeric END,1) AS bloat,
  CASE WHEN ipages < iotta THEN '0' ELSE (bs*(ipages-iotta))::bigint END AS raw_waste
FROM
  index_bloat) bloat_summary
ORDER BY raw_waste DESC, bloat DESC
`

  let output = yield pg.psql.exec(db, query)
  process.stdout.write(output)
}
github heroku / heroku-pg-extras / lib / util.js View on Github external
function * ensurePGStatStatement (db) {
  let query = `
SELECT exists(
  SELECT 1 FROM pg_extension e LEFT JOIN pg_namespace n ON n.oid = e.extnamespace
  WHERE e.extname='pg_stat_statements' AND n.nspname = 'public'
) AS available`
  let output = yield pg.psql.exec(db, query)

  if (!output.includes('t')) {
    throw new Error(`pg_stat_statements extension need to be installed in the public schema first.
You can install it by running:

    CREATE EXTENSION pg_stat_statements;`)
  }
}

@heroku-cli/plugin-pg-v5

Heroku CLI plugin to manage Postgres.

ISC
Latest version published 8 months ago

Package Health Score

74 / 100
Full package analysis