-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
sql: fix the handling of current_schema and search_path #24718
Conversation
6fe4d35
to
0b48e95
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there's also a search_path_test.go
, not sure if there's an appropriate way to add this to that, but it does exist. either way LGTM
pkg/sql/sem/builtins/builtins.go
Outdated
@@ -2196,6 +2196,7 @@ CockroachDB supports the following flags: | |||
|
|||
// Metadata functions. | |||
|
|||
// https://www.postgresql.org/docs/9.6/static/functions-info.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe better to reference the most recent docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done here and below
pkg/sql/sem/builtins/builtins.go
Outdated
@@ -2208,6 +2209,7 @@ CockroachDB supports the following flags: | |||
}, | |||
}, | |||
|
|||
// https://www.postgresql.org/docs/9.6/static/functions-info.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
pkg/sql/sem/builtins/builtins.go
Outdated
@@ -2223,52 +2225,77 @@ CockroachDB supports the following flags: | |||
}, | |||
}, | |||
|
|||
// https://www.postgresql.org/docs/9.6/static/functions-info.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
pkg/sql/sem/builtins/builtins.go
Outdated
// For now, schemas are the same as databases. So, current_schemas | ||
// returns the current database (if one has been set by the user) | ||
// and the session's database search path. | ||
// https://www.postgresql.org/docs/9.6/static/functions-info.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
pkg/sql/sem/builtins/builtins.go
Outdated
}, | ||
}, | ||
|
||
// https://www.postgresql.org/docs/9.6/static/functions-info.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto
Prior to this patch, CockroachDB was following the pg docs to the letter regarding the relationship between `search_path` and the "current schema": the current schema was taken to always be the first item in `search_path`, as indicated in https://www.postgresql.org/docs/10/static/functions-info.html ("current_schema returns the name of the schema that is first in the search path (or a null value if the search path is empty).") This in turn impacts the creation of objects (tables, views, etc) when the name is unqualified: pg also specifies that such objects are to be created in the current schema. Unfortunately the pg doc is incorrect, as pg has a more nuanced behavior: only the _valid_ schemas in `search_path` are considered to determine the current schema in that way. This was causing an unfortunate visible compatibility bug: if the user specified an invalid schema in the search path, subsequent create operations would fail with CockroachDB and succeed with pg. This patch corrects this issue by adopting pg's behavior. Ditto for `current_schemas`. Release note (sql change, bug fix): the built-in functions `current_schema` and `current_schemas` now only consider valid schemas, like PostgreSQL does.
0b48e95
to
ea95d9d
Compare
The search_path_test.go is at a lower level than this patch (and does not have access to a lookup context that can check the validity of schemas), so that doesn't seem the right place. |
bors r+ |
Build failed (retrying...) |
24718: sql: fix the handling of current_schema and search_path r=knz a=knz Fixes #24472. Prior to this patch, CockroachDB was following the pg docs to the letter regarding the relationship between `search_path` and the "current schema": the current schema was taken to always be the first item in `search_path`, as indicated in https://www.postgresql.org/docs/9.6/static/functions-info.html ("current_schema returns the name of the schema that is first in the search path (or a null value if the search path is empty).") This in turn impacts the creation of objects (tables, views, etc) when the name is unqualified: pg also specifies that such objects are to be created in the current schema. Unfortunately the pg doc is incorrect, as pg has a more nuanced behavior: only the _valid_ schemas in `search_path` are considered to determine the current schema in that way. This was causing an unfortunate visible compatibility bug: if the user specified an invalid schema in the search path, subsequent create operations would fail with CockroachDB and succeed with pg. This patch corrects this issue by adopting pg's behavior. Ditto for `current_schemas`. Release note (sql change, bug fix): the built-in functions `current_schema` and `current_schemas` now only consider valid schemas, like PostgreSQL does. Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
Build succeeded |
Fixes #24472.
Prior to this patch, CockroachDB was following the pg docs to the
letter regarding the relationship between
search_path
and the"current schema": the current schema was taken to always be the first
item in
search_path
, as indicated inhttps://www.postgresql.org/docs/9.6/static/functions-info.html
("current_schema returns the name of the schema that is first in the
search path (or a null value if the search path is empty).")
This in turn impacts the creation of objects (tables, views, etc) when
the name is unqualified: pg also specifies that such objects are to be
created in the current schema.
Unfortunately the pg doc is incorrect, as pg has a more nuanced
behavior: only the valid schemas in
search_path
are considered todetermine the current schema in that way.
This was causing an unfortunate visible compatibility bug: if the user
specified an invalid schema in the search path, subsequent create
operations would fail with CockroachDB and succeed with pg.
This patch corrects this issue by adopting pg's behavior.
Ditto for
current_schemas
.Release note (sql change, bug fix): the built-in functions
current_schema
andcurrent_schemas
now only consider validschemas, like PostgreSQL does.