You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
pg_shard needs to support sequences and SERIAL/BIGSERIAL. The simplest way to do these for now is to have the sequence live on the master. Later we'll need to do something more complicated, but 9.5 will have machinery for that, I think.
Comment by jasonmp85 Thursday Dec 04, 2014 at 23:57 GMT
Having the sequence live on the master is essentially a must right now, as INSERT statements are commutable: conditions could arise wherein two racing INSERT commands result in different sequence values across replicas. Perhaps that's an argument for looking into logical replication, but the feature wasn't mature enough for us to investigate earlier in pg_shard's lifecycle.
Right now we eagerly evaluate any "constant expressions" during planning, e.g. we transform char_length('Jason') into the integer 5. Certain other functions could reasonably be supported by eagerly evaluating them on the master during planning, even things like rand or in your case, nextval. Since sequence values are never rolled back, our loose transactional semantics don't even get in the way here.
For the immediate future, we could expand the types of expressions we eagerly evaluate, or even offer a special eager function to allow users to force eager evaluation of functions on the master.
Comment by jasonmp85 Friday Dec 05, 2014 at 00:00 GMT
Nothing stands out in my mind about special support for this kind of thing in 9.5… do you have a quick link to a pgsql-hackers thread on the topic? I'm a pretty heavy lurker in that list but don't remember anything about this.
Comment by jberkus Friday Dec 05, 2014 at 00:11 GMT
Jason: there's some stuff buried in the Bi-directional replication code which allows replicas to pull "chunks" of sequences, in effect allowing for distributed sequences. However, this doesn't at all solve the consistency issue between shards.
Maybe we'd be better off implementing a UUID which is compatible with pg_shard instead, something which will be consistent yet unique across shards. Not quite sure how that would work, though.
Travis Jeffery (@travisjeffery) I think the easiest workaround is do create an "IMMUTABLE" function which actually calls nextval() internally. The master will automatically call the function while planning the query.
As workaround I want to create trigger BEFORE INSERT but saw there is another way to complete that. Brian Cloutier (@lithp) : may you supply a link to how to create such function which will be called by master while planning the query?
Even a function with no side-effects needs to be labeled VOLATILE if its value can change within a single query; some examples are random(), currval(), timeofday().
But doing so (mark function as immutable) the next query may be optimized and this function will not be called instead it will be replaced by value from previous call. But this is not the result we are trying to achieve.
row insertion honors serial type and evaluates sequence
Our original thinking on the first part was to replicate sequence on workers so that shard creation does not fail due to non-exiting sequence at worker. Having a sequence at worker would not have any other function other than being there doing shard table creation.
Can we instead drop sequence reference from shard table ? I think that would make our intention more explicit. If go this way, table definition at master will have the serial type and sequence definition, however shard table in worker would only have type but not default value coming from sequence.
You should now be able to use sequences in INSERT statements. They are forbidden in DELETE and UPDATE statements and a little wonky in SELECT statements (nextval calls used in SELECT get pushed down to the workers), but using nextval in a SELECT is a little wonky in and of itself.
There are some separate issues around using serial: #511 (comment)
Reacted by Brian Cloutier and Julián Cruz Cañada Racinet
Activity
citus-github-bot commentedon Feb 4, 2016
Thursday Dec 04, 2014 at 23:57 GMT
Having the sequence live on the master is essentially a must right now, as
INSERTstatements are commutable: conditions could arise wherein two racingINSERTcommands result in different sequence values across replicas. Perhaps that's an argument for looking into logical replication, but the feature wasn't mature enough for us to investigate earlier inpg_shard's lifecycle.Right now we eagerly evaluate any "constant expressions" during planning, e.g. we transform
char_length('Jason')into the integer5. Certain other functions could reasonably be supported by eagerly evaluating them on the master during planning, even things likerandor in your case,nextval. Since sequence values are never rolled back, our loose transactional semantics don't even get in the way here.For the immediate future, we could expand the types of expressions we eagerly evaluate, or even offer a special
eagerfunction to allow users to force eager evaluation of functions on the master.citus-github-bot commentedon Feb 4, 2016
Friday Dec 05, 2014 at 00:00 GMT
Nothing stands out in my mind about special support for this kind of thing in 9.5… do you have a quick link to a pgsql-hackers thread on the topic? I'm a pretty heavy lurker in that list but don't remember anything about this.
citus-github-bot commentedon Feb 4, 2016
Friday Dec 05, 2014 at 00:11 GMT
Jason: there's some stuff buried in the Bi-directional replication code which allows replicas to pull "chunks" of sequences, in effect allowing for distributed sequences. However, this doesn't at all solve the consistency issue between shards.
Maybe we'd be better off implementing a UUID which is compatible with pg_shard instead, something which will be consistent yet unique across shards. Not quite sure how that would work, though.
citus-github-bot commentedon Feb 4, 2016
Friday Dec 05, 2014 at 18:31 GMT
Maybe we should borrow code from here? https://github.com/OptionsHouse/shard_manager
Shaun has a fairly advanced distributed ID-generation widget. We may need to work out licensing issues though.
citus-github-bot commentedon Feb 4, 2016
Thursday Jul 23, 2015 at 12:29 GMT
implementing this would be highly appriciated.
citus-github-bot commentedon Feb 4, 2016
Sunday Aug 09, 2015 at 16:06 GMT
Support sequences is definitely a nice to have.
Thanks for
pg_shardit's a huge improvement for postgresql.ozgune commentedon Feb 25, 2016
We frequently hear this feature request. Most application libraries that work with PostgreSQL use sequences (Active Record) by default.
travisjeffery commentedon Apr 21, 2016
What's the best workaround for this right now?
lithp commentedon Apr 28, 2016
Travis Jeffery (@travisjeffery) I think the easiest workaround is do create an "IMMUTABLE" function which actually calls nextval() internally. The master will automatically call the function while planning the query.
KES777 commentedon May 10, 2016
As workaround I want to create trigger BEFORE INSERT but saw there is another way to complete that. Brian Cloutier (@lithp) : may you supply a link to how to create such function which will be called by master while planning the query?
KES777 commentedon May 10, 2016
Answer my question: http://www.postgresql.org/docs/9.5/static/xfunc-volatility.html
But doing so (mark function as immutable) the next query may be optimized and this function will not be called instead it will be replaced by value from previous call. But this is not the result we are trying to achieve.
sumedhpathak commentedon Jun 3, 2016
Murat Tuncer (@mtuncer) Before starting on this task, could you sync up with Andres Freund (@anarazel) ?
mtuncer commentedon Jun 10, 2016
There are 2 legs for this work:
Our original thinking on the first part was to replicate sequence on workers so that shard creation does not fail due to non-exiting sequence at worker. Having a sequence at worker would not have any other function other than being there doing shard table creation.
Can we instead drop sequence reference from shard table ? I think that would make our intention more explicit. If go this way, table definition at master will have the serial type and sequence definition, however shard table in worker would only have type but not default value coming from sequence.
sumedhpathak commentedon Jun 17, 2016
Currently blocked on #213
marcocitus commentedon Jul 15, 2016
You should now be able to use sequences in INSERT statements. They are forbidden in DELETE and UPDATE statements and a little wonky in SELECT statements (nextval calls used in SELECT get pushed down to the workers), but using nextval in a SELECT is a little wonky in and of itself.
There are some separate issues around using serial:
#511 (comment)