@@ -565,8 +565,9 @@ parse_partitioning_expression(const Oid relid,
565565 char * * query_string_out , /* ret value #1 */
566566 Node * * parsetree_out ) /* ret value #2 */
567567{
568- SelectStmt * select_stmt ;
569- List * parsetree_list ;
568+ SelectStmt * select_stmt ;
569+ List * parsetree_list ;
570+ MemoryContext old_mcxt ;
570571
571572 const char * sql = "SELECT (%s) FROM ONLY %s.%s" ;
572573 char * relname = get_rel_name (relid ),
@@ -575,7 +576,31 @@ parse_partitioning_expression(const Oid relid,
575576 quote_identifier (nspname ),
576577 quote_identifier (relname ));
577578
578- parsetree_list = raw_parser (query_string );
579+ old_mcxt = CurrentMemoryContext ;
580+
581+ PG_TRY ();
582+ {
583+ parsetree_list = raw_parser (query_string );
584+ }
585+ PG_CATCH ();
586+ {
587+ ErrorData * error ;
588+
589+ /* Switch to the original context & copy edata */
590+ MemoryContextSwitchTo (old_mcxt );
591+ error = CopyErrorData ();
592+ FlushErrorState ();
593+
594+ error -> detail = error -> message ;
595+ error -> message = "partitioning expression parse error" ;
596+ error -> sqlerrcode = ERRCODE_INVALID_PARAMETER_VALUE ;
597+ error -> cursorpos = 0 ;
598+ error -> internalpos = 0 ;
599+
600+ ReThrowError (error );
601+ }
602+ PG_END_TRY ();
603+
579604 if (list_length (parsetree_list ) != 1 )
580605 elog (ERROR , "expression \"%s\" produced more than one query" , exp_cstr );
581606
@@ -660,8 +685,32 @@ cook_partitioning_expression(const Oid relid,
660685 */
661686 old_mcxt = MemoryContextSwitchTo (parse_mcxt );
662687
663- /* This will fail with elog in case of wrong expression */
664- querytree_list = pg_analyze_and_rewrite (parsetree , query_string , NULL , 0 );
688+ PG_TRY ();
689+ {
690+ /* This will fail with elog in case of wrong expression */
691+ querytree_list = pg_analyze_and_rewrite (parsetree , query_string , NULL , 0 );
692+ }
693+ PG_CATCH ();
694+ {
695+ ErrorData * error ;
696+
697+ /* Switch to the original context & copy edata */
698+ MemoryContextSwitchTo (old_mcxt );
699+ error = CopyErrorData ();
700+ FlushErrorState ();
701+
702+ error -> detail = error -> message ;
703+ error -> message = "partitioning expression analyze error" ;
704+ error -> sqlerrcode = ERRCODE_INVALID_PARAMETER_VALUE ;
705+ error -> cursorpos = 0 ;
706+ error -> internalpos = 0 ;
707+
708+ /* Enable pathman hooks */
709+ pathman_hooks_enabled = true;
710+ ReThrowError (error );
711+ }
712+ PG_END_TRY ();
713+
665714 if (list_length (querytree_list ) != 1 )
666715 elog (ERROR , "partitioning expression produced more than 1 query" );
667716
0 commit comments