Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit75aba11

Browse files
committed
Fix support for CREATE TABLE IF NOT EXISTS AS EXECUTE
The grammar IF NOT EXISTS for CTAS is supported since 9.5 and documentedas such, however the case of using EXECUTE as query has never beencovered as EXECUTE CTAS statements and normal CTAS statements are parsedseparately.Author: Andreas KarlssonDiscussion:https://postgr.es/m/2ddcc188-e37c-a0be-32bf-a56b07c3559e@proxel.seBackpatch-through: 9.5
1 parentfaf1324 commit75aba11

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

‎src/backend/parser/gram.y

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10773,11 +10773,29 @@ ExecuteStmt: EXECUTE name execute_param_clause
1077310773
ctas->into =$4;
1077410774
ctas->relkind = OBJECT_TABLE;
1077510775
ctas->is_select_into =false;
10776+
ctas->if_not_exists =false;
1077610777
/* cram additional flags into the IntoClause*/
1077710778
$4->rel->relpersistence =$2;
1077810779
$4->skipData = !($9);
1077910780
$$ = (Node *) ctas;
1078010781
}
10782+
|CREATEOptTempTABLEIF_PNOTEXISTScreate_as_targetAS
10783+
EXECUTEnameexecute_param_clauseopt_with_data
10784+
{
10785+
CreateTableAsStmt *ctas = makeNode(CreateTableAsStmt);
10786+
ExecuteStmt *n = makeNode(ExecuteStmt);
10787+
n->name =$10;
10788+
n->params =$11;
10789+
ctas->query = (Node *) n;
10790+
ctas->into =$7;
10791+
ctas->relkind = OBJECT_TABLE;
10792+
ctas->is_select_into =false;
10793+
ctas->if_not_exists =true;
10794+
/* cram additional flags into the IntoClause*/
10795+
$7->rel->relpersistence =$2;
10796+
$7->skipData = !($12);
10797+
$$ = (Node *) ctas;
10798+
}
1078110799
;
1078210800

1078310801
execute_param_clause:'('expr_list')'{$$ =$2; }

‎src/test/regress/expected/create_table.out

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,20 @@ ERROR: relation "as_select1" already exists
263263
CREATE TABLE IF NOT EXISTS as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
264264
NOTICE: relation "as_select1" already exists, skipping
265265
DROP TABLE as_select1;
266+
PREPARE select1 AS SELECT 1 as a;
267+
CREATE TABLE as_select1 AS EXECUTE select1;
268+
CREATE TABLE as_select1 AS EXECUTE select1;
269+
ERROR: relation "as_select1" already exists
270+
SELECT * FROM as_select1;
271+
a
272+
---
273+
1
274+
(1 row)
275+
276+
CREATE TABLE IF NOT EXISTS as_select1 AS EXECUTE select1;
277+
NOTICE: relation "as_select1" already exists, skipping
278+
DROP TABLE as_select1;
279+
DEALLOCATE select1;
266280
-- create an extra wide table to test for issues related to that
267281
-- (temporarily hide query, to avoid the long CREATE TABLE stmt)
268282
\set ECHO none

‎src/test/regress/sql/create_table.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,14 @@ CREATE TABLE as_select1 AS SELECT * FROM pg_class WHERE relkind = 'r';
278278
CREATETABLEIF NOT EXISTS as_select1ASSELECT*FROM pg_classWHERE relkind='r';
279279
DROPTABLE as_select1;
280280

281+
PREPARE select1ASSELECT1as a;
282+
CREATETABLEas_select1AS EXECUTE select1;
283+
CREATETABLEas_select1AS EXECUTE select1;
284+
SELECT*FROM as_select1;
285+
CREATETABLEIF NOT EXISTS as_select1AS EXECUTE select1;
286+
DROPTABLE as_select1;
287+
DEALLOCATE select1;
288+
281289
-- create an extra wide table to test for issues related to that
282290
-- (temporarily hide query, to avoid the long CREATE TABLE stmt)
283291
\set ECHO none

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp