Skip to content

Expose API to subscribe to Postgres notice messages #570

Closed
@fkomauli

Description

@fkomauli

Add a notices flux to PostgresqlConnection to receive Postgres notice messages.

Postgres may send notice messages through a connection, which may contain log information or metadata related to the submitted commands.

Example use case

A Postgres foreign data wrapper implementation may access external tables stored in multiple files (CSVs, proprietary formats, etc.), and it must send their metadata along with extracted records (type, size, data ranges, etc.). A straightforward implementation would use Postgres notices to send text messages to the client (e.g. in a parsable format like JSON).

Implementation

Notices travel on the same backend messaging subsystem used for notifications. The implementation needs to process NoticeResponses in a way similar to NotificationResponses.

This implementation choice, with the relative integration tests, is already available in a forked repository.

Example usage

import io.r2dbc.postgresql.api.PostgresqlConnection;
import io.r2dbc.postgresql.message.backend.Field;
import reactor.core.Disposable;
import java.util.function.Consumer;

public class Example {

    public static Disposable tryRegisterNoticesToConnection(PostgresqlConnection pgsql, Consumer<String> onNotice) {
        return pgsql.getNotices()
                .filter(notice -> notice.fields.containsKey(Field.FieldType.MESSAGE))
                .doOnNext(notice -> onNotice.accept(notice.fields.get(Field.FieldType.MESSAGE)))
                .subscribe();
    }
}

Other

I will open a pull request with the already implemented changes as soon as you accept to evaluate this feature

Best regards

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions