Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1edjec-0002Nm-SG for pgsql-general@arkaria.postgresql.org; Mon, 22 Jan 2018 21:29:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1edjec-0008P9-Ew for pgsql-general@arkaria.postgresql.org; Mon, 22 Jan 2018 21:29:22 +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 1edjeb-0008Ow-Gz for pgsql-general@lists.postgresql.org; Mon, 22 Jan 2018 21:29:22 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1edjeY-0001g5-Ap for pgsql-general@postgresql.org; Mon, 22 Jan 2018 21:29:20 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 52E0B211C6; Mon, 22 Jan 2018 16:29:15 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Mon, 22 Jan 2018 16:29:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=AwVlmPIPxy5mrTACUabaEqDMlHH0T IG3RnUFihUORO8=; b=WG/1o2w7Lv4vr+hueK7aPZnsklW10tX5VVYRO2z45R322 SdpkBgzhvNEO9MIrRM7WvJErF9payDNNzgJalDbuGb970mT+y9O3bM3mcmz6Je4b FVlrJz/4OJDWDv8qcCx6Pn3dhsZh0MQilYySRHnbdC3cc73W4GBBNX+vtH8T8b+h d2CJBIapdcX+Ly7pCTOAFWAnmM3+/1ojA2EmKQbOCt2UAa4egJ9wvFa9lSFs+v5O XwRrpfgSyixso930nbv9lshPflgH+d01TjLWRwNbHYY8dr8TdM+DyKNGtfEQ1MQm 9h03Yc6nCN8h679goSNjq3ZF7Am2xAbGnj5LUDqNQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=AwVlmP IPxy5mrTACUabaEqDMlHH0TIG3RnUFihUORO8=; b=sUgmnZDcSSaO0tuxGKHxsZ PvSxBRfAe7xhqd0+0/i0jyaiN6p2p4aO0OTZgkr5SmGw9BgppPxqQJ8q3tZobXyh lcf/P8M6X80j0PtOgUP2pNaufZkNSf9lShk2y5OJysSkGQOspBfTsW6yxWcWEVmy wUd4GtQB5RYnL/GhI7N2KgYSObXdV9fCaN2y+0pUTT/ElWms3Sydd6ksLoOgNRyU I1B/g0DQGm3NNsK5tdevcs4Asu/aDkXA35xI7ydFRcWt/gyNrOZqU+5EqAtG29f0 yvAUlSQKqzWjg6Y6JkYGfFIDZ02d+ATSdsu5VMt2fjoK3RMe8mx41E1tmy1j7vZA == X-ME-Sender: Received: from ranger.suse (unknown [67.40.177.89]) by mail.messagingengine.com (Postfix) with ESMTPA id 8E48C7E38D; Mon, 22 Jan 2018 16:29:14 -0500 (EST) Subject: Re: Slow alter sequence with PG10.1 To: =?UTF-8?Q?Michael_Kr=c3=bcger?= , pgsql-general@postgresql.org References: From: Adrian Klaver Message-ID: <2ab64bf6-b3e3-3ae9-8c69-0eb58ebca3c1@aklaver.com> Date: Mon, 22 Jan 2018 13:29:13 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 01/22/2018 07:24 AM, Michael Krüger wrote: > Dear community, > > I'm using PG10.1 on CentOS Linux release 7.4.1708 (Core) after upgrading > it from PG9.6.6. My application heavily uses sequences and requires > different increments of sequence numbers, e.g. a range of 100, 1000 or > 5000 numbers, so it is not possible to set a fixed increment on a > sequence that can be used by my application. > > With PG10.1 the performance has dropped seriously so that my application > becomes unusable. After investigating different aspects, I was able to > isolate the issue to be related to the sequences in Postgres 10.1. > > Below shows a simple test script showing the problem: > -- 1) Create a sequence > CREATE SEQUENCE my_sequence_1 INCREMENT BY 1 MINVALUE 1 NO MAXVALUE > START WITH 1 CYCLE; > > -- 2) Create a function that allows to request a number range > CREATE OR REPLACE FUNCTION multi_nextval( > use_seqname text, > use_increment integer) >     RETURNS bigint >     LANGUAGE 'plpgsql' >     COST 100 >     VOLATILE > AS $BODY$ > DECLARE >     reply int8; >     lock_id int4; > BEGIN >     SELECT oid::int4 INTO lock_id FROM pg_class WHERE relname = > split_part(use_seqname, '.', 2); >     perform pg_advisory_lock(lock_id); >     execute 'ALTER SEQUENCE ' || use_seqname || ' INCREMENT BY ' || > use_increment::text; >     reply := nextval(use_seqname); >     execute 'ALTER SEQUENCE ' || use_seqname || ' INCREMENT BY 1'; >     perform pg_advisory_unlock(lock_id); >     return reply - use_increment + 1; > END; > $BODY$; Not entirely sure I understand how the batching above works, still maybe something like this: CREATE OR REPLACE FUNCTION public.multi_nextval(use_seqname text, use_increment integer) RETURNS bigint LANGUAGE plpgsql AS $function$ DECLARE reply int8; lock_id int4; seq_idx int8 :=nextval(use_seqname); BEGIN SELECT oid::int4 INTO lock_id FROM pg_class WHERE relname = split_part(use_seqname, '.', 2); perform pg_advisory_lock(lock_id); perform setval(use_seqname, seq_idx + use_increment, 't'); reply := nextval(use_seqname); perform pg_advisory_unlock(lock_id); return reply; END; $function$ On an older laptop this does the 20000 loops in about 1.6 secs. > > -- 3) Loop 20000 times and request 5000 values each time > DO $$ > DECLARE > -- >   i_index integer; >   i_value bigint; > BEGIN >   FOR i_index IN select * from generate_series(1,20000,1) >   LOOP >     SELECT multi_nextval('my_sequence_1',5000) INTO i_value ; >     if (i_index % 250 = 0) THEN >       raise notice 'Loop: % - NextVal: %', i_index, i_value; >     end if; >   END LOOP; > END$$; > > On my computer I tried this code on PG9.6.6 and it executed in roughly 3 > seconds. > When running it on PG10.1 it takes over 7 minutes. > > Further investigation showed that the problem is related to ALTER > SEQUENCE... > > I can't believe that PG10.1 was changed that dramatically without > providing a workaround or a way to switch to the old PG9.6 performance, > at least I can't find anything in the documentation. > > Is this a bug? > > Thanks in advance, > Michael > > > > > > > -- > Email:   michael@kruegers.email > Mobile: 0152 5891 8787 -- Adrian Klaver adrian.klaver@aklaver.com