GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add `onConnect` callback which allows "setup" of a pooled client by brianc · Pull Request #3620 · brianc/node-postgres

Conversation

@brianc

@brianc brianc commented Mar 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Owner

This allows you to do

const pool = new Pool({
  onConnect: async (client) => await client.query('SET search_path=foo,public')
})

for example.

If the onConnect function throws or rejects the error will be sent out to the callsite of pool.connect or pool.query & the client will be disposed of. This only triggers when the client is first connected to postgres not when an existing connected client is checked out.

Why did I call it pool.connect so long ago instead of pool.getClient() or pool.aquire() or something? I don't know. All I know is I'm not very fond of myself from 10 years ago. Oh well! I'll make sure to include good documentation on this change before I actually merge it but want to put it up for some 👀

This should fix #3617

I omitted the other hook ideas I had as I need to think through them more, and in the case of onClose the release method is sync, and some times clients are closed in the background so error handling there is a different issue I'll need to think about.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
brianc added 5 commits March 2, 2026 16:08
@brianc
wip
0d9e1fd
@brianc
Initial connect lifecycle working
d9a83be
@brianc
Connect hook working
001771f
@brianc
Rename 'hooks.bla' to 'onBla'
a850c5c
@brianc
Add more tests
f733875
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Mar 2, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Deploying node-postgres with  Cloudflare Pages  Cloudflare Pages

Latest commit: dd906fe
Status: ✅  Deploy successful!
Preview URL: https://2715f9f7.node-postgres.pages.dev
Branch Preview URL: https://bmc-pool-callbacks.node-postgres.pages.dev

View logs

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brianc
More cleanup testing
ca8c336
@brianc brianc mentioned this pull request Mar 3, 2026
Future Support for Setup Queries When a New Pool Client Connects #3617
Closed
charmander
charmander reviewed Mar 3, 2026
View reviewed changes
Comment thread packages/pg-pool/index.js Outdated
maxLifetimeTimeout.unref()
client.once('end', () => clearTimeout(maxLifetimeTimeout))
if (this.options.onConnect) {
let hookResult

@charmander charmander Mar 3, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about a single promiseTry(this.options.onConnect).then(…, …) path? Where

const promiseTry = Promise.try ?? (f) => new Promise((resolve) => {
  resolve(f())
})

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@charmander charmander Mar 3, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess this gets a little more complicated if it has to support the custom Promise config option.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@charmander charmander Mar 3, 2026

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Note: I’m bringing this up ahead of publishing instead of making a follow-up PR because the behaviour is slightly different, so it would be a breaking change.)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brianc brianc Mar 3, 2026

Copy link
Copy Markdown
Owner Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

much appreciated! Though I'm not sure what you mean by a breaking change...what's a breaking change, exactly?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@charmander charmander Mar 4, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The promiseTry suggestion, compared to what’s currently in this PR. They’re slightly different from each other, so it couldn’t wait until afterwards, I mean.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brianc brianc Mar 4, 2026

Copy link
Copy Markdown
Owner Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yah i get it, i just don't understand how they'd be different enough to introduce a breaking change later. I'll try your approach & see if the tests still pass. I kinda approached this from a TDD perspective - mostly want a "pricipal of least surprise" where if your onConnect throws for any reaons (sync or async) the connection is just aborted & the error is propagated out to the caller of pool.connect / pool.query. Usually my first approach is "if the tests pass, i'm happy, and can optimize or change the internals later if needed." Anyways...will try your approach out now locally & see how it does.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brianc brianc Mar 4, 2026

Copy link
Copy Markdown
Owner Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@charmander updated to use Promise.try and still attempt to support BYOP. Though we're removing BYOP in 9.0. I'm going to work on the 9.0 roadmap today here by pulling in ideas and discussions from the discussion about it. I'm hoping to finalize it today, cut a branch tomorrow, and start working on it. I don't think its a TON of work & I plan on doing most or all of it...just wanted to share! thanks again for all you do! ❤️

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

charmander reacted with heart emoji
All reactions
  • ❤️ 1 reaction
@brianc
Use promise.try
dd906fe
charmander
charmander approved these changes Mar 4, 2026
View reviewed changes

@charmander charmander left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation looks good!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@charmander

charmander commented Mar 4, 2026

Copy link
Copy Markdown
Collaborator

I forgot verify existed until I saw it while reviewing this. Maybe that overlap should be addressed…?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brianc

brianc commented Mar 4, 2026

Copy link
Copy Markdown
Owner Author

yah i dunno what that verify thing is anymore, but imma deprecate it in a follow up PR once this ships as a kinda just better alternative I think. Will look into it more tomorrow. TY For the review!

charmander reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brianc
brianc merged commit 0c7040e into master Mar 4, 2026
15 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@brianc
brianc deleted the bmc/pool-callbacks branch March 4, 2026 23:52
@brianc brianc mentioned this pull request Mar 4, 2026
Docs updates #3622
Merged
@danieldiekmeier danieldiekmeier mentioned this pull request Mar 5, 2026
Don't encourage people to use pool.on('connect', ...) for setup anymore #3623
Merged
@brianc brianc mentioned this pull request Mar 6, 2026
feat: evict connections with open transactions on pool release #3597
Open
@coderabbitai coderabbitai Bot mentioned this pull request Jul 9, 2026
Cat2 Teja-web/food_delivery_app#5
Open
@coderabbitai coderabbitai Bot mentioned this pull request Jul 27, 2026
fix(auth): set search_path per connection, not only via startup options prisma/composer#185
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@charmander charmander charmander approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Future Support for Setup Queries When a New Pool Client Connects

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@brianc @charmander
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.