Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hnlYm-0003N8-TP for pgsql-general@arkaria.postgresql.org; Wed, 17 Jul 2019 15:09:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hnlYl-0002ZR-Ni for pgsql-general@arkaria.postgresql.org; Wed, 17 Jul 2019 15:09:35 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hnlYl-0002Wj-DP for pgsql-general@lists.postgresql.org; Wed, 17 Jul 2019 15:09:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hnlYi-0002Ks-Oy for pgsql-general@postgresql.org; Wed, 17 Jul 2019 15:09:35 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x6HF9TJb020054; Wed, 17 Jul 2019 11:09:29 -0400 From: Tom Lane To: Volkan Unsal cc: pgsql-general@postgresql.org Subject: Re: Removing a key from jsonb is sloooow In-reply-to: References: Comments: In-reply-to Volkan Unsal message dated "Wed, 17 Jul 2019 10:30:40 -0400" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <20052.1563376169.1@sss.pgh.pa.us> Date: Wed, 17 Jul 2019 11:09:29 -0400 Message-ID: <20053.1563376169@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Volkan Unsal writes: > I'm trying to remove a key from a jsonb column in a table with 10K rows, > and the performance is abysmal. When the key is missing, it takes 5 > minutes. When the key is present, it takes even longer. How wide are the jsonb values? It seems likely that most of this is TOAST overhead [1], ie time to reassemble wide jsonb values and then split them up again. As Tumasgiu already mentioned, it'd likely be useful to suppress updates of rows that don't actually need to change, assuming that the key appears in a minority of rows. And an index could help even more, by avoiding the need to reconstruct wide values to see if the key appears in them. Of course, if most of the rows need an update, neither of these will help and you just gotta live with it. Possibly reconsider your approach of using a large JSONB value to contain fields you need to update individually. That's never going to be great for performance. SQL (or at least Postgres) is incapable of updating portions of columns efficiently. regards, tom lane [1] https://www.postgresql.org/docs/current/storage-toast.html