Re: Add a hook for handling logical decoding messages on subscribers.

From: Bharath Rupireddy <bharath(dot)rupireddyforpostgres(at)gmail(dot)com>
To: Amit Kapila <amit(dot)kapila16(at)gmail(dot)com>
Cc: Masahiko Sawada <sawada(dot)mshk(at)gmail(dot)com>, Chao Li <li(dot)evan(dot)chao(at)gmail(dot)com>, Fujii Masao <masao(dot)fujii(at)gmail(dot)com>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Add a hook for handling logical decoding messages on subscribers.
Date: 2026-09-21 16:20:00
Message-ID: CALj2ACVj+khUL=8Y3ukqJ6uF8cr0kmY6Q++fxJKYZ4P4bww=AA@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

On Sun, Sep 20, 2026 at 11:24 PM Amit Kapila <amit(dot)kapila16(at)gmail(dot)com> wrote:
>
> 3.
> Restrict who can emit messages
> + on the publisher. Revoke the default privilege and grant it to a
> role created
>
> That above security recipe sounds reasonable, but it asks the
> subscriber's security to be enforced by an action in a cluster the
> subscriber admin frequently does not control. The Security section
> should say something on the lines of: if you don't administer both the
> subscriber and publisher, don't install a message handler that does
> anything you wouldn't let every publisher role do. Also, I think the
> above REVOKE has to be performed in all databases separately.
>
> 4. Based on above two points, I think we can fill the hole with something like:
>
> CREATE SUBSCRIPTION ... WITH (messages = true, message_handler_role =
> 'msg_handler')
>
> and then run the hook under SwitchToUntrustedUser(that_role). That
> gives the subscriber admin a local, explicit privilege mechanism that
> does not depend on the publisher's ACLs, doesn't touch WAL or the
> protocol, and reuses machinery already in usercontext.c. If the option
> is unset, either don't call the hook or keep today's behavior with the
> documented warning.

I like this idea more than all the other approaches. The user
installing the hook would have to make a conscious choice, and even if
the role applying the message is more privileged than the emitter on
the publisher, that is the user's choice. Also, I don't think
defaulting the message handler role to the subscription owner (which
can lead to the same security concern) or not calling the hook
silently (which can lead to silent data loss) are good defaults. So, I
think requiring the handler role to be specified by the user is a good
choice, in my opinion.

That said, not having a default role would make the ALTER SUBSCRIPTION
code for this option a bit more complex I guess (I haven't checked it
in depth though).

On Sun, Sep 20, 2026 at 11:54 PM Amit Kapila <amit(dot)kapila16(at)gmail(dot)com> wrote:
>
> One more comment:
> +static void
> +apply_handle_message(StringInfo s)
> +{
> + LogicalRepMessageData msg;
> +
> + /* Tablesync worker should never receive MESSAGE */
> + if (am_tablesync_worker())
> + ereport(ERROR,
> + errcode(ERRCODE_PROTOCOL_VIOLATION),
> + errmsg_internal("tablesync worker received a MESSAGE message"));
>
> I think we should have a similar ERROR for parallel apply worker. I
> don't think this message can reach there but we should protect it like
> above.

Also, error out for the sequence sync worker?

--
Bharath Rupireddy
Amazon Web Services: https://aws.amazon.com

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Greg Burd 2026-09-21 16:24:45 [PATCH] unrecognized win32 error 448 (ERROR_UNTRUSTED_MOUNT_POINT) breaks tablespaces on Win11 26200
Previous Message Melanie Plageman 2026-09-21 16:18:41 Re: eliminate xl_heap_visible to reduce WAL (and eventually set VM on-access)