Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e0gub-0001Kg-Fc for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Oct 2017 04:40:29 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e0gua-0004tY-U2 for pgsql-bugs@arkaria.postgresql.org; Sat, 07 Oct 2017 04:40:28 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e0guZ-0004sV-Tx for pgsql-bugs@postgresql.org; Sat, 07 Oct 2017 04:40:28 +0000 Received: from burlywood.ash.relay.mailchannels.net ([23.83.222.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1e0guR-0000L3-Q1 for pgsql-bugs@postgresql.org; Sat, 07 Oct 2017 04:40:26 +0000 X-Sender-Id: a2hosting|x-authuser|bench@silentmedia.com Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id B537B200A15; Sat, 7 Oct 2017 04:40:14 +0000 (UTC) Received: from a2ss24.a2hosting.com (unknown [100.96.126.244]) (Authenticated sender: a2hosting) by relay.mailchannels.net (Postfix) with ESMTPA id B02B52009F3; Sat, 7 Oct 2017 04:40:08 +0000 (UTC) X-Sender-Id: a2hosting|x-authuser|bench@silentmedia.com Received: from a2ss24.a2hosting.com (a2ss24.a2hosting.com [172.20.82.214]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384) by 0.0.0.0:2500 (trex/5.9.14); Sat, 07 Oct 2017 04:40:14 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: a2hosting|x-authuser|bench@silentmedia.com X-MailChannels-Auth-Id: a2hosting X-Thread-Troubled: 21b5a1873f72ee04_1507351209024_1039830411 X-MC-Loop-Signature: 1507351209024:1768218922 X-MC-Ingress-Time: 1507351209023 Received: from 71-212-67-43.tukw.qwest.net ([71.212.67.43]:62690 helo=[10.2.3.3]) by a2ss24.a2hosting.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.89) (envelope-from ) id 1e0guF-003bcm-Nz; Sat, 07 Oct 2017 00:40:07 -0400 From: Ben Chobot Message-Id: <4367C2EE-ECE5-40C5-9EB6-C5375CB8D24E@silentmedia.com> Content-Type: multipart/alternative; boundary="Apple-Mail=_3A9B7850-688F-49AD-A635-86B613884DF4" Mime-Version: 1.0 (Mac OS X Mail 11.0 \(3445.1.6\)) Subject: Re: pg_logical_slot_peek_changes crashes postgres when called from inside pl/pgsql Date: Fri, 6 Oct 2017 21:40:06 -0700 In-Reply-To: <20171007043130.zronedebya6iwtum@alap3.anarazel.de> Cc: pgsql-bugs@postgresql.org To: Andres Freund References: <73FBA179-C68C-4540-9473-71E865408B15@silentmedia.com> <20171007043130.zronedebya6iwtum@alap3.anarazel.de> X-Mailer: Apple Mail (2.3445.1.6) X-AuthUser: bench@silentmedia.com List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --Apple-Mail=_3A9B7850-688F-49AD-A635-86B613884DF4 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii On Oct 6, 2017, at 9:31 PM, Andres Freund wrote: >=20 > Hi, >=20 > On 2017-10-04 13:59:20 -0700, Ben Chobot wrote: >> postgres=3D# CREATE OR REPLACE FUNCTION = logical_replication_slot_lsn_delta(slot text) RETURNS pg_lsn AS >> postgres-# $$ >> postgres$# BEGIN >> postgres$# return location from = pg_logical_slot_peek_changes(slot,null,1) limit 1; >> postgres$# END >> postgres$# $$ language plpgsql; >=20 > As the issue is fixed now, I just want to mention that looking at > logical decoding output via the SQL interface, especially when doing = it > in very small increments as you're suggesting here, is way much more > expensive than continually streaming changes via the replication > protocol. In a lot of cases it'll be orders of magnitude more = expensive. > So if you can change your usecase to use that, you'll benefit. It also > avoids having to change between peek/get, because you can just send = back > messages specifying up to where you've processed changes safely. Oh, for sure, and understood. When we actually pull data from the slot, = we'll be doing it via the streaming interface. This function is reduced = from what it was originally intended to be, which was an infrequent = check to an alerting system to make sure nobody had stopped consuming = data from their logical replication slot. FWIW, what we ended up with = was this SQL function, which would have been a little easier to follow = in pl/pgsql, but works just fine in this form: CREATE OR REPLACE FUNCTION logical_replication_slot_lsn_delta(slot text) = RETURNS numeric AS $$ select pg_current_xlog_location()- case when active then (select flush_location from pg_stat_replication where = pid=3Dactive_pid) else (select location from pg_logical_slot_peek_changes($1,null,1) union select pg_current_xlog_location() order by location limit 1) end from pg_replication_slots where slot_name=3D$1; $$ language sql security definer;= --Apple-Mail=_3A9B7850-688F-49AD-A635-86B613884DF4 Content-Transfer-Encoding: quoted-printable Content-Type: text/html; charset=us-ascii On = Oct 6, 2017, at 9:31 PM, Andres Freund <andres@anarazel.de> = wrote:

Hi,
On 2017-10-04 13:59:20 -0700, Ben Chobot = wrote:
postgres=3D# = CREATE OR REPLACE FUNCTION logical_replication_slot_lsn_delta(slot text) = RETURNS pg_lsn AS
postgres-# $$
postgres$# = BEGIN
postgres$#     return location = from pg_logical_slot_peek_changes(slot,null,1) limit 1;
postgres$# END
postgres$# $$ language = plpgsql;

As the issue is fixed = now, I just want to mention that looking at
logical = decoding output via the SQL interface, especially when doing it
in very small increments as you're suggesting here, is way = much more
expensive than continually streaming changes via = the replication
protocol. In a lot of cases it'll be = orders of magnitude more expensive.
So if you can change = your usecase to use that, you'll benefit. It also
avoids = having to change between peek/get, because you can just send back
messages specifying up to where you've processed changes = safely.

Oh, for sure, and understood. When we actually pull data from = the slot, we'll be doing it via the streaming interface. This function = is reduced from what it was originally intended to be, which was an = infrequent check to an alerting system to make sure nobody had stopped = consuming data from their logical replication slot. FWIW, what we ended = up with was this SQL function, which would have been a little easier to = follow in pl/pgsql, but works just fine in this form:

CREATE OR REPLACE FUNCTION = logical_replication_slot_lsn_delta(slot text) RETURNS numeric = AS
$$
  select = pg_current_xlog_location()-
  case = when active then
    (select = flush_location from pg_stat_replication where pid=3Dactive_pid)
  else
    = (select location from pg_logical_slot_peek_changes($1,null,1) = union
    select = pg_current_xlog_location() order by location limit 1)
  end
  from = pg_replication_slots where slot_name=3D$1;
$$ = language sql security definer;
= --Apple-Mail=_3A9B7850-688F-49AD-A635-86B613884DF4--