-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[YSQL] CREATE TABLE without a primary key followed by ALTER TABLE adding a primary key should end up with one primary key #1104
Comments
Once Hector completes the work on partitioning, this enhancement can be done. |
One possible approach can be to enhance index backfill for this: #448 Similar to creating index, when we run
|
This currently makes restoring from |
@ben-pr-p You can use |
Oh, neat! I'm having difficulty finding installation instructions for |
It'll be in |
Original PostgreSQL code takes |
Note that the current implementation has a few limitations. Specifically:
|
Hello guys, good work. Is there an ETA for this feature? adding PK after column is really important with my project. |
Summary: Enables `ALTER TABLE ... ADD PRIMARY KEY (...)` syntax, allowing adding a primary key to a table that had none defined previously. ## Description Since the primary key is an intrinsic part of a DocDB table, we cannot literally "add" one to an existing table. Instead, this is implemented by renaming an old table, creating a new one in its stead, migrating data, and dropping an old table. All of this is performed as a single DDL transaction, so failure on any step would roll back the whole thing. Operation is logically divided onto the following phases: 1. Rename an old table 2. Create a replacement table 3. Copy CHECK and FK constraints 4. Copy table content (i.e. data itself). 5. Copy indexes (renaming old indexes in the process because of name conflicts) 6. Migrate sequences owned by old table 7. Copy triggers 8. Drop an old table ## Known limitations: * The following cases are not supported as of now: * Colocated tables (including tablegroups). * Partitioned tables and table partitions. * Table having rules. * `ALTER TABLE ADD CONSTRAINT PK USING INDEX` * If the table was created using `WITH (table_old = x)` (beta feature), that OID is lost without notice. * There are minor mismatches between YB and PG when migrating data fails (`SQLSTATE` is matching): * Null value in PK column * Duplicate PK --- Resolves yugabyte#1104 and yugabyte#6585 Test Plan: ybd --java-test org.yb.pgsql.TestPgAlterTableAddPrimaryKey ybd --java-test org.yb.pgsql.TestPgAlterTable#testRenameAndRecreate Reviewers: neil, nicolas, dmitry, mihnea Reviewed By: dmitry, mihnea Subscribers: dsrinivasan, zyu, bogdan, yql Differential Revision: https://phabricator.dev.yugabyte.com/D10057
Sorry @uwejan, missed your comment. It's included in v2.6 onward. |
Summary: In #1104 / D10057 we implemented `ALTER TABLE ADD PRIMARY KEY` command. This diff extends that work to implement `ALTER TABLE DROP CONSTRAINT pkey`. We cannot drop primary key of: * Partitioned tables and table partitions. * Tables having rules. Note that just like `ALTER TABLE ADD PRIMARY KEY`, this is designed for database setup and ORM migrations, and does not guarantee data safety when the table is being concurrently modified! --- Resolves #8735 Test Plan: ybd --java-test 'org.yb.pgsql.TestPgAlterTableChangePrimaryKey' `TestPgAlterTableAddPrimaryKey` has been tweaked into `TestPgAlterTableChangePrimaryKey` which tests both adds and drops. Reviewers: mihnea, tverona, neil, jason, myang, dmitry Reviewed By: dmitry Subscribers: yql Differential Revision: https://phabricator.dev.yugabyte.com/D17939
Currently, the PK specification has to be done inline as part of the CREATE TABLE. In YugabyteDB, the PK columns guides the primary dimension in which the table is laid out in storage (so that retrieval by those PK column lookups is as efficient).
Creating the PK afterwards using an ALTER is not just syntax sugar work, but will actually involve reorganizing the on-disk structure of the table in a way that it is now organized by the PK as the primary dimension. So, with YugabyteDB it'll be advantageous if the system can be told upfront as part of the CREATE TABLE what the PK columns are.
Nevertheless, we should support ALTER TABLE to allow adding a PRIMARY KEY after the fact to be compatible with the broader ecosystem of tools.
The text was updated successfully, but these errors were encountered: