Skip to content

Support SERIAL/BIGSERIAL non-partition columns - #673

Merged
Jason Petersen (jasonmp85) merged 1 commit into
masterfrom
feature/improve_sequence_support
Jul 29, 2016
Merged

Jason Petersen (jasonmp85) merged 1 commit into
masterfrom
feature/improve_sequence_support

Conversation

@jasonmp85

@jasonmp85 Jason Petersen (jasonmp85) commented Jul 26, 2016 •

Copy link
Copy Markdown
Contributor

This adds support for SERIAL/BIGSERIAL column types. Because we now can evaluate functions on the master (during execution), adding this is a matter of ensuring the table creation step works properly.

To accomplish this, I've added some logic to detect sequences owned by a table (i.e. those related to its columns). Simply creating a sequence and using it in a default value is insufficient; users who do so must ensure the sequence is owned by the column using it.

Fortunately, this is exactly what SERIAL and BIGSERIAL do, which is the use case we're targeting with this feature. While testing this, I found that worker_apply_shard_ddl_command actually adds shard identifiers to sequence names, though I found no places that use or test this path. I removed that code so that sequence names are not mutated and will match those used by a SERIAL default value expression.

Our use of the new-to-9.5 CREATE SEQUENCE IF NOT EXISTS syntax means we are dropping support for 9.4 (which is being done regardless, but makes this change simpler). I've removed 9.4 from the Travis build matrix.

Fixes #211

  • fix FIXMEs
  • fix upgrade script
  • don't error out if a sequence owner is changed to it's current owner
  • add comment around list_truncate()
  • get rid of superuser() check

@jasonmp85 Jason Petersen (jasonmp85) added the needs-review to track proposals or requests that are pending review to triage label Jul 26, 2016
@jasonmp85

Copy link
Copy Markdown
Contributor Author

Assigned Andres Freund (@anarazel) since he'll be reviewing this.

Andres Freund (@anarazel) — I thought my drop logic was working correctly, but it turns out I was connected to the wrong node in my terminal when I was testing and the sequences were not actually being dropped on the workers as I believed.

