3636#include "utils/datum.h"
3737#include "utils/memutils.h"
3838#include "utils/lsyscache.h"
39+ #include "utils/syscache.h"
3940#include "utils/typcache.h"
4041#include "utils/resowner.h"
4142#include "utils/snapmgr.h"
@@ -488,6 +489,17 @@ bgw_main_concurrent_part(Datum main_arg)
488489int ret ;
489490bool isnull ;
490491
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+
491503ret = SPI_execute_with_args (sql ,2 ,types ,vals ,nulls , false,0 );
492504if (ret == SPI_OK_SELECT )
493505{
@@ -525,25 +537,6 @@ bgw_main_concurrent_part(Datum main_arg)
525537
526538FreeErrorData (error );
527539
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-
547540/* Set 'failed' flag */
548541failed = true;
549542}
@@ -552,7 +545,27 @@ bgw_main_concurrent_part(Datum main_arg)
552545SPI_finish ();
553546PopActiveSnapshot ();
554547
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 )
556569{
557570/* Abort transaction and sleep for a second */
558571AbortCurrentTransaction ();