Skip to content
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

Merged
merged 1 commit into from
Apr 13, 2018

Conversation

knz
Copy link
Contributor

@knz knz commented Apr 12, 2018

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.

@knz knz requested review from jordanlewis, justinj and a team April 12, 2018 10:35
@cockroach-teamcity
Copy link
Member

This change is Reviewable

Copy link
Contributor

@justinj justinj left a 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

@@ -2196,6 +2196,7 @@ CockroachDB supports the following flags:

// Metadata functions.

// https://www.postgresql.org/docs/9.6/static/functions-info.html
Copy link
Contributor

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done here and below

@@ -2208,6 +2209,7 @@ CockroachDB supports the following flags:
},
},

// https://www.postgresql.org/docs/9.6/static/functions-info.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@@ -2223,52 +2225,77 @@ CockroachDB supports the following flags:
},
},

// https://www.postgresql.org/docs/9.6/static/functions-info.html
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

// 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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

},
},

// https://www.postgresql.org/docs/9.6/static/functions-info.html
Copy link
Contributor

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.
@knz knz force-pushed the 20180412-current-schemas branch from 0b48e95 to ea95d9d Compare April 12, 2018 22:55
@knz
Copy link
Contributor Author

knz commented Apr 12, 2018

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.

@knz
Copy link
Contributor Author

knz commented Apr 12, 2018

bors r+

@craig
Copy link
Contributor

craig bot commented Apr 12, 2018

Build failed (retrying...)

craig bot pushed a commit that referenced this pull request Apr 12, 2018
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>
@craig
Copy link
Contributor

craig bot commented Apr 13, 2018

Build succeeded

@craig craig bot merged commit ea95d9d into cockroachdb:master Apr 13, 2018
craig bot pushed a commit that referenced this pull request Apr 13, 2018
24758: cherry-pick 2.0: sql: fix the handling of current_schema and search_path r=knz a=knz

Picks #24718.

cc @cockroachdb/release 

Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
@knz knz deleted the 20180412-current-schemas branch April 13, 2018 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

sql: current_schema should skip over invalid schemas
3 participants