Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1aK7W2-00065P-T0 for pgsql-bugs@arkaria.postgresql.org; Fri, 15 Jan 2016 16:46:22 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1aK7W2-0001Qi-Fd for pgsql-bugs@arkaria.postgresql.org; Fri, 15 Jan 2016 16:46:22 +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_SHA384:256) (Exim 4.84) (envelope-from ) id 1aJoqM-0006TR-9S for pgsql-bugs@postgresql.org; Thu, 14 Jan 2016 20:50:06 +0000 Received: from wrigleys.postgresql.org ([204.145.124.246]) by magus.postgresql.org with esmtp (Exim 4.84) (envelope-from ) id 1aJoqJ-0006aO-7q for pgsql-bugs@postgresql.org; Thu, 14 Jan 2016 20:50:05 +0000 MIME-Version: 1.0 Subject: BUG #13868: Strange performance of type conversion in jsonb operations To: pgsql-bugs@postgresql.org From: xtracoder@gmail.com Date: Thu, 14 Jan 2016 20:49:44 +0000 Message-ID: <20160114204944.2970.25153@wrigleys.postgresql.org> X-Pg-Spam-Score: -0.3 (/) Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit 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 The following bug has been logged on the website: Bug reference: 13868 Logged by: Xtra Coder Email address: xtracoder@gmail.com PostgreSQL version: 9.5.0 Operating system: Windows Description: I suspect these 3 questions arise from some kind of bugs, because actual result seems somewhat weird to me: * why implicit type cast int=jsonb works, whereas explicit type cast via ::int or cast(jsonb as int) says: 'cannot cast type jsonb to integer' * why assignment int=text has same performance as int=int - the first one should have some significant performance penalty for parcing of text to int * why assignment int=jsonb->'int_field' is significantly slower than int=jsonb->>'int_field' - to my mind it should be opposite because value is already stored as int in jsonb To check if i'm missing something i've asked question at stackoverflow, but received now answer: http://stackoverflow.com/questions/34716098/strange-performance-of-postgresql-type-conversion-in-jsonb-operations Copy-pasting entire question here just for reference: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - I'm thinking about implementation of 'global consts' via jsonb stored in a table. To check what can be expected in terms of performance I've created some simple perf test, which revealed same questions that I have no clear answer. - why implicit type cast `int=jsonb` works, whereas explicit type cast via `::int` or `cast(jsonb as int)` says: '_cannot cast type jsonb to integer_' - why assignment `int=text` has same performance as `int=int` - _the first one should have some significant performance penalty for parcing of text to int_ - why assignment `int=jsonb->'int_field'` is significantly slower than `int=jsonb->>'int_field'` - _to my mind it should be opposite because value is already stored as int in jsonb_ PS: extraction of JSONB from DB is rather quick - reading 10K jsonb takes 1 sec per 100K cycles. After that I will need rather excessive access to dictionary of `name->int` values and looking for the most performant implementation. The test: DO LANGUAGE plpgsql $$ DECLARE json_struct jsonb; json_primitive jsonb; str_val text; int_val int; int_val2 int; count int; BEGIN count = 0; json_struct = '{"val": 11}'; json_primitive = 11; str_val = 11; int_val = json_struct->'val'; raise notice 'int_val = %', int_val; int_val = json_struct->>'val'; raise notice 'int_val = %', int_val; LOOP --int_val = count; -- 90ms --int_val2 = int_val; -- 111ms = +21ms --int_val = json_primitive; -- 130ms = +40ms --int_val = json_struct->'val'; -- 160ms = +70ms --json_primitive = json_struct->'val'; -- 120ms = +30ms --int_val = str_val; -- 111ms = +21ms --int_val = json_struct->>'val'; -- 125ms = +35ms --int_val = cast(json_primitive as int); -- ERROR: cannot cast type jsonb to integer --int_val = json_primitive::int; -- ERROR: cannot cast type jsonb to integer --int_val = json_primitive::text::int; -- 140ms = +50ms count = count + 1; EXIT WHEN count > 100000; END LOOP; END; $$ -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs