Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: support NOTIFY, LISTEN, and UNLISTEN commands of postgresql #41522

Open
Tracked by #93550 ...
yuzefovich opened this issue Oct 10, 2019 · 24 comments · May be fixed by #48308
Open
Tracked by #93550 ...

sql: support NOTIFY, LISTEN, and UNLISTEN commands of postgresql #41522

yuzefovich opened this issue Oct 10, 2019 · 24 comments · May be fixed by #48308
Assignees
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-anchored-telemetry The issue number is anchored by telemetry references.

Comments

@yuzefovich
Copy link
Member

yuzefovich commented Oct 10, 2019

PostgreSQL has support for NOTIFY, LISTEN, and UNLISTEN commands which generate and listen for a notification, respectively. This is not a part of SQL standard; however, some ORMs (like go-pg) seems to be using it.

Jira issue: CRDB-5438

@yuzefovich yuzefovich added C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) A-sql-pgcompat Semantic compatibility with PostgreSQL labels Oct 10, 2019
@asubiotto
Copy link
Contributor

asubiotto commented Oct 11, 2019

The https://github.com/lib/pq test suite tests this as well

@yuzefovich yuzefovich added the X-anchored-telemetry The issue number is anchored by telemetry references. label Oct 15, 2019
@jordanlewis
Copy link
Member

I think that this would be pretty easy to implement with changefeeds.

Here's a mini proposal:

CREATE TABLE system.pgnotify (
    channel STRING PRIMARY KEY,
    payload STRING
);

When a user runs LISTEN x, which is an asynchronous command, we start a goroutine running a SQL changefeed against system.pgnotify:

EXPERIMENTAL CHANGEFEED FOR system.pgnotify;

The goroutine filters the changefeed for rows with channel equal to x, and sends them as asynchronous notifications to the pgwire connection.

When a user runs NOTIFY x, we run:

UPSERT INTO system.pgnotify VALUES (x, NULL);

When a user runs NOTIFY x 'value', we run:

UPSERT INTO system.pgnotify VALUES(x, 'value');

When a user runs UNLISTEN x, we stop the goroutine running the SQL changefeed for x.

Once changefeeds can be filtered in SQL, we can accordingly update the implementation to be more efficient.

@jordanlewis
Copy link
Member

Might be a decent Friday project!

@lsraj
Copy link

lsraj commented Dec 4, 2019

Our S3 lambda notifications are published into cockroach db. Monitoring lambda events with
LISTEN/NOTIFY cannot be done lacking cockroach db support for these APIs. I have tested
by using pq.NewListener/Listen() and they fail as:

NewListener - event : 3, err - pq: syntax error at or near "listen"
NewListener - event : 3, err - pq: syntax error at or near "listen"
NewListener - event : 3, err - pq: syntax error at or near "listen"

Are there any options or workaround?
Is there a timeline when this could be supported?

@jordanlewis jordanlewis linked a pull request May 2, 2020 that will close this issue
jordanlewis added a commit to jordanlewis/cockroach that referenced this issue May 4, 2020
Closes cockroachdb#41522.

This commit enables LISTEN/NOTIFY. Rangefeeds must be enabled.

One problem is that the 32 bit session ID is back to bite us
from cockroachdb#34520. We don't have a 32 bit identifier that's unique across all
sessions of the cluster. Currently, we report the NodeID for the PID of
the notifying process instead.

Release note (sql change): implement the LISTEN/NOTIFY Postgres
protocol; LISTEN, NOTIFY, UNLISTEN, and pg_notify.
@jmls
Copy link

jmls commented Sep 16, 2020

did this feature make it into a release ?

@sandstrom
Copy link

This is also useful for popular Rails ActiveJob adapters such as Good Job and Que, since they rely on NOTIFY/LISTEN.

https://github.com/bensheldon/good_job

@blathers-crl blathers-crl bot added the T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) label Nov 1, 2021
@vy-ton
Copy link
Contributor

vy-ton commented Nov 1, 2021

A Serverless user inquired about this feature.

Curious about @amruss or @shermanCRL thoughts on Jordan's suggestion to user changefeeds.

