Skip to content

Commit

Permalink
Add support for concurrent index creation statements (#148)
Browse files Browse the repository at this point in the history
PG supports a `CONCURRENTLY` keyword on index creation that changes how the indexing proceeds under the hood. It caused a parse error before, now it doesn't!

See https://www.postgresql.org/docs/current/sql-createindex.html
  • Loading branch information
airhorns authored Jan 8, 2024
1 parent 727463b commit a43e896
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/syntax/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ export interface CreateIndexStatement extends PGNode {
where?: Expr;
unique?: true;
ifNotExists?: true;
concurrently?: true;
indexName?: Name;
tablespace?: string;
with?: CreateIndexWith[];
Expand Down
18 changes: 10 additions & 8 deletions src/syntax/create-index.ne
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ createindex_statement
-> %kw_create
%kw_unique:?
kw_index
%kw_concurrently:?
kw_ifnotexists:?
word:?
%kw_on
Expand All @@ -24,14 +25,15 @@ createindex_statement
{% x => track(x, {
type: 'create index',
...x[1] && { unique: true },
...x[3] && { ifNotExists: true },
...x[4] && { indexName: asName(x[4]) },
table: x[6],
...x[7] && { using: asName(x[7]) },
expressions: x[9],
...x[11] && { with: x[11] },
...x[12] && { tablespace: unwrap(x[12]) },
...x[13] && { where: unwrap(x[13]) },
...x[3] && { concurrently: true },
...x[4] && { ifNotExists: true },
...x[5] && { indexName: asName(x[5]) },
table: x[7],
...x[8] && { using: asName(x[8]) },
expressions: x[10],
...x[12] && { with: x[12] },
...x[13] && { tablespace: unwrap(x[13]) },
...x[14] && { where: unwrap(x[14]) },
}) %}

createindex_expressions -> createindex_expression (comma createindex_expression {% last %}):* {% ([head, tail]) => {
Expand Down
27 changes: 27 additions & 0 deletions src/syntax/create-index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,4 +215,31 @@ describe('Create index', () => {
}],
tablespace: 'abc',
});

checkCreateIndex(['create index concurrently blah on test(col)'], {
type: 'create index',
indexName: { name: 'blah' },
table: { name: 'test' },
concurrently: true,
expressions: [{
expression: { type: 'ref', name: 'col' },
}],
});
checkCreateIndex(['create index concurrently on test(col)'], {
type: 'create index',
table: { name: 'test', },
concurrently: true,
expressions: [{
expression: { type: 'ref', name: 'col' },
}],
});
checkCreateIndex(['create unique index concurrently on test(col)'], {
type: 'create index',
table: { name: 'test', },
concurrently: true,
unique: true,
expressions: [{
expression: { type: 'ref', name: 'col' },
}],
});
});
3 changes: 3 additions & 0 deletions src/to-sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,9 @@ const visitor = astVisitor<IAstFullVisitor>(m => ({

createIndex: c => {
ret.push(c.unique ? 'CREATE UNIQUE INDEX ' : 'CREATE INDEX ');
if (c.concurrently) {
ret.push('CONCURRENTLY ');
}
if (c.ifNotExists) {
ret.push(' IF NOT EXISTS ');
}
Expand Down

0 comments on commit a43e896

Please sign in to comment.