@@ -195,7 +195,7 @@ static void check_tablespace(void);
195
195
static bool preliminary_checks (char * errbuf , size_t errsize );
196
196
static void repack_all_databases (const char * order_by );
197
197
static bool repack_one_database (const char * order_by , char * errbuf , size_t errsize );
198
- static void repack_one_table (const repack_table * table , const char * order_by );
198
+ static void repack_one_table (repack_table * table , const char * order_by );
199
199
static bool repack_table_indexes (PGresult * index_details );
200
200
static bool repack_all_indexes (char * errbuf , size_t errsize );
201
201
static void repack_cleanup (bool fatal , const repack_table * table );
@@ -669,10 +669,6 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
669
669
const char * create_table_2 ;
670
670
const char * tablespace ;
671
671
const char * ckey ;
672
- PGresult * indexres = NULL ;
673
- const char * indexparams [2 ];
674
- char buffer [12 ];
675
- int j ;
676
672
int c = 0 ;
677
673
678
674
table .target_name = getstr (res , i , c ++ );
@@ -741,41 +737,6 @@ repack_one_database(const char *orderby, char *errbuf, size_t errsize)
741
737
table .create_table = sql .data ;
742
738
}
743
739
744
- indexparams [0 ] = utoa (table .target_oid , buffer );
745
- indexparams [1 ] = moveidx ? tablespace : NULL ;
746
-
747
- /* First, just display a warning message for any invalid indexes
748
- * which may be on the table (mostly to match the behavior of 1.1.8).
749
- */
750
- indexres = execute (
751
- "SELECT pg_get_indexdef(indexrelid)"
752
- " FROM pg_index WHERE indrelid = $1 AND NOT indisvalid" ,
753
- 1 , indexparams );
754
-
755
- for (j = 0 ; j < PQntuples (indexres ); j ++ )
756
- {
757
- const char * indexdef ;
758
- indexdef = getstr (indexres , j , 0 );
759
- elog (WARNING , "skipping invalid index: %s" , indexdef );
760
- }
761
-
762
- indexres = execute (
763
- "SELECT indexrelid,"
764
- " repack.repack_indexdef(indexrelid, indrelid, $2, FALSE) "
765
- " FROM pg_index WHERE indrelid = $1 AND indisvalid" ,
766
- 2 , indexparams );
767
-
768
- table .n_indexes = PQntuples (indexres );
769
- table .indexes = pgut_malloc (table .n_indexes * sizeof (repack_index ));
770
-
771
- for (j = 0 ; j < table .n_indexes ; j ++ )
772
- {
773
- table .indexes [j ].target_oid = getoid (indexres , j , 0 );
774
- table .indexes [j ].create_index = getstr (indexres , j , 1 );
775
- table .indexes [j ].status = UNPROCESSED ;
776
- table .indexes [j ].worker_idx = -1 ; /* Unassigned */
777
- }
778
-
779
740
repack_one_table (& table , orderby );
780
741
}
781
742
ret = true;
@@ -1021,17 +982,20 @@ rebuild_indexes(const repack_table *table)
1021
982
* Re-organize one table.
1022
983
*/
1023
984
static void
1024
- repack_one_table (const repack_table * table , const char * orderby )
985
+ repack_one_table (repack_table * table , const char * orderby )
1025
986
{
1026
987
PGresult * res = NULL ;
1027
988
const char * params [2 ];
1028
989
int num ;
1029
990
int num_waiting = 0 ;
1030
- int i ;
1031
991
char * vxid = NULL ;
1032
992
char buffer [12 ];
1033
993
StringInfoData sql ;
1034
994
bool ret = false;
995
+ PGresult * indexres = NULL ;
996
+ const char * indexparams [2 ];
997
+ char indexbuffer [12 ];
998
+ int j ;
1035
999
1036
1000
/* Keep track of whether we have gotten through setup to install
1037
1001
* the z_repack_trigger, log table, etc. ourselves. We don't want to
@@ -1065,11 +1029,6 @@ repack_one_table(const repack_table *table, const char *orderby)
1065
1029
elog (DEBUG2 , "sql_delete : %s" , table -> sql_delete );
1066
1030
elog (DEBUG2 , "sql_update : %s" , table -> sql_update );
1067
1031
elog (DEBUG2 , "sql_pop : %s" , table -> sql_pop );
1068
- for (i = 0 ; i < table -> n_indexes ; i ++ )
1069
- {
1070
- elog (DEBUG2 , "index[%d].target_oid : %u" , i , table -> indexes [i ].target_oid );
1071
- elog (DEBUG2 , "index[%d].create_index : %s" , i , table -> indexes [i ].create_index );
1072
- }
1073
1032
1074
1033
if (dryrun )
1075
1034
return ;
@@ -1090,6 +1049,53 @@ repack_one_table(const repack_table *table, const char *orderby)
1090
1049
goto cleanup ;
1091
1050
}
1092
1051
1052
+ /*
1053
+ * pg_get_indexdef requires an access share lock, so do those calls while
1054
+ * we have an access exclusive lock anyway, so we know they won't block.
1055
+ */
1056
+
1057
+ indexparams [0 ] = utoa (table -> target_oid , indexbuffer );
1058
+ indexparams [1 ] = moveidx ? tablespace : NULL ;
1059
+
1060
+ /* First, just display a warning message for any invalid indexes
1061
+ * which may be on the table (mostly to match the behavior of 1.1.8).
1062
+ */
1063
+ indexres = execute (
1064
+ "SELECT pg_get_indexdef(indexrelid)"
1065
+ " FROM pg_index WHERE indrelid = $1 AND NOT indisvalid" ,
1066
+ 1 , indexparams );
1067
+
1068
+ for (j = 0 ; j < PQntuples (indexres ); j ++ )
1069
+ {
1070
+ const char * indexdef ;
1071
+ indexdef = getstr (indexres , j , 0 );
1072
+ elog (WARNING , "skipping invalid index: %s" , indexdef );
1073
+ }
1074
+
1075
+ indexres = execute (
1076
+ "SELECT indexrelid,"
1077
+ " repack.repack_indexdef(indexrelid, indrelid, $2, FALSE) "
1078
+ " FROM pg_index WHERE indrelid = $1 AND indisvalid" ,
1079
+ 2 , indexparams );
1080
+
1081
+ table -> n_indexes = PQntuples (indexres );
1082
+ table -> indexes = pgut_malloc (table -> n_indexes * sizeof (repack_index ));
1083
+
1084
+ for (j = 0 ; j < table -> n_indexes ; j ++ )
1085
+ {
1086
+ table -> indexes [j ].target_oid = getoid (indexres , j , 0 );
1087
+ table -> indexes [j ].create_index = getstr (indexres , j , 1 );
1088
+ table -> indexes [j ].status = UNPROCESSED ;
1089
+ table -> indexes [j ].worker_idx = -1 ; /* Unassigned */
1090
+ }
1091
+
1092
+ for (j = 0 ; j < table -> n_indexes ; j ++ )
1093
+ {
1094
+ elog (DEBUG2 , "index[%d].target_oid : %u" , j , table -> indexes [j ].target_oid );
1095
+ elog (DEBUG2 , "index[%d].create_index : %s" , j , table -> indexes [j ].create_index );
1096
+ }
1097
+
1098
+
1093
1099
/*
1094
1100
* Check z_repack_trigger is the trigger executed last so that
1095
1101
* other before triggers cannot modify triggered tuples.
@@ -1284,6 +1290,8 @@ repack_one_table(const repack_table *table, const char *orderby)
1284
1290
if (!rebuild_indexes (table ))
1285
1291
goto cleanup ;
1286
1292
1293
+ /* don't clear indexres until after rebuild_indexes or bad things happen */
1294
+ CLEARPGRES (indexres );
1287
1295
CLEARPGRES (res );
1288
1296
1289
1297
/*
0 commit comments