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: column ordinal position mismatch in information_schema.columns after dropping a column #39787

Closed
yuzefovich opened this issue Aug 21, 2019 · 0 comments · Fixed by #40485
Closed
Assignees
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.

Comments

@yuzefovich
Copy link
Member

@nvanbenschoten while reviewing #39785 noticed that there is a mismatch between information_schema.columns.ordinal_position and pg_catalog.pg_attribute.attnum after a column has been dropped. This should be fixed. Here is the relevant repro:
In Postgres:

nathan=# drop table if exists hcp_test;
DROP TABLE
nathan=# create table hcp_test(a int, b int, c int);
CREATE TABLE
nathan=# select attname, attnum from pg_attribute where attrelid = 'hcp_test'::regclass;
 attname  | attnum
----------+--------
 tableoid |     -7
 cmax     |     -6
 xmax     |     -5
 cmin     |     -4
 xmin     |     -3
 ctid     |     -1
 a        |      1
 b        |      2
 c        |      3
(9 rows)

nathan=# SELECT column_name, ordinal_position from information_schema.columns where table_name = 'hcp_test';
 column_name | ordinal_position
-------------+------------------
 a           |                1
 b           |                2
 c           |                3
(3 rows)

nathan=# alter table hcp_test drop column b;
ALTER TABLE
nathan=# select attname, attnum from pg_attribute where attrelid = 'hcp_test'::regclass;
           attname            | attnum
------------------------------+--------
 tableoid                     |     -7
 cmax                         |     -6
 xmax                         |     -5
 cmin                         |     -4
 xmin                         |     -3
 ctid                         |     -1
 a                            |      1
 ........pg.dropped.2........ |      2
 c                            |      3
(9 rows)

nathan=# SELECT column_name, ordinal_position from information_schema.columns where table_name = 'hcp_test';
 column_name | ordinal_position
-------------+------------------
 a           |                1
 c           |                3
(2 rows)

In CockroachDB:

root@127.0.0.1:58308/movr> drop table if exists hcp_test;
DROP TABLE
root@127.0.0.1:58308/movr> create table hcp_test(a int, b int, c int);
CREATE TABLE
root@127.0.0.1:58308/movr> select attname, attnum from pg_attribute where attrelid = 'hcp_test'::regclass;
  attname | attnum
+---------+--------+
  a       |      1
  b       |      2
  c       |      3
  rowid   |      4
(4 rows)
root@127.0.0.1:58308/movr> SELECT column_name, ordinal_position from information_schema.columns where table_name = 'hcp_test';
  column_name | ordinal_position
+-------------+------------------+
  a           |                1
  b           |                2
  c           |                3
  rowid       |                4
(4 rows)
root@127.0.0.1:58308/movr> alter table hcp_test drop column b;
ALTER TABLE
root@127.0.0.1:58308/movr> select attname, attnum from pg_attribute where attrelid = 'hcp_test'::regclass;
  attname | attnum
+---------+--------+
  a       |      1
  c       |      3
  rowid   |      4
(3 rows)
root@127.0.0.1:58308/movr> SELECT column_name, ordinal_position from information_schema.columns where table_name = 'hcp_test';
  column_name | ordinal_position
+-------------+------------------+
  a           |                1
  c           |                2
  rowid       |                3
(3 rows)
@yuzefovich yuzefovich added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-sql-pgcompat Semantic compatibility with PostgreSQL labels Aug 21, 2019
@jordanlewis jordanlewis changed the title sql: column ordinal position mismatch after dropping a column sql: column ordinal position mismatch in information_schema.columns after dropping a column Sep 3, 2019
arulajmani added a commit to arulajmani/cockroach that referenced this issue Sep 5, 2019
When a column other than the last is dropped, ordinal_position in
information_schema.columns virtual table no longer matches attnum from
the pg_attribute table. This PR fixes this issue.

Fixes cockroachdb#39787

Release note (sql change): ordinal_position matches pg_attribute.attnum after a column is dropped.
craig bot pushed a commit that referenced this issue Sep 5, 2019
40436: sql: more test fixes for opt-driven foreign keys r=RaduBerinde a=RaduBerinde

Fixing up some expected errors in tests and making sure we don't
buffer the mutation input if we fall back to the legacy path (the
bufferNode is unnecessary and interferes with an interleaved delete
fast path).

Release note: None

40451: testcluster: don't overwrite localities indiscriminately r=andreimatei a=andreimatei

Before this patch, a TestCluster would set localities for all nodes to a
static values. This was overwriting any values set throught the
TestClusterArgs. This patch makes it so that, if any localities are set
in the args, we don't overwrite them.

Release note: None

40485: sql: Fix a bug with ordinal_position in information_schema.columns r=arulajmani a=arulajmani

When a column other than the last is dropped, ordinal_position in
information_schema.columns virtual table no longer matches attnum from
the pg_attribute table. This PR fixes this issue.

Fixes #39787

Release note (bug fix): ordinal_position in information_schema.columns
matches pg_attribute.attnum after a column is dropped.

40511: exec: fix explain(vec) for queries with subqueries r=jordanlewis a=jordanlewis

Also add logic tests that show the explain(vec) plans for all of the
tpch queries.

Closes #40484.

Release note: None

Co-authored-by: Radu Berinde <radu@cockroachlabs.com>
Co-authored-by: Andrei Matei <andrei@cockroachlabs.com>
Co-authored-by: Arul Ajmani <arula@cockroachlabs.com>
Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
craig bot pushed a commit that referenced this issue Sep 5, 2019
40485: sql: Fix a bug with ordinal_position in information_schema.columns r=arulajmani a=arulajmani

When a column other than the last is dropped, ordinal_position in
information_schema.columns virtual table no longer matches attnum from
the pg_attribute table. This PR fixes this issue.

Fixes #39787

Release note (bug fix): ordinal_position in information_schema.columns
matches pg_attribute.attnum after a column is dropped.

40511: exec: fix explain(vec) for queries with subqueries r=jordanlewis a=jordanlewis

Also add logic tests that show the explain(vec) plans for all of the
tpch queries.

Closes #40484.

Release note: None

40516: sql: fix table lookup for drop index  r=pbardea a=pbardea

Previously, when searching for the table relevant to a particular index
when dropping the index, we would fetch all object names and require that
all those tables exist. However, if a table was deleted in the same
transaction that table name would not be resolvable and we would error.
We already had a check to see if the table being looked up was nil, but
this check would not be used because the `required` flag was set to true.

This PR just sets the flag to false, and looks at moves on to the next
table if one of them no longer is resolvable.

Addresses #38768.

Release note (bug fix): Fix faulty error when trying to delete a table
and an unrelated index in the same transaction.

Co-authored-by: Arul Ajmani <arula@cockroachlabs.com>
Co-authored-by: Jordan Lewis <jordanthelewis@gmail.com>
Co-authored-by: Paul Bardea <pbardea@gmail.com>
@craig craig bot closed this as completed in ce00e81 Sep 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-pgcompat Semantic compatibility with PostgreSQL C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants