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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jmorecroft67/pg-stream-gcp

v0.20.0

Published

Google Cloud Platform (GCP) PubSub handler for pg-stream-core

Downloads

3

Readme

pg-stream-gcp

A PostgreSQL transaction log handler for the pg-stream-core library to enable logical replication streaming output to Google Cloud Platform (GCP) PubSub.

The pg-stream CLI utility is the simplest way to use the functionality in pg-stream-gcp without writing any code.

Usage

import * as pg from '@jmorecroft67/pg-stream-core';
import * as gcp from '@jmorecroft67/pg-stream-gcp';
import { pipe } from 'fp-ts/function';
import * as RTE from 'fp-ts/ReaderTaskEither';
import * as TE from 'fp-ts/TaskEither';
import { createLogger } from 'bunyan';

export const logger = createLogger({
  name: 'test',
  level: 'debug'
});

const handler = gcp.pubSubHandler({
  projectId: 'gcp-project-id',
  topicName: 'gcp-topic-name',
  noOrdering: false, // preserve message ordering per table
  includeAll: false, // only send table-related messages
  retryOptions: {
    // retry on failures
    delay: 1000, // 1 second initial wait
    exponentialBackoff: {
      // exponentially increase wait time
      capDelay: 30000 // max 30 second wait
    },
    maxRetries: 5 // abort after 5 retries
  }
});

const options: pg.RecvlogicalOptions = {
  host: 'localhost',
  port: 5432,
  database: 'postgres',
  username: 'postgres',
  password: 'topsecret',
  publicationName: 'testpub',
  slotName: 'testslot',
  xlogs: {
    handler,
    concurrency: 5
  },
  createTempSlotIfNone: true,
  createPublicationIfNone: true
};

TE.bracket(
  pg.open({ options, logger }),
  (conn) =>
    pipe(
      { conn, logger },
      pipe(
        pg.start(options, true),
        RTE.chain(() => pg.recvlogical(options))
      )
    ),
  (conn) => pg.close({ conn, logger })
)();

We can demonstrate this program by running it, then manipulating some data in our database, which we can do by running the test query program in the pg-stream-core README.

$ $(npm bin)/ts-node src/test-gcp.ts | bunyan &
$ $(npm bin)/ts-node src/test-query.ts | bunyan

The output of the first program is then:

[2022-05-23T01:23:50.149Z]  WARN: test/8825 on Jesss-MBP: SSL is not enabled on the Postgres server but connecting anyway.
[2022-05-23T01:23:50.149Z]  INFO: test/8825 on Jesss-MBP: Connection established. (host=localhost, port=5432)
[2022-05-23T01:23:50.184Z] DEBUG: test/8825 on Jesss-MBP: Startup sequence completed.
[2022-05-23T01:23:50.185Z] DEBUG: test/8825 on Jesss-MBP: Sending SQL query.
    sql: SELECT confirmed_flush_lsn FROM pg_replication_slots WHERE slot_name = 'testslot'
[2022-05-23T01:23:50.188Z]  INFO: test/8825 on Jesss-MBP: Command complete. (tag="SELECT 0")
[2022-05-23T01:23:50.188Z]  INFO: test/8825 on Jesss-MBP: Query complete. (result=[])
[2022-05-23T01:23:50.189Z] DEBUG: test/8825 on Jesss-MBP: Sending SQL query.
    sql: SELECT * FROM pg_publication WHERE pubname = 'testpub'
[2022-05-23T01:23:50.191Z]  INFO: test/8825 on Jesss-MBP: Command complete. (tag="SELECT 1")
[2022-05-23T01:23:50.191Z]  INFO: test/8825 on Jesss-MBP: Query complete.
    result: [{"oid":"16582","pubname":"testpub","pubowner":"10","puballtables":true,"pubinsert":true,"pubupdate":true,"pubdelete":true,"pubtruncate":true,"pubviaroot":false}]
[2022-05-23T01:23:50.191Z] DEBUG: test/8825 on Jesss-MBP: Sending SQL query.
    sql: CREATE_REPLICATION_SLOT testslot TEMPORARY LOGICAL pgoutput NOEXPORT_SNAPSHOT
[2022-05-23T01:23:50.197Z]  INFO: test/8825 on Jesss-MBP: Command complete. (tag=CREATE_REPLICATION_SLOT)
[2022-05-23T01:23:50.197Z]  INFO: test/8825 on Jesss-MBP: Query complete.
    result: [{"slot_name":"testslot","consistent_point":"0/1E4C450","snapshot_name":null,"output_plugin":"pgoutput"}]
[2022-05-23T01:24:14.128Z] DEBUG: test/8825 on Jesss-MBP: Sent message(s) to topic. (projectId=gcp-project-id, topicName=gcp-topic-name)
    acks: [
      "1"
    ]
[2022-05-23T01:24:14.128Z] DEBUG: test/8825 on Jesss-MBP: Sent message(s) to topic. (projectId=gcp-project-id, topicName=gcp-topic-name)
    acks: [
      "2"
    ]
[2022-05-23T01:24:14.128Z] DEBUG: test/8825 on Jesss-MBP: Sent message(s) to topic. (projectId=gcp-project-id, topicName=gcp-topic-name)
    acks: [
      "3"
    ]
[2022-05-23T01:24:14.128Z] DEBUG: test/8825 on Jesss-MBP: Sent message(s) to topic. (projectId=gcp-project-id, topicName=gcp-topic-name)
    acks: [
      "4"
    ]
[2022-05-23T01:24:14.128Z] DEBUG: test/8825 on Jesss-MBP: Sent message(s) to topic. (projectId=gcp-project-id, topicName=gcp-topic-name)
    acks: [
      "5"
    ]
kill -SIGINT 8825
[2022-05-23T01:24:26.145Z]  INFO: test/8825 on Jesss-MBP: Logical streaming complete.
[2022-05-23T01:24:26.146Z]  INFO: test/8825 on Jesss-MBP: Connection closed.

This is running against the GCP PubSub emulator, which is returning the acknowledgement IDs of "1", "2", etc. We can see from these logs that the five messages were sent in entirely separate batches, which makes sense since the number of database events (5) matched our concurrency setting.