@rafiss seems like the issue was on a closed project so moving to SQL Experience temporarily

@lessless
Copy link

lessless commented Aug 19, 2023

Hey CockroachLabs,

I appreciate this is a big undertaking but also an important one. Could you please advise where it's on the priority list?

Cheers

@yuzefovich
Copy link
Member Author

AFAIK this feature hasn't been prioritized yet, so it won't be available at least until 24.1 (and perhaps even later), cc @dikshant

@lessless
Copy link

Understood. I believe that having that functionality would unlock a broader adoption for CockroachDB. For example we have two packages in our current codebase that rely on NOTIFY/LISTEN and I believe it's the case with many other products out there.

@dikshant
Copy link

dikshant commented Sep 29, 2023

We don't have plans to invest in this area right now. Could you tell us more about your use case? Some or most of this functionality can be achieved in CockroachDB today using Changefeeds:

Changefeed:
https://www.cockroachlabs.com/docs/stable/changefeed-for

Enterprise Changefeed:
https://www.cockroachlabs.com/docs/stable/create-changefeed

@jkthorne
Copy link

jkthorne commented Oct 1, 2023

I think the use case was listed in the comment above.

You have a codebase and cockroachdb is sold as a dropin replacement. The use case is taking an existing codebase with Notify/Listen and try it with cockroachdb.

@lessless
Copy link

lessless commented Oct 1, 2023

@dikshant if that's an official CockroachDB position, it's quite hard to comprehend. CDC is a completely separate feature that doesn't enable using CockroachDB with libraries relying on NOTIFY/LISTEN.

Is there anyone else who can provide more details as to why this functionality is not a part of the foreseeable future?

@benperiton
Copy link

From my POV, I use PG as a queue in some instances with https://github.com/graphile/worker and I'd rather have NOTIFY/LISTEN than polling. It is pretty much the only thing stopping me from moving over at the moment.

@sandstrom
Copy link

Our use-case is that we'd rather stay with our current work queue infrastructure running on Postgres, than switch to a database where we cannot use the current work queue system (https://github.com/bensheldon/good_job).

@maitredede
Copy link

Hello :)

An other use case (that should be a showcase) is Odoo, a big ERP using Postgresql. They use NOTIFY/LISTEN for scheduling and async operations. Since these features are not available in CRDB, Odoo does simply not start.

@Paillat-dev
Copy link

Paillat-dev commented Jan 19, 2024

Hello :)

An other use case (that should be a showcase) is Odoo, a big ERP using Postgresql. They use NOTIFY/LISTEN for scheduling and async operations. Since these features are not available in CRDB, Odoo does simply not start.

I'm here for the exact same reason

For reference: odoo/odoo#83730

@TeddyAlbina
Copy link

TeddyAlbina commented Feb 2, 2024

Our use-case is that we'd rather stay with our current work queue infrastructure running on Postgres, than switch to a database where we cannot use the current work queue system (https://github.com/bensheldon/good_job).

I have the same issue with the Hangfire library, i'm implementing cockroach and think CHANGEFEED can do the trick just fine

@lessless
Copy link

lessless commented Feb 2, 2024 via email

asg0451 added a commit to asg0451/cockroach that referenced this issue Jul 29, 2024
asg0451 added a commit to asg0451/cockroach that referenced this issue Jul 29, 2024
asg0451 added a commit to asg0451/cockroach that referenced this issue Jul 29, 2024
Built on top of#127818, which adds parsing
support, this patch adds the capability to send
asynchronous notifications to clients over the
pgwire protocol.

Part of: cockroachdb#41522

Release note: None
@asg0451 asg0451 self-assigned this Jul 29, 2024
asg0451 added a commit to asg0451/cockroach that referenced this issue Jul 30, 2024
Add a new system table, system.notifications, to
back the LISTEN/NOTIFY mechanism.

Part of: cockroachdb#41522

Release note: None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-enhancement Solution expected to add code/behavior + preserve backward-compat (pg compat issues are exception) T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-anchored-telemetry The issue number is anchored by telemetry references.
Projects
None yet
Development

Successfully merging a pull request may close this issue.