1

Based on this answered question regarding pg-promise, when an existing connection/session is returned by request A to the pool and being re-used by a totally diff request B. Would pg-promise automatically do DISCARD so B won't see anything left by A? If not, can I issue it manually using pg-promise?

Thank you.

Jeb50
  • 6,272
  • 6
  • 49
  • 87

1 Answers1

1

Would pg-promise automatically do DISCARD?

No.

Can I issue it manually using pg-promise?

Yes, but for individual queries it would do you no good, because those control connection by themselves, so you would not even know which session you are discarding.

I can see when this might be of use only inside a task or tx methods, but there you can easily add your own DISCARD query at the end, if needed.

await db.task(async t => {

    // do your things here...

    // then run discard at the end, if needed:
    await t.none('DISCARD $1:value', ['PLANS']);
});
vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • "I can see when this might be of use only inside a task or tx methods, but there you can easily just add your own DISCARD query at the end." Haven't used pg-promise, would you throw a quick Node sample of `task`? – Jeb50 Feb 25 '21 at 00:23
  • @Jeb50 There ;) – vitaly-t Feb 25 '21 at 00:26
  • If task is expected to return something, say a `select from myfunc()`, then `DISCARD` must proceed the `return` as: `db.task(async t => { await t.none('DISCARD $1:value', ['TEMP']); return t.any('select * from myfunc()'); // return is needed. })...` – Jeb50 Feb 25 '21 at 19:28
  • @Jeb It's all consistent with the general promise usage (asynchronous paradigm). Whatever you return from the task callback, is what the task will resolve with. – vitaly-t Feb 25 '21 at 19:53