In Postgres 9.6, Why GIN index is not used for a JSONB column with text/int array?












0















In Postgres 9.6, why the GIN index is not used on my SELECT query below for a JSONB column with text/int array? How do i force Postgres to us GIN index?




SET enable_seqscan to FALSE;



EXPLAIN ANALYZE SELECT * FROM items WHERE int_array_cast(metadata->>'types') @> '{52, 53}'



Output of EXPLAIN



Seq Scan on items  (cost=10000000000.00..10000000016.07 rows=1 width=2391) (actual time=0.073..0.117 rows=1 loops=1)
Filter: (int_array_cast((metadata ->> 'types'::text)) @> '{10,14}'::integer)
Rows Removed by Filter: 37
Planning Time: 0.201 ms
Execution Time: 0.197 ms


Table Structure



CREATE TABLE "items" ( 
"item_uuid" UUid NOT NULL,
"metadata" JSONB,
PRIMARY KEY ("item_uuid")
);


int_array_cast function defintion



CREATE OR REPLACE FUNCTION int_array_cast(TEXT) RETURNS INT 
AS
$$
SELECT CAST($1 AS INT)
$$
IMMUTABLE
LANGUAGE SQL
RETURNS NULL ON NULL INPUT


Index created using GIN



CREATE INDEX IF NOT EXISTS items_metadata_dok_index ON items USING GIN(int_array_cast(metadata->>'types'))


Sample items in the table



item_uuid | metadata



    1 | {"types":"{1,2}", "name": "item_name1"}
2 | {"types":"{10,11}", "name": "item_name2"}
3 | {"types":"12", "name": "item_name3"}
3 | {"name": "item_name4"}








share







