Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/backend/optimizer/path/indxpath.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
#include "utils/lsyscache.h"
#include "utils/selfuncs.h"

/* Hook for extensions to provide index clauses for unmatched OpExpr */
match_opclause_to_indexcol_hook_type match_opclause_to_indexcol_hook = NULL;

/* XXX see PartCollMatchesExprColl */
#define IndexCollMatchesExprColl(idxcollation, exprcollation) \
Expand Down Expand Up @@ -2964,12 +2966,17 @@ match_opclause_to_indexcol(PlannerInfo *root,
* function for the operator's underlying function.
*/
set_opfuncid(clause); /* make sure we have opfuncid */
return get_index_clause_from_support(root,
iclause = get_index_clause_from_support(root,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you repro the index scan not being chosen in PG?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dev-dsk-ruchask-1c-a99d43f3 % psql -h localhost -d postgres 
psql (18.3)
Type "help" for help.

postgres=# CREATE TABLE test_oid (id oid PRIMARY KEY, data text);
CREATE TABLE
postgres=#  INSERT INTO test_oid SELECT g, 'row ' || g FROM generate_series(1, 100000) g;
INSERT 0 100000
postgres=# EXPLAIN SELECT * FROM test_oid WHERE id = 12345;
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Index Scan using test_oid_pkey on test_oid  (cost=0.29..8.31 rows=1 width=36)
   Index Cond: (id = '12345'::oid)
(2 rows)

postgres=# EXPLAIN SELECT * FROM test_oid WHERE (id)::int = 12345;
                         QUERY PLAN                         
------------------------------------------------------------
 Seq Scan on test_oid  (cost=0.00..1399.84 rows=1 width=36)
   Filter: ((id)::integer = 12345)
(2 rows)

postgres=#  EXPLAIN SELECT * FROM test_oid WHERE (id)::int4 = 12345;
                         QUERY PLAN                         
------------------------------------------------------------
 Seq Scan on test_oid  (cost=0.00..1791.00 rows=1 width=13)
   Filter: ((id)::integer = 12345)
(2 rows)

postgres=# 

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With catalog table:

postgres=# EXPLAIN SELECT * FROM pg_class WHERE oid = 12345;
                                       QUERY PLAN                                      
  -------------------------------------------------------------------------------------
   Index Scan using pg_class_oid_index on pg_class  (cost=0.27..8.29 rows=1 width=277)
     Index Cond: (oid = '12345'::oid)
  (2 rows)
  
  postgres=#  EXPLAIN SELECT * FROM pg_class WHERE (oid)::int4 = 12345;
                          QUERY PLAN                         
  -----------------------------------------------------------
   Seq Scan on pg_class  (cost=0.00..19.19 rows=1 width=277)
     Filter: ((oid)::integer = 12345)
  (2 rows)
  

rinfo,
clause->opfuncid,
0, /* indexarg on left */
indexcol,
index);
if (iclause)
return iclause;
if (match_opclause_to_indexcol_hook)
return match_opclause_to_indexcol_hook(root, rinfo, indexcol, index);
return NULL;
}

if (match_index_to_operand(rightop, indexcol, index) &&
Expand Down Expand Up @@ -3004,12 +3011,17 @@ match_opclause_to_indexcol(PlannerInfo *root,
* function for the operator's underlying function.
*/
set_opfuncid(clause); /* make sure we have opfuncid */
return get_index_clause_from_support(root,
iclause = get_index_clause_from_support(root,
rinfo,
clause->opfuncid,
1, /* indexarg on right */
indexcol,
index);
if (iclause)
return iclause;
if (match_opclause_to_indexcol_hook)
return match_opclause_to_indexcol_hook(root, rinfo, indexcol, index);
return NULL;
}

return NULL;
Expand Down
7 changes: 7 additions & 0 deletions src/include/optimizer/paths.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ typedef RelOptInfo *(*join_search_hook_type) (PlannerInfo *root,
List *initial_rels);
extern PGDLLIMPORT join_search_hook_type join_search_hook;

/* Hook for plugins to provide index clauses for unmatched OpExpr */
typedef IndexClause *(*match_opclause_to_indexcol_hook_type) (PlannerInfo *root,
RestrictInfo *rinfo,
int indexcol,
IndexOptInfo *index);
extern PGDLLIMPORT match_opclause_to_indexcol_hook_type match_opclause_to_indexcol_hook;


extern RelOptInfo *make_one_rel(PlannerInfo *root, List *joinlist);
extern RelOptInfo *standard_join_search(PlannerInfo *root, int levels_needed,
Expand Down
Loading