8

Let's imagine that we're building GitHub and we have two tables: repos and issues. Every GitHub repo has a collection of issues, and so the issues table has a foreign key of repo_id.

Now, when you're browsing a GitHub repo's issues, you don't want to be exposed to the internal id. Instead, you want something like a number, which increments from 1..n for only that repository. You want your first issue in your new repo to be numbered 1, not whatever the next id for an issue on GitHub is.

Of course, you need a way to increment, and you want to make sure that the number is unique when scoped to the repo. And so you especially want to avoid then any race condition where the same number can be generated twice.

What's the most straightforward way of handling this? A trigger? Something else entirely?

I am using PostgreSQL but would prefer approaches that are vanilla SQL where possible, e.g. triggers. If there's a simpler Postgres approach, then that would also be useful.

Any code that demonstrate your approach would be extraordinarily useful. Thanks!

4 Answers 4

1

I don't think there is a way to do this without the possibility of a race condition. This should minimize race conditions but not eliminate them. There may be better ways within specific database architectures. Assuming "REPOSITORY_ID" is provided by your application code:

insert into issues (repo_id,line_id) values (
    REPOSITORY_ID,
    coalesce((select max(line_id)+1 from issues where repo_id=REPOSITORY_ID),0)
);

This pulls the current highest line_id and increments it at the time of the insert. If there are no records, it defaults to 0. There is a small chance of a race if two inserts hit at the exact same time, but it seems unlikely. If you enforce uniqueness you can check for an error on insert and retry on failure.

1

Suppose you want to add a new issue to a certain repo, you could execute the following operations:

  1. start a new explicit transaction;
  2. select the repo you want to modify with a SELECT ... FOR UPDATE. This will put a row-level lock on it and prevent other transactions that want to add a new issue for that repo to proceed concurrently;
  3. get the new issue number for that repo in some way (for instance you could have a latest_issue column in issue, as in one of the answers, or you could perform a query to find it);
  4. insert the new issue with the correct issue number;
  5. terminate the transaction: this will release the lock and allow other transactions that want to work on the same repo to continue.

So you could define a stored procedure in this way, and call it every time you want to insert a new issue. Under the hypothesis that there are not too many concurrent transactions trying to insert new issues for the same repository, this would prevent race conditions and still operate with a reasonable efficiency.

1

Here is how I would tackle it:

create table repo(
  id serial primary key
);

create table issue(
  id integer not null,
  id_repo integer not null references repo(id),
  primary key (id, id_repo)
);

create function create_issue_seq() returns trigger as $$
  begin
    execute format('create sequence issue_%s_seq', new.id);
    return new;
  end
$$ language plpgsql;

create trigger create_issue_seq after insert on repo
  for each row execute procedure create_issue_seq();

create function assign_issue_id() returns trigger as $$
  begin
    new.id = nextval(format('issue_%s_seq', new.id_repo));
    return new;
  end
$$ language plpgsql;

create trigger assign_issue_id before insert on issue
  for each row execute procedure assign_issue_id();

One trigger creates issue id sequence after repo is created (dedicated for given repo) and second one utilizes existing dedicated sequence to properly fill issue id before issue is inserted.

Pros:

  • race-free as it's using sequences in fact
  • no exclusive locking needed

Cons:

  • it creates a lot of sequences (although quick test with 1M repos shown it doesn't cause any serious performance loss)

Notes:

  • probably it is wise to implement also some trigger which deletes sequence after delete on repo.
  • I assumed immutable repo id (which I strongly believe it is the right way: to have immutable PK)
0

I would keep a latest_issue column in repos and initialize it to 0.
In issues I'd create a column nr with a UNIQUE constraint on (repo_id, nr), where repo_id is the foreign key column in issues.

Whenever an issue is created, latest_issue in repos is increased. This number is then used as nr for issues.

2
  • Isn't it race vulnerable without locking? Commented Aug 19, 2016 at 14:19
  • No, just use UPDATE ... RETURNING to get the new issue number in a single transaction. That will do all the locking you need. Commented Aug 21, 2016 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.