Using the utility hook does not seem to capture the sequence drop portion when dropping a sequence-owning table. The event trigger seems natural, though by the time the loop informs me a sequence is being dropped, I can no longer figure out what relation it was associated with (its information in pg_depend is gone). And since the EVENT TRIGGER drops the table (and shards) before processing the sequence drop, even the placement rows are already gone (so finding the affected nodes isn't possible at that point in time). I'll fix this early in the morning.

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.

Entirely independent of this change afaics.

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.

Yeah, citus_indent did it; I suppose I can move it to another commit.

I'm inching ever closer to having Travis builds fail if citus_indent fails.

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.

FWIW, I don't have a problem with making Travis fail.

@anarazel

Copy link
Copy Markdown
Contributor

Using the utility hook does not seem to capture the sequence drop
portion when dropping a sequence-owning table.

Indeed. That's why the event drop trigger exists.

The event trigger seems natural, though by the time the loop informs
me a sequence is being dropped, I can no longer figure out what
relation it was associated with (its information in pg_depend is
gone).

For simplicity a reasonable approach is to simply always replay drop
sequence commands on workers. ISTM that before long we'll
unconditionally replicate CREATE FUNCTION/OPERATOR/TYPE/SEQUENCE,
because things are just too painful to use without that.

@jasonmp85
Jason Petersen (jasonmp85) force-pushed the feature/improve_sequence_support branch from 4127a54 to b91e817 Compare July 26, 2016 18:38
@jasonmp85

Copy link
Copy Markdown
Contributor Author

For simplicity a reasonable approach is to simply always replay drop sequence commands on workers. ISTM that before long we'll unconditionally replicate CREATE FUNCTION/OPERATOR/TYPE/SEQUENCE, because things are just too painful to use without that.

I've pushed up the DELETE handling. Basically I accumulate the nodes and sequences during the shard drop phase (in the event trigger) and then do a second pass to drop all the sequences on the nodes.

@jasonmp85

Copy link
Copy Markdown
Contributor Author

For simplicity a reasonable approach is to simply always replay drop sequence commands on workers

This elides the fact that there is no canonical set of "workers". Just the list of to-be-workers-when-shards-are-next-created in pg_worker_list.conf and the list of de facto workers specified by the hostnames and ports in pg_dist_shard_placement. Seems safest to actually record where the shards are being dropped, and send sequence commands there (which is what I've done).

Comment thread src/backend/distributed/citus.sql Outdated

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.

It'd be a bit better to use array_agg(DISTINCT) or something, || is O(n^2).

@jasonmp85

Copy link
Copy Markdown
Contributor Author

Andres Freund (@anarazel) I still need to add the test you suggested, but I've addressed everything else (I think). Can you verify that I didn't miss anything?

Comment thread src/backend/distributed/citus.sql Outdated

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.

Hm. Wonder if we really only want to do this with '1'.

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.

Huh. I could have sworn master_drop_all_shards used only finalized placements, but apparently not. Removing.

@jasonmp85
Jason Petersen (jasonmp85) force-pushed the feature/improve_sequence_support branch from 7a5a01c to e184b3b Compare July 27, 2016 21:33

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.

We should only error out if the owner is different, lots of tools just specify all the options.

@anarazel

Andres Freund (anarazel) commented Jul 28, 2016 •

Copy link
Copy Markdown
Contributor

Some minor details to fix (added check list in PR description), other than those...

@jasonmp85

Copy link
Copy Markdown
Contributor Author

added check list in PR description

Hooray! Thanks.

@jasonmp85
Jason Petersen (jasonmp85) force-pushed the feature/improve_sequence_support branch from dceab55 to 56d8f97 Compare July 28, 2016 03:51
@jasonmp85

Copy link
Copy Markdown
Contributor Author

OK, everything should be addressed Andres Freund (@anarazel). Look over how I addressed everything and I can merge it all in the morning with your blessing 🙏.

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.

Uh, are you sure this doesn't reset that property, given that's an earlier upgrade script? I'd much rather copy that here.

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.

I had the same thought, but wanted to ask you about it. If you're fine copying it here, will do so.

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.

(I checked BTW, and CREATE OR REPLACE apparently preserves the value of parameters not specified, so it remains SECURITY DEFINER).

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.

Done.

@marcocitus

Marco Slot (marcocitus) commented Jul 28, 2016 •

Copy link
Copy Markdown
Contributor

General uninformed question: Should we do something about sequences in append-distributed tables? They allow COPY directly from the worker and will thus use the sequences on the worker and see duplicate values.

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.

Is that list_length() check correct? pg_catalog.serial is sometimes auto-generated.

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.

I hope so, since this is copy-pasted-edited from transformColumnDefinition. I'll add a comment.

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.

Done.

@anarazel

Copy link
Copy Markdown
Contributor

On 2016-07-28 10:52:38 -0700, Marco Slot wrote:

General uninformed question: Should we do something about sequences in append-distributed tables, since they allow COPY directly from the worker and will see duplicate values?

I don't really see what we can do about that for now, except maybe
adding an error check in the copy code, when going through the worker
directly?

@jasonmp85

Copy link
Copy Markdown
Contributor Author

General uninformed question: Should we do something about sequences in append-distributed tables? They allow COPY directly from the worker and will thus use the sequences on the worker and see duplicate values.

Marco Slot (@marcocitus): can we add a check to worker-side copy to ban any default values that contain mutable functions? If not, why not? Was this an oversight, or conscious for some use case?

If so, I'll add that, which would cover this. If not, I can add a more conservative check for sequences.

@lfittl

Lukas Fittl (lfittl) commented Jul 28, 2016 •

Copy link
Copy Markdown
Contributor

Jason Petersen (@jasonmp85) I've given this PR testing with the Rails app, and first of all: Looks good!

However I am encountering this issue if the SERIAL column is the partition column:

ActiveRecord::StatementInvalid: PG::FeatureNotSupported: ERROR:  values given for the partition column must be constants or constant expressions
: INSERT INTO "ads" ("name", "image_url", "target_url", "campaign_id", "created_at", "updated_at") VALUES ('Enhanced Senses', 'https://placehold.it/600x100.png', 'http://gerhold.net/annabelle_raynor', 1, '2016-07-28 19:25:29.526637', '2016-07-28 19:25:29.526637') RETURNING "id"
postgres=# \d ads
                                         Table "public.ads"
      Column       |            Type             |                    Modifiers                     
-------------------+-----------------------------+--------------------------------------------------
 id                | integer                     | not null default nextval('ads_id_seq'::regclass)

Should I open a separate issue for this, or is it related to this PR?

@jasonmp85

Copy link
Copy Markdown
Contributor Author

However I am encountering this issue if the SERIAL column is the partition column […]

That's expected behavior. Right now we can't evaluate nextval in the partition column (that wasn't part of the "evaluate functions on master" review), so I can't do anything about that.

You'd get a similar error before, though during shard creation. Now we let you get past shard creation, you just can't use the DEFAULT value of that column.

I had thought adding a check for this would be burdensome, but now I realize it's like one or two statements, tops. Would you like me to make a more explicit error (at shard creation—not master_create_distributed_table—time, probably)?

@jasonmp85

Copy link
Copy Markdown
Contributor Author

Lukas Fittl (@lfittl) Actually, I suppose it could be at master_create_distributed_table time, too. Why not.

@anarazel

Copy link
Copy Markdown
Contributor

On 2016-07-28 12:42:07 -0700, Jason Petersen wrote:

Lukas Fittl (@lfittl) Actually, I suppose it could be at master_create_distributed_table time, too. Why not.

It works for copy, so let's leave it the current way.

@lfittl

Copy link
Copy Markdown
Contributor

On Thursday, July 28, 2016, Jason Petersen [email protected] wrote:

Lukas Fittl (@lfittl) https://github.com/lfittl Actually, I suppose it could be at
master_create_distributed_table time, too. Why not.

I don't think thats necessary - the current error is fine.

I'll make a new issue so we can track this for future release planning. The
same problem applies when using UUIDs with a default value to generate
them, so its not related to SERIAL per se.

Lukas Fittl

Skype: lfittl
Phone: +1 415 321 0630

@jasonmp85

Copy link
Copy Markdown
Contributor Author

It works for copy, so let's leave it the current way.

I think I knew this right after the master functions review was merged but had since forgotten. Makes this a moot point. Added a comment to Brian Cloutier (@lithp) in #488 about this message.

@jasonmp85
Jason Petersen (jasonmp85) force-pushed the feature/improve_sequence_support branch from 07df5c9 to 2c59e9d Compare July 28, 2016 23:17
@anarazel

Copy link
Copy Markdown
Contributor

Changes look good to me, but apparently needs a rebase with actual conflicts.

@jasonmp85
Jason Petersen (jasonmp85) force-pushed the feature/improve_sequence_support branch 2 times, most recently from 2db194b to 3672634 Compare July 28, 2016 23:21
@jasonmp85

Copy link
Copy Markdown
Contributor Author

Any updates? It's been flattened and debased, AFAIK.

Jason Petersen
Software Engineer | Citus Data
303.736.9255
[email protected]

On Jul 28, 2016, at 5:18 PM, Andres Freund [email protected] wrote:

Changes look good to me, but apparently needs a rebase with actual
conflicts.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#673 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AADF8YWgDtJu2kQeo5ag1XlbLg-6f-WPks5qaTikgaJpZM4JU1VM
.

@anarazel

Copy link
Copy Markdown
Contributor

On 2016-07-28 18:13:40 -0700, Jason Petersen wrote:

Any updates? It's been flattened and debased, AFAIK.

debased? Interesting ;)

I earlier basically meant to imply that you have a :shipit: after rebasing.

@jasonmp85

Copy link
Copy Markdown
Contributor Author

I was going to claim it was a Pixies reference but it's actually just
autocomplete as I uncharacteristically obsessively checked my phone for
review updates.

Merging soon, thanks!

Jason Petersen
Software Engineer | Citus Data
303.736.9255
[email protected]

On Jul 28, 2016, at 7:16 PM, Andres Freund [email protected] wrote:

On 2016-07-28 18:13:40 -0700, Jason Petersen wrote:

Any updates? It's been flattened and debased, AFAIK.

debased? Interesting ;)

I earlier basically meant to imply that you have a :shipit: after rebasing.

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
#673 (comment), or mute
the thread
https://github.com/notifications/unsubscribe-auth/AADF8dnlfI4yzefgGHmxEzxnecvnyLuGks5qaVSKgaJpZM4JU1VM
.

This adds support for SERIAL/BIGSERIAL column types. Because we now can
evaluate functions on the master (during execution), adding this is a
matter of ensuring the table creation step works properly.

To accomplish this, I've added some logic to detect sequences owned by
a table (i.e. those related to its columns). Simply creating a sequence
and using it in a default value is insufficient; users who do so must
ensure the sequence is owned by the column using it.

Fortunately, this is exactly what SERIAL and BIGSERIAL do, which is the
use case we're targeting with this feature. While testing this, I found
that worker_apply_shard_ddl_command actually adds shard identifiers to
sequence names, though I found no places that use or test this path. I
removed that code so that sequence names are not mutated and will match
those used by a SERIAL default value expression.

Our use of the new-to-9.5 CREATE SEQUENCE IF NOT EXISTS syntax means we
are dropping support for 9.4 (which is being done regardless, but makes
this change simpler). I've removed 9.4 from the Travis build matrix.

Some edge cases are possible in ALTER SEQUENCE, COPY FROM (on workers),
and CREATE SEQUENCE OWNED BY. I've added errors for each so that users
understand when and why certain operations are prohibited.
@jasonmp85
Jason Petersen (jasonmp85) force-pushed the feature/improve_sequence_support branch from 3672634 to abe7304 Compare July 29, 2016 05:58
@jasonmp85
Jason Petersen (jasonmp85) merged commit f1ed052 into master Jul 29, 2016
@jasonmp85
Jason Petersen (jasonmp85) deleted the feature/improve_sequence_support branch July 29, 2016 06:11
@jasonmp85 Jason Petersen (jasonmp85) removed the needs-review to track proposals or requests that are pending review to triage label Jul 29, 2016
Dimitri Fontaine (DimCitus) pushed a commit that referenced this pull request Jan 10, 2018
Support SERIAL/BIGSERIAL non-partition columns

cr: @anarazel
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.

Support sequences

6 participants