@@ -607,15 +607,13 @@ extractcolumns_from_node(Node *expr, bool *cols, AttrNumber natts)
607
607
return ecCtx .found ;
608
608
}
609
609
610
- static TableScanDesc
611
- aoco_beginscan_extractcolumns (Relation rel , Snapshot snapshot , int nkeys , struct ScanKeyData * key ,
612
- ParallelTableScanDesc parallel_scan ,
613
- PlanState * ps , uint32 flags )
610
+ AOCSScanDesc
611
+ aoco_beginscan_extractcolumns_quals (Relation rel , Snapshot snapshot ,
612
+ List * targetlist , List * qual ,
613
+ ParallelTableScanDesc parallel_scan , uint32 flags )
614
614
{
615
615
AOCSScanDesc aoscan ;
616
616
AttrNumber natts = RelationGetNumberOfAttributes (rel );
617
- List * targetlist = ps -> plan -> targetlist ;
618
- List * qual = ps -> plan -> qual ;
619
617
bool * cols ;
620
618
bool found = false;
621
619
@@ -639,6 +637,19 @@ aoco_beginscan_extractcolumns(Relation rel, Snapshot snapshot, int nkeys, struct
639
637
flags );
640
638
641
639
pfree (cols );
640
+ return aoscan ;
641
+ }
642
+
643
+ static TableScanDesc
644
+ aoco_beginscan_extractcolumns (Relation rel , Snapshot snapshot , int nkeys , struct ScanKeyData * key ,
645
+ ParallelTableScanDesc parallel_scan ,
646
+ PlanState * ps , uint32 flags )
647
+ {
648
+ AOCSScanDesc aoscan ;
649
+ List * targetlist = ps -> plan -> targetlist ;
650
+ List * qual = ps -> plan -> qual ;
651
+
652
+ aoscan = aoco_beginscan_extractcolumns_quals (rel , snapshot , targetlist , qual , parallel_scan , flags );
642
653
643
654
if (gp_enable_predicate_pushdown )
644
655
ps -> qual = aocs_predicate_pushdown_prepare (aoscan , qual , ps -> qual , ps -> ps_ExprContext , ps );
@@ -1687,14 +1698,13 @@ aoco_index_build_range_scan(Relation heapRelation,
1687
1698
int64 total_blockcount = 0 ;
1688
1699
BlockNumber lastBlock = start_blockno ;
1689
1700
int64 blockcounts = 0 ;
1690
- #if 0
1691
1701
bool need_create_blk_directory = false;
1692
1702
List * tlist = NIL ;
1693
1703
List * qual = indexInfo -> ii_Predicate ;
1694
- #endif
1695
1704
Oid blkdirrelid ;
1696
1705
Oid blkidxrelid ;
1697
1706
int64 previous_blkno = -1 ;
1707
+ Relation blkdir ;
1698
1708
1699
1709
/*
1700
1710
* sanity checks
@@ -1734,6 +1744,17 @@ aoco_index_build_range_scan(Relation heapRelation,
1734
1744
/* Set up execution state for predicate, if any. */
1735
1745
predicate = ExecPrepareQual (indexInfo -> ii_Predicate , estate );
1736
1746
1747
+ /*
1748
+ * If block directory is empty, it must also be built along with the index.
1749
+ */
1750
+ GetAppendOnlyEntryAuxOids (heapRelation , NULL ,
1751
+ & blkdirrelid , & blkidxrelid , NULL , NULL );
1752
+
1753
+ blkdir = relation_open (blkdirrelid , AccessShareLock );
1754
+
1755
+ need_create_blk_directory = RelationGetNumberOfBlocks (blkdir ) == 0 ;
1756
+ relation_close (blkdir , NoLock );
1757
+
1737
1758
if (!scan )
1738
1759
{
1739
1760
/*
@@ -1744,12 +1765,50 @@ aoco_index_build_range_scan(Relation heapRelation,
1744
1765
*/
1745
1766
snapshot = SnapshotAny ;
1746
1767
1747
- scan = table_beginscan_strat (heapRelation , /* relation */
1748
- snapshot , /* snapshot */
1749
- 0 , /* number of keys */
1750
- NULL , /* scan key */
1751
- true, /* buffer access strategy OK */
1752
- allow_sync ); /* syncscan OK? */
1768
+ /*
1769
+ * Scan all columns if we need to create block directory.
1770
+ */
1771
+ if (need_create_blk_directory )
1772
+ {
1773
+ scan = table_beginscan_strat (heapRelation , /* relation */
1774
+ snapshot , /* snapshot */
1775
+ 0 , /* number of keys */
1776
+ NULL , /* scan key */
1777
+ true, /* buffer access strategy OK */
1778
+ allow_sync ); /* syncscan OK? */
1779
+ }
1780
+ else
1781
+ {
1782
+ uint32 flags = SO_TYPE_SEQSCAN |
1783
+ SO_ALLOW_STRAT | SO_ALLOW_SYNC | SO_ALLOW_PAGEMODE ;
1784
+ /*
1785
+ * if block directory has created, we can only scan needed column.
1786
+ */
1787
+ for (int i = 0 ; i < indexInfo -> ii_NumIndexAttrs ; i ++ )
1788
+ {
1789
+ AttrNumber attrnum = indexInfo -> ii_IndexAttrNumbers [i ];
1790
+ Form_pg_attribute attr = TupleDescAttr (RelationGetDescr (heapRelation ), attrnum - 1 );
1791
+ Var * var = makeVar (i ,
1792
+ attrnum ,
1793
+ attr -> atttypid ,
1794
+ attr -> atttypmod ,
1795
+ attr -> attcollation ,
1796
+ 0 );
1797
+
1798
+ /* Build a target list from index info */
1799
+ tlist = lappend (tlist ,
1800
+ makeTargetEntry ((Expr * ) var ,
1801
+ list_length (tlist ) + 1 ,
1802
+ NULL ,
1803
+ false));
1804
+ }
1805
+
1806
+ /* Push down target list and qual to scan */
1807
+ scan = (TableScanDesc )aoco_beginscan_extractcolumns_quals (heapRelation , /* relation */
1808
+ snapshot , /* snapshot */
1809
+ tlist , /* targetlist */
1810
+ qual , NULL , flags ); /* qual */
1811
+ }
1753
1812
}
1754
1813
else
1755
1814
{
@@ -1767,11 +1826,6 @@ aoco_index_build_range_scan(Relation heapRelation,
1767
1826
1768
1827
aocoscan = (AOCSScanDesc ) scan ;
1769
1828
1770
- /*
1771
- * If block directory is empty, it must also be built along with the index.
1772
- */
1773
- GetAppendOnlyEntryAuxOids (heapRelation , NULL ,
1774
- & blkdirrelid , & blkidxrelid , NULL , NULL );
1775
1829
/*
1776
1830
* Note that block directory is created during creation of the first
1777
1831
* index. If it is found empty, it means the block directory was created
@@ -1781,8 +1835,7 @@ aoco_index_build_range_scan(Relation heapRelation,
1781
1835
* blocked. We can rest assured of exclusive access to the block
1782
1836
* directory relation.
1783
1837
*/
1784
- Relation blkdir = relation_open (blkdirrelid , AccessShareLock );
1785
- if (RelationGetNumberOfBlocks (blkdir ) == 0 )
1838
+ if (need_create_blk_directory )
1786
1839
{
1787
1840
/*
1788
1841
* Allocate blockDirectory in scan descriptor to let the access method
@@ -1792,7 +1845,6 @@ aoco_index_build_range_scan(Relation heapRelation,
1792
1845
Assert (aocoscan -> blockDirectory == NULL );
1793
1846
aocoscan -> blockDirectory = palloc0 (sizeof (AppendOnlyBlockDirectory ));
1794
1847
}
1795
- relation_close (blkdir , NoLock );
1796
1848
1797
1849
1798
1850
/* Publish number of blocks to scan */
0 commit comments