@@ -1037,15 +1037,20 @@ static void
10371037UserTableUpdateOpenIndexes ()
10381038{
10391039 List * recheckIndexes = NIL ;
1040+ #if PG_VERSION_NUM >=120000
1041+ HeapTuple tuple = ExecFetchSlotHeapTuple (slot , true, NULL );
1042+ #else
1043+ HeapTuple tuple = slot -> tts_tuple ;
1044+ #endif
10401045
10411046 /* HOT update does not require index inserts */
1042- if (HeapTupleIsHeapOnly (slot -> tts_tuple ))
1047+ if (HeapTupleIsHeapOnly (tuple ))
10431048 return ;
10441049
10451050 if (estate -> es_result_relation_info -> ri_NumIndices > 0 )
10461051 {
10471052 recheckIndexes = ExecInsertIndexTuples (slot ,
1048- & slot -> tts_tuple -> t_self ,
1053+ & tuple -> t_self ,
10491054 estate , false, NULL , NIL );
10501055
10511056 if (recheckIndexes != NIL )
@@ -1060,7 +1065,7 @@ static void begin_batch_insert(Oid oid)
10601065{
10611066 ResultRelInfo * resultRelInfo ;
10621067
1063- rel = heap_open (oid , NoLock );
1068+ rel = heap_open (oid , RowExclusiveLock );
10641069
10651070 PushActiveSnapshot (GetTransactionSnapshot ());
10661071
@@ -1075,7 +1080,9 @@ static void begin_batch_insert(Oid oid)
10751080 estate -> es_num_result_relations = 1 ;
10761081 estate -> es_result_relation_info = resultRelInfo ;
10771082 ExecOpenIndices (estate -> es_result_relation_info , false);
1078- #if PG_VERSION_NUM >=110000
1083+ #if PG_VERSION_NUM >=120000
1084+ slot = ExecInitExtraTupleSlot (estate , RelationGetDescr (rel ), & TTSOpsHeapTuple );
1085+ #elif PG_VERSION_NUM >=110000
10791086 slot = ExecInitExtraTupleSlot (estate , RelationGetDescr (rel ));
10801087#else
10811088 slot = ExecInitExtraTupleSlot (estate );
@@ -1086,8 +1093,13 @@ static void begin_batch_insert(Oid oid)
10861093static void insert_tuple (Datum * values , bool * nulls )
10871094{
10881095 HeapTuple tup = heap_form_tuple (RelationGetDescr (rel ), values , nulls );
1096+ #if PG_VERSION_NUM >=120000
1097+ ExecStoreHeapTuple (tup , slot , true);
1098+ simple_heap_insert (rel , ExecFetchSlotHeapTuple (slot , true, NULL ));
1099+ #else
10891100 ExecStoreTuple (tup , slot , InvalidBuffer , true);
10901101 simple_heap_insert (rel , slot -> tts_tuple );
1102+ #endif
10911103 UserTableUpdateOpenIndexes ();
10921104}
10931105
@@ -3162,7 +3174,11 @@ Datum vops_unnest(PG_FUNCTION_ARGS)
31623174 user_ctx -> nulls = (bool * )palloc (sizeof (bool )* n_attrs );
31633175 user_ctx -> types = (vops_type * )palloc (sizeof (vops_type )* n_attrs );
31643176 user_ctx -> tiles = (vops_tile_hdr * * )palloc (sizeof (vops_tile_hdr * )* n_attrs );
3177+ #if PG_VERSION_NUM >=120000
3178+ user_ctx -> desc = CreateTemplateTupleDesc (n_attrs );
3179+ #else
31653180 user_ctx -> desc = CreateTemplateTupleDesc (n_attrs , false);
3181+ #endif
31663182 func_ctx -> user_fctx = user_ctx ;
31673183 user_ctx -> n_attrs = n_attrs ;
31683184 user_ctx -> tile_pos = 0 ;
@@ -3721,10 +3737,6 @@ vops_pullvars_walker(Node *node, vops_pullvar_context *ctx)
37213737 ctx -> scope = scope ;
37223738 node = (Node * )from -> fromlist ;
37233739 }
3724- else if (IsA (node , Query ))
3725- {
3726- return query_tree_walker ((Query * )node , vops_pullvars_walker , ctx , 0 );
3727- }
37283740 (void ) expression_tree_walker (node , vops_pullvars_walker , ctx );
37293741 ctx -> scope = scope ;
37303742 return false;
@@ -3838,6 +3850,42 @@ vops_add_literal_type_casts(Node* node, Const** consts)
38383850 return node ;
38393851}
38403852
3853+ static RangeVar *
3854+ vops_get_join_rangevar (Node * node , int * relno )
3855+ {
3856+ if (IsA (node , JoinExpr ))
3857+ {
3858+ RangeVar * rv ;
3859+ JoinExpr * join = (JoinExpr * )node ;
3860+ rv = vops_get_join_rangevar (join -> larg , relno );
3861+ if (rv != NULL )
3862+ return rv ;
3863+ rv = vops_get_join_rangevar (join -> rarg , relno );
3864+ if (rv != NULL )
3865+ return rv ;
3866+ }
3867+ else
3868+ {
3869+ Assert (IsA (node , RangeVar ));
3870+ if (-- * relno == 0 )
3871+ return (RangeVar * )node ;
3872+ }
3873+ return NULL ;
3874+ }
3875+
3876+ static RangeVar *
3877+ vops_get_rangevar (List * from , int relno )
3878+ {
3879+ ListCell * cell ;
3880+ foreach (cell , from )
3881+ {
3882+ Node * node = (Node * )lfirst (cell );
3883+ RangeVar * rv = vops_get_join_rangevar (node , & relno );
3884+ if (rv != NULL )
3885+ return rv ;
3886+ }
3887+ Assert (false);
3888+ }
38413889/*
38423890 * Try to substitute tables with their VOPS projections.
38433891 * Criterias for such substitution:
@@ -3871,7 +3919,7 @@ vops_substitute_tables_with_projections(char const* queryString, Query *query)
38713919 pullvar_ctx .consts = (Const * * )palloc0 ((strlen (queryString ) + 1 )* sizeof (Const * ));
38723920 pullvar_ctx .scope = SCOPE_DEFAULT ;
38733921 pullvar_ctx .refs = palloc0 (n_rels * sizeof (vops_table_refs ));
3874- query_tree_walker (query , vops_pullvars_walker , & pullvar_ctx , 0 );
3922+ query_tree_walker (query , vops_pullvars_walker , & pullvar_ctx , QTW_IGNORE_CTE_SUBQUERIES | QTW_IGNORE_RANGE_TABLE );
38753923
38763924 SPI_connect ();
38773925
@@ -3954,7 +4002,10 @@ vops_substitute_tables_with_projections(char const* queryString, Query *query)
39544002 }
39554003 /* Replace table with partition */
39564004 elog (DEBUG1 , "Use projection %s instead of table %d" , projectionName , rte -> relid );
3957- rv = list_nth_node (RangeVar , select -> fromClause , fromno - 1 );
4005+ rv = vops_get_rangevar (select -> fromClause , fromno );
4006+ Assert (rv != NULL );
4007+ if (rv -> alias == NULL )
4008+ rv -> alias = makeAlias (rv -> relname , NULL );
39584009 rv -> relname = pstrdup (projectionName );
39594010
39604011 /* Update vector/scalar bitmap sets for this query for this projection */
@@ -4135,8 +4186,13 @@ static void vops_explain_hook(Query *query,
41354186 params == NULL ) /* do not support prepared statements yet */
41364187 {
41374188 char * explain = pstrdup (queryString );
4138- char * select = strstr (explain , "select" );
4139- size_t prefix = (select != NULL ) ? (select - explain ) : 7 ;
4189+ char * select ;
4190+ size_t prefix ;
4191+ select = strstr (explain , "select" );
4192+ if (select == NULL ) {
4193+ select = strstr (explain , "SELECT" );
4194+ }
4195+ prefix = (select != NULL ) ? (select - explain ) : 7 ;
41404196 memset (explain , ' ' , prefix ); /* clear "explain" prefix: we need to preseve node locations */
41414197 vops_substitute_tables_with_projections (explain , query );
41424198 }
0 commit comments