Postgres partial index on IS NULL not working












0















CREATE TABLE tickets (
id bigserial primary key,
state character varying,
closed timestamp
);

CREATE INDEX "state_index" ON "tickets" ("state")
WHERE ((state)::text = 'open'::text));


A query with a condition on state performs well using an index scan as expected:



explain analyze select * from tickets where state = 'open';

Index Scan using state_index on tickets (cost=0.29..16093.57 rows=36599 width=212) (actual time=0.025..22.875 rows=37346 loops=1)
Planning time: 0.212 ms
Execution time: 25.697 ms


I am trying to achieve the same or better performance for the query with the condition closed IS NULL:



select * from tickets where closed IS NULL;


However, my partial indexes does not result in a single index scan like the first query.



CREATE INDEX  "closed_index" ON "tickets"  ("closed") WHERE (closed IS NULL)

explain analyze select * from tickets where closed IS NULL;

Bitmap Heap Scan on tickets (cost=604.22..38697.91 rows=36559 width=212) (actual time=12.879..48.780 rows=37348 loops=1)
Recheck Cond: (closed IS NULL)
Heap Blocks: exact=14757
-> Bitmap Index Scan on closed_index (cost=0.00..595.09 rows=36559 width=0) (actual time=7.585..7.585 rows=37348 loops=1)
Planning time: 4.831 ms
Execution time: 52.068 ms








share



























    0















    CREATE TABLE tickets (
    id bigserial primary key,
    state character varying,
    closed timestamp
    );

    CREATE INDEX "state_index" ON "tickets" ("state")
    WHERE ((state)::text = 'open'::text));


    A query with a condition on state performs well using an index scan as expected:



    explain analyze select * from tickets where state = 'open';

    Index Scan using state_index on tickets (cost=0.29..16093.57 rows=36599 width=212) (actual time=0.025..22.875 rows=37346 loops=1)
    Planning time: 0.212 ms
    Execution time: 25.697 ms


    I am trying to achieve the same or better performance for the query with the condition closed IS NULL:



    select * from tickets where closed IS NULL;


    However, my partial indexes does not result in a single index scan like the first query.



    CREATE INDEX  "closed_index" ON "tickets"  ("closed") WHERE (closed IS NULL)

    explain analyze select * from tickets where closed IS NULL;

    Bitmap Heap Scan on tickets (cost=604.22..38697.91 rows=36559 width=212) (actual time=12.879..48.780 rows=37348 loops=1)
    Recheck Cond: (closed IS NULL)
    Heap Blocks: exact=14757
    -> Bitmap Index Scan on closed_index (cost=0.00..595.09 rows=36559 width=0) (actual time=7.585..7.585 rows=37348 loops=1)
    Planning time: 4.831 ms
    Execution time: 52.068 ms








    share

























      0












      0








      0








      CREATE TABLE tickets (
      id bigserial primary key,
      state character varying,
      closed timestamp
      );

      CREATE INDEX "state_index" ON "tickets" ("state")
      WHERE ((state)::text = 'open'::text));


      A query with a condition on state performs well using an index scan as expected:



      explain analyze select * from tickets where state = 'open';

      Index Scan using state_index on tickets (cost=0.29..16093.57 rows=36599 width=212) (actual time=0.025..22.875 rows=37346 loops=1)
      Planning time: 0.212 ms
      Execution time: 25.697 ms


      I am trying to achieve the same or better performance for the query with the condition closed IS NULL:



      select * from tickets where closed IS NULL;


      However, my partial indexes does not result in a single index scan like the first query.



      CREATE INDEX  "closed_index" ON "tickets"  ("closed") WHERE (closed IS NULL)

      explain analyze select * from tickets where closed IS NULL;

      Bitmap Heap Scan on tickets (cost=604.22..38697.91 rows=36559 width=212) (actual time=12.879..48.780 rows=37348 loops=1)
      Recheck Cond: (closed IS NULL)
      Heap Blocks: exact=14757
      -> Bitmap Index Scan on closed_index (cost=0.00..595.09 rows=36559 width=0) (actual time=7.585..7.585 rows=37348 loops=1)
      Planning time: 4.831 ms
      Execution time: 52.068 ms








      share














      CREATE TABLE tickets (
      id bigserial primary key,
      state character varying,
      closed timestamp
      );

      CREATE INDEX "state_index" ON "tickets" ("state")
      WHERE ((state)::text = 'open'::text));


      A query with a condition on state performs well using an index scan as expected:



      explain analyze select * from tickets where state = 'open';

      Index Scan using state_index on tickets (cost=0.29..16093.57 rows=36599 width=212) (actual time=0.025..22.875 rows=37346 loops=1)
      Planning time: 0.212 ms
      Execution time: 25.697 ms


      I am trying to achieve the same or better performance for the query with the condition closed IS NULL:



      select * from tickets where closed IS NULL;


      However, my partial indexes does not result in a single index scan like the first query.



      CREATE INDEX  "closed_index" ON "tickets"  ("closed") WHERE (closed IS NULL)

      explain analyze select * from tickets where closed IS NULL;

      Bitmap Heap Scan on tickets (cost=604.22..38697.91 rows=36559 width=212) (actual time=12.879..48.780 rows=37348 loops=1)
      Recheck Cond: (closed IS NULL)
      Heap Blocks: exact=14757
      -> Bitmap Index Scan on closed_index (cost=0.00..595.09 rows=36559 width=0) (actual time=7.585..7.585 rows=37348 loops=1)
      Planning time: 4.831 ms
      Execution time: 52.068 ms






      postgresql index postgresql-performance





      share












      share










      share



      share










      asked 5 mins ago









      GeekJockGeekJock

      1134




      1134






















          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
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229924%2fpostgres-partial-index-on-is-null-not-working%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
















          draft saved

          draft discarded




















































          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%2f229924%2fpostgres-partial-index-on-is-null-not-working%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

          SQL Server 17 - Attemping to backup to remote NAS but Access is denied

          Always On Availability groups resolving state after failover - Remote harden of transaction...

          Restoring from pg_dump with foreign key constraints