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: add composite types to pg_class and pg_attribute #111179

Merged
merged 1 commit into from
Oct 23, 2023

Conversation

annrpom
Copy link
Contributor

@annrpom annrpom commented Sep 25, 2023

Previously, user-defined composite types were not populated
in 2 of pg_catalog's tables: pg_class and pg_attribute
(where the former's row entries pertain to the type and the latter's
pertain to the "columns" of the type). This patch addresses this
PostgreSQL-incompatible behavior by populating such tables with
user-defined composite types.

Epic: none
Fixes: #109675

Release note (sql change): pg_catalog's pg_class and pg_attribute
tables now have context of user-defined composite types (in accordance
with PostgreSQL's corresponding pg_catalog tables).

@cockroach-teamcity
Copy link
Member

This change is Reviewable

@annrpom annrpom changed the title sql: add compositite types to pg_class and pg_attribute wip (ignore): sql: add compositite types to pg_class and pg_attribute Sep 27, 2023
@annrpom annrpom changed the title wip (ignore): sql: add compositite types to pg_class and pg_attribute wip (ignore): sql: add composite types to pg_class and pg_attribute Sep 28, 2023
@annrpom annrpom force-pushed the populate-comp-typ branch 3 times, most recently from cc45867 to 86c3224 Compare October 5, 2023 02:01
@annrpom annrpom changed the title wip (ignore): sql: add composite types to pg_class and pg_attribute sql: add composite types to pg_class and pg_attribute Oct 5, 2023
@annrpom annrpom marked this pull request as ready for review October 5, 2023 14:09
@annrpom annrpom requested a review from a team as a code owner October 5, 2023 14:09
pkg/sql/logictest/testdata/logic_test/pg_catalog Outdated Show resolved Hide resolved
pkg/sql/pg_catalog.go Outdated Show resolved Hide resolved
pkg/sql/pg_catalog.go Outdated Show resolved Hide resolved
pkg/sql/pg_catalog.go Outdated Show resolved Hide resolved
pkg/sql/pg_catalog.go Outdated Show resolved Hide resolved
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @annrpom)


pkg/sql/pg_catalog.go line 1248 at r1 (raw file):

					// because we know the underlying descriptor will generate no rows, since
					// the ID being queried is *not* a table.
					table, ok := desc.(catalog.TableDescriptor)

we'll also need to update this part - it should also check if the descriptor is a type descriptor. this logic is used when querying the table by ID -- doing that makes the execution engine use the "virtual index" that is defined on the table, rather than using the populateAll function defined above. the reason it exists is because it lets us avoid fetching all the descriptors if we are only querying the table for one of them.

to test it out, could you add a few tests that are like select <cols> from pg_class where oid = 'typ_name'::regtype::oid (where typ_name is a composite type)? and similar for pg_attribute

@annrpom annrpom force-pushed the populate-comp-typ branch 2 times, most recently from 4b7230c to f468116 Compare October 18, 2023 18:00
Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @annrpom)


