Skip to content

Evaluate functions on master - #488

Merged
Andres Freund (anarazel) merged 6 commits into
masterfrom
213-support-stable-functions
Jul 13, 2016
Merged

Andres Freund (anarazel) merged 6 commits into
masterfrom
213-support-stable-functions

Conversation

@lithp

@lithp Brian Cloutier (lithp) commented May 4, 2016 •

Copy link
Copy Markdown
Contributor

Adds support for:

 -- now() evaluated on master and "col = '2016-05-03 ...'" pushed down
UPDATE tbl SET col = now() WHERE hits_single_shard;

-- stable_func(10) is evaluated on master
UPDATE tbl SET col = col + stable_func(10) WHERE hits_single_shard;

-- not allowed because we use statement replication
UPDATE tbl SET col = 10 + stable_func(col) WHERE hits_single_shard;

-- as long as the partition column is IMMUTABLE anything goes
INSERT INTO tbl VALUES (...);

-- joinTree is also evaluated
UPDATE tbl SET col = 10 WHERE hits_single_shard AND stamp > now() - interval '1 hour';
DELETE FROM tbl WHERE hits_single_shard AND stamp < now() - interval '1 day';

Decisions made:

  • Non-constant partition columns are not allowed, the planner must be able to route a query.
  • During INSERT everything, including VOLATILE functions, is allowed.
  • During UPDATE, STABLE functions are allowed. Everything which does't rely on the value of a Var is evaluated before being pushed down to the workers. (Volatile is not allowed because multiple rows could be updated, and Volatile functions have to be run once per row)
  • mutable are not allowed within CASE and COALESCE.
  • both the targetlist and jointree->quals are allowed to have mutable functions
  • functions in the onConflictWhere clause of UPSERTs are not supported.

If you enable DEBUG4 then some output is logged with the results of function evaluation.

Fixes #213

@lithp Brian Cloutier (lithp) added the needs-review to track proposals or requests that are pending review to triage label May 4, 2016
@lithp Brian Cloutier (lithp) changed the title [Prototype] 213 Evaluate functions on master [Prototype] Evaluate functions on master May 4, 2016
@marcocitus

Marco Slot (marcocitus) commented May 7, 2016 •

Copy link
Copy Markdown
Contributor

But is that okay, what's our policy on copying code from postgres?

It's better to avoid it if at all possible, but we copy a lot of code from postgres (grep -r INDENT-OFF for examples).

I don't think evaluate_expr is suitable for evaluating volatile functions that modify the database. You should probably look at ExecEvalExpr.


Oid relid = ((RangeTblEntry *) linitial(queryTree->rtable))->relid;
List *deparseContext = deparse_context_for("table", relid);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same rules apply in the WHERE clause.

@lithp
Brian Cloutier (lithp) force-pushed the 213-support-stable-functions branch from 73e2f10 to dccdec3 Compare May 12, 2016 12:17
@sumedhpathak

Sumedh Pathak (sumedhpathak) commented May 17, 2016 •

Copy link
Copy Markdown

Brian Cloutier (@lithp) Whats the status here? Do you have a time estimate for this change? Does this fix #211 as well?

@lithp Brian Cloutier (lithp) removed the needs-review to track proposals or requests that are pending review to triage label May 20, 2016
@lithp
Brian Cloutier (lithp) force-pushed the 213-support-stable-functions branch from ca25e7d to a3de24a Compare May 23, 2016 15:53
@sumedhpathak

Copy link
Copy Markdown

Brian Cloutier (@lithp) Assigning Andres Freund (@anarazel) as reviewer. There are a couple of todo's which are not checked. Is the PR still ready to go?

@lithp

Copy link
Copy Markdown
Contributor Author

Sumedh Pathak (@sumedhpathak) I think it's ready to be reviewed :)


/* drop into the router executor */
RouterExecutorRun(queryDesc, direction, count, task);
RouterExecutorRun(queryDesc, direction, count);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't worry this time, but generally mixing mostly unrelated refactoring in with functional changes, makes the whole harder to review.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right it wasn't necessary but it led to a small performance improvement, I didn't have to call GetMultiPlan multiple times. It probably should have been broken out into a separate commit, I'll be more careful.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine. Just put in a separate commit with that explanation next time round.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

He. Didn't now evaluable is an actual word. But it is. :)

@anarazel

Copy link
Copy Markdown
Contributor

Brian Cloutier (@lithp) I made a few, fairly minor, comments. Shouldn't be too hard to address. Close to the finishing line imo.

@lithp

Copy link
Copy Markdown
Contributor Author

Andres Freund (@anarazel) I think this last commit addresses all your comments

Brian Cloutier (lithp) and others added 6 commits July 13, 2016 11:45
- Enables using VOLATILE functions (like nextval()) in INSERT queries
- Enables using STABLE functions (like now()) targetLists and joinTrees

UPDATE and INSERT can now contain non-immutable functions. INSERT can contain any kind of
expression, while UPDATE can contain any STABLE function, so long as a Var is not passed
into the STABLE function, even indirectly. UPDATE TagetEntry's can now also include Vars.

There's an exception, CASE/COALESCE statements may not contain mutable functions.

Functions calls in master_modify_multiple_shards are also evaluated.
@anarazel
Andres Freund (anarazel) force-pushed the 213-support-stable-functions branch from 3789e70 to 4cf0a4e Compare July 13, 2016 18:49
@anarazel

Copy link
Copy Markdown
Contributor

Hm. I think the adjustment to master_modify_multiple_shards() isn't correct. The planner hasn't verified that the requirements for function evaluation are fulfilled. I'm pulling that part out, it doesn't seem quite right; and is separate functionality.

@anarazel

Copy link
Copy Markdown
Contributor

Ah, scratch that. Sorry for the noise.

@anarazel
Andres Freund (anarazel) merged commit a69a257 into master Jul 13, 2016
@anarazel Andres Freund (anarazel) removed the needs-review to track proposals or requests that are pending review to triage label Jul 13, 2016
@anarazel

Copy link
Copy Markdown
Contributor

And yay, merged!

@jasonmp85

Copy link
Copy Markdown
Contributor

Non-constant partition columns are not allowed, the planner must be able to route a query

Hey Brian Cloutier (@lithp), Lukas Fittl (@lfittl) was asking about the error we emit when someone attempts to use a function in a partition column slot. I don't think we should put too much polish into it, as I expected we'd plan to eventually permit such functions (evaluate on master before pruning).

Is there an issue open for eventually supporting master-evaluated functions in partition slots? I couldn't find it. Thanks!

@lfittl

Copy link
Copy Markdown
Contributor

Jason Petersen (@jasonmp85) Brian Cloutier (@lithp) FWIW, I couldn't find any issue related to this, so I've opened #687

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants