npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@agstack/storage-postgres

v1.0.0

Published

Production-grade PostgreSQL storage plugin for AGStack Runtime Kernel — batch writes, connection pooling, partitioning, retry, health monitoring

Downloads

96

Readme

@agstack/storage-postgres

Version 1.0.0

Production-grade PostgreSQL storage plugin for the AGStack Runtime Kernel. Batch writes, connection pooling, partitioning, retry with exponential backoff, health monitoring, and automatic schema migrations.

Architecture

Runtime Kernel
    ↓
Plugin Pipeline
    ↓
PostgresStoragePlugin.save(transaction)
    ↓
BatchManager (buffers in memory)
    ↓
Flush (batchSize or flushInterval)
    ↓
ConnectionPool (pg.Pool)
    ↓
Prepared Bulk INSERT ... ON CONFLICT DO UPDATE
    ↓
PostgreSQL

All database work happens in background workers. HTTP requests are never blocked.

Installation

npm install @agstack/storage-postgres

Quick Start

import { initialize, getKernel, shutdown } from "@agstack/logger";
import { PostgresStoragePlugin } from "@agstack/storage-postgres";

const storage = new PostgresStoragePlugin({
  host: "localhost",
  port: 5432,
  database: "agstack",
  username: "postgres",
  password: "secret",
  schema: "agstack",
  autoMigrate: true,
});

await initialize();

getKernel().registerPlugin(storage);

// The plugin will automatically batch and flush transactions
await storage.save(transaction);
await storage.saveBatch(transactions);

// Force flush remaining
await storage.flush();

await shutdown();

Configuration

| Option | Default | Description | |--------|---------|-------------| | host | localhost | PostgreSQL host | | port | 5432 | PostgreSQL port | | database | agstack | Database name | | schema | agstack | Database schema | | username | postgres | Database user | | password | "" | Database password | | ssl | false | SSL/TLS config | | poolMin | 2 | Minimum pool connections | | poolMax | 10 | Maximum pool connections | | poolIdleTimeoutMs | 30000 | Idle connection timeout | | batchSize | 100 | Transactions per batch insert | | flushIntervalMs | 5000 | Max interval between flushes | | retryMaxAttempts | 3 | Max retry attempts | | retryBaseDelayMs | 100 | Base delay for exponential backoff | | retryJitter | true | Enable jitter for retry delay | | partitionBy | daily | Partition strategy (daily/monthly/none) | | partitionRetentionDays | 30 | Days to retain partitions | | autoMigrate | true | Auto-run schema migrations | | healthCheckIntervalMs | 30000 | Health check interval | | applicationName | agstack-storage-postgres | PG application name | | debug | false | Enable debug logging |

Database Schema

The plugin automatically creates and migrates the transactions table:

  • 50+ columns covering request, response, headers, body, errors, events, metadata
  • JSONB fields for query, headers, errors, events, metadata, client_info, geo_info, etc.
  • GIN indexes on JSONB columns for efficient querying
  • B-tree indexes on correlation_id, trace_id, status, method, status_code, client_ip, path, started_at, duration
  • ON CONFLICT (id) DO UPDATE for idempotent inserts

Migrations

The plugin includes versioned migrations (currently v1-v3) that run automatically when autoMigrate: true. Migration history is tracked in _agstack_migrations table.

Partitioning

Supports daily or monthly partitioning of the transactions table:

  • partitionBy: "daily" — creates partitions like transactions_20240101
  • partitionBy: "monthly" — creates partitions like transactions_202401
  • partitionBy: "none" — no partitioning

Old partitions are automatically dropped after partitionRetentionDays.

Batch Manager

  • Transactions are buffered in memory and inserted in batches
  • Configurable batch size (default 100) and flush interval (default 5s)
  • Uses parameterized bulk INSERT with ON CONFLICT DO UPDATE
  • Failed batches go to dead-letter queue (configurable max size)
  • Dead-letter items can be retried via retryDeadLetter()

Retry Policy

Only transient failures are retried:

| Condition | Behavior | |-----------|----------| | Connection refused/timeout/reset | Retry with backoff | | Deadlock detected | Retry with backoff | | Too many connections | Retry with backoff | | Network errors (ECONNRESET, EPIPE, etc.) | Retry with backoff | | Syntax errors, constraint violations | No retry (immediate failure) | | Authentication failures | No retry |

Health Monitoring

const health = await storagePlugin.health();
console.log(health.status, health.metrics);

Exposed metrics:

  • uptimeMs — plugin uptime
  • transactionsProcessed — successful inserts
  • queueSize — pending batch items
  • errorRate — failure percentage
  • avgLatencyMs — average insert latency
  • totalCount, idleCount, waitingCount, activeCount — pool stats

Graceful Shutdown

// On shutdown:
await storagePlugin.flush();       // Flush remaining batches
await storagePlugin.shutdown();     // Close pool

The Runtime Kernel handles shutdown order automatically.

Testing

npm test

Tests cover: metadata validation, configuration, health reporting, retry policy (success, retry, permanent failure, transient detection), defaults, plugin interface compliance, config overrides, state transitions.

Benchmarks

node benchmarks/index.js

License

MIT