pkg/sql/pg_catalog.go line 1175 at r2 (raw file):

	) error,
) virtualSchemaTable {
	isIncludingCompositeTypes := populateFromType != nil

super nit: includesCompositeTypes might be a more clear name


pkg/sql/pg_catalog.go line 1251 at r2 (raw file):

								desc, err = p.byIDGetterBuilder().WithoutNonPublic().Get().Desc(ctx, id)
								if err != nil {
									// This is under the assumption that the OIDs for user-defined "things" are not hashed.

hm, i think this has one more case that's missing -- composite types also get a matching array type, and that has a different OID. handling that here might get messy.

maybe it would be better to keep the old logic after all, so we don't do a second lookup. if we keep the old return !IsMaybeHashedOid(oid.Oid(id)), nil line here, it should cause the index to fallback to populating the whole table

that would also mean we should change the definition of incomplete to

	incomplete: includesIndexEntries || includesCompositeTypes,

pkg/sql/pg_catalog.go line 1270 at r2 (raw file):

					var tableDesc catalog.TableDescriptor
					var typeDesc catalog.TypeDescriptor
					switch desc.DescriptorType() {

nit: you can use a switch type assertion to make this more concise. e.g.

switch d := desc.(type) {
case catalog.TableDescriptor:
  // now you can use `d`, and it will be of the catalog.TableDescriptor type.
case catalog.TypeDescriptor:
  // now you can use `d`, and it will be of the catalog.TypeDescriptor type.
default:
  return true, nil
}

pkg/sql/logictest/testdata/logic_test/pg_catalog line 2010 at r2 (raw file):

2210    _regclass              A            false           true          ,         0         2205     0
2211    _regtype               A            false           true          ,         0         2206     0
2249    record                 P            false           true          ,         2249      0        2287

nit: since this is for the "default" record type, this one should have an oid of zero still. looks like that's what PG has too


pkg/sql/logictest/testdata/logic_test/pg_catalog line 5032 at r2 (raw file):

    pg_catalog.pg_class
WHERE
    oid = 'u'::regtype::oid;

could you add one more test for looking up the array type. that is WHERE oid = 'u[]'::regtype::oid;

i think the index lookup won't work, but it should still fallback to populating the full table

Copy link
Contributor Author

@annrpom annrpom left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @rafiss)


pkg/sql/pg_catalog.go line 1175 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

super nit: includesCompositeTypes might be a more clear name

sgtm


pkg/sql/pg_catalog.go line 1251 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

hm, i think this has one more case that's missing -- composite types also get a matching array type, and that has a different OID. handling that here might get messy.

maybe it would be better to keep the old logic after all, so we don't do a second lookup. if we keep the old return !IsMaybeHashedOid(oid.Oid(id)), nil line here, it should cause the index to fallback to populating the whole table

that would also mean we should change the definition of incomplete to

	incomplete: includesIndexEntries || includesCompositeTypes,

(related to the comment below about the test) -> we shouldn't need to worry about the array type, i think


pkg/sql/pg_catalog.go line 1270 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: you can use a switch type assertion to make this more concise. e.g.

switch d := desc.(type) {
case catalog.TableDescriptor:
  // now you can use `d`, and it will be of the catalog.TableDescriptor type.
case catalog.TypeDescriptor:
  // now you can use `d`, and it will be of the catalog.TypeDescriptor type.
default:
  return true, nil
}

💯 I forgot my basic coding knowledge for a sec with this one


pkg/sql/logictest/testdata/logic_test/pg_catalog line 2010 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

nit: since this is for the "default" record type, this one should have an oid of zero still. looks like that's what PG has too

ah oversight on my end - ty for catching this


pkg/sql/logictest/testdata/logic_test/pg_catalog line 5032 at r2 (raw file):

Previously, rafiss (Rafi Shamim) wrote…

could you add one more test for looking up the array type. that is WHERE oid = 'u[]'::regtype::oid;

i think the index lookup won't work, but it should still fallback to populating the full table

apologies - I could just not be understanding something here, but it seems like the pg_class table does not have context of the array type generated by the composite type (since it still is an array type after all). same with pg_attribute, but i was able to add a test for pg_type

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @annrpom)


pkg/sql/pg_catalog.go line 1251 at r2 (raw file):

Previously, annrpom (annie pompa) wrote…

(related to the comment below about the test) -> we shouldn't need to worry about the array type, i think

ah nice! great


pkg/sql/pg_catalog.go line 5115 at r2 (raw file):

}

func populateVirtualIndexForTable(

nit: these two new functions could use a comment

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @annrpom)


pkg/sql/logictest/testdata/logic_test/pg_catalog line 5032 at r2 (raw file):

Previously, annrpom (annie pompa) wrote…

apologies - I could just not be understanding something here, but it seems like the pg_class table does not have context of the array type generated by the composite type (since it still is an array type after all). same with pg_attribute, but i was able to add a test for pg_type

nice! looks like you are right, so we can keep this simple

Copy link
Collaborator

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

this looks good! just had one more optional comment to consider, then feel free to merge

Reviewable status: :shipit: complete! 0 of 0 LGTMs obtained (waiting on @annrpom)


pkg/sql/pg_catalog.go line 1275 at r3 (raw file):

						return populateVirtualIndexForTable(ctx, p, db, d, addRow, populateFromTable)
					case catalog.TypeDescriptor:
						if !includesCompositeTypes || d.GetKind() != descpb.TypeDescriptor_COMPOSITE {

should it be up to the populateFromType function to decide if non-composite types should be filtered?

that's what the populateAll function indicates above:

	// Generate rows for some/all user defined types (depending on function populateFromType).

Previously, user-defined composite types were not populated
in 2 of `pg_catalog`'s tables: `pg_class` and `pg_attribute`
(where the former's row entries pertain to the type and the latter's
pertain to the "columns" of the type). This patch addresses this
PostgreSQL-incompatible behavior by populating such tables with
user-defined composite types.

This patch also ensures that the `typrelid` column in the `pg_type`
table has the proper oid for composite types.

Epic: none
Fixes: cockroachdb#109675

Release note (sql change): `pg_catalog`'s `pg_class` and `pg_attribute`
tables now have context of user-defined composite types (in accordance
with PostgreSQL's corresponding `pg_catalog` tables). In addition, the
`typrelid` column in the `pg_type` table has the proper oid for composite
types.
@annrpom
Copy link
Contributor Author

annrpom commented Oct 23, 2023

you make a good point!

TFTR! ('-')7

bors r=rafiss

@craig
Copy link
Contributor

craig bot commented Oct 23, 2023

Build succeeded:

@craig craig bot merged commit 3c05cb0 into cockroachdb:master Oct 23, 2023
6 of 8 checks passed
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.

Populate pg_class and pg_attribute with composite types
3 participants