From 045248f01db8a8dc08589154a20aa555feaa75a3 Mon Sep 17 00:00:00 2001 From: "Paul A. Jungwirth" Date: Fri, 25 Jun 2021 18:54:35 -0700 Subject: [PATCH v4 3/4] Add UPDATE/DELETE FOR PORTION OF - Added bison support for FOR PORTION OF syntax. The bounds must be constant, so we forbid column references, subqueries, etc. But we permit the magic MINVALUE and MAXVALUE keywords (to match the terms for partitions) to perform an unbounded update/delete. We also accept functions like NOW(). - Added AFTER ROW triggers to insert new rows for the "leftover" part of a record touched by a FOR PORTION OF query. We install these triggers on any table with a temporal primary key. - Added tg_temporal descriptor to the TriggerData struct that we pass to trigger functions. Our triggers use this to learn what bounds were given in the FOR PORTION OF clause. - Documented FOR PORTION OF. - Documented tg_temporal struct. - Added tests. --- doc/src/sgml/ddl.sgml | 4 + doc/src/sgml/ref/delete.sgml | 46 ++ doc/src/sgml/ref/update.sgml | 47 ++ doc/src/sgml/trigger.sgml | 60 +- src/backend/access/brin/brin_minmax_multi.c | 28 +- src/backend/commands/indexcmds.c | 6 + src/backend/commands/tablecmds.c | 65 ++ src/backend/commands/trigger.c | 55 ++ src/backend/executor/execMain.c | 1 + src/backend/executor/nodeModifyTable.c | 33 + src/backend/nodes/copyfuncs.c | 47 ++ src/backend/nodes/equalfuncs.c | 35 + src/backend/nodes/nodeFuncs.c | 10 + src/backend/nodes/outfuncs.c | 23 + src/backend/nodes/readfuncs.c | 25 + src/backend/optimizer/plan/createplan.c | 8 +- src/backend/optimizer/plan/planner.c | 1 + src/backend/optimizer/util/pathnode.c | 3 +- src/backend/parser/analyze.c | 374 ++++++++++- src/backend/parser/gram.y | 47 +- src/backend/parser/parse_agg.c | 10 + src/backend/parser/parse_collate.c | 1 + src/backend/parser/parse_expr.c | 8 + src/backend/parser/parse_func.c | 3 + src/backend/rewrite/rewriteHandler.c | 24 + src/backend/tcop/utility.c | 2 +- src/backend/utils/adt/Makefile | 1 + src/backend/utils/adt/rangetypes.c | 42 ++ src/include/catalog/pg_proc.dat | 6 + src/include/commands/tablecmds.h | 2 + src/include/commands/trigger.h | 1 + src/include/nodes/execnodes.h | 22 + src/include/nodes/nodes.h | 3 + src/include/nodes/parsenodes.h | 43 +- src/include/nodes/pathnodes.h | 1 + src/include/nodes/plannodes.h | 2 + src/include/nodes/primnodes.h | 27 + src/include/optimizer/pathnode.h | 2 +- src/include/parser/kwlist.h | 1 + src/include/parser/parse_node.h | 1 + src/include/utils/rangetypes.h | 3 + src/test/regress/expected/for_portion_of.out | 662 +++++++++++++++++++ src/test/regress/expected/sanity_check.out | 1 + src/test/regress/parallel_schedule | 2 +- src/test/regress/sql/for_portion_of.sql | 550 +++++++++++++++ 45 files changed, 2280 insertions(+), 58 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/doc/src/sgml/ddl.sgml b/doc/src/sgml/ddl.sgml index b5f0cfcc0c..cd828e4479 100644 --- a/doc/src/sgml/ddl.sgml +++ b/doc/src/sgml/ddl.sgml @@ -1188,6 +1188,10 @@ CREATE TABLE billing_addresses ( and second a period (or range column) that qualifies when the row applies. So a temporal primary permits multiple rows with equal values in the ordinary key parts, as long as those rows don't have overlapping periods. Each row makes a statement about the entity identified by the ordinary key parts, but applying only to the span given by the period. Temporal primary keys are essentially exclusion constraints where the first key parts are compared for equality and the last part for overlaps. + + + Any table with a temporal primary key supports temporal UPDATE and DELETE using FOR PORTION OF syntax. See and for details. + diff --git a/doc/src/sgml/ref/delete.sgml b/doc/src/sgml/ref/delete.sgml index 1b81b4e7d7..2f052f80ae 100644 --- a/doc/src/sgml/ref/delete.sgml +++ b/doc/src/sgml/ref/delete.sgml @@ -23,6 +23,7 @@ PostgreSQL documentation [ WITH [ RECURSIVE ] with_query [, ...] ] DELETE FROM [ ONLY ] table_name [ * ] [ [ AS ] alias ] + [ FOR PORTION OF range_or_period_name FROM start_time TO end_time ] [ USING from_item [, ...] ] [ WHERE condition | WHERE CURRENT OF cursor_name ] [ RETURNING * | output_expression [ [ AS ] output_name ] [, ...] ] @@ -54,6 +55,15 @@ DELETE FROM [ ONLY ] table_name [ * circumstances. + + If the table has a temporal + primary key, you may supply a + FOR PORTION OF clause, and your delete will only affect rows + that overlap the given interval. Furthermore, if a row's span extends outside + the FOR PORTION OF bounds, then new rows spanning the "cut + off" duration will be inserted to preserve the old values. + + The optional RETURNING clause causes DELETE to compute and return value(s) based on each row actually deleted. @@ -116,6 +126,42 @@ DELETE FROM [ ONLY ] table_name [ * + + range_or_period_name + + + The range column or period to use when performing a temporal delete. This + must match the range or period used in the table's temporal primary key. + + + + + + start_time + + + The earliest time (inclusive) to change in a temporal delete. + This must be a value matching the base type of the range or period from + range_or_period_name. It may also + be the special value MINVALUE to indicate a delete whose + beginning is unbounded. + + + + + + end_time + + + The latest time (exclusive) to change in a temporal delete. + This must be a value matching the base type of the range or period from + range_or_period_name. It may also + be the special value MAXVALUE to indicate a delete whose + end is unbounded. + + + + from_item diff --git a/doc/src/sgml/ref/update.sgml b/doc/src/sgml/ref/update.sgml index 3fa54e5f70..8baa13daf9 100644 --- a/doc/src/sgml/ref/update.sgml +++ b/doc/src/sgml/ref/update.sgml @@ -23,6 +23,7 @@ PostgreSQL documentation [ WITH [ RECURSIVE ] with_query [, ...] ] UPDATE [ ONLY ] table_name [ * ] [ [ AS ] alias ] + [ FOR PORTION OF range_or_period_name FROM start_time TO end_time ] SET { column_name = { expression | DEFAULT } | ( column_name [, ...] ) = [ ROW ] ( { expression | DEFAULT } [, ...] ) | ( column_name [, ...] ) = ( sub-SELECT ) @@ -51,6 +52,16 @@ UPDATE [ ONLY ] table_name [ * ] [ circumstances. + + If the table has a temporal + primary key, you may supply a + FOR PORTION OF clause, and your update will only affect rows + that overlap the given interval. Furthermore, if a row's span extends outside + the FOR PORTION OF bounds, then it will be truncated to fit + within the bounds, and new rows spanning the "cut off" duration will be + inserted to preserve the old values. + + The optional RETURNING clause causes UPDATE to compute and return value(s) based on each row actually updated. @@ -114,6 +125,42 @@ UPDATE [ ONLY ] table_name [ * ] [ + + range_or_period_name + + + The range column or period to use when performing a temporal update. This + must match the range or period used in the table's temporal primary key. + + + + + + start_time + + + The earliest time (inclusive) to change in a temporal update. + This must be a value matching the base type of the range or period from + range_or_period_name. It may also + be the special value MINVALUE to indicate an update whose + beginning is unbounded. + + + + + + end_time + + + The latest time (exclusive) to change in a temporal update. + This must be a value matching the base type of the range or period from + range_or_period_name. It may also + be the special value MAXVALUE to indicate an update whose + end is unbounded. + + + + column_name diff --git a/doc/src/sgml/trigger.sgml b/doc/src/sgml/trigger.sgml index f1a845f756..1b8cfb894a 100644 --- a/doc/src/sgml/trigger.sgml +++ b/doc/src/sgml/trigger.sgml @@ -506,17 +506,18 @@ CALLED_AS_TRIGGER(fcinfo) typedef struct TriggerData { - NodeTag type; - TriggerEvent tg_event; - Relation tg_relation; - HeapTuple tg_trigtuple; - HeapTuple tg_newtuple; - Trigger *tg_trigger; - TupleTableSlot *tg_trigslot; - TupleTableSlot *tg_newslot; - Tuplestorestate *tg_oldtable; - Tuplestorestate *tg_newtable; - const Bitmapset *tg_updatedcols; + NodeTag type; + TriggerEvent tg_event; + Relation tg_relation; + HeapTuple tg_trigtuple; + HeapTuple tg_newtuple; + Trigger *tg_trigger; + TupleTableSlot *tg_trigslot; + TupleTableSlot *tg_newslot; + Tuplestorestate *tg_oldtable; + Tuplestorestate *tg_newtable; + const Bitmapset *tg_updatedcols; + ForPortionOfState *tg_temporal; } TriggerData; @@ -783,6 +784,43 @@ typedef struct Trigger + + + tg_temporal + + + Set for UPDATE and DELETE queries + that use FOR PORTION OF, otherwise NULL. + Contains a pointer to a structure of type + ForPortionOfState, defined in + nodes/execnodes.h: + + +typedef struct ForPortionOfState +{ + NodeTag type; + + char *fp_rangeName; /* the column/PERIOD named in FOR PORTION OF */ + Oid fp_rangeType; /* the type of the FOR PORTION OF expression */ + bool fp_hasPeriod; /* true iff this is a PERIOD not a range */ + char *fp_periodStartName; /* the PERIOD's start column */ + char *fp_periodEndName; /* the PERIOD's end column */ + Datum fp_targetRange; /* the range from FOR PORTION OF */ +} ForPortionOfState; + + + where fp_rangeName is the period or range + column named in the FOR PORTION OF clause, + fp_rangeType is its range type, + fp_hasPeriod indicates whether a period was used + or a range column, fp_periodStartName and + fp_periodEndName are the names of the period's + start and end columns (or NULL if a range column was used), + and fp_targetRange is a rangetype value created + by evaluating the FOR PORTION OF bounds. + + + diff --git a/src/backend/access/brin/brin_minmax_multi.c b/src/backend/access/brin/brin_minmax_multi.c index e3c98c2ffd..8495ad3fab 100644 --- a/src/backend/access/brin/brin_minmax_multi.c +++ b/src/backend/access/brin/brin_minmax_multi.c @@ -197,7 +197,7 @@ typedef struct Ranges * with basic metadata, followed by the boundary values. It has a varlena * header, so can be treated as varlena directly. * - * See range_serialize/range_deserialize for serialization details. + * See brin_range_serialize/brin_range_deserialize for serialization details. */ typedef struct SerializedRanges { @@ -216,9 +216,9 @@ typedef struct SerializedRanges char data[FLEXIBLE_ARRAY_MEMBER]; } SerializedRanges; -static SerializedRanges *range_serialize(Ranges *range); +static SerializedRanges *brin_range_serialize(Ranges *range); -static Ranges *range_deserialize(int maxvalues, SerializedRanges *range); +static Ranges *brin_range_deserialize(int maxvalues, SerializedRanges *range); /* @@ -565,14 +565,14 @@ range_deduplicate_values(Ranges *range) /* - * range_serialize + * brin_range_serialize * Serialize the in-memory representation into a compact varlena value. * * Simply copy the header and then also the individual values, as stored * in the in-memory value array. */ static SerializedRanges * -range_serialize(Ranges *range) +brin_range_serialize(Ranges *range) { Size len; int nvalues; @@ -711,14 +711,14 @@ range_serialize(Ranges *range) } /* - * range_deserialize + * brin_range_deserialize * Serialize the in-memory representation into a compact varlena value. * * Simply copy the header and then also the individual values, as stored * in the in-memory value array. */ static Ranges * -range_deserialize(int maxvalues, SerializedRanges *serialized) +brin_range_deserialize(int maxvalues, SerializedRanges *serialized) { int i, nvalues; @@ -2388,7 +2388,7 @@ brin_minmax_multi_serialize(BrinDesc *bdesc, Datum src, Datum *dst) /* At this point everything has to be fully sorted. */ Assert(ranges->nsorted == ranges->nvalues); - s = range_serialize(ranges); + s = brin_range_serialize(ranges); dst[0] = PointerGetDatum(s); } @@ -2511,7 +2511,7 @@ brin_minmax_multi_add_value(PG_FUNCTION_ARGS) maxvalues = Max(maxvalues, MINMAX_BUFFER_MIN); maxvalues = Min(maxvalues, MINMAX_BUFFER_MAX); - ranges = range_deserialize(maxvalues, serialized); + ranges = brin_range_deserialize(maxvalues, serialized); ranges->attno = attno; ranges->colloid = colloid; @@ -2564,7 +2564,7 @@ brin_minmax_multi_consistent(PG_FUNCTION_ARGS) attno = column->bv_attno; serialized = (SerializedRanges *) PG_DETOAST_DATUM(column->bv_values[0]); - ranges = range_deserialize(serialized->maxvalues, serialized); + ranges = brin_range_deserialize(serialized->maxvalues, serialized); /* inspect the ranges, and for each one evaluate the scan keys */ for (rangeno = 0; rangeno < ranges->nranges; rangeno++) @@ -2759,8 +2759,8 @@ brin_minmax_multi_union(PG_FUNCTION_ARGS) serialized_a = (SerializedRanges *) PG_DETOAST_DATUM(col_a->bv_values[0]); serialized_b = (SerializedRanges *) PG_DETOAST_DATUM(col_b->bv_values[0]); - ranges_a = range_deserialize(serialized_a->maxvalues, serialized_a); - ranges_b = range_deserialize(serialized_b->maxvalues, serialized_b); + ranges_a = brin_range_deserialize(serialized_a->maxvalues, serialized_a); + ranges_b = brin_range_deserialize(serialized_b->maxvalues, serialized_b); /* make sure neither of the ranges is NULL */ Assert(ranges_a && ranges_b); @@ -2842,7 +2842,7 @@ brin_minmax_multi_union(PG_FUNCTION_ARGS) /* cleanup and update the serialized value */ pfree(serialized_a); - col_a->bv_values[0] = PointerGetDatum(range_serialize(ranges_a)); + col_a->bv_values[0] = PointerGetDatum(brin_range_serialize(ranges_a)); PG_RETURN_VOID(); } @@ -3024,7 +3024,7 @@ brin_minmax_multi_summary_out(PG_FUNCTION_ARGS) fmgr_info(outfunc, &fmgrinfo); /* deserialize the range info easy-to-process pieces */ - ranges_deserialized = range_deserialize(ranges->maxvalues, ranges); + ranges_deserialized = brin_range_deserialize(ranges->maxvalues, ranges); appendStringInfo(&str, "nranges: %u nvalues: %u maxvalues: %u", ranges_deserialized->nranges, diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c index b322ad2355..e646076e50 100644 --- a/src/backend/commands/indexcmds.c +++ b/src/backend/commands/indexcmds.c @@ -1171,6 +1171,12 @@ DefineIndex(Oid relationId, ObjectAddressSet(address, RelationRelationId, indexRelationId); + /* + * If we created a temporal PK, create triggers for FOR PORTION OF queries. + */ + if (stmt->primary && stmt->istemporal) + CreateTemporalPrimaryKeyTriggers(rel, createdConstraintId, indexRelationId); + /* * Revert to original default_tablespace. Must do this before any return * from this function, but after index_create, so this is a good time. diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 41ab7dc2ea..b894f6a2ec 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -4218,6 +4218,67 @@ AlterTableInternal(Oid relid, List *cmds, bool recurse, AlterTableUtilityContext ATController(NULL, rel, cmds, recurse, lockmode, context); } +/* + * CreateTemporalPrimaryKeyTriggers + * Create the triggers to perform implicit INSERTs in FOR PORTION OF + * queries. + */ +void +CreateTemporalPrimaryKeyTriggers(Relation rel, Oid constraintOid, Oid indexOid) +{ + CreateTrigStmt *pk_trigger; + + /* + * Build and execute a CREATE TRIGGER statement AFTER UPDATE. + */ + pk_trigger = makeNode(CreateTrigStmt); + pk_trigger->trigname = "PK_TemporalTrigger"; + pk_trigger->relation = NULL; + pk_trigger->row = true; + pk_trigger->timing = TRIGGER_TYPE_AFTER; + pk_trigger->events = TRIGGER_TYPE_UPDATE; + pk_trigger->columns = NIL; + pk_trigger->transitionRels = NIL; + pk_trigger->whenClause = NULL; + pk_trigger->isconstraint = false; + pk_trigger->constrrel = NULL; + pk_trigger->deferrable = false; + pk_trigger->initdeferred = false; + pk_trigger->funcname = SystemFuncName("FP_insert_leftovers"); + pk_trigger->args = NIL; + + (void) CreateTrigger(pk_trigger, NULL, RelationGetRelid(rel), InvalidOid, + constraintOid, + indexOid, InvalidOid, InvalidOid, NULL, true, false); + + /* Make changes-so-far visible */ + CommandCounterIncrement(); + + /* + * Build and execute a CREATE TRIGGER statement AFTER DELETE. + */ + pk_trigger = makeNode(CreateTrigStmt); + pk_trigger->trigname = "PK_TemporalTrigger"; + pk_trigger->relation = NULL; + pk_trigger->row = true; + pk_trigger->timing = TRIGGER_TYPE_AFTER; + pk_trigger->events = TRIGGER_TYPE_DELETE; + pk_trigger->columns = NIL; + pk_trigger->transitionRels = NIL; + pk_trigger->whenClause = NULL; + pk_trigger->isconstraint = false; + pk_trigger->constrrel = NULL; + pk_trigger->deferrable = false; + pk_trigger->initdeferred = false; + pk_trigger->funcname = SystemFuncName("FP_insert_leftovers"); + pk_trigger->args = NIL; + + (void) CreateTrigger(pk_trigger, NULL, RelationGetRelid(rel), InvalidOid, + constraintOid, + indexOid, InvalidOid, InvalidOid, NULL, true, false); + +} + /* * AlterTableGetLockLevel * @@ -9041,6 +9102,9 @@ ATExecAddIndexConstraint(AlteredTableInfo *tab, Relation rel, allowSystemTableMods, false); /* is_internal */ + if (stmt->primary && stmt->istemporal) + CreateTemporalPrimaryKeyTriggers(rel, address.objectId, index_oid); + index_close(indexRel, NoLock); return address; @@ -11577,6 +11641,7 @@ validateForeignKeyConstraint(char *conname, trigdata.tg_trigtuple = ExecFetchSlotHeapTuple(slot, false, NULL); trigdata.tg_trigslot = slot; trigdata.tg_trigger = &trig; + trigdata.tg_temporal = NULL; fcinfo->context = (Node *) &trigdata; diff --git a/src/backend/commands/trigger.c b/src/backend/commands/trigger.c index 499134800b..395fd915ab 100644 --- a/src/backend/commands/trigger.c +++ b/src/backend/commands/trigger.c @@ -54,6 +54,7 @@ #include "utils/acl.h" #include "utils/builtins.h" #include "utils/bytea.h" +#include "utils/datum.h" #include "utils/fmgroids.h" #include "utils/inval.h" #include "utils/lsyscache.h" @@ -2409,6 +2410,7 @@ ExecBSDeleteTriggers(EState *estate, ResultRelInfo *relinfo) LocTriggerData.tg_event = TRIGGER_EVENT_DELETE | TRIGGER_EVENT_BEFORE; LocTriggerData.tg_relation = relinfo->ri_RelationDesc; + LocTriggerData.tg_temporal = relinfo->ri_forPortionOf; for (i = 0; i < trigdesc->numtriggers; i++) { Trigger *trigger = &trigdesc->triggers[i]; @@ -2504,6 +2506,7 @@ ExecBRDeleteTriggers(EState *estate, EPQState *epqstate, TRIGGER_EVENT_ROW | TRIGGER_EVENT_BEFORE; LocTriggerData.tg_relation = relinfo->ri_RelationDesc; + LocTriggerData.tg_temporal = relinfo->ri_forPortionOf; for (i = 0; i < trigdesc->numtriggers; i++) { HeapTuple newtuple; @@ -2585,6 +2588,7 @@ ExecIRDeleteTriggers(EState *estate, ResultRelInfo *relinfo, TRIGGER_EVENT_ROW | TRIGGER_EVENT_INSTEAD; LocTriggerData.tg_relation = relinfo->ri_RelationDesc; + LocTriggerData.tg_temporal = relinfo->ri_forPortionOf; ExecForceStoreHeapTuple(trigtuple, slot, false); @@ -2648,6 +2652,7 @@ ExecBSUpdateTriggers(EState *estate, ResultRelInfo *relinfo) TRIGGER_EVENT_BEFORE; LocTriggerData.tg_relation = relinfo->ri_RelationDesc; LocTriggerData.tg_updatedcols = updatedCols; + LocTriggerData.tg_temporal = relinfo->ri_forPortionOf; for (i = 0; i < trigdesc->numtriggers; i++) { Trigger *trigger = &trigdesc->triggers[i]; @@ -2760,6 +2765,7 @@ ExecBRUpdateTriggers(EState *estate, EPQState *epqstate, TRIGGER_EVENT_ROW | TRIGGER_EVENT_BEFORE; LocTriggerData.tg_relation = relinfo->ri_RelationDesc; + LocTriggerData.tg_temporal = relinfo->ri_forPortionOf; updatedCols = ExecGetAllUpdatedCols(relinfo, estate); LocTriggerData.tg_updatedcols = updatedCols; for (i = 0; i < trigdesc->numtriggers; i++) @@ -2883,6 +2889,7 @@ ExecIRUpdateTriggers(EState *estate, ResultRelInfo *relinfo, TRIGGER_EVENT_ROW | TRIGGER_EVENT_INSTEAD; LocTriggerData.tg_relation = relinfo->ri_RelationDesc; + LocTriggerData.tg_temporal = relinfo->ri_forPortionOf; ExecForceStoreHeapTuple(trigtuple, oldslot, false); @@ -3321,6 +3328,7 @@ typedef struct AfterTriggerSharedData Oid ats_tgoid; /* the trigger's ID */ Oid ats_relid; /* the relation it's on */ CommandId ats_firing_id; /* ID for firing cycle */ + ForPortionOfState *for_portion_of; /* the FOR PORTION OF clause */ struct AfterTriggersTableData *ats_table; /* transition table access */ Bitmapset *ats_modifiedcols; /* modified columns */ } AfterTriggerSharedData; @@ -3549,6 +3557,7 @@ static SetConstraintState SetConstraintStateCreate(int numalloc); static SetConstraintState SetConstraintStateCopy(SetConstraintState state); static SetConstraintState SetConstraintStateAddItem(SetConstraintState state, Oid tgoid, bool tgisdeferred); +static ForPortionOfState *CopyForPortionOfState(ForPortionOfState *src); static void cancel_prior_stmt_triggers(Oid relid, CmdType cmdType, int tgevent); @@ -4026,6 +4035,7 @@ AfterTriggerExecute(EState *estate, LocTriggerData.tg_relation = rel; if (TRIGGER_FOR_UPDATE(LocTriggerData.tg_trigger->tgtype)) LocTriggerData.tg_updatedcols = evtshared->ats_modifiedcols; + LocTriggerData.tg_temporal = evtshared->for_portion_of; MemoryContextReset(per_tuple_context); @@ -5422,6 +5432,50 @@ AfterTriggerPendingOnRel(Oid relid) return false; } +/* ---------- + * ForPortionOfState() + * + * Copies a ForPortionOfState into the current memory context. + */ +static ForPortionOfState * +CopyForPortionOfState(ForPortionOfState *src) +{ + ForPortionOfState *dst = NULL; + if (src) { + MemoryContext oldctx; + RangeType *r; + TypeCacheEntry *typcache; + + /* + * Need to lift the FOR PORTION OF details into a higher memory context + * because cascading foreign key update/deletes can cause triggers to fire + * triggers (both other TRI triggers and the insert_leftovers trigger), + * and the AfterTriggerEvents will outlive the FPO details of the original + * query. + */ + oldctx = MemoryContextSwitchTo(TopTransactionContext); + dst = makeNode(ForPortionOfState); + dst->fp_rangeName = pstrdup(src->fp_rangeName); + dst->fp_rangeType = src->fp_rangeType; + dst->fp_hasPeriod = src->fp_hasPeriod; + + if (src->fp_periodStartName) + dst->fp_periodStartName = pstrdup(src->fp_periodStartName); + else + dst->fp_periodStartName = NULL; + + if (src->fp_periodEndName) + dst->fp_periodEndName = pstrdup(src->fp_periodEndName); + else + dst->fp_periodEndName = NULL; + + r = DatumGetRangeTypeP(src->fp_targetRange); + typcache = lookup_type_cache(RangeTypeGetOid(r), TYPECACHE_RANGE_INFO); + dst->fp_targetRange = datumCopy(src->fp_targetRange, typcache->typbyval, typcache->typlen); + MemoryContextSwitchTo(oldctx); + } + return dst; +} /* ---------- * AfterTriggerSaveEvent() @@ -5741,6 +5795,7 @@ AfterTriggerSaveEvent(EState *estate, ResultRelInfo *relinfo, else new_shared.ats_table = NULL; new_shared.ats_modifiedcols = modifiedCols; + new_shared.for_portion_of = CopyForPortionOfState(relinfo->ri_forPortionOf); afterTriggerAddEvent(&afterTriggers.query_stack[afterTriggers.query_depth].events, &new_event, &new_shared); diff --git a/src/backend/executor/execMain.c b/src/backend/executor/execMain.c index b3ce4bae53..80a3cdfc22 100644 --- a/src/backend/executor/execMain.c +++ b/src/backend/executor/execMain.c @@ -1240,6 +1240,7 @@ InitResultRelInfo(ResultRelInfo *resultRelInfo, resultRelInfo->ri_projectReturning = NULL; resultRelInfo->ri_onConflictArbiterIndexes = NIL; resultRelInfo->ri_onConflict = NULL; + resultRelInfo->ri_forPortionOf = NULL; resultRelInfo->ri_ReturningSlot = NULL; resultRelInfo->ri_TrigOldSlot = NULL; resultRelInfo->ri_TrigNewSlot = NULL; diff --git a/src/backend/executor/nodeModifyTable.c b/src/backend/executor/nodeModifyTable.c index c24684aa6f..930feea214 100644 --- a/src/backend/executor/nodeModifyTable.c +++ b/src/backend/executor/nodeModifyTable.c @@ -51,6 +51,8 @@ #include "utils/builtins.h" #include "utils/datum.h" #include "utils/memutils.h" +#include "utils/rangetypes.h" +#include "utils/lsyscache.h" #include "utils/rel.h" @@ -3046,6 +3048,37 @@ ExecInitModifyTable(ModifyTable *node, EState *estate, int eflags) } } + /* + * If needed, initialize the target range for FOR PORTION OF. + */ + if (node->forPortionOf) + { + ForPortionOfExpr *forPortionOf = (ForPortionOfExpr *) node->forPortionOf; + Datum targetRange; + bool isNull; + ExprContext *econtext; + ExprState *exprState; + + /* Eval the FOR PORTION OF target */ + if (mtstate->ps.ps_ExprContext == NULL) + ExecAssignExprContext(estate, &mtstate->ps); + econtext = mtstate->ps.ps_ExprContext; + + exprState = ExecPrepareExpr((Expr *) forPortionOf->targetRange, estate); + targetRange = ExecEvalExpr(exprState, econtext, &isNull); + + if (isNull) elog(ERROR, "Got a NULL FOR PORTION OF target range"); + + resultRelInfo->ri_forPortionOf = makeNode(ForPortionOfState); + resultRelInfo->ri_forPortionOf->fp_rangeName = forPortionOf->range_name; + resultRelInfo->ri_forPortionOf->fp_periodStartName = forPortionOf->period_start_name; + resultRelInfo->ri_forPortionOf->fp_periodEndName = forPortionOf->period_end_name; + resultRelInfo->ri_forPortionOf->fp_targetRange = targetRange; + resultRelInfo->ri_forPortionOf->fp_rangeType = forPortionOf->rangeType; + + /* Don't free the ExprContext here because the result must last for the whole query */ + } + /* * If we have any secondary relations in an UPDATE or DELETE, they need to * be treated like non-locked relations in SELECT FOR UPDATE, ie, the diff --git a/src/backend/nodes/copyfuncs.c b/src/backend/nodes/copyfuncs.c index 71070aa117..8630ef9a77 100644 --- a/src/backend/nodes/copyfuncs.c +++ b/src/backend/nodes/copyfuncs.c @@ -215,6 +215,7 @@ _copyModifyTable(const ModifyTable *from) COPY_NODE_FIELD(rowMarks); COPY_SCALAR_FIELD(epqParam); COPY_SCALAR_FIELD(onConflictAction); + COPY_NODE_FIELD(forPortionOf); COPY_NODE_FIELD(arbiterIndexes); COPY_NODE_FIELD(onConflictSet); COPY_NODE_FIELD(onConflictCols); @@ -2302,6 +2303,33 @@ _copyOnConflictExpr(const OnConflictExpr *from) return newnode; } +/* + * _copyForPortionOfExpr + */ +static ForPortionOfExpr * +_copyForPortionOfExpr(const ForPortionOfExpr *from) +{ + ForPortionOfExpr *newnode = makeNode(ForPortionOfExpr); + + COPY_SCALAR_FIELD(range_attno); + COPY_SCALAR_FIELD(start_attno); + COPY_SCALAR_FIELD(end_attno); + COPY_STRING_FIELD(range_name); + COPY_STRING_FIELD(period_start_name); + COPY_STRING_FIELD(period_end_name); + COPY_NODE_FIELD(range); + COPY_NODE_FIELD(startCol); + COPY_NODE_FIELD(endCol); + COPY_NODE_FIELD(targetStart); + COPY_NODE_FIELD(targetEnd); + COPY_NODE_FIELD(targetRange); + COPY_SCALAR_FIELD(rangeType); + COPY_NODE_FIELD(overlapsExpr); + COPY_NODE_FIELD(rangeSet); + + return newnode; +} + /* **************************************************************** * pathnodes.h copy functions * @@ -2677,6 +2705,19 @@ _copyCTECycleClause(const CTECycleClause *from) return newnode; } +static ForPortionOfClause * +_copyForPortionOfClause(const ForPortionOfClause *from) +{ + ForPortionOfClause *newnode = makeNode(ForPortionOfClause); + + COPY_STRING_FIELD(range_name); + COPY_SCALAR_FIELD(range_name_location); + COPY_NODE_FIELD(target_start); + COPY_NODE_FIELD(target_end); + + return newnode; +} + static CommonTableExpr * _copyCommonTableExpr(const CommonTableExpr *from) { @@ -5307,6 +5348,9 @@ copyObjectImpl(const void *from) case T_OnConflictExpr: retval = _copyOnConflictExpr(from); break; + case T_ForPortionOfExpr: + retval = _copyForPortionOfExpr(from); + break; /* * RELATION NODES @@ -5853,6 +5897,9 @@ copyObjectImpl(const void *from) case T_CTECycleClause: retval = _copyCTECycleClause(from); break; + case T_ForPortionOfClause: + retval = _copyForPortionOfClause(from); + break; case T_CommonTableExpr: retval = _copyCommonTableExpr(from); break; diff --git a/src/backend/nodes/equalfuncs.c b/src/backend/nodes/equalfuncs.c index e372fbf862..cb893f566e 100644 --- a/src/backend/nodes/equalfuncs.c +++ b/src/backend/nodes/equalfuncs.c @@ -828,6 +828,24 @@ _equalOnConflictExpr(const OnConflictExpr *a, const OnConflictExpr *b) return true; } +static bool +_equalForPortionOfExpr(const ForPortionOfExpr *a, const ForPortionOfExpr *b) +{ + COMPARE_SCALAR_FIELD(range_attno); + COMPARE_STRING_FIELD(range_name); + COMPARE_NODE_FIELD(range); + COMPARE_NODE_FIELD(startCol); + COMPARE_NODE_FIELD(endCol); + COMPARE_NODE_FIELD(targetStart); + COMPARE_NODE_FIELD(targetEnd); + COMPARE_NODE_FIELD(targetRange); + COMPARE_SCALAR_FIELD(rangeType); + COMPARE_NODE_FIELD(overlapsExpr); + COMPARE_NODE_FIELD(rangeSet); + + return true; +} + /* * Stuff from pathnodes.h */ @@ -2925,6 +2943,17 @@ _equalCTECycleClause(const CTECycleClause *a, const CTECycleClause *b) return true; } +static bool +_equalForPortionOfClause(const ForPortionOfClause *a, const ForPortionOfClause *b) +{ + COMPARE_STRING_FIELD(range_name); + COMPARE_SCALAR_FIELD(range_name_location); + COMPARE_NODE_FIELD(target_start); + COMPARE_NODE_FIELD(target_end); + + return true; +} + static bool _equalCommonTableExpr(const CommonTableExpr *a, const CommonTableExpr *b) { @@ -3304,6 +3333,9 @@ equal(const void *a, const void *b) case T_OnConflictExpr: retval = _equalOnConflictExpr(a, b); break; + case T_ForPortionOfExpr: + retval = _equalForPortionOfExpr(a, b); + break; case T_JoinExpr: retval = _equalJoinExpr(a, b); break; @@ -3840,6 +3872,9 @@ equal(const void *a, const void *b) case T_CTECycleClause: retval = _equalCTECycleClause(a, b); break; + case T_ForPortionOfClause: + retval = _equalForPortionOfClause(a, b); + break; case T_CommonTableExpr: retval = _equalCommonTableExpr(a, b); break; diff --git a/src/backend/nodes/nodeFuncs.c b/src/backend/nodes/nodeFuncs.c index ce6c2534f9..abd092cdd3 100644 --- a/src/backend/nodes/nodeFuncs.c +++ b/src/backend/nodes/nodeFuncs.c @@ -2237,6 +2237,14 @@ expression_tree_walker(Node *node, return true; } break; + case T_ForPortionOfExpr: + { + ForPortionOfExpr *forPortionOf = (ForPortionOfExpr *) node; + + if (walker((Node *) forPortionOf->targetRange, context)) + return true; + } + break; case T_PartitionPruneStepOp: { PartitionPruneStepOp *opstep = (PartitionPruneStepOp *) node; @@ -2373,6 +2381,8 @@ query_tree_walker(Query *query, return true; if (walker((Node *) query->withCheckOptions, context)) return true; + if (walker((Node *) query->forPortionOf, context)) + return true; if (walker((Node *) query->onConflict, context)) return true; if (walker((Node *) query->returningList, context)) diff --git a/src/backend/nodes/outfuncs.c b/src/backend/nodes/outfuncs.c index 4e0469a580..0ba0775100 100644 --- a/src/backend/nodes/outfuncs.c +++ b/src/backend/nodes/outfuncs.c @@ -416,10 +416,12 @@ _outModifyTable(StringInfo str, const ModifyTable *node) WRITE_NODE_FIELD(rowMarks); WRITE_INT_FIELD(epqParam); WRITE_ENUM_FIELD(onConflictAction, OnConflictAction); + WRITE_NODE_FIELD(forPortionOf); WRITE_NODE_FIELD(arbiterIndexes); WRITE_NODE_FIELD(onConflictSet); WRITE_NODE_FIELD(onConflictCols); WRITE_NODE_FIELD(onConflictWhere); + // TODO: add things for ForPortionOf WRITE_UINT_FIELD(exclRelRTI); WRITE_NODE_FIELD(exclRelTlist); } @@ -1740,6 +1742,24 @@ _outOnConflictExpr(StringInfo str, const OnConflictExpr *node) WRITE_NODE_FIELD(exclRelTlist); } +static void +_outForPortionOfExpr(StringInfo str, const ForPortionOfExpr *node) +{ + WRITE_NODE_TYPE("FORPORTIONOFEXPR"); + + WRITE_INT_FIELD(range_attno); + WRITE_STRING_FIELD(range_name); + WRITE_NODE_FIELD(range); + WRITE_NODE_FIELD(startCol); + WRITE_NODE_FIELD(endCol); + WRITE_NODE_FIELD(targetStart); + WRITE_NODE_FIELD(targetEnd); + WRITE_NODE_FIELD(targetRange); + WRITE_OID_FIELD(rangeType); + WRITE_NODE_FIELD(overlapsExpr); + WRITE_NODE_FIELD(rangeSet); +} + /***************************************************************************** * * Stuff from pathnodes.h. @@ -4194,6 +4214,9 @@ outNode(StringInfo str, const void *obj) case T_OnConflictExpr: _outOnConflictExpr(str, obj); break; + case T_ForPortionOfExpr: + _outForPortionOfExpr(str, obj); + break; case T_Path: _outPath(str, obj); break; diff --git a/src/backend/nodes/readfuncs.c b/src/backend/nodes/readfuncs.c index f0b34ecfac..829a1d7bf6 100644 --- a/src/backend/nodes/readfuncs.c +++ b/src/backend/nodes/readfuncs.c @@ -1421,6 +1421,28 @@ _readAppendRelInfo(void) * Stuff from parsenodes.h. */ +/* + * _readForPortionOfExpr + */ +static ForPortionOfExpr * +_readForPortionOfExpr(void) +{ + READ_LOCALS(ForPortionOfExpr); + + READ_INT_FIELD(range_attno); + READ_STRING_FIELD(range_name); + READ_NODE_FIELD(range); + READ_NODE_FIELD(startCol); + READ_NODE_FIELD(endCol); + READ_NODE_FIELD(targetStart); + READ_NODE_FIELD(targetEnd); + READ_NODE_FIELD(targetRange); + READ_OID_FIELD(rangeType); + READ_NODE_FIELD(overlapsExpr); + READ_NODE_FIELD(rangeSet); + READ_DONE(); +} + /* * _readRangeTblEntry */ @@ -1695,6 +1717,7 @@ _readModifyTable(void) READ_NODE_FIELD(rowMarks); READ_INT_FIELD(epqParam); READ_ENUM_FIELD(onConflictAction, OnConflictAction); + READ_NODE_FIELD(forPortionOf); READ_NODE_FIELD(arbiterIndexes); READ_NODE_FIELD(onConflictSet); READ_NODE_FIELD(onConflictCols); @@ -2844,6 +2867,8 @@ parseNodeString(void) return_value = _readOnConflictExpr(); else if (MATCH("APPENDRELINFO", 13)) return_value = _readAppendRelInfo(); + else if (MATCH("FORPORTIONOFEXPR", 16)) + return_value = _readForPortionOfExpr(); else if (MATCH("RTE", 3)) return_value = _readRangeTblEntry(); else if (MATCH("RANGETBLFUNCTION", 16)) diff --git a/src/backend/optimizer/plan/createplan.c b/src/backend/optimizer/plan/createplan.c index 439e6b6426..16aab15ff6 100644 --- a/src/backend/optimizer/plan/createplan.c +++ b/src/backend/optimizer/plan/createplan.c @@ -311,7 +311,8 @@ static ModifyTable *make_modifytable(PlannerInfo *root, Plan *subplan, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, - List *rowMarks, OnConflictExpr *onconflict, int epqParam); + List *rowMarks, OnConflictExpr *onconflict, + ForPortionOfExpr *forPortionOf, int epqParam); static GatherMerge *create_gather_merge_plan(PlannerInfo *root, GatherMergePath *best_path); @@ -2756,6 +2757,7 @@ create_modifytable_plan(PlannerInfo *root, ModifyTablePath *best_path) best_path->returningLists, best_path->rowMarks, best_path->onconflict, + best_path->forPortionOf, best_path->epqParam); copy_generic_path_info(&plan->plan, &best_path->path); @@ -6879,7 +6881,8 @@ make_modifytable(PlannerInfo *root, Plan *subplan, List *resultRelations, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, - List *rowMarks, OnConflictExpr *onconflict, int epqParam) + List *rowMarks, OnConflictExpr *onconflict, + ForPortionOfExpr *forPortionOf, int epqParam) { ModifyTable *node = makeNode(ModifyTable); List *fdw_private_list; @@ -6944,6 +6947,7 @@ make_modifytable(PlannerInfo *root, Plan *subplan, node->exclRelTlist = onconflict->exclRelTlist; } node->updateColnosLists = updateColnosLists; + node->forPortionOf = (Node *) forPortionOf; node->withCheckOptionLists = withCheckOptionLists; node->returningLists = returningLists; node->rowMarks = rowMarks; diff --git a/src/backend/optimizer/plan/planner.c b/src/backend/optimizer/plan/planner.c index 1868c4eff4..e586083c8f 100644 --- a/src/backend/optimizer/plan/planner.c +++ b/src/backend/optimizer/plan/planner.c @@ -1851,6 +1851,7 @@ grouping_planner(PlannerInfo *root, double tuple_fraction) 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 9ce5f95e3b..ed573b5f43 100644 --- a/src/backend/optimizer/util/pathnode.c +++ b/src/backend/optimizer/util/pathnode.c @@ -3633,7 +3633,7 @@ create_modifytable_path(PlannerInfo *root, RelOptInfo *rel, List *updateColnosLists, List *withCheckOptionLists, List *returningLists, List *rowMarks, OnConflictExpr *onconflict, - int epqParam) + ForPortionOfExpr *forPortionOf, int epqParam) { ModifyTablePath *pathnode = makeNode(ModifyTablePath); @@ -3698,6 +3698,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 438b077004..0246cd25fd 100644 --- a/src/backend/parser/analyze.c +++ b/src/backend/parser/analyze.c @@ -25,6 +25,7 @@ #include "postgres.h" #include "access/sysattr.h" +#include "catalog/pg_period.h" #include "catalog/pg_proc.h" #include "catalog/pg_type.h" #include "miscadmin.h" @@ -44,12 +45,14 @@ #include "parser/parse_relation.h" #include "parser/parse_target.h" #include "parser/parse_type.h" +#include "parser/parser.h" #include "parser/parsetree.h" #include "rewrite/rewriteManip.h" #include "utils/backend_status.h" #include "utils/builtins.h" #include "utils/guc.h" #include "utils/queryjumble.h" +#include "utils/lsyscache.h" #include "utils/rel.h" #include "utils/syscache.h" @@ -65,6 +68,10 @@ static List *transformInsertRow(ParseState *pstate, List *exprlist, bool strip_indirection); static OnConflictExpr *transformOnConflictClause(ParseState *pstate, OnConflictClause *onConflictClause); +static Node *transformForPortionOfBound(Node *n); +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); @@ -77,7 +84,8 @@ static Query *transformReturnStmt(ParseState *pstate, ReturnStmt *stmt); static Query *transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt); static List *transformReturningList(ParseState *pstate, List *returningList); static List *transformUpdateTargetList(ParseState *pstate, - List *targetList); + List *targetList, + ForPortionOfExpr *forPortionOf); static Query *transformPLAssignStmt(ParseState *pstate, PLAssignStmt *stmt); static Query *transformDeclareCursorStmt(ParseState *pstate, @@ -430,6 +438,7 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt) { Query *qry = makeNode(Query); ParseNamespaceItem *nsitem; + Node *whereClause; Node *qual; qry->commandType = CMD_DELETE; @@ -468,7 +477,20 @@ transformDeleteStmt(ParseState *pstate, DeleteStmt *stmt) nsitem->p_lateral_only = false; nsitem->p_lateral_ok = true; - qual = transformWhereClause(pstate, stmt->whereClause, + if (stmt->forPortionOf) + qry->forPortionOf = transformForPortionOfClause(pstate, qry->resultRelation, stmt->forPortionOf); + + // TODO: DRY with UPDATE + if (stmt->forPortionOf) + { + if (stmt->whereClause) + whereClause = (Node *) makeBoolExpr(AND_EXPR, list_make2(qry->forPortionOf->overlapsExpr, stmt->whereClause), -1); + else + whereClause = qry->forPortionOf->overlapsExpr; + } + else + whereClause = stmt->whereClause; + qual = transformWhereClause(pstate, whereClause, EXPR_KIND_WHERE, "WHERE"); qry->returningList = transformReturningList(pstate, stmt->returningList); @@ -1085,7 +1107,7 @@ transformOnConflictClause(ParseState *pstate, * Now transform the UPDATE subexpressions. */ onConflictSet = - transformUpdateTargetList(pstate, onConflictClause->targetList); + transformUpdateTargetList(pstate, onConflictClause->targetList, NULL); onConflictWhere = transformWhereClause(pstate, onConflictClause->whereClause, @@ -1115,6 +1137,329 @@ transformOnConflictClause(ParseState *pstate, return result; } +/* + * transformForPortionOfBound + * transforms MINVALUE and MAXVALUE pseudo-column references to NULL + * (which represent "unbounded" in a range type, otherwise returns + * its input unchanged. + */ +static Node * +transformForPortionOfBound(Node *n) +{ + if (nodeTag(n) == T_ColumnRef) + { + ColumnRef *cref = (ColumnRef *) n; + char *cname = ""; + + if (list_length(cref->fields) == 1 && + IsA(linitial(cref->fields), String)) + cname = strVal(linitial(cref->fields)); + + if (strcmp("minvalue", cname) == 0 || strcmp("maxvalue", cname) == 0) + { + A_Const *n2 = makeNode(A_Const); + n2->val.type = T_Null; + n2->location = ((ColumnRef *)n)->location; + return (Node *)n2; + } + else + return n; + } + else + return n; +} + +/* + * 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_nsitem->p_rte; + char *range_name = forPortionOf->range_name; + char *range_type_name; + int range_attno = InvalidOid; + int start_attno = InvalidOid; + int end_attno = InvalidOid; + char *startcolname = NULL; + char *endcolname = NULL; + ForPortionOfExpr *result; + List *targetList; + Oid pkoid; + HeapTuple indexTuple; + Form_pg_index pk; + Node *target_start, *target_end; + FuncCall *fc; + + result = makeNode(ForPortionOfExpr); + result->range = NULL; + + /* Make sure the table has a primary key */ + 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? + indexTuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(pkoid)); + if (!HeapTupleIsValid(indexTuple)) /* should not happen */ + elog(ERROR, "cache lookup failed for index %u", pkoid); + pk = (Form_pg_index) GETSTRUCT(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))); + + /* + * First look for a range column, then look for a period. + */ + range_attno = attnameAttNum(targetrel, range_name, false); + if (range_attno != InvalidAttrNumber) + { + Form_pg_attribute attr = TupleDescAttr(targetrel->rd_att, range_attno - 1); + Var *v; + + // 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 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))); + } + + v = makeVar( + rtindex, + range_attno, + attr->atttypid, + attr->atttypmod, + attr->attcollation, + 0); + v->location = forPortionOf->range_name_location; + result->range = (Expr *) v; + result->rangeType = attr->atttypid; + range_type_name = get_typname(attr->atttypid); + + } else { + Oid relid = RelationGetRelid(targetrel); + HeapTuple perTuple = SearchSysCache2(PERIODNAME, + ObjectIdGetDatum(relid), + PointerGetDatum(range_name)); + if (HeapTupleIsValid(perTuple)) + { + Form_pg_period per = (Form_pg_period) GETSTRUCT(perTuple); + Form_pg_attribute startattr, endattr; + bool pkeyIncludesPeriod = false; + Var *startvar, *endvar; + FuncCall *periodRange; + + Type rngtype = typeidType(per->perrngtype); + range_type_name = typeTypeName(rngtype); + ReleaseSysCache(rngtype); + start_attno = per->perstart; + end_attno = per->perend; + + startattr = TupleDescAttr(targetrel->rd_att, start_attno - 1); + endattr = TupleDescAttr(targetrel->rd_att, end_attno - 1); + + startcolname = NameStr(startattr->attname); + endcolname = NameStr(endattr->attname); + result->period_start_name = startcolname; + result->period_end_name = endcolname; + + /* Make sure the period is the last part of the pkey. */ + if (pk->indkey.values[pk->indnkeyatts - 1] == InvalidOid) + { + /* + * The PK ends with an expression. Make sure it's for our period. + * There should be only one entry in indexprs, and it should be ours. + */ + pkeyIncludesPeriod = pk->indperiod == per->oid; + } + + if (!pkeyIncludesPeriod) + 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))); + + startvar = makeVar( + rtindex, + per->perstart, + startattr->atttypid, + startattr->atttypmod, + startattr->attcollation, + 0); + startvar->location = forPortionOf->range_name_location; + + endvar = makeVar( + rtindex, + per->perend, + endattr->atttypid, + endattr->atttypmod, + endattr->attcollation, + 0); + endvar->location = forPortionOf->range_name_location; + + ReleaseSysCache(perTuple); + + periodRange = makeFuncCall(SystemFuncName(range_type_name), + list_make2(startvar, endvar), + COERCE_EXPLICIT_CALL, + forPortionOf->range_name_location); + result->range = (Expr *) periodRange; + result->rangeType = typenameTypeId(pstate, typeStringToTypeName(range_type_name)); + } + } + ReleaseSysCache(indexTuple); + + if (result->range == NULL) + 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))); + + /* + * Build a range from the FROM ... TO .... bounds. + * This should give a constant result, so we accept functions like NOW() + * but not column references, subqueries, etc. + * + * It also permits MINVALUE and MAXVALUE like declarative partitions. + */ + target_start = transformForPortionOfBound(forPortionOf->target_start); + target_end = transformForPortionOfBound(forPortionOf->target_end); + fc = makeFuncCall(SystemFuncName(range_type_name), + list_make2(target_start, target_end), + COERCE_EXPLICIT_CALL, + forPortionOf->range_name_location); + result->targetRange = transformExpr(pstate, (Node *) fc, EXPR_KIND_UPDATE_PORTION); + + /* 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, (Node *) fc, + forPortionOf->range_name_location); + + /* + * Now make sure we update the start/end time of the record. + * For a range col (r) this is `r = r * targetRange`. + * For a PERIOD with cols (s, e) this is `s = lower(tsrange(s, e) * targetRange)` + * and `e = upper(tsrange(s, e) * targetRange` (of course not necessarily with + * tsrange, but with whatever range type is used there)). + * + * We also compute the possible left-behind bits at the start and end of the tuple, + * so that we can INSERT them if necessary. + */ + // TODO: Only do this for UPDATE, not DELETE: + targetList = NIL; + if (range_attno != InvalidAttrNumber) + { + Expr *rangeSetExpr = (Expr *) makeSimpleA_Expr(AEXPR_OP, "*", + // TODO: Maybe need a copy here?: + (Node *) result->range, (Node *) fc, + forPortionOf->range_name_location); + TargetEntry *tle; + + rangeSetExpr = (Expr *) transformExpr(pstate, (Node *) rangeSetExpr, EXPR_KIND_UPDATE_PORTION); + tle = makeTargetEntry(rangeSetExpr, + range_attno, + range_name, + false); + + targetList = lappend(targetList, tle); + + /* Mark the range column as requiring update permissions */ + target_rte->updatedCols = bms_add_member(target_rte->updatedCols, + range_attno - FirstLowInvalidHeapAttributeNumber); + + } else { + Expr *intersectExpr; + Expr *boundSetExpr; + TargetEntry *tle; + + /* Set up targetList for the PERIOD start column */ + + intersectExpr = (Expr *) makeSimpleA_Expr(AEXPR_OP, "*", + // TODO: copy? + (Node *) result->range, (Node *) fc, + forPortionOf->range_name_location); + + boundSetExpr = (Expr *) makeFuncCall(SystemFuncName("lower"), + list_make1(intersectExpr), + COERCE_EXPLICIT_CALL, + forPortionOf->range_name_location); + boundSetExpr = (Expr *) transformExpr(pstate, (Node *) boundSetExpr, EXPR_KIND_UPDATE_PORTION); + + tle = makeTargetEntry(boundSetExpr, + start_attno, + startcolname, + false); + + targetList = lappend(targetList, tle); + + /* Set up targetList for the PERIOD end column */ + + boundSetExpr = (Expr *) makeFuncCall(SystemFuncName("upper"), + list_make1(intersectExpr), + COERCE_EXPLICIT_CALL, + forPortionOf->range_name_location); + boundSetExpr = (Expr *) transformExpr(pstate, (Node *) boundSetExpr, EXPR_KIND_UPDATE_PORTION); + + tle = makeTargetEntry(boundSetExpr, + end_attno, + endcolname, + false); + + targetList = lappend(targetList, tle); + + /* Mark the bound columns as requiring update permissions */ + target_rte->updatedCols = bms_add_member(target_rte->updatedCols, + start_attno - FirstLowInvalidHeapAttributeNumber); + target_rte->updatedCols = bms_add_member(target_rte->updatedCols, + end_attno - FirstLowInvalidHeapAttributeNumber); + } + + result->rangeSet = targetList; + result->range_name = range_name; + result->range_attno = range_attno; + result->start_attno = start_attno; + result->end_attno = end_attno; + + return result; +} /* * BuildOnConflictExcludedTargetlist @@ -2307,6 +2652,7 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) { Query *qry = makeNode(Query); ParseNamespaceItem *nsitem; + Node *whereClause; Node *qual; qry->commandType = CMD_UPDATE; @@ -2324,6 +2670,10 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) stmt->relation->inh, true, ACL_UPDATE); + + if (stmt->forPortionOf) + qry->forPortionOf = transformForPortionOfClause(pstate, qry->resultRelation, stmt->forPortionOf); + nsitem = pstate->p_target_nsitem; /* subqueries in FROM cannot access the result relation */ @@ -2340,7 +2690,16 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) nsitem->p_lateral_only = false; nsitem->p_lateral_ok = true; - qual = transformWhereClause(pstate, stmt->whereClause, + if (stmt->forPortionOf) + { + if (stmt->whereClause) + whereClause = (Node *) makeBoolExpr(AND_EXPR, list_make2(qry->forPortionOf->overlapsExpr, stmt->whereClause), -1); + else + whereClause = qry->forPortionOf->overlapsExpr; + } + else + whereClause = stmt->whereClause; + qual = transformWhereClause(pstate, whereClause, EXPR_KIND_WHERE, "WHERE"); qry->returningList = transformReturningList(pstate, stmt->returningList); @@ -2349,7 +2708,7 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) * Now we are done with SELECT-like processing, and can get on with * transforming the target list to match the UPDATE target columns. */ - qry->targetList = transformUpdateTargetList(pstate, stmt->targetList); + qry->targetList = transformUpdateTargetList(pstate, stmt->targetList, qry->forPortionOf); qry->rtable = pstate->p_rtable; qry->jointree = makeFromExpr(pstate->p_joinlist, qual); @@ -2367,7 +2726,7 @@ transformUpdateStmt(ParseState *pstate, UpdateStmt *stmt) * handle SET clause in UPDATE/INSERT ... ON CONFLICT UPDATE */ static List * -transformUpdateTargetList(ParseState *pstate, List *origTlist) +transformUpdateTargetList(ParseState *pstate, List *origTlist, ForPortionOfExpr *forPortionOf) { List *tlist = NIL; RangeTblEntry *target_rte; @@ -2417,6 +2776,9 @@ transformUpdateTargetList(ParseState *pstate, List *origTlist) RelationGetRelationName(pstate->p_target_relation)), parser_errposition(pstate, origTarget->location))); + /* TODO: Make sure user isn't trying to SET the range attribute directly --- TODO or permit it?? */ + + updateTargetListEntry(pstate, tle, origTarget->name, attrno, origTarget->indirection, diff --git a/src/backend/parser/gram.y b/src/backend/parser/gram.y index 90fff07405..dbc5b38d00 100644 --- a/src/backend/parser/gram.y +++ b/src/backend/parser/gram.y @@ -244,6 +244,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; @@ -520,6 +521,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 @@ -694,7 +696,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 @@ -767,6 +769,16 @@ static Node *makeRecursiveViewSelect(char *relname, List *aliases, Node *query); %nonassoc '<' '>' '=' LESS_EQUALS GREATER_EQUALS NOT_EQUALS %nonassoc BETWEEN IN_P LIKE ILIKE SIMILAR NOT_LA %nonassoc ESCAPE /* ESCAPE must be just above LIKE/ILIKE/SIMILAR */ +/* + * We need to handle this shift/reduce conflict: + * FOR PORTION OF valid_at FROM INTERVAL YEAR TO MONTH TO foo. + * This is basically the classic "dangling else" problem, and we want a + * similar resolution: treat the TO as part of the INTERVAL, not as part of + * the FROM ... TO .... Users can add parentheses if that's a problem. + * TO just needs to be higher precedence than YEAR_P etc. + */ +%nonassoc YEAR_P MONTH_P DAY_P HOUR_P MINUTE_P +%nonassoc TO /* * To support target_el without AS, it used to be necessary to assign IDENT an * explicit precedence just less than Op. While that's not really necessary @@ -11161,13 +11173,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; } @@ -11230,6 +11244,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 @@ -11237,10 +11252,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; } @@ -12518,6 +12534,19 @@ relation_expr_opt_alias: relation_expr %prec UMINUS } ; +for_portion_of_clause: + FOR PORTION OF ColId FROM a_expr TO a_expr + { + ForPortionOfClause *n = makeNode(ForPortionOfClause); + n->range_name = $4; + n->range_name_location = @4; + n->target_start = $6; + n->target_end = $8; + $$ = n; + } + | /*EMPTY*/ { $$ = NULL; } + ; + /* * TABLESAMPLE decoration in a FROM item */ @@ -15706,6 +15735,7 @@ unreserved_keyword: | PASSWORD | PLANS | POLICY + | PORTION | PRECEDING | PREPARE | PREPARED @@ -16287,6 +16317,7 @@ bare_label_keyword: | PLACING | PLANS | POLICY + | PORTION | POSITION | PRECEDING | PREPARE diff --git a/src/backend/parser/parse_agg.c b/src/backend/parser/parse_agg.c index a25f8d5b98..80b951ec24 100644 --- a/src/backend/parser/parse_agg.c +++ b/src/backend/parser/parse_agg.c @@ -555,6 +555,13 @@ check_agglevels_and_constraints(ParseState *pstate, Node *expr) case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_UPDATE_PORTION: + if (isAgg) + err = _("aggregate functions are not allowed in FOR PORTION OF expressions"); + else + err = _("grouping operations are not allowed in FOR PORTION OF expressions"); + + break; /* * There is intentionally no default: case here, so that the @@ -950,6 +957,9 @@ transformWindowFuncCall(ParseState *pstate, WindowFunc *wfunc, case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_UPDATE_PORTION: + err = _("window functions are not allowed in FOR PORTION OF expressions"); + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/parser/parse_collate.c b/src/backend/parser/parse_collate.c index 4133526f04..9991da3b3e 100644 --- a/src/backend/parser/parse_collate.c +++ b/src/backend/parser/parse_collate.c @@ -484,6 +484,7 @@ assign_collations_walker(Node *node, assign_collations_context *context) case T_JoinExpr: case T_FromExpr: case T_OnConflictExpr: + case T_ForPortionOfExpr: case T_SortGroupClause: (void) expression_tree_walker(node, assign_collations_walker, diff --git a/src/backend/parser/parse_expr.c b/src/backend/parser/parse_expr.c index f928c32311..565e100521 100644 --- a/src/backend/parser/parse_expr.c +++ b/src/backend/parser/parse_expr.c @@ -518,6 +518,9 @@ transformColumnRef(ParseState *pstate, ColumnRef *cref) case EXPR_KIND_PARTITION_BOUND: err = _("cannot use column reference in partition bound expression"); break; + case EXPR_KIND_UPDATE_PORTION: + err = _("cannot use column reference in FOR PORTION OF expression"); + break; /* * There is intentionally no default: case here, so that the @@ -1769,6 +1772,9 @@ transformSubLink(ParseState *pstate, SubLink *sublink) case EXPR_KIND_GENERATED_COLUMN: err = _("cannot use subquery in column generation expression"); break; + case EXPR_KIND_UPDATE_PORTION: + err = _("cannot use subquery in FOR PORTION OF expression"); + break; /* * There is intentionally no default: case here, so that the @@ -3044,6 +3050,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_func.c b/src/backend/parser/parse_func.c index 3cec8de7da..9ce85c3bc3 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -2655,6 +2655,9 @@ check_srf_call_placement(ParseState *pstate, Node *last_srf, int location) case EXPR_KIND_CYCLE_MARK: errkind = true; break; + case EXPR_KIND_UPDATE_PORTION: + err = _("set-returning functions are not allowed in FOR PORTION OF expressions"); + break; /* * There is intentionally no default: case here, so that the diff --git a/src/backend/rewrite/rewriteHandler.c b/src/backend/rewrite/rewriteHandler.c index 88a9e95e33..204062671d 100644 --- a/src/backend/rewrite/rewriteHandler.c +++ b/src/backend/rewrite/rewriteHandler.c @@ -1568,6 +1568,7 @@ rewriteValuesRTE(Query *parsetree, RangeTblEntry *rte, int rti, /* * Record in target_rte->extraUpdatedCols the indexes of any generated columns * that depend on any columns mentioned in target_rte->updatedCols. + * If the update uses FOR PORTION OF, include the PK range. */ void fill_extraUpdatedCols(RangeTblEntry *target_rte, Relation target_relation) @@ -3099,6 +3100,19 @@ rewriteTargetView(Query *parsetree, Relation view) } } + // TODO: This doesn't look like it's getting used: + if (parsetree->forPortionOf) + { + foreach(lc, parsetree->forPortionOf->rangeSet) + { + TargetEntry *tle = (TargetEntry *) lfirst(lc); + + if (!tle->resjunk) + modified_cols = bms_add_member(modified_cols, + tle->resno - FirstLowInvalidHeapAttributeNumber); + } + } + auto_update_detail = view_cols_are_auto_updatable(viewquery, modified_cols, NULL, @@ -3727,6 +3741,16 @@ RewriteQuery(Query *parsetree, List *rewrite_events) } else if (event == CMD_UPDATE) { + /* Update FOR PORTION OF column(s) automatically */ + if (parsetree->forPortionOf) + { + ListCell *tl; + foreach(tl, parsetree->forPortionOf->rangeSet) + { + TargetEntry *tle = (TargetEntry *) lfirst(tl); + parsetree->targetList = lappend(parsetree->targetList, tle); + } + } parsetree->targetList = rewriteTargetListIU(parsetree->targetList, parsetree->commandType, diff --git a/src/backend/tcop/utility.c b/src/backend/tcop/utility.c index 7a2da9dab4..6a4ce381fc 100644 --- a/src/backend/tcop/utility.c +++ b/src/backend/tcop/utility.c @@ -1524,7 +1524,7 @@ ProcessUtilitySlow(ParseState *pstate, true, /* check_rights */ true, /* check_not_in_use */ false, /* skip_build */ - false); /* quiet */ + false); /* quiet */ /* * Add the CREATE INDEX node itself to stash right away; diff --git a/src/backend/utils/adt/Makefile b/src/backend/utils/adt/Makefile index 41b486bcef..dc741f2fc9 100644 --- a/src/backend/utils/adt/Makefile +++ b/src/backend/utils/adt/Makefile @@ -38,6 +38,7 @@ OBJS = \ float.o \ format_type.o \ formatting.o \ + fp_triggers.o \ genfile.o \ geo_ops.o \ geo_selfuncs.o \ diff --git a/src/backend/utils/adt/rangetypes.c b/src/backend/utils/adt/rangetypes.c index 815175a654..5a38178485 100644 --- a/src/backend/utils/adt/rangetypes.c +++ b/src/backend/utils/adt/rangetypes.c @@ -1195,6 +1195,48 @@ range_split_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeT return false; } +/* + * range_leftover_internal - Sets output1 and output2 to the remaining parts of r1 + * after subtracting r2, or if nothing is left then to the empty range. + * output1 will always be "before" r2 and output2 "after". + */ +void +range_leftover_internal(TypeCacheEntry *typcache, const RangeType *r1, + const RangeType *r2, RangeType **output1, RangeType **output2) +{ + RangeBound lower1, + lower2; + RangeBound upper1, + upper2; + bool empty1, + empty2; + + range_deserialize(typcache, r1, &lower1, &upper1, &empty1); + range_deserialize(typcache, r2, &lower2, &upper2, &empty2); + + if (range_cmp_bounds(typcache, &lower1, &lower2) < 0) + { + lower2.inclusive = !lower2.inclusive; + lower2.lower = false; + *output1 = make_range(typcache, &lower1, &lower2, false); + } + else + { + *output1 = make_empty_range(typcache); + } + + if (range_cmp_bounds(typcache, &upper1, &upper2) > 0) + { + upper2.inclusive = !upper2.inclusive; + upper2.lower = true; + *output2 = make_range(typcache, &upper2, &upper1, false); + } + else + { + *output2 = make_empty_range(typcache); + } +} + /* range -> range aggregate functions */ Datum diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index fde251fa4f..815f569641 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -3815,6 +3815,12 @@ prorettype => 'trigger', proargtypes => '', prosrc => 'RI_FKey_noaction_upd' }, +# Temporal leftovers triggers +{ oid => '8157', descr => 'temporal leftovers trigger function', + proname => 'FP_insert_leftovers', provolatile => 'v', + prorettype => 'trigger', proargtypes => '', + prosrc => 'FP_insert_leftovers' }, + { oid => '1666', proname => 'varbiteq', proleakproof => 't', prorettype => 'bool', proargtypes => 'varbit varbit', prosrc => 'biteq' }, diff --git a/src/include/commands/tablecmds.h b/src/include/commands/tablecmds.h index 6a51426056..d2f2f1061e 100644 --- a/src/include/commands/tablecmds.h +++ b/src/include/commands/tablecmds.h @@ -41,6 +41,8 @@ extern void ATExecChangeOwner(Oid relationOid, Oid newOwnerId, bool recursing, L extern void AlterTableInternal(Oid relid, List *cmds, bool recurse, struct AlterTableUtilityContext *context); +extern void CreateTemporalPrimaryKeyTriggers(Relation rel, Oid constraintOid, Oid indexOid); + extern Oid AlterTableMoveAll(AlterTableMoveAllStmt *stmt); extern ObjectAddress AlterTableNamespace(AlterObjectSchemaStmt *stmt, diff --git a/src/include/commands/trigger.h b/src/include/commands/trigger.h index 9e557cfbce..524f94eb2a 100644 --- a/src/include/commands/trigger.h +++ b/src/include/commands/trigger.h @@ -40,6 +40,7 @@ typedef struct TriggerData Tuplestorestate *tg_oldtable; Tuplestorestate *tg_newtable; const Bitmapset *tg_updatedcols; + ForPortionOfState *tg_temporal; } TriggerData; /* diff --git a/src/include/nodes/execnodes.h b/src/include/nodes/execnodes.h index 4919e5ce25..a21b5c9f41 100644 --- a/src/include/nodes/execnodes.h +++ b/src/include/nodes/execnodes.h @@ -26,12 +26,14 @@ #include "storage/condition_variable.h" #include "utils/hsearch.h" #include "utils/queryenvironment.h" +#include "utils/rangetypes.h" #include "utils/reltrigger.h" #include "utils/sharedtuplestore.h" #include "utils/snapshot.h" #include "utils/sortsupport.h" #include "utils/tuplesort.h" #include "utils/tuplestore.h" +#include "utils/typcache.h" struct PlanState; /* forward references in this file */ struct ParallelHashJoinState; @@ -387,6 +389,23 @@ typedef struct OnConflictSetState ExprState *oc_WhereClause; /* state for the WHERE clause */ } OnConflictSetState; +/* + * ForPortionOfState + * + * Executor state of a FOR PORTION OF operation. + */ +typedef struct ForPortionOfState +{ + NodeTag type; + + char *fp_rangeName; /* the column/PERIOD named in FOR PORTION OF */ + Oid fp_rangeType; /* the type of the FOR PORTION OF expression */ + bool fp_hasPeriod; /* true iff this is a PERIOD not a range */ + char *fp_periodStartName; /* the PERIOD's start column */ + char *fp_periodEndName; /* the PERIOD's end column */ + Datum fp_targetRange; /* the range from FOR PORTION OF */ +} ForPortionOfState; + /* * ResultRelInfo * @@ -498,6 +517,9 @@ typedef struct ResultRelInfo /* ON CONFLICT evaluation state */ OnConflictSetState *ri_onConflict; + /* FOR PORTION OF evaluation state */ + ForPortionOfState *ri_forPortionOf; + /* partition check expression state (NULL if not set up yet) */ ExprState *ri_PartitionCheckExpr; diff --git a/src/include/nodes/nodes.h b/src/include/nodes/nodes.h index 4b12a8d365..1d340b5323 100644 --- a/src/include/nodes/nodes.h +++ b/src/include/nodes/nodes.h @@ -35,6 +35,7 @@ typedef enum NodeTag T_ProjectionInfo, T_JunkFilter, T_OnConflictSetState, + T_ForPortionOfState, T_ResultRelInfo, T_EState, T_TupleTableSlot, @@ -201,6 +202,7 @@ typedef enum NodeTag T_JoinExpr, T_FromExpr, T_OnConflictExpr, + T_ForPortionOfExpr, T_IntoClause, /* @@ -483,6 +485,7 @@ typedef enum NodeTag T_OnConflictClause, T_CTESearchClause, T_CTECycleClause, + T_ForPortionOfClause, T_CommonTableExpr, T_RoleSpec, T_TriggerTransition, diff --git a/src/include/nodes/parsenodes.h b/src/include/nodes/parsenodes.h index 2f91ffd269..a874d969ad 100644 --- a/src/include/nodes/parsenodes.h +++ b/src/include/nodes/parsenodes.h @@ -130,6 +130,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 */ @@ -1411,6 +1413,19 @@ typedef struct RowMarkClause bool pushedDown; /* pushed down from higher query level? */ } 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 @@ -1592,12 +1607,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; /* ---------------------- @@ -1606,13 +1622,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; /* ---------------------- diff --git a/src/include/nodes/pathnodes.h b/src/include/nodes/pathnodes.h index b7b2817a5d..42ee658dc3 100644 --- a/src/include/nodes/pathnodes.h +++ b/src/include/nodes/pathnodes.h @@ -1882,6 +1882,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 aaa3b65d04..7d603295cd 100644 --- a/src/include/nodes/plannodes.h +++ b/src/include/nodes/plannodes.h @@ -231,6 +231,8 @@ typedef struct ModifyTable List *rowMarks; /* PlanRowMarks (non-locking only) */ int epqParam; /* ID of Param for EvalPlanQual re-eval */ OnConflictAction onConflictAction; /* ON CONFLICT action */ + // TODO: Instead of re-using Expr here, break it into pieces like onConflict{Action,Set,Where}? + Node *forPortionOf; /* FOR PORTION OF clause for UPDATE/DELETE */ List *arbiterIndexes; /* List of ON CONFLICT arbiter index OIDs */ List *onConflictSet; /* INSERT ON CONFLICT DO UPDATE targetlist */ List *onConflictCols; /* target column numbers for onConflictSet */ diff --git a/src/include/nodes/primnodes.h b/src/include/nodes/primnodes.h index 9ae851d847..5ef93845dd 100644 --- a/src/include/nodes/primnodes.h +++ b/src/include/nodes/primnodes.h @@ -1581,4 +1581,31 @@ 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; + int range_attno; /* Range column number */ + int start_attno; /* PERIOD start column number */ + int end_attno; /* PERIOD end column number */ + char *range_name; /* Range name */ + char *period_start_name; /* PERIOD start column name */ + char *period_end_name; /* PERIOD end column name */ + Expr *range; /* Range column or expression */ + // TODO: I do really use all these fields?: + 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 */ + Oid rangeType; /* type of targetRange */ + Node *overlapsExpr; /* range && targetRange */ + List *rangeSet; /* List of TargetEntrys to set the time column(s) */ +} ForPortionOfExpr; + #endif /* PRIMNODES_H */ diff --git a/src/include/optimizer/pathnode.h b/src/include/optimizer/pathnode.h index 53261ee91f..88a30c6d07 100644 --- a/src/include/optimizer/pathnode.h +++ b/src/include/optimizer/pathnode.h @@ -275,7 +275,7 @@ extern ModifyTablePath *create_modifytable_path(PlannerInfo *root, List *updateColnosLists, 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 43fc561075..7545038a9a 100644 --- a/src/include/parser/kwlist.h +++ b/src/include/parser/kwlist.h @@ -316,6 +316,7 @@ PG_KEYWORD("period", PERIOD, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("placing", PLACING, RESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("plans", PLANS, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("policy", POLICY, UNRESERVED_KEYWORD, BARE_LABEL) +PG_KEYWORD("portion", PORTION, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("position", POSITION, COL_NAME_KEYWORD, BARE_LABEL) PG_KEYWORD("preceding", PRECEDING, UNRESERVED_KEYWORD, BARE_LABEL) PG_KEYWORD("precision", PRECISION, COL_NAME_KEYWORD, AS_LABEL) diff --git a/src/include/parser/parse_node.h b/src/include/parser/parse_node.h index 1500de2dd0..9f858a24ab 100644 --- a/src/include/parser/parse_node.h +++ b/src/include/parser/parse_node.h @@ -55,6 +55,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/rangetypes.h b/src/include/utils/rangetypes.h index 04c302c619..d8c1f1656a 100644 --- a/src/include/utils/rangetypes.h +++ b/src/include/utils/rangetypes.h @@ -147,5 +147,8 @@ extern RangeType *make_empty_range(TypeCacheEntry *typcache); extern bool range_split_internal(TypeCacheEntry *typcache, const RangeType *r1, const RangeType *r2, RangeType **output1, RangeType **output2); +extern void range_leftover_internal(TypeCacheEntry *typcache, const RangeType *r1, + const RangeType *r2, RangeType **output1, + RangeType **output2); #endif /* RANGETYPES_H */ 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..8dfe031e99 --- /dev/null +++ b/src/test/regress/expected/for_portion_of.out @@ -0,0 +1,662 @@ +-- Tests for UPDATE/DELETE FOR PORTION OF +-- Fails on tables without a temporal PK: +CREATE TABLE for_portion_of_test ( + id int4range PRIMARY KEY, + valid_at tsrange NOT NULL, + name text NOT NULL +); +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE +SET name = 'foo'; +ERROR: relation "for_portion_of_test" does not have a temporal primary key +LINE 2: FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE + ^ +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE; +ERROR: relation "for_portion_of_test" does not have a temporal primary key +LINE 2: FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE; + ^ +DROP TABLE for_portion_of_test; +CREATE TABLE for_portion_of_test ( + id int4range NOT NULL, + valid_at tsrange NOT NULL, + name text NOT NULL, + CONSTRAINT for_portion_of_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +INSERT INTO for_portion_of_test +VALUES +('[1,2)', '[2018-01-02,2018-02-03)', 'one'), +('[1,2)', '[2018-02-03,2018-03-03)', 'one'), +('[1,2)', '[2018-03-03,2018-04-04)', 'one'), +('[2,3)', '[2018-01-01,2018-01-05)', 'two'), +('[3,4)', '[2018-01-01,)', 'three'), +('[4,5)', '(,2018-04-01)', 'four'), +('[5,6)', '(,)', 'five') +; +-- +-- UPDATE tests +-- +-- Setting with a missing column fails +UPDATE for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'foo' +WHERE id = '[5,6)'; +ERROR: column or period "invalid_at" of relation "for_portion_of_test" does not exist +LINE 2: FOR PORTION OF invalid_at FROM '2018-06-01' TO MAXVALUE + ^ +-- Setting the range fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET valid_at = '[1990-01-01,1999-01-01)' +WHERE id = '[5,6)'; +ERROR: multiple assignments to same column "valid_at" +-- Setting with timestamps reversed fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +SET name = 'three^1' +WHERE id = '[3,4)'; +ERROR: range lower bound must be less than or equal to range upper bound +-- Setting with a subquery fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM (SELECT '2018-01-01') TO '2018-06-01' +SET name = 'nope' +WHERE id = '[3,4)'; +ERROR: cannot use subquery in FOR PORTION OF expression +LINE 2: FOR PORTION OF valid_at FROM (SELECT '2018-01-01') TO '2018-... + ^ +-- Setting with a column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM lower(valid_at) TO MAXVALUE +SET name = 'nope' +WHERE id = '[3,4)'; +ERROR: cannot use column reference in FOR PORTION OF expression +LINE 2: FOR PORTION OF valid_at FROM lower(valid_at) TO MAXVALUE + ^ +-- Setting with timestamps equal does nothing +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +SET name = 'three^0' +WHERE id = '[3,4)'; +-- Updating a finite/open portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'three^1' +WHERE id = '[3,4)'; +-- Updating a finite/open portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-03-01' +SET name = 'three^2' +WHERE id = '[3,4)'; +-- Updating an open/finite portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-02-01' +SET name = 'four^1' +WHERE id = '[4,5)'; +-- Updating an open/finite portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO MAXVALUE +SET name = 'four^2' +WHERE id = '[4,5)'; +-- Updating a finite/finite portion with an exact fit +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2018-02-01' +SET name = 'four^3' +WHERE id = '[4,5)'; +-- Updating an enclosed span +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO MAXVALUE +SET name = 'two^2' +WHERE id = '[2,3)'; +-- Updating an open/open portion with a finite/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-01' TO '2019-01-01' +SET name = 'five^2' +WHERE id = '[5,6)'; +-- Updating an enclosed span with separate protruding spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2020-01-01' +SET name = 'five^3' +WHERE id = '[5,6)'; +-- Updating multiple enclosed spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +SET name = 'one^2' +WHERE id = '[1,2)'; +-- Updating with a shift/reduce conflict +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '1' HOUR TO MINUTE + TO '2019-01-01' +SET name = 'one^3' +WHERE id = '[1,2)'; +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '2' HOUR + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; +ERROR: syntax error at or near "'2019-01-01'" +LINE 4: TO '2019-01-01' + ^ +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM ('2018-03-01' AT TIME ZONE INTERVAL '2' HOUR) + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; +-- Updating the non-range part of the PK: +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-15' TO NULL +SET id = '[6,7)' +WHERE id = '[1,2)'; +-- UPDATE with no WHERE clause +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2030-01-01' TO NULL +SET name = name || '*'; +SELECT * FROM for_portion_of_test ORDER BY id, valid_at; + id | valid_at | name +-------+---------------------------------------------------------+---------- + [1,2) | ["Tue Jan 02 00:00:00 2018","Sat Feb 03 00:00:00 2018") | one^2 + [1,2) | ["Sat Feb 03 00:00:00 2018","Thu Feb 15 00:00:00 2018") | one^2 + [2,3) | ["Mon Jan 01 00:00:00 2018","Fri Jan 05 00:00:00 2018") | two^2 + [3,4) | ["Mon Jan 01 00:00:00 2018","Thu Mar 01 00:00:00 2018") | three^2 + [3,4) | ["Thu Mar 01 00:00:00 2018","Fri Jun 01 00:00:00 2018") | three + [3,4) | ["Fri Jun 01 00:00:00 2018","Tue Jan 01 00:00:00 2030") | three^1 + [3,4) | ["Tue Jan 01 00:00:00 2030",) | three^1* + [4,5) | (,"Sun Jan 01 00:00:00 2017") | four^1 + [4,5) | ["Sun Jan 01 00:00:00 2017","Thu Feb 01 00:00:00 2018") | four^3 + [4,5) | ["Thu Feb 01 00:00:00 2018","Sun Apr 01 00:00:00 2018") | four^2 + [5,6) | (,"Sun Jan 01 00:00:00 2017") | five + [5,6) | ["Sun Jan 01 00:00:00 2017","Mon Jan 01 00:00:00 2018") | five^3 + [5,6) | ["Mon Jan 01 00:00:00 2018","Tue Jan 01 00:00:00 2019") | five^3 + [5,6) | ["Tue Jan 01 00:00:00 2019","Wed Jan 01 00:00:00 2020") | five^3 + [5,6) | ["Wed Jan 01 00:00:00 2020","Tue Jan 01 00:00:00 2030") | five + [5,6) | ["Tue Jan 01 00:00:00 2030",) | five* + [6,7) | ["Thu Feb 15 00:00:00 2018","Thu Mar 01 08:01:00 2018") | one^2 + [6,7) | ["Thu Mar 01 08:01:00 2018","Thu Mar 01 10:00:00 2018") | one^3 + [6,7) | ["Thu Mar 01 10:00:00 2018","Sat Mar 03 00:00:00 2018") | one^4 + [6,7) | ["Sat Mar 03 00:00:00 2018","Wed Apr 04 00:00:00 2018") | one^4 +(20 rows) + +-- +-- DELETE tests +-- +-- Deleting with a missing column fails +DELETE FROM for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO NULL +WHERE id = '[5,6)'; +ERROR: column or period "invalid_at" of relation "for_portion_of_test" does not exist +LINE 2: FOR PORTION OF invalid_at FROM '2018-06-01' TO NULL + ^ +-- Deleting with timestamps reversed fails +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +WHERE id = '[3,4)'; +ERROR: range lower bound must be less than or equal to range upper bound +-- Deleting with timestamps equal does nothing +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +WHERE id = '[3,4)'; +-- Deleting with a closed/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2020-06-01' +WHERE id = '[5,6)'; +-- Deleting with a closed/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO NULL +WHERE id = '[3,4)'; +-- Deleting with an open/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO '2018-02-08' +WHERE id = '[1,2)'; +-- Deleting with an open/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +WHERE id = '[6,7)'; +-- DELETE with no WHERE clause +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2025-01-01' TO NULL; +SELECT * FROM for_portion_of_test ORDER BY id, valid_at; + id | valid_at | name +-------+---------------------------------------------------------+--------- + [1,2) | ["Thu Feb 08 00:00:00 2018","Thu Feb 15 00:00:00 2018") | one^2 + [2,3) | ["Mon Jan 01 00:00:00 2018","Fri Jan 05 00:00:00 2018") | two^2 + [3,4) | ["Mon Jan 01 00:00:00 2018","Thu Mar 01 00:00:00 2018") | three^2 + [3,4) | ["Thu Mar 01 00:00:00 2018","Sun Apr 01 00:00:00 2018") | three + [4,5) | (,"Sun Jan 01 00:00:00 2017") | four^1 + [4,5) | ["Sun Jan 01 00:00:00 2017","Thu Feb 01 00:00:00 2018") | four^3 + [4,5) | ["Thu Feb 01 00:00:00 2018","Sun Apr 01 00:00:00 2018") | four^2 + [5,6) | (,"Sun Jan 01 00:00:00 2017") | five + [5,6) | ["Sun Jan 01 00:00:00 2017","Mon Jan 01 00:00:00 2018") | five^3 + [5,6) | ["Mon Jan 01 00:00:00 2018","Fri Jun 01 00:00:00 2018") | five^3 + [5,6) | ["Mon Jun 01 00:00:00 2020","Wed Jan 01 00:00:00 2025") | five +(11 rows) + +-- UPDATE ... RETURNING returns only the updated values (not the inserted side values) +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-01' TO '2018-02-15' +SET name = 'three^3' +WHERE id = '[3,4)' +RETURNING *; + id | valid_at | name +-------+---------------------------------------------------------+--------- + [3,4) | ["Thu Feb 01 00:00:00 2018","Thu Feb 15 00:00:00 2018") | three^3 +(1 row) + +-- test that we run triggers on the UPDATE/DELETEd row and the INSERTed rows +CREATE FUNCTION for_portion_of_trigger() +RETURNS trigger +AS +$$ +BEGIN + RAISE NOTICE '% % % of %', TG_WHEN, TG_OP, NEW.valid_at, OLD.valid_at; + IF TG_OP = 'DELETE' THEN + RETURN OLD; + ELSE + RETURN NEW; + END IF; +END; +$$ +LANGUAGE plpgsql; +CREATE TRIGGER trg_for_portion_of_before_insert + BEFORE INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_after_insert + AFTER INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_before_update + BEFORE UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_after_update + AFTER UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_before_delete + BEFORE DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_after_delete + AFTER DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2021-01-01' TO '2022-01-01' +SET name = 'five^4' +WHERE id = '[5,6)'; +NOTICE: BEFORE UPDATE ["Fri Jan 01 00:00:00 2021","Sat Jan 01 00:00:00 2022") of ["Mon Jun 01 00:00:00 2020","Wed Jan 01 00:00:00 2025") +NOTICE: BEFORE INSERT ["Mon Jun 01 00:00:00 2020","Fri Jan 01 00:00:00 2021") of +NOTICE: BEFORE INSERT ["Sat Jan 01 00:00:00 2022","Wed Jan 01 00:00:00 2025") of +NOTICE: AFTER INSERT ["Mon Jun 01 00:00:00 2020","Fri Jan 01 00:00:00 2021") of +NOTICE: AFTER INSERT ["Sat Jan 01 00:00:00 2022","Wed Jan 01 00:00:00 2025") of +NOTICE: AFTER UPDATE ["Fri Jan 01 00:00:00 2021","Sat Jan 01 00:00:00 2022") of ["Mon Jun 01 00:00:00 2020","Wed Jan 01 00:00:00 2025") +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2023-01-01' TO '2024-01-01' +WHERE id = '[5,6)'; +NOTICE: BEFORE DELETE of ["Sat Jan 01 00:00:00 2022","Wed Jan 01 00:00:00 2025") +NOTICE: BEFORE INSERT ["Sat Jan 01 00:00:00 2022","Sun Jan 01 00:00:00 2023") of +NOTICE: BEFORE INSERT ["Mon Jan 01 00:00:00 2024","Wed Jan 01 00:00:00 2025") of +NOTICE: AFTER INSERT ["Sat Jan 01 00:00:00 2022","Sun Jan 01 00:00:00 2023") of +NOTICE: AFTER INSERT ["Mon Jan 01 00:00:00 2024","Wed Jan 01 00:00:00 2025") of +NOTICE: AFTER DELETE of ["Sat Jan 01 00:00:00 2022","Wed Jan 01 00:00:00 2025") +SELECT * FROM for_portion_of_test ORDER BY id, valid_at; + id | valid_at | name +-------+---------------------------------------------------------+--------- + [1,2) | ["Thu Feb 08 00:00:00 2018","Thu Feb 15 00:00:00 2018") | one^2 + [2,3) | ["Mon Jan 01 00:00:00 2018","Fri Jan 05 00:00:00 2018") | two^2 + [3,4) | ["Mon Jan 01 00:00:00 2018","Thu Feb 01 00:00:00 2018") | three^2 + [3,4) | ["Thu Feb 01 00:00:00 2018","Thu Feb 15 00:00:00 2018") | three^3 + [3,4) | ["Thu Feb 15 00:00:00 2018","Thu Mar 01 00:00:00 2018") | three^2 + [3,4) | ["Thu Mar 01 00:00:00 2018","Sun Apr 01 00:00:00 2018") | three + [4,5) | (,"Sun Jan 01 00:00:00 2017") | four^1 + [4,5) | ["Sun Jan 01 00:00:00 2017","Thu Feb 01 00:00:00 2018") | four^3 + [4,5) | ["Thu Feb 01 00:00:00 2018","Sun Apr 01 00:00:00 2018") | four^2 + [5,6) | (,"Sun Jan 01 00:00:00 2017") | five + [5,6) | ["Sun Jan 01 00:00:00 2017","Mon Jan 01 00:00:00 2018") | five^3 + [5,6) | ["Mon Jan 01 00:00:00 2018","Fri Jun 01 00:00:00 2018") | five^3 + [5,6) | ["Mon Jun 01 00:00:00 2020","Fri Jan 01 00:00:00 2021") | five + [5,6) | ["Fri Jan 01 00:00:00 2021","Sat Jan 01 00:00:00 2022") | five^4 + [5,6) | ["Sat Jan 01 00:00:00 2022","Sun Jan 01 00:00:00 2023") | five + [5,6) | ["Mon Jan 01 00:00:00 2024","Wed Jan 01 00:00:00 2025") | five +(16 rows) + +-- +-- Now re-run the same tests but with a PERIOD instead of a range: +-- +DROP TABLE for_portion_of_test; +-- Fails on tables without a temporal PK: +CREATE TABLE for_portion_of_test ( + id int4range PRIMARY KEY, + valid_from timestamp, + valid_til timestamp, + PERIOD FOR valid_at (valid_from, valid_til), + name text NOT NULL +); +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE +SET name = 'foo'; +ERROR: relation "for_portion_of_test" does not have a temporal primary key +LINE 2: FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE + ^ +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE; +ERROR: relation "for_portion_of_test" does not have a temporal primary key +LINE 2: FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE; + ^ +DROP TABLE for_portion_of_test; +CREATE TABLE for_portion_of_test ( + id int4range NOT NULL, + valid_from timestamp, + valid_til timestamp, + PERIOD FOR valid_at (valid_from, valid_til), + name text NOT NULL, + CONSTRAINT for_portion_of_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +INSERT INTO for_portion_of_test +VALUES +('[1,2)', '2018-01-02', '2018-02-03', 'one'), +('[1,2)', '2018-02-03', '2018-03-03', 'one'), +('[1,2)', '2018-03-03', '2018-04-04', 'one'), +('[2,3)', '2018-01-01', '2018-01-05', 'two'), +('[3,4)', '2018-01-01', null, 'three'), +('[4,5)', null, '2018-04-01', 'four'), +('[5,6)', null, null, 'five') +; +-- +-- UPDATE tests +-- +-- Setting with a missing column fails +UPDATE for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'foo' +WHERE id = '[5,6)'; +ERROR: column or period "invalid_at" of relation "for_portion_of_test" does not exist +LINE 2: FOR PORTION OF invalid_at FROM '2018-06-01' TO MAXVALUE + ^ +-- Setting the start column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET valid_from = '1990-01-01' +WHERE id = '[5,6)'; +ERROR: multiple assignments to same column "valid_from" +-- Setting the end column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET valid_til = '1999-01-01' +WHERE id = '[5,6)'; +ERROR: multiple assignments to same column "valid_til" +-- Setting with timestamps reversed fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +SET name = 'three^1' +WHERE id = '[3,4)'; +ERROR: range lower bound must be less than or equal to range upper bound +-- Setting with a subquery fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM (SELECT '2018-01-01') TO '2018-06-01' +SET name = 'nope' +WHERE id = '[3,4)'; +ERROR: cannot use subquery in FOR PORTION OF expression +LINE 2: FOR PORTION OF valid_at FROM (SELECT '2018-01-01') TO '2018-... + ^ +-- Setting with a column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM valid_from TO MAXVALUE +SET name = 'nope' +WHERE id = '[3,4)'; +ERROR: cannot use column reference in FOR PORTION OF expression +LINE 2: FOR PORTION OF valid_at FROM valid_from TO MAXVALUE + ^ +-- Setting with timestamps equal does nothing +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +SET name = 'three^0' +WHERE id = '[3,4)'; +-- Updating a finite/open portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'three^1' +WHERE id = '[3,4)'; +-- Updating a finite/open portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-03-01' +SET name = 'three^2' +WHERE id = '[3,4)'; +-- Updating an open/finite portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-02-01' +SET name = 'four^1' +WHERE id = '[4,5)'; +-- Updating an open/finite portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO MAXVALUE +SET name = 'four^2' +WHERE id = '[4,5)'; +-- Updating a finite/finite portion with an exact fit +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2018-02-01' +SET name = 'four^3' +WHERE id = '[4,5)'; +-- Updating an enclosed span +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO MAXVALUE +SET name = 'two^2' +WHERE id = '[2,3)'; +-- Updating an open/open portion with a finite/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-01' TO '2019-01-01' +SET name = 'five^2' +WHERE id = '[5,6)'; +-- Updating an enclosed span with separate protruding spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2020-01-01' +SET name = 'five^3' +WHERE id = '[5,6)'; +-- Updating multiple enclosed spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +SET name = 'one^2' +WHERE id = '[1,2)'; +-- Updating with a shift/reduce conflict +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '1' HOUR TO MINUTE + TO '2019-01-01' +SET name = 'one^3' +WHERE id = '[1,2)'; +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '2' HOUR + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; +ERROR: syntax error at or near "'2019-01-01'" +LINE 4: TO '2019-01-01' + ^ +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM ('2018-03-01' AT TIME ZONE INTERVAL '2' HOUR) + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; +-- Updating the non-range part of the PK: +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-15' TO NULL +SET id = '[6,7)' +WHERE id = '[1,2)'; +-- UPDATE with no WHERE clause +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2030-01-01' TO NULL +SET name = name || '*'; +-- TODO: UPDATE with generated columns too +SELECT * FROM for_portion_of_test ORDER BY id, valid_from NULLS FIRST, valid_til; + id | valid_from | valid_til | name +-------+--------------------------+--------------------------+---------- + [1,2) | Tue Jan 02 00:00:00 2018 | Sat Feb 03 00:00:00 2018 | one^2 + [1,2) | Sat Feb 03 00:00:00 2018 | Thu Feb 15 00:00:00 2018 | one^2 + [2,3) | Mon Jan 01 00:00:00 2018 | Fri Jan 05 00:00:00 2018 | two^2 + [3,4) | Mon Jan 01 00:00:00 2018 | Thu Mar 01 00:00:00 2018 | three^2 + [3,4) | Thu Mar 01 00:00:00 2018 | Fri Jun 01 00:00:00 2018 | three + [3,4) | Fri Jun 01 00:00:00 2018 | Tue Jan 01 00:00:00 2030 | three^1 + [3,4) | Tue Jan 01 00:00:00 2030 | | three^1* + [4,5) | | Sun Jan 01 00:00:00 2017 | four^1 + [4,5) | Sun Jan 01 00:00:00 2017 | Thu Feb 01 00:00:00 2018 | four^3 + [4,5) | Thu Feb 01 00:00:00 2018 | Sun Apr 01 00:00:00 2018 | four^2 + [5,6) | | Sun Jan 01 00:00:00 2017 | five + [5,6) | Sun Jan 01 00:00:00 2017 | Mon Jan 01 00:00:00 2018 | five^3 + [5,6) | Mon Jan 01 00:00:00 2018 | Tue Jan 01 00:00:00 2019 | five^3 + [5,6) | Tue Jan 01 00:00:00 2019 | Wed Jan 01 00:00:00 2020 | five^3 + [5,6) | Wed Jan 01 00:00:00 2020 | Tue Jan 01 00:00:00 2030 | five + [5,6) | Tue Jan 01 00:00:00 2030 | | five* + [6,7) | Thu Feb 15 00:00:00 2018 | Thu Mar 01 08:01:00 2018 | one^2 + [6,7) | Thu Mar 01 08:01:00 2018 | Thu Mar 01 10:00:00 2018 | one^3 + [6,7) | Thu Mar 01 10:00:00 2018 | Sat Mar 03 00:00:00 2018 | one^4 + [6,7) | Sat Mar 03 00:00:00 2018 | Wed Apr 04 00:00:00 2018 | one^4 +(20 rows) + +-- +-- DELETE tests +-- +-- Deleting with a missing column fails +DELETE FROM for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO NULL +WHERE id = '[5,6)'; +ERROR: column or period "invalid_at" of relation "for_portion_of_test" does not exist +LINE 2: FOR PORTION OF invalid_at FROM '2018-06-01' TO NULL + ^ +-- Deleting with timestamps reversed fails +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +WHERE id = '[3,4)'; +ERROR: range lower bound must be less than or equal to range upper bound +-- Deleting with timestamps equal does nothing +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +WHERE id = '[3,4)'; +-- Deleting with a closed/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2020-06-01' +WHERE id = '[5,6)'; +-- Deleting with a closed/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO NULL +WHERE id = '[3,4)'; +-- Deleting with an open/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO '2018-02-08' +WHERE id = '[1,2)'; +-- Deleting with an open/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +WHERE id = '[6,7)'; +-- DELETE with no WHERE clause +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2025-01-01' TO NULL; +SELECT * FROM for_portion_of_test ORDER BY id, valid_from NULLS FIRST, valid_til; + id | valid_from | valid_til | name +-------+--------------------------+--------------------------+--------- + [1,2) | Thu Feb 08 00:00:00 2018 | Thu Feb 15 00:00:00 2018 | one^2 + [2,3) | Mon Jan 01 00:00:00 2018 | Fri Jan 05 00:00:00 2018 | two^2 + [3,4) | Mon Jan 01 00:00:00 2018 | Thu Mar 01 00:00:00 2018 | three^2 + [3,4) | Thu Mar 01 00:00:00 2018 | Sun Apr 01 00:00:00 2018 | three + [4,5) | | Sun Jan 01 00:00:00 2017 | four^1 + [4,5) | Sun Jan 01 00:00:00 2017 | Thu Feb 01 00:00:00 2018 | four^3 + [4,5) | Thu Feb 01 00:00:00 2018 | Sun Apr 01 00:00:00 2018 | four^2 + [5,6) | | Sun Jan 01 00:00:00 2017 | five + [5,6) | Sun Jan 01 00:00:00 2017 | Mon Jan 01 00:00:00 2018 | five^3 + [5,6) | Mon Jan 01 00:00:00 2018 | Fri Jun 01 00:00:00 2018 | five^3 + [5,6) | Mon Jun 01 00:00:00 2020 | Wed Jan 01 00:00:00 2025 | five +(11 rows) + +-- UPDATE ... RETURNING returns only the updated values (not the inserted side values) +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-01' TO '2018-02-15' +SET name = 'three^3' +WHERE id = '[3,4)' +RETURNING *; + id | valid_from | valid_til | name +-------+--------------------------+--------------------------+--------- + [3,4) | Thu Feb 01 00:00:00 2018 | Thu Feb 15 00:00:00 2018 | three^3 +(1 row) + +-- test that we run triggers on the UPDATE/DELETEd row and the INSERTed rows +CREATE FUNCTION for_portion_of_trigger2() +RETURNS trigger +AS +$$ +BEGIN + RAISE NOTICE '% % [%,%) of [%,%)', TG_WHEN, TG_OP, NEW.valid_from, NEW.valid_til, OLD.valid_from, OLD.valid_til; + IF TG_OP = 'DELETE' THEN + RETURN OLD; + ELSE + RETURN NEW; + END IF; +END; +$$ +LANGUAGE plpgsql; +CREATE TRIGGER trg_for_portion_of_before_insert + BEFORE INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_after_insert + AFTER INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_before_update + BEFORE UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_after_update + AFTER UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_before_delete + BEFORE DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_after_delete + AFTER DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2021-01-01' TO '2022-01-01' +SET name = 'five^4' +WHERE id = '[5,6)'; +NOTICE: BEFORE UPDATE [Fri Jan 01 00:00:00 2021,Sat Jan 01 00:00:00 2022) of [Mon Jun 01 00:00:00 2020,Wed Jan 01 00:00:00 2025) +NOTICE: BEFORE INSERT [Mon Jun 01 00:00:00 2020,Fri Jan 01 00:00:00 2021) of [,) +NOTICE: BEFORE INSERT [Sat Jan 01 00:00:00 2022,Wed Jan 01 00:00:00 2025) of [,) +NOTICE: AFTER INSERT [Mon Jun 01 00:00:00 2020,Fri Jan 01 00:00:00 2021) of [,) +NOTICE: AFTER INSERT [Sat Jan 01 00:00:00 2022,Wed Jan 01 00:00:00 2025) of [,) +NOTICE: AFTER UPDATE [Fri Jan 01 00:00:00 2021,Sat Jan 01 00:00:00 2022) of [Mon Jun 01 00:00:00 2020,Wed Jan 01 00:00:00 2025) +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2023-01-01' TO '2024-01-01' +WHERE id = '[5,6)'; +NOTICE: BEFORE DELETE [,) of [Sat Jan 01 00:00:00 2022,Wed Jan 01 00:00:00 2025) +NOTICE: BEFORE INSERT [Sat Jan 01 00:00:00 2022,Sun Jan 01 00:00:00 2023) of [,) +NOTICE: BEFORE INSERT [Mon Jan 01 00:00:00 2024,Wed Jan 01 00:00:00 2025) of [,) +NOTICE: AFTER INSERT [Sat Jan 01 00:00:00 2022,Sun Jan 01 00:00:00 2023) of [,) +NOTICE: AFTER INSERT [Mon Jan 01 00:00:00 2024,Wed Jan 01 00:00:00 2025) of [,) +NOTICE: AFTER DELETE [,) of [Sat Jan 01 00:00:00 2022,Wed Jan 01 00:00:00 2025) +SELECT * FROM for_portion_of_test ORDER BY id, valid_from NULLS FIRST, valid_til; + id | valid_from | valid_til | name +-------+--------------------------+--------------------------+--------- + [1,2) | Thu Feb 08 00:00:00 2018 | Thu Feb 15 00:00:00 2018 | one^2 + [2,3) | Mon Jan 01 00:00:00 2018 | Fri Jan 05 00:00:00 2018 | two^2 + [3,4) | Mon Jan 01 00:00:00 2018 | Thu Feb 01 00:00:00 2018 | three^2 + [3,4) | Thu Feb 01 00:00:00 2018 | Thu Feb 15 00:00:00 2018 | three^3 + [3,4) | Thu Feb 15 00:00:00 2018 | Thu Mar 01 00:00:00 2018 | three^2 + [3,4) | Thu Mar 01 00:00:00 2018 | Sun Apr 01 00:00:00 2018 | three + [4,5) | | Sun Jan 01 00:00:00 2017 | four^1 + [4,5) | Sun Jan 01 00:00:00 2017 | Thu Feb 01 00:00:00 2018 | four^3 + [4,5) | Thu Feb 01 00:00:00 2018 | Sun Apr 01 00:00:00 2018 | four^2 + [5,6) | | Sun Jan 01 00:00:00 2017 | five + [5,6) | Sun Jan 01 00:00:00 2017 | Mon Jan 01 00:00:00 2018 | five^3 + [5,6) | Mon Jan 01 00:00:00 2018 | Fri Jun 01 00:00:00 2018 | five^3 + [5,6) | Mon Jun 01 00:00:00 2020 | Fri Jan 01 00:00:00 2021 | five + [5,6) | Fri Jan 01 00:00:00 2021 | Sat Jan 01 00:00:00 2022 | five^4 + [5,6) | Sat Jan 01 00:00:00 2022 | Sun Jan 01 00:00:00 2023 | five + [5,6) | Mon Jan 01 00:00:00 2024 | Wed Jan 01 00:00:00 2025 | five +(16 rows) + diff --git a/src/test/regress/expected/sanity_check.out b/src/test/regress/expected/sanity_check.out index 1e6b282212..2fd708e0f8 100644 --- a/src/test/regress/expected/sanity_check.out +++ b/src/test/regress/expected/sanity_check.out @@ -48,6 +48,7 @@ f_star|f fast_emp4000|t float4_tbl|f float8_tbl|f +for_portion_of_test|t func_index_heap|t hash_f8_heap|t hash_i4_heap|t diff --git a/src/test/regress/parallel_schedule b/src/test/regress/parallel_schedule index bc4aa1d605..86cc135bae 100644 --- a/src/test/regress/parallel_schedule +++ b/src/test/regress/parallel_schedule @@ -58,7 +58,7 @@ test: create_index create_index_spgist create_view index_including index_includi # ---------- # Another group of parallel tests # ---------- -test: create_aggregate create_function_3 create_cast constraints triggers select inherit typed_table periods vacuum drop_if_exists updatable_views roleattributes create_am hash_func errors infinite_recurse +test: create_aggregate create_function_3 create_cast constraints triggers select inherit typed_table periods vacuum drop_if_exists updatable_views roleattributes create_am hash_func errors infinite_recurse for_portion_of # ---------- # sanity_check does a vacuum, affecting the sort order of SELECT * 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..1d77c5d979 --- /dev/null +++ b/src/test/regress/sql/for_portion_of.sql @@ -0,0 +1,550 @@ +-- Tests for UPDATE/DELETE FOR PORTION OF + +-- Fails on tables without a temporal PK: +CREATE TABLE for_portion_of_test ( + id int4range PRIMARY KEY, + valid_at tsrange NOT NULL, + name text NOT NULL +); + +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE +SET name = 'foo'; + +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE; + +DROP TABLE for_portion_of_test; +CREATE TABLE for_portion_of_test ( + id int4range NOT NULL, + valid_at tsrange NOT NULL, + name text NOT NULL, + CONSTRAINT for_portion_of_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +INSERT INTO for_portion_of_test +VALUES +('[1,2)', '[2018-01-02,2018-02-03)', 'one'), +('[1,2)', '[2018-02-03,2018-03-03)', 'one'), +('[1,2)', '[2018-03-03,2018-04-04)', 'one'), +('[2,3)', '[2018-01-01,2018-01-05)', 'two'), +('[3,4)', '[2018-01-01,)', 'three'), +('[4,5)', '(,2018-04-01)', 'four'), +('[5,6)', '(,)', 'five') +; + +-- +-- UPDATE tests +-- + +-- Setting with a missing column fails +UPDATE for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'foo' +WHERE id = '[5,6)'; + +-- Setting the range fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET valid_at = '[1990-01-01,1999-01-01)' +WHERE id = '[5,6)'; + +-- Setting with timestamps reversed fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +SET name = 'three^1' +WHERE id = '[3,4)'; + +-- Setting with a subquery fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM (SELECT '2018-01-01') TO '2018-06-01' +SET name = 'nope' +WHERE id = '[3,4)'; + +-- Setting with a column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM lower(valid_at) TO MAXVALUE +SET name = 'nope' +WHERE id = '[3,4)'; + +-- Setting with timestamps equal does nothing +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +SET name = 'three^0' +WHERE id = '[3,4)'; + +-- Updating a finite/open portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'three^1' +WHERE id = '[3,4)'; + +-- Updating a finite/open portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-03-01' +SET name = 'three^2' +WHERE id = '[3,4)'; + +-- Updating an open/finite portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-02-01' +SET name = 'four^1' +WHERE id = '[4,5)'; + +-- Updating an open/finite portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO MAXVALUE +SET name = 'four^2' +WHERE id = '[4,5)'; + +-- Updating a finite/finite portion with an exact fit +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2018-02-01' +SET name = 'four^3' +WHERE id = '[4,5)'; + +-- Updating an enclosed span +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO MAXVALUE +SET name = 'two^2' +WHERE id = '[2,3)'; + +-- Updating an open/open portion with a finite/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-01' TO '2019-01-01' +SET name = 'five^2' +WHERE id = '[5,6)'; + +-- Updating an enclosed span with separate protruding spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2020-01-01' +SET name = 'five^3' +WHERE id = '[5,6)'; + +-- Updating multiple enclosed spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +SET name = 'one^2' +WHERE id = '[1,2)'; + +-- Updating with a shift/reduce conflict +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '1' HOUR TO MINUTE + TO '2019-01-01' +SET name = 'one^3' +WHERE id = '[1,2)'; + +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '2' HOUR + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; + +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM ('2018-03-01' AT TIME ZONE INTERVAL '2' HOUR) + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; + +-- Updating the non-range part of the PK: +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-15' TO NULL +SET id = '[6,7)' +WHERE id = '[1,2)'; + +-- UPDATE with no WHERE clause +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2030-01-01' TO NULL +SET name = name || '*'; + +SELECT * FROM for_portion_of_test ORDER BY id, valid_at; + +-- +-- DELETE tests +-- + +-- Deleting with a missing column fails +DELETE FROM for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO NULL +WHERE id = '[5,6)'; + +-- Deleting with timestamps reversed fails +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +WHERE id = '[3,4)'; + +-- Deleting with timestamps equal does nothing +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +WHERE id = '[3,4)'; + +-- Deleting with a closed/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2020-06-01' +WHERE id = '[5,6)'; + +-- Deleting with a closed/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO NULL +WHERE id = '[3,4)'; + +-- Deleting with an open/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO '2018-02-08' +WHERE id = '[1,2)'; + +-- Deleting with an open/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +WHERE id = '[6,7)'; + +-- DELETE with no WHERE clause +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2025-01-01' TO NULL; + +SELECT * FROM for_portion_of_test ORDER BY id, valid_at; + +-- UPDATE ... RETURNING returns only the updated values (not the inserted side values) +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-01' TO '2018-02-15' +SET name = 'three^3' +WHERE id = '[3,4)' +RETURNING *; + +-- test that we run triggers on the UPDATE/DELETEd row and the INSERTed rows + +CREATE FUNCTION for_portion_of_trigger() +RETURNS trigger +AS +$$ +BEGIN + RAISE NOTICE '% % % of %', TG_WHEN, TG_OP, NEW.valid_at, OLD.valid_at; + IF TG_OP = 'DELETE' THEN + RETURN OLD; + ELSE + RETURN NEW; + END IF; +END; +$$ +LANGUAGE plpgsql; + +CREATE TRIGGER trg_for_portion_of_before_insert + BEFORE INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_after_insert + AFTER INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_before_update + BEFORE UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_after_update + AFTER UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_before_delete + BEFORE DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); +CREATE TRIGGER trg_for_portion_of_after_delete + AFTER DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger(); + +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2021-01-01' TO '2022-01-01' +SET name = 'five^4' +WHERE id = '[5,6)'; + +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2023-01-01' TO '2024-01-01' +WHERE id = '[5,6)'; + +SELECT * FROM for_portion_of_test ORDER BY id, valid_at; + +-- +-- Now re-run the same tests but with a PERIOD instead of a range: +-- + +DROP TABLE for_portion_of_test; + +-- Fails on tables without a temporal PK: +CREATE TABLE for_portion_of_test ( + id int4range PRIMARY KEY, + valid_from timestamp, + valid_til timestamp, + PERIOD FOR valid_at (valid_from, valid_til), + name text NOT NULL +); + +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE +SET name = 'foo'; + +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-15' TO MAXVALUE; + +DROP TABLE for_portion_of_test; +CREATE TABLE for_portion_of_test ( + id int4range NOT NULL, + valid_from timestamp, + valid_til timestamp, + PERIOD FOR valid_at (valid_from, valid_til), + name text NOT NULL, + CONSTRAINT for_portion_of_pk PRIMARY KEY (id, valid_at WITHOUT OVERLAPS) +); +INSERT INTO for_portion_of_test +VALUES +('[1,2)', '2018-01-02', '2018-02-03', 'one'), +('[1,2)', '2018-02-03', '2018-03-03', 'one'), +('[1,2)', '2018-03-03', '2018-04-04', 'one'), +('[2,3)', '2018-01-01', '2018-01-05', 'two'), +('[3,4)', '2018-01-01', null, 'three'), +('[4,5)', null, '2018-04-01', 'four'), +('[5,6)', null, null, 'five') +; + +-- +-- UPDATE tests +-- + +-- Setting with a missing column fails +UPDATE for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'foo' +WHERE id = '[5,6)'; + +-- Setting the start column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET valid_from = '1990-01-01' +WHERE id = '[5,6)'; + +-- Setting the end column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET valid_til = '1999-01-01' +WHERE id = '[5,6)'; + +-- Setting with timestamps reversed fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +SET name = 'three^1' +WHERE id = '[3,4)'; + +-- Setting with a subquery fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM (SELECT '2018-01-01') TO '2018-06-01' +SET name = 'nope' +WHERE id = '[3,4)'; + +-- Setting with a column fails +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM valid_from TO MAXVALUE +SET name = 'nope' +WHERE id = '[3,4)'; + +-- Setting with timestamps equal does nothing +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +SET name = 'three^0' +WHERE id = '[3,4)'; + +-- Updating a finite/open portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO MAXVALUE +SET name = 'three^1' +WHERE id = '[3,4)'; + +-- Updating a finite/open portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-03-01' +SET name = 'three^2' +WHERE id = '[3,4)'; + +-- Updating an open/finite portion with an open/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO '2018-02-01' +SET name = 'four^1' +WHERE id = '[4,5)'; + +-- Updating an open/finite portion with a finite/open target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO MAXVALUE +SET name = 'four^2' +WHERE id = '[4,5)'; + +-- Updating a finite/finite portion with an exact fit +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2018-02-01' +SET name = 'four^3' +WHERE id = '[4,5)'; + +-- Updating an enclosed span +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM MINVALUE TO MAXVALUE +SET name = 'two^2' +WHERE id = '[2,3)'; + +-- Updating an open/open portion with a finite/finite target +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-01-01' TO '2019-01-01' +SET name = 'five^2' +WHERE id = '[5,6)'; + +-- Updating an enclosed span with separate protruding spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2017-01-01' TO '2020-01-01' +SET name = 'five^3' +WHERE id = '[5,6)'; + +-- Updating multiple enclosed spans +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +SET name = 'one^2' +WHERE id = '[1,2)'; + +-- Updating with a shift/reduce conflict +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '1' HOUR TO MINUTE + TO '2019-01-01' +SET name = 'one^3' +WHERE id = '[1,2)'; + +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM '2018-03-01' AT TIME ZONE INTERVAL '2' HOUR + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; + +UPDATE for_portion_of_test +FOR PORTION OF valid_at + FROM ('2018-03-01' AT TIME ZONE INTERVAL '2' HOUR) + TO '2019-01-01' +SET name = 'one^4' +WHERE id = '[1,2)'; + +-- Updating the non-range part of the PK: +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-15' TO NULL +SET id = '[6,7)' +WHERE id = '[1,2)'; + +-- UPDATE with no WHERE clause +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2030-01-01' TO NULL +SET name = name || '*'; + +-- TODO: UPDATE with generated columns too +SELECT * FROM for_portion_of_test ORDER BY id, valid_from NULLS FIRST, valid_til; + +-- +-- DELETE tests +-- + +-- Deleting with a missing column fails +DELETE FROM for_portion_of_test +FOR PORTION OF invalid_at FROM '2018-06-01' TO NULL +WHERE id = '[5,6)'; + +-- Deleting with timestamps reversed fails +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2018-01-01' +WHERE id = '[3,4)'; + +-- Deleting with timestamps equal does nothing +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO '2018-04-01' +WHERE id = '[3,4)'; + +-- Deleting with a closed/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-06-01' TO '2020-06-01' +WHERE id = '[5,6)'; + +-- Deleting with a closed/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2018-04-01' TO NULL +WHERE id = '[3,4)'; + +-- Deleting with an open/closed target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO '2018-02-08' +WHERE id = '[1,2)'; + +-- Deleting with an open/open target +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM NULL TO NULL +WHERE id = '[6,7)'; + +-- DELETE with no WHERE clause +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2025-01-01' TO NULL; + +SELECT * FROM for_portion_of_test ORDER BY id, valid_from NULLS FIRST, valid_til; + +-- UPDATE ... RETURNING returns only the updated values (not the inserted side values) +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2018-02-01' TO '2018-02-15' +SET name = 'three^3' +WHERE id = '[3,4)' +RETURNING *; + +-- test that we run triggers on the UPDATE/DELETEd row and the INSERTed rows + +CREATE FUNCTION for_portion_of_trigger2() +RETURNS trigger +AS +$$ +BEGIN + RAISE NOTICE '% % [%,%) of [%,%)', TG_WHEN, TG_OP, NEW.valid_from, NEW.valid_til, OLD.valid_from, OLD.valid_til; + IF TG_OP = 'DELETE' THEN + RETURN OLD; + ELSE + RETURN NEW; + END IF; +END; +$$ +LANGUAGE plpgsql; + +CREATE TRIGGER trg_for_portion_of_before_insert + BEFORE INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_after_insert + AFTER INSERT ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_before_update + BEFORE UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_after_update + AFTER UPDATE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_before_delete + BEFORE DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); +CREATE TRIGGER trg_for_portion_of_after_delete + AFTER DELETE ON for_portion_of_test + FOR EACH ROW + EXECUTE FUNCTION for_portion_of_trigger2(); + +UPDATE for_portion_of_test +FOR PORTION OF valid_at FROM '2021-01-01' TO '2022-01-01' +SET name = 'five^4' +WHERE id = '[5,6)'; + +DELETE FROM for_portion_of_test +FOR PORTION OF valid_at FROM '2023-01-01' TO '2024-01-01' +WHERE id = '[5,6)'; + +SELECT * FROM for_portion_of_test ORDER BY id, valid_from NULLS FIRST, valid_til; -- 2.26.2