From 7e80e3cb3486187a712e6235209c4aabb9a8a6ab Mon Sep 17 00:00:00 2001 From: "Paul A. Jungwirth" Date: Fri, 25 Oct 2019 20:16:50 -0700 Subject: [PATCH v2 2/2] Added FOR PORTION OF to gram.y FOR PORTION OF: parse analysis start Set contemporal for temporal PKs not just FKs Add a few validations for the column in FOR PORTION OF FOR PORTION OF: slightly more precise error message FOR PORTION OF: filter rows based on FROM ... TO Add forPortionOf to ModifyTablePath --- src/backend/catalog/index.c | 6 +- src/backend/commands/indexcmds.c | 2 + src/backend/nodes/copyfuncs.c | 1 + src/backend/optimizer/plan/planner.c | 2 + src/backend/optimizer/util/pathnode.c | 3 +- src/backend/parser/analyze.c | 162 ++++++++++++++++++++++++++- src/backend/parser/gram.y | 37 ++++-- src/backend/parser/parse_expr.c | 2 + src/backend/parser/parse_utilcmd.c | 2 + src/backend/utils/cache/lsyscache.c | 26 +++++ src/backend/utils/cache/relcache.c | 10 +- src/include/catalog/index.h | 1 + src/include/nodes/nodes.h | 2 + src/include/nodes/parsenodes.h | 44 +++++--- src/include/nodes/pathnodes.h | 1 + src/include/nodes/plannodes.h | 1 + src/include/nodes/primnodes.h | 19 ++++ src/include/optimizer/pathnode.h | 2 +- src/include/parser/kwlist.h | 1 + src/include/parser/parse_node.h | 1 + src/include/utils/lsyscache.h | 1 + src/test/regress/expected/for_portion_of.out | 0 src/test/regress/parallel_schedule | 4 +- src/test/regress/serial_schedule | 1 + src/test/regress/sql/for_portion_of.sql | 16 +++ 25 files changed, 317 insertions(+), 30 deletions(-) create mode 100644 src/test/regress/expected/for_portion_of.out create mode 100644 src/test/regress/sql/for_portion_of.sql diff --git a/src/backend/catalog/index.c b/src/backend/catalog/index.c index 6c055012d7..557c5e92b1 100644 --- a/src/backend/catalog/index.c +++ b/src/backend/catalog/index.c @@ -1782,6 +1782,7 @@ index_concurrently_set_dead(Oid heapId, Oid indexId) * INDEX_CONSTR_CREATE_UPDATE_INDEX: update the pg_index row * INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS: remove existing dependencies * of index on table's columns + * INDEX_CONSTR_CREATE_TEMPORAL: constraint is for a temporal primary key * allow_system_table_mods: allow table to be a system catalog * is_internal: index is constructed due to internal process */ @@ -1805,11 +1806,13 @@ index_constraint_create(Relation heapRelation, bool mark_as_primary; bool islocal; bool noinherit; + bool is_temporal; int inhcount; deferrable = (constr_flags & INDEX_CONSTR_CREATE_DEFERRABLE) != 0; initdeferred = (constr_flags & INDEX_CONSTR_CREATE_INIT_DEFERRED) != 0; mark_as_primary = (constr_flags & INDEX_CONSTR_CREATE_MARK_AS_PRIMARY) != 0; + is_temporal = (constr_flags & INDEX_CONSTR_CREATE_TEMPORAL) != 0; /* constraint creation support doesn't work while bootstrapping */ Assert(!IsBootstrapProcessingMode()); @@ -1884,8 +1887,7 @@ index_constraint_create(Relation heapRelation, islocal, inhcount, noinherit, - // TODO: set this for PKs too - false, /* contemporal */ + is_temporal, /* contemporal */ is_internal); /* diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index 374e2d0efe..57e8f39e99 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -982,6 +982,8 @@ DefineIndex(Oid relationId, constr_flags |= INDEX_CONSTR_CREATE_DEFERRABLE; if (stmt->initdeferred) constr_flags |= INDEX_CONSTR_CREATE_INIT_DEFERRED; + if (stmt->istemporal) + constr_flags |= INDEX_CONSTR_CREATE_TEMPORAL; indexRelationId = index_create(rel, indexRelationName, indexRelationId, parentIndexId, diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index d629aa2339..b0fee3ac4e 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -3475,6 +3475,7 @@ _copyIndexStmt(const IndexStmt *from) COPY_SCALAR_FIELD(unique); COPY_SCALAR_FIELD(primary); COPY_SCALAR_FIELD(isconstraint); + COPY_SCALAR_FIELD(istemporal); COPY_SCALAR_FIELD(deferrable); COPY_SCALAR_FIELD(initdeferred); COPY_SCALAR_FIELD(transformed); diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 17c5f086fb..a6e011b720 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1794,6 +1794,7 @@ inheritance_planner(PlannerInfo *root) returningLists, rowMarks, NULL, + parse->forPortionOf, assign_special_exec_param(root))); } @@ -2377,6 +2378,7 @@ grouping_planner(PlannerInfo *root, bool inheritance_update, returningLists, rowMarks, parse->onConflict, + parse->forPortionOf, assign_special_exec_param(root)); } diff --git a/src/backend/optimizer/util/pathnode.c b/src/backend/optimizer/util/pathnode.c index 34acb732ee..5e8a590d1d 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -3449,7 +3449,7 @@ create_modifytable_path(PlannerInfo *root, RelOptInfo *rel, List *subroots, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, - int epqParam) + ForPortionOfExpr *forPortionOf, int epqParam) { ModifyTablePath *pathnode = makeNode(ModifyTablePath); double total_size; @@ -3520,6 +3520,7 @@ create_modifytable_path(PlannerInfo *root, RelOptInfo *rel, pathnode->returningLists = returningLists; pathnode->rowMarks = rowMarks; pathnode->onconflict = onconflict; + pathnode->forPortionOf = forPortionOf; pathnode->epqParam = epqParam; return pathnode; diff --git a/src/backend/parser/analyze.c b/src/backend/parser/analyze.c index 85d7a96406..8c5c785487 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -42,9 +42,12 @@ #include "parser/parse_param.h" #include "parser/parse_relation.h" #include "parser/parse_target.h" +#include "parser/parser.h" #include "parser/parsetree.h" #include "rewrite/rewriteManip.h" +#include "utils/lsyscache.h" #include "utils/rel.h" +#include "utils/syscache.h" /* Hook for plugins to get control at end of parse analysis */ @@ -58,6 +61,9 @@ static List *transformInsertRow(ParseState *pstate, List *exprlist, bool strip_indirection); static OnConflictExpr *transformOnConflictClause(ParseState *pstate, OnConflictClause *onConflictClause); +static ForPortionOfExpr *transformForPortionOfClause(ParseState *pstate, + int rtindex, + ForPortionOfClause *forPortionOfClause); static int count_rowexpr_columns(ParseState *pstate, Node *expr); static Query *transformSelectStmt(ParseState *pstate, SelectStmt *stmt); static Query *transformValuesClause(ParseState *pstate, SelectStmt *stmt); @@ -1074,6 +1080,149 @@ transformOnConflictClause(ParseState *pstate, return result; } +/* + * transformForPortionOfClause + * transforms a ForPortionOfClause in an UPDATE/DELETE statement + */ +static ForPortionOfExpr * +transformForPortionOfClause(ParseState *pstate, + int rtindex, + ForPortionOfClause *forPortionOf) +{ + Relation targetrel = pstate->p_target_relation; + RangeTblEntry *target_rte = pstate->p_target_rangetblentry; + char *range_name = forPortionOf->range_name; + char *range_type_name; + int range_attno; + ForPortionOfExpr *result; + + result = makeNode(ForPortionOfExpr); + + /* + * First look for a range column, then look for a period. + */ + range_attno = attnameAttNum(targetrel, range_name, true); + if (range_attno != InvalidAttrNumber) + { + Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, range_attno - 1); + + // TODO: check attr->attisdropped ? + + /* Make sure it's a range column */ + if (!type_is_range(attr->atttypid)) + ereport(ERROR, + (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), + errmsg("column \"%s\" of relation \"%s\" is not a range type", + range_name, + RelationGetRelationName(pstate->p_target_relation)), + parser_errposition(pstate, forPortionOf->range_name_location))); + + /* Make sure the table has a primary key */ + Oid pkoid = RelationGetPrimaryKeyIndex(targetrel); + if (pkoid == InvalidOid) + ereport(ERROR, + (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), + errmsg("relation \"%s\" does not have a temporal primary key", + RelationGetRelationName(pstate->p_target_relation)), + parser_errposition(pstate, forPortionOf->range_name_location))); + + /* Make sure the primary key is a temporal key */ + // TODO: need a lock here? + HeapTuple indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(pkoid)); + if (!HeapTupleIsValid(indexTuple)) /* should not happen */ + elog(ERROR, "cache lookup failed for index %u", pkoid); + Form_pg_index pk = (Form_pg_index) GETSTRUCT(indexTuple); + ReleaseSysCache(indexTuple); + + /* + * Only temporal pkey indexes have both isprimary and isexclusion. + * Checking those saves us from scanning pg_constraint + * like in RelationGetExclusionInfo. + */ + if (!(pk->indisprimary && pk->indisexclusion)) + { + ereport(ERROR, + (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), + errmsg("relation \"%s\" does not have a temporal primary key", + RelationGetRelationName(pstate->p_target_relation)), + parser_errposition(pstate, forPortionOf->range_name_location))); + } + + /* Make sure the range attribute is the last part of the pkey. */ + if (range_attno != pk->indkey.values[pk->indnkeyatts - 1]) + { + ereport(ERROR, + (errcode(ERRCODE_INVALID_COLUMN_REFERENCE), + errmsg("column \"%s\" is not the temporal part of the primary key for relation \"%s\"", + range_name, + RelationGetRelationName(pstate->p_target_relation)), + parser_errposition(pstate, forPortionOf->range_name_location))); + } + + Var *v = makeVar( + rtindex, + range_attno, // TODO: 0-indexed or 1-indexed? + attr->atttypid, + attr->atttypmod, + attr->attcollation, + 0); + v->location = forPortionOf->range_name_location; + result->range = (Expr *) v; + range_type_name = get_typname(attr->atttypid); + } else { + // TODO: Try to find a period, + // and set result->range to an Expr like tsrange(period->start_col, period->end_col) + // Probably we can make an A_Expr and call transformExpr on it, right? + + /* + * We need to choose a range type based on the period's columns' type. + * Normally inferring a range type from an element type is not allowed, + * because there might be more than one. + * In this case SQL:2011 only has periods for timestamp, timestamptz, and date, + * which all have built-in range types. + * Let's just take the first range we have for that type, + * ordering by oid, so that we get built-in range types first. + */ + + // TODO: set result->range + // TODO: set range_type_name + } + + if (range_attno == InvalidAttrNumber) + ereport(ERROR, + (errcode(ERRCODE_UNDEFINED_COLUMN), + errmsg("column or period \"%s\" of relation \"%s\" does not exist", + range_name, + RelationGetRelationName(pstate->p_target_relation)), + parser_errposition(pstate, forPortionOf->range_name_location))); + + /* + * targetStart and End are literal strings + * that we'll coerce to the range's element type later. + */ + result->targetStart = forPortionOf->target_start; + result->targetEnd = forPortionOf->target_end; + + FuncCall *fc = makeFuncCall(SystemFuncName(range_type_name), + list_make2(forPortionOf->target_start, + forPortionOf->target_end), + // TODO: FROM...TO... location instead?: + forPortionOf->range_name_location); + result->targetRange = (Node *) fc; + + /* overlapsExpr is something we can add to the whereClause */ + result->overlapsExpr = (Node *) makeSimpleA_Expr(AEXPR_OP, "&&", + // TODO: Maybe need a copy here?: + (Node *) result->range, result->targetRange, + forPortionOf->range_name_location); + + // TODO: if it's a period, mark its start/end columns instead: + /* Mark the range column as requiring update permissions */ + target_rte->updatedCols = bms_add_member(target_rte->updatedCols, + range_attno - FirstLowInvalidHeapAttributeNumber); + + return result; +} /* * BuildOnConflictExcludedTargetlist @@ -2219,6 +2368,7 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) { Query *qry = makeNode(Query); ParseNamespaceItem *nsitem; + Node *whereClause; Node *qual; qry->commandType = CMD_UPDATE; @@ -2237,6 +2387,9 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) true, ACL_UPDATE); + if (stmt->forPortionOf) + qry->forPortionOf = transformForPortionOfClause(pstate, qry->resultRelation, stmt->forPortionOf); + /* grab the namespace item made by setTargetTable */ nsitem = (ParseNamespaceItem *) llast(pstate->p_namespace); @@ -2254,7 +2407,12 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) nsitem->p_lateral_only = false; nsitem->p_lateral_ok = true; - qual = transformWhereClause(pstate, stmt->whereClause, + if (stmt->forPortionOf) + // TODO: Is this messing up cnf? Or does that come later?: + whereClause = (Node *) makeBoolExpr(AND_EXPR, list_make2(qry->forPortionOf->overlapsExpr, stmt->whereClause), -1); + else + whereClause = stmt->whereClause; + qual = transformWhereClause(pstate, whereClause, EXPR_KIND_WHERE, "WHERE"); qry->returningList = transformReturningList(pstate, stmt->returningList); @@ -2332,6 +2490,8 @@ transformUpdateTargetList(ParseState *pstate, List *origTlist) RelationGetRelationName(pstate->p_target_relation)), parser_errposition(pstate, origTarget->location))); + // TODO: If the column is also used in FOR PORTION OF, forbid SETing it: + updateTargetListEntry(pstate, tle, origTarget->name, attrno, origTarget->indirection, diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 081757d841..520ed0d500 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -229,6 +229,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); RangeVar *range; IntoClause *into; WithClause *with; + ForPortionOfClause *forportionof; InferClause *infer; OnConflictClause *onconflict; A_Indices *aind; @@ -498,6 +499,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %type joined_table %type relation_expr %type relation_expr_opt_alias +%type for_portion_of_clause %type tablesample_clause opt_repeatable_clause %type target_el set_target insert_column_item @@ -670,7 +672,7 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); OVER OVERLAPS OVERLAY OVERRIDING OWNED OWNER PARALLEL PARSER PARTIAL PARTITION PASSING PASSWORD PERIOD PLACING PLANS POLICY - POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY + PORTION POSITION PRECEDING PRECISION PRESERVE PREPARE PREPARED PRIMARY PRIOR PRIVILEGES PROCEDURAL PROCEDURE PROCEDURES PROGRAM PUBLICATION QUOTE @@ -11038,13 +11040,15 @@ returning_clause: *****************************************************************************/ DeleteStmt: opt_with_clause DELETE_P FROM relation_expr_opt_alias + for_portion_of_clause using_clause where_or_current_clause returning_clause { DeleteStmt *n = makeNode(DeleteStmt); n->relation = $4; - n->usingClause = $5; - n->whereClause = $6; - n->returningList = $7; + n->forPortionOf = $5; + n->usingClause = $6; + n->whereClause = $7; + n->returningList = $8; n->withClause = $1; $$ = (Node *)n; } @@ -11107,6 +11111,7 @@ opt_nowait_or_skip: *****************************************************************************/ UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias + for_portion_of_clause SET set_clause_list from_clause where_or_current_clause @@ -11114,10 +11119,11 @@ UpdateStmt: opt_with_clause UPDATE relation_expr_opt_alias { UpdateStmt *n = makeNode(UpdateStmt); n->relation = $3; - n->targetList = $5; - n->fromClause = $6; - n->whereClause = $7; - n->returningList = $8; + n->forPortionOf = $4; + n->targetList = $6; + n->fromClause = $7; + n->whereClause = $8; + n->returningList = $9; n->withClause = $1; $$ = (Node *)n; } @@ -12222,6 +12228,20 @@ relation_expr_opt_alias: relation_expr %prec UMINUS } ; +for_portion_of_clause: + FOR PORTION OF ColId FROM Sconst TO Sconst + { + ForPortionOfClause *n = makeNode(ForPortionOfClause); + n->range_name = $4; + n->range_name_location = @4; + n->target_start = makeStringConst($6, @6); + n->target_end = makeStringConst($8, @8); + $$ = n; + } + | /*EMPTY*/ { $$ = NULL; } + ; + + /* * TABLESAMPLE decoration in a FROM item */ @@ -15245,6 +15265,7 @@ unreserved_keyword: | PASSWORD | PLANS | POLICY + | PORTION | PRECEDING | PREPARE | PREPARED diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index 76f3dd7076..54bf2aaf46 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -3520,6 +3520,8 @@ ParseExprKindName(ParseExprKind exprKind) case EXPR_KIND_UPDATE_SOURCE: case EXPR_KIND_UPDATE_TARGET: return "UPDATE"; + case EXPR_KIND_UPDATE_PORTION: + return "FOR PORTION OF"; case EXPR_KIND_GROUP_BY: return "GROUP BY"; case EXPR_KIND_ORDER_BY: diff --git a/src/backend/parser/parse_utilcmd.c b/src/backend/parser/parse_utilcmd.c index 50a5a36e41..d5d6166964 100644 --- a/src/backend/parser/parse_utilcmd.c +++ b/src/backend/parser/parse_utilcmd.c @@ -1403,6 +1403,7 @@ generateClonedIndexStmt(RangeVar *heapRel, Relation source_idx, index->oldNode = InvalidOid; index->unique = idxrec->indisunique; index->primary = idxrec->indisprimary; + index->istemporal = idxrec->indisprimary && idxrec->indisexclusion; index->transformed = true; /* don't need transformIndexStmt */ index->concurrent = false; index->if_not_exists = false; @@ -1980,6 +1981,7 @@ transformIndexConstraint(Constraint *constraint, CreateStmtContext *cxt) */ } index->isconstraint = true; + index->istemporal = constraint->without_overlaps != NULL; index->deferrable = constraint->deferrable; index->initdeferred = constraint->initdeferred; diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c index 360a71427e..3fd7d79cdb 100644 --- a/src/backend/utils/cache/lsyscache.c +++ b/src/backend/utils/cache/lsyscache.c @@ -1967,6 +1967,32 @@ get_typisdefined(Oid typid) } /* + * get_typname + * + * Returns the name of a given type + * + * Returns a palloc'd copy of the string, or NULL if no such type. + */ +char * +get_typname(Oid typid) +{ + HeapTuple tp; + + tp = SearchSysCache1(TYPEOID, ObjectIdGetDatum(typid)); + if (HeapTupleIsValid(tp)) + { + Form_pg_type typtup = (Form_pg_type) GETSTRUCT(tp); + char *result; + + result = pstrdup(NameStr(typtup->typname)); + ReleaseSysCache(tp); + return result; + } + else + return NULL; +} + +/* * get_typlen * * Given the type OID, return the length of the type. diff --git a/src/backend/utils/cache/relcache.c b/src/backend/utils/cache/relcache.c index af1d91fbc9..c05620a83b 100644 --- a/src/backend/utils/cache/relcache.c +++ b/src/backend/utils/cache/relcache.c @@ -4401,11 +4401,17 @@ RelationGetIndexList(Relation relation) * interesting for either oid indexes or replication identity indexes, * so don't check them. */ - if (!index->indisvalid || !index->indisunique || - !index->indimmediate || + if (!index->indisvalid || !index->indimmediate || !heap_attisnull(htup, Anum_pg_index_indpred, NULL)) continue; + /* + * Non-unique indexes aren't interesting either, + * except when they are temporal primary keys. + */ + if (!index->indisunique && !index->indisprimary) + continue; + /* remember primary key index if any */ if (index->indisprimary) pkeyIndex = index->indexrelid; diff --git a/src/include/catalog/index.h b/src/include/catalog/index.h index 1113d25b2d..b3a116e412 100644 --- a/src/include/catalog/index.h +++ b/src/include/catalog/index.h @@ -77,6 +77,7 @@ extern Oid index_create(Relation heapRelation, #define INDEX_CONSTR_CREATE_INIT_DEFERRED (1 << 2) #define INDEX_CONSTR_CREATE_UPDATE_INDEX (1 << 3) #define INDEX_CONSTR_CREATE_REMOVE_OLD_DEPS (1 << 4) +#define INDEX_CONSTR_CREATE_TEMPORAL (1 << 5) extern Oid index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId, diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index bce2d59b0d..3cf1916c7a 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -195,6 +195,7 @@ typedef enum NodeTag T_JoinExpr, T_FromExpr, T_OnConflictExpr, + T_ForPortionOfExpr, T_IntoClause, /* @@ -468,6 +469,7 @@ typedef enum NodeTag T_WithClause, T_InferClause, T_OnConflictClause, + T_ForPortionOfClause, T_CommonTableExpr, T_RoleSpec, T_TriggerTransition, diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index aa7da0c79f..832f923109 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -122,6 +122,8 @@ typedef struct Query int resultRelation; /* rtable index of target relation for * INSERT/UPDATE/DELETE; 0 for SELECT */ + ForPortionOfExpr *forPortionOf; /* FOR PORTION OF clause for UPDATE/DELETE */ + bool hasAggs; /* has aggregates in tlist or havingQual */ bool hasWindowFuncs; /* has window functions in tlist */ bool hasTargetSRFs; /* has set-returning functions in tlist */ @@ -1363,6 +1365,19 @@ typedef struct RowMarkClause } RowMarkClause; /* + * ForPortionOfClause + * representation of FOR PORTION OF FROM TO + */ +typedef struct ForPortionOfClause +{ + NodeTag type; + char *range_name; + int range_name_location; + Node *target_start; + Node *target_end; +} ForPortionOfClause; + +/* * WithClause - * representation of WITH clause * @@ -1517,12 +1532,13 @@ typedef struct InsertStmt */ typedef struct DeleteStmt { - NodeTag type; - RangeVar *relation; /* relation to delete from */ - List *usingClause; /* optional using clause for more tables */ - Node *whereClause; /* qualifications */ - List *returningList; /* list of expressions to return */ - WithClause *withClause; /* WITH clause */ + NodeTag type; + RangeVar *relation; /* relation to delete from */ + ForPortionOfClause *forPortionOf; /* FOR PORTION OF clause */ + List *usingClause; /* optional using clause for more tables */ + Node *whereClause; /* qualifications */ + List *returningList; /* list of expressions to return */ + WithClause *withClause; /* WITH clause */ } DeleteStmt; /* ---------------------- @@ -1531,13 +1547,14 @@ typedef struct DeleteStmt */ typedef struct UpdateStmt { - NodeTag type; - RangeVar *relation; /* relation to update */ - List *targetList; /* the target list (of ResTarget) */ - Node *whereClause; /* qualifications */ - List *fromClause; /* optional from clause for more tables */ - List *returningList; /* list of expressions to return */ - WithClause *withClause; /* WITH clause */ + NodeTag type; + RangeVar *relation; /* relation to update */ + ForPortionOfClause *forPortionOf; /* FOR PORTION OF clause */ + List *targetList; /* the target list (of ResTarget) */ + Node *whereClause; /* qualifications */ + List *fromClause; /* optional from clause for more tables */ + List *returningList; /* list of expressions to return */ + WithClause *withClause; /* WITH clause */ } UpdateStmt; /* ---------------------- @@ -2770,6 +2787,7 @@ typedef struct IndexStmt bool unique; /* is index unique? */ bool primary; /* is index a primary key? */ bool isconstraint; /* is it for a pkey/unique constraint? */ + bool istemporal; /* is it for a temporal pkey? */ bool deferrable; /* is the constraint DEFERRABLE? */ bool initdeferred; /* is the constraint INITIALLY DEFERRED? */ bool transformed; /* true when transformIndexStmt is finished */ diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index 23a06d718e..75191d1bb8 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -1781,6 +1781,7 @@ typedef struct ModifyTablePath List *returningLists; /* per-target-table RETURNING tlists */ List *rowMarks; /* PlanRowMarks (non-locking only) */ OnConflictExpr *onconflict; /* ON CONFLICT clause, or NULL */ + ForPortionOfExpr *forPortionOf; /* FOR PORTION OF clause for UPDATE/DELETE */ int epqParam; /* ID of Param for EvalPlanQual re-eval */ } ModifyTablePath; diff --git a/src/include/nodes/plannodes.h b/src/include/nodes/plannodes.h index 8e6594e355..8f94519582 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -232,6 +232,7 @@ typedef struct ModifyTable List *rowMarks; /* PlanRowMarks (non-locking only) */ int epqParam; /* ID of Param for EvalPlanQual re-eval */ OnConflictAction onConflictAction; /* ON CONFLICT action */ + ForPortionOfExpr *forPortionOf; /* FOR PORTION OF clause for UPDATE/DELETE */ List *arbiterIndexes; /* List of ON CONFLICT arbiter index OIDs */ List *onConflictSet; /* SET for INSERT ON CONFLICT DO UPDATE */ Node *onConflictWhere; /* WHERE for ON CONFLICT UPDATE */ diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 860a84de7c..d6f3c40db9 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1524,4 +1524,23 @@ typedef struct OnConflictExpr List *exclRelTlist; /* tlist of the EXCLUDED pseudo relation */ } OnConflictExpr; +/*---------- + * ForPortionOfExpr - represents a FOR PORTION OF ... expression + * + * TODO: more notes as needed + *---------- + */ +typedef struct ForPortionOfExpr +{ + NodeTag type; + // TODO: either a Var or a FuncExpr: + Expr *range; /* Range column or expression */ + Node *startCol; /* Start column if using a PERIOD */ + Node *endCol; /* End column if using a PERIOD */ + Node *targetStart; /* Same type as the range's elements */ + Node *targetEnd; /* Same type as the range's elements */ + Node *targetRange; /* A range from targetStart to targetEnd */ + Node *overlapsExpr; /* range && targetRange */ +} ForPortionOfExpr; + #endif /* PRIMNODES_H */ diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h index a12af54971..c5bdd3f3f8 100644 --- a/src/include/optimizer/pathnode.h +++ b/src/include/optimizer/pathnode.h @@ -258,7 +258,7 @@ extern ModifyTablePath *create_modifytable_path(PlannerInfo *root, List *subroots, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, - int epqParam); + ForPortionOfExpr *forPortionOf, int epqParam); extern LimitPath *create_limit_path(PlannerInfo *root, RelOptInfo *rel, Path *subpath, Node *limitOffset, Node *limitCount, diff --git a/src/include/parser/kwlist.h b/src/include/parser/kwlist.h index 92628f390a..fb05dc47ca 100644 --- a/src/include/parser/kwlist.h +++ b/src/include/parser/kwlist.h @@ -303,6 +303,7 @@ PG_KEYWORD("period", PERIOD, RESERVED_KEYWORD) PG_KEYWORD("placing", PLACING, RESERVED_KEYWORD) PG_KEYWORD("plans", PLANS, UNRESERVED_KEYWORD) PG_KEYWORD("policy", POLICY, UNRESERVED_KEYWORD) +PG_KEYWORD("portion", PORTION, UNRESERVED_KEYWORD) PG_KEYWORD("position", POSITION, COL_NAME_KEYWORD) PG_KEYWORD("preceding", PRECEDING, UNRESERVED_KEYWORD) PG_KEYWORD("precision", PRECISION, COL_NAME_KEYWORD) diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index 7c099e7084..ea153b678f 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -50,6 +50,7 @@ typedef enum ParseExprKind EXPR_KIND_INSERT_TARGET, /* INSERT target list item */ EXPR_KIND_UPDATE_SOURCE, /* UPDATE assignment source item */ EXPR_KIND_UPDATE_TARGET, /* UPDATE assignment target item */ + EXPR_KIND_UPDATE_PORTION, /* UPDATE FOR PORTION OF item */ EXPR_KIND_GROUP_BY, /* GROUP BY */ EXPR_KIND_ORDER_BY, /* ORDER BY */ EXPR_KIND_DISTINCT_ON, /* DISTINCT ON */ diff --git a/src/include/utils/lsyscache.h b/src/include/utils/lsyscache.h index ffdd90d69c..eafa21b6fc 100644 --- a/src/include/utils/lsyscache.h +++ b/src/include/utils/lsyscache.h @@ -134,6 +134,7 @@ extern char get_rel_persistence(Oid relid); extern Oid get_transform_fromsql(Oid typid, Oid langid, List *trftypes); extern Oid get_transform_tosql(Oid typid, Oid langid, List *trftypes); extern bool get_typisdefined(Oid typid); +extern char *get_typname(Oid typid); extern int16 get_typlen(Oid typid); extern bool get_typbyval(Oid typid); extern void get_typlenbyval(Oid typid, int16 *typlen, bool *typbyval); diff --git a/src/test/regress/expected/for_portion_of.out b/src/test/regress/expected/for_portion_of.out new file mode 100644 index 0000000000..e69de29bb2 diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule index b3e72673bf..17934bed2f 100644 --- a/src/test/regress/parallel_schedule +++ b/src/test/regress/parallel_schedule @@ -108,12 +108,12 @@ test: json jsonb json_encoding jsonpath jsonpath_encoding jsonb_jsonpath # NB: temp.sql does a reconnect which transiently uses 2 connections, # so keep this parallel group to at most 19 tests # ---------- -test: plancache limit plpgsql copy2 temp domain rangefuncs prepare conversion truncate alter_table sequence polymorphism rowtypes returning largeobject with xml +test: plancache limit plpgsql copy2 temp domain rangefuncs prepare conversion truncate alter_table sequence polymorphism rowtypes returning largeobject with xml without_overlaps # ---------- # Another group of parallel tests # ---------- -test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info without_overlaps +test: partition_join partition_prune reloptions hash_part indexing partition_aggregate partition_info for_portion_of # event triggers cannot run concurrently with any test that runs DDL test: event_trigger diff --git a/src/test/regress/serial_schedule b/src/test/regress/serial_schedule index dbcd919a84..78ab872bc8 100644 --- a/src/test/regress/serial_schedule +++ b/src/test/regress/serial_schedule @@ -21,6 +21,7 @@ test: money test: rangetypes test: multirangetypes test: without_overlaps +test: for_portion_of test: pg_lsn test: regproc test: strings diff --git a/src/test/regress/sql/for_portion_of.sql b/src/test/regress/sql/for_portion_of.sql new file mode 100644 index 0000000000..b18f7377c8 --- /dev/null +++ b/src/test/regress/sql/for_portion_of.sql @@ -0,0 +1,16 @@ +-- Tests for UPDATE/DELETE FOR PORTION OF + +-- +-- test input parser +-- + +-- tables are without_overlaps_test +SELECT * FROM without_overlaps_test; + +explain analyze UPDATE without_overlaps_test +FOR PORTION OF valid_at FROM '2018-06-01' TO 'infinity' +-- too add a non PK column so we have something to update: +SET id = '[5,6)' +WHERE id = '[3,4)'; + +SELECT * FROM without_overlaps_test; -- 2.11.0