New contributor




Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    0















    In Postgres 9.6, why the GIN index is not used on my SELECT query below for a JSONB column with text/int array? How do i force Postgres to us GIN index?




    SET enable_seqscan to FALSE;



    EXPLAIN ANALYZE SELECT * FROM items WHERE int_array_cast(metadata->>'types') @> '{52, 53}'



    Output of EXPLAIN



    Seq Scan on items  (cost=10000000000.00..10000000016.07 rows=1 width=2391) (actual time=0.073..0.117 rows=1 loops=1)
    Filter: (int_array_cast((metadata ->> 'types'::text)) @> '{10,14}'::integer)
    Rows Removed by Filter: 37
    Planning Time: 0.201 ms
    Execution Time: 0.197 ms


    Table Structure



    CREATE TABLE "items" ( 
    "item_uuid" UUid NOT NULL,
    "metadata" JSONB,
    PRIMARY KEY ("item_uuid")
    );


    int_array_cast function defintion



    CREATE OR REPLACE FUNCTION int_array_cast(TEXT) RETURNS INT 
    AS
    $$
    SELECT CAST($1 AS INT)
    $$
    IMMUTABLE
    LANGUAGE SQL
    RETURNS NULL ON NULL INPUT


    Index created using GIN



    CREATE INDEX IF NOT EXISTS items_metadata_dok_index ON items USING GIN(int_array_cast(metadata->>'types'))


    Sample items in the table



    item_uuid | metadata



        1 | {"types":"{1,2}", "name": "item_name1"}
    2 | {"types":"{10,11}", "name": "item_name2"}
    3 | {"types":"12", "name": "item_name3"}
    3 | {"name": "item_name4"}








    share







    New contributor




    Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      0












      0








      0








      In Postgres 9.6, why the GIN index is not used on my SELECT query below for a JSONB column with text/int array? How do i force Postgres to us GIN index?




      SET enable_seqscan to FALSE;



      EXPLAIN ANALYZE SELECT * FROM items WHERE int_array_cast(metadata->>'types') @> '{52, 53}'



      Output of EXPLAIN



      Seq Scan on items  (cost=10000000000.00..10000000016.07 rows=1 width=2391) (actual time=0.073..0.117 rows=1 loops=1)
      Filter: (int_array_cast((metadata ->> 'types'::text)) @> '{10,14}'::integer)
      Rows Removed by Filter: 37
      Planning Time: 0.201 ms
      Execution Time: 0.197 ms


      Table Structure



      CREATE TABLE "items" ( 
      "item_uuid" UUid NOT NULL,
      "metadata" JSONB,
      PRIMARY KEY ("item_uuid")
      );


      int_array_cast function defintion



      CREATE OR REPLACE FUNCTION int_array_cast(TEXT) RETURNS INT 
      AS
      $$
      SELECT CAST($1 AS INT)
      $$
      IMMUTABLE
      LANGUAGE SQL
      RETURNS NULL ON NULL INPUT


      Index created using GIN



      CREATE INDEX IF NOT EXISTS items_metadata_dok_index ON items USING GIN(int_array_cast(metadata->>'types'))


      Sample items in the table



      item_uuid | metadata



          1 | {"types":"{1,2}", "name": "item_name1"}
      2 | {"types":"{10,11}", "name": "item_name2"}
      3 | {"types":"12", "name": "item_name3"}
      3 | {"name": "item_name4"}








      share







      New contributor




      Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      In Postgres 9.6, why the GIN index is not used on my SELECT query below for a JSONB column with text/int array? How do i force Postgres to us GIN index?




      SET enable_seqscan to FALSE;



      EXPLAIN ANALYZE SELECT * FROM items WHERE int_array_cast(metadata->>'types') @> '{52, 53}'



      Output of EXPLAIN



      Seq Scan on items  (cost=10000000000.00..10000000016.07 rows=1 width=2391) (actual time=0.073..0.117 rows=1 loops=1)
      Filter: (int_array_cast((metadata ->> 'types'::text)) @> '{10,14}'::integer)
      Rows Removed by Filter: 37
      Planning Time: 0.201 ms
      Execution Time: 0.197 ms


      Table Structure



      CREATE TABLE "items" ( 
      "item_uuid" UUid NOT NULL,
      "metadata" JSONB,
      PRIMARY KEY ("item_uuid")
      );


      int_array_cast function defintion



      CREATE OR REPLACE FUNCTION int_array_cast(TEXT) RETURNS INT 
      AS
      $$
      SELECT CAST($1 AS INT)
      $$
      IMMUTABLE
      LANGUAGE SQL
      RETURNS NULL ON NULL INPUT


      Index created using GIN



      CREATE INDEX IF NOT EXISTS items_metadata_dok_index ON items USING GIN(int_array_cast(metadata->>'types'))


      Sample items in the table



      item_uuid | metadata



          1 | {"types":"{1,2}", "name": "item_name1"}
      2 | {"types":"{10,11}", "name": "item_name2"}
      3 | {"types":"12", "name": "item_name3"}
      3 | {"name": "item_name4"}






      postgresql postgresql-performance postgresql-9.6





      share







      New contributor




      Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.










      share







      New contributor




      Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.








      share



      share






      New contributor




      Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 3 mins ago









      Bharathi RamBharathi Ram

      11




      11




      New contributor




      Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Bharathi Ram is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes












          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "182"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });






          Bharathi Ram is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233797%2fin-postgres-9-6-why-gin-index-is-not-used-for-a-jsonb-column-with-text-int-arra%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Bharathi Ram is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Bharathi Ram is a new contributor. Be nice, and check out our Code of Conduct.













          Bharathi Ram is a new contributor. Be nice, and check out our Code of Conduct.












          Bharathi Ram is a new contributor. Be nice, and check out our Code of Conduct.
















          Thanks for contributing an answer to Database Administrators Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f233797%2fin-postgres-9-6-why-gin-index-is-not-used-for-a-jsonb-column-with-text-int-arra%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Liste der Baudenkmale in Friedland (Mecklenburg)

          Single-Malt-Whisky

          Czorneboh