34
34
#include "miscadmin.h"
35
35
#include "parser/parse_func.h"
36
36
#include "parser/parse_relation.h"
37
- #include "parser/parse_utilcmd.h"
38
37
#include "tcop/utility.h"
39
38
#include "utils/builtins.h"
40
39
#include "utils/datum.h"
@@ -681,8 +680,6 @@ create_single_partition_internal(Oid parent_relid,
681
680
682
681
/* Elements of the "CREATE TABLE" query tree */
683
682
RangeVar * parent_rv ;
684
- TableLikeClause like_clause ;
685
- CreateStmt create_stmt ;
686
683
List * create_stmts ;
687
684
ListCell * lc ;
688
685
@@ -702,6 +699,27 @@ create_single_partition_internal(Oid parent_relid,
702
699
elog (ERROR , "table \"%s\" is not partitioned" ,
703
700
get_rel_name_or_relid (parent_relid ));
704
701
702
+ /* Do we have to escalate privileges? */
703
+ if (need_priv_escalation )
704
+ {
705
+ /* Get current user's Oid and security context */
706
+ GetUserIdAndSecContext (& save_userid , & save_sec_context );
707
+
708
+ /* Check that user's allowed to spawn partitions */
709
+ if (ACLCHECK_OK != pg_class_aclcheck (parent_relid , save_userid ,
710
+ ACL_SPAWN_PARTITIONS ))
711
+ ereport (ERROR ,
712
+ (errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
713
+ errmsg ("permission denied for parent relation \"%s\"" ,
714
+ get_rel_name_or_relid (parent_relid )),
715
+ errdetail ("user is not allowed to create new partitions" ),
716
+ errhint ("consider granting INSERT privilege" )));
717
+
718
+ /* Become superuser in order to bypass various ACL checks */
719
+ SetUserIdAndSecContext (BOOTSTRAP_SUPERUSERID ,
720
+ save_sec_context | SECURITY_LOCAL_USERID_CHANGE );
721
+ }
722
+
705
723
/* Cache parent's namespace and name */
706
724
parent_name = get_rel_name (parent_relid );
707
725
parent_nsp = get_rel_namespace (parent_relid );
@@ -725,52 +743,9 @@ create_single_partition_internal(Oid parent_relid,
725
743
if (!tablespace )
726
744
tablespace = get_tablespace_name (get_rel_tablespace (parent_relid ));
727
745
728
- /* Initialize TableLikeClause structure */
729
- NodeSetTag (& like_clause , T_TableLikeClause );
730
- like_clause .relation = copyObject (parent_rv );
731
- like_clause .options = CREATE_TABLE_LIKE_DEFAULTS |
732
- CREATE_TABLE_LIKE_INDEXES |
733
- CREATE_TABLE_LIKE_STORAGE ;
734
-
735
- /* Initialize CreateStmt structure */
736
- NodeSetTag (& create_stmt , T_CreateStmt );
737
- create_stmt .relation = copyObject (partition_rv );
738
- create_stmt .tableElts = list_make1 (copyObject (& like_clause ));
739
- create_stmt .inhRelations = list_make1 (copyObject (parent_rv ));
740
- create_stmt .ofTypename = NULL ;
741
- create_stmt .constraints = NIL ;
742
- create_stmt .options = NIL ;
743
- create_stmt .oncommit = ONCOMMIT_NOOP ;
744
- create_stmt .tablespacename = tablespace ;
745
- create_stmt .if_not_exists = false;
746
-
747
- #if defined(PGPRO_EE ) && PG_VERSION_NUM >= 90600
748
- create_stmt .partition_info = NULL ;
749
- #endif
750
-
751
- /* Do we have to escalate privileges? */
752
- if (need_priv_escalation )
753
- {
754
- /* Get current user's Oid and security context */
755
- GetUserIdAndSecContext (& save_userid , & save_sec_context );
756
-
757
- /* Check that user's allowed to spawn partitions */
758
- if (ACLCHECK_OK != pg_class_aclcheck (parent_relid , save_userid ,
759
- ACL_SPAWN_PARTITIONS ))
760
- ereport (ERROR ,
761
- (errcode (ERRCODE_INSUFFICIENT_PRIVILEGE ),
762
- errmsg ("permission denied for parent relation \"%s\"" ,
763
- get_rel_name_or_relid (parent_relid )),
764
- errdetail ("user is not allowed to create new partitions" ),
765
- errhint ("consider granting INSERT privilege" )));
766
-
767
- /* Become superuser in order to bypass various ACL checks */
768
- SetUserIdAndSecContext (BOOTSTRAP_SUPERUSERID ,
769
- save_sec_context | SECURITY_LOCAL_USERID_CHANGE );
770
- }
771
-
772
- /* Generate columns using the parent table */
773
- create_stmts = transformCreateStmt (& create_stmt , NULL );
746
+ /* Obtain the sequence of Stmts to create partition and link it to parent */
747
+ create_stmts = init_createstmts_for_partition (parent_rv , partition_rv ,
748
+ tablespace );
774
749
775
750
/* Create the partition and all required relations */
776
751
foreach (lc , create_stmts )
0 commit comments