36
36
#include "utils/datum.h"
37
37
#include "utils/memutils.h"
38
38
#include "utils/lsyscache.h"
39
+ #include "utils/syscache.h"
39
40
#include "utils/typcache.h"
40
41
#include "utils/resowner.h"
41
42
#include "utils/snapmgr.h"
@@ -488,6 +489,17 @@ bgw_main_concurrent_part(Datum main_arg)
488
489
int ret ;
489
490
bool isnull ;
490
491
492
+ /* Make sure that relation exists and has partitions */
493
+ if (!SearchSysCacheExists1 (RELOID , ObjectIdGetDatum (part_slot -> relid )) ||
494
+ get_pathman_relation_info (part_slot -> relid ) == NULL )
495
+ {
496
+ /* Fail fast */
497
+ failures_count = PART_WORKER_MAX_ATTEMPTS ;
498
+
499
+ elog (ERROR , "relation %u is not partitioned (or does not exist)" ,
500
+ part_slot -> relid );
501
+ }
502
+
491
503
ret = SPI_execute_with_args (sql , 2 , types , vals , nulls , false, 0 );
492
504
if (ret == SPI_OK_SELECT )
493
505
{
@@ -525,25 +537,6 @@ bgw_main_concurrent_part(Datum main_arg)
525
537
526
538
FreeErrorData (error );
527
539
528
- /*
529
- * The most common exception we can catch here is a deadlock with
530
- * concurrent user queries. Check that attempts count doesn't exceed
531
- * some reasonable value
532
- */
533
- if (failures_count >= PART_WORKER_MAX_ATTEMPTS )
534
- {
535
- /* Mark slot as FREE */
536
- cps_set_status (part_slot , CPS_FREE );
537
-
538
- elog (LOG ,
539
- "concurrent partitioning worker has canceled the task because "
540
- "maximum amount of attempts (%d) had been exceeded, "
541
- "see the error message below" ,
542
- PART_WORKER_MAX_ATTEMPTS );
543
-
544
- return ; /* exit quickly */
545
- }
546
-
547
540
/* Set 'failed' flag */
548
541
failed = true;
549
542
}
@@ -552,7 +545,27 @@ bgw_main_concurrent_part(Datum main_arg)
552
545
SPI_finish ();
553
546
PopActiveSnapshot ();
554
547
555
- if (failed )
548
+ /*
549
+ * The most common exception we can catch here is a deadlock with
550
+ * concurrent user queries. Check that attempts count doesn't exceed
551
+ * some reasonable value
552
+ */
553
+ if (failures_count >= PART_WORKER_MAX_ATTEMPTS )
554
+ {
555
+ AbortCurrentTransaction ();
556
+
557
+ /* Mark slot as FREE */
558
+ cps_set_status (part_slot , CPS_FREE );
559
+
560
+ elog (LOG ,
561
+ "concurrent partitioning worker has canceled the task because "
562
+ "maximum amount of attempts (%d) had been exceeded, "
563
+ "see the error message below" ,
564
+ PART_WORKER_MAX_ATTEMPTS );
565
+
566
+ return ;
567
+ }
568
+ else if (failed )
556
569
{
557
570
/* Abort transaction and sleep for a second */
558
571
AbortCurrentTransaction ();
0 commit comments