Skip to content

Conversation

@BuonOmo
Copy link
Collaborator

@BuonOmo BuonOmo commented Oct 9, 2025

TODOs:

  • publish the new rgeo-activerecord 8.1 compatible gem (see bump to 8.1 rgeo/rgeo-activerecord#84)
  • verify that the new implemetation of #foreign_keys works if t2 doesn't belong to the same namespace as t1

@BuonOmo BuonOmo force-pushed the feat/rails-8-1 branch 2 times, most recently from cb4869f to d8e3e07 Compare October 9, 2025 11:36
@BuonOmo BuonOmo force-pushed the feat/rails-8-1 branch 2 times, most recently from ab74ce6 to dc9a906 Compare October 30, 2025 10:20
#
# @see: https://github.com/rails/rails/blob/8695b028261bdd244e254993255c6641bdbc17a5/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb#L829
# OVERRIDE(v8.1.1):
# - comment is retrieved differently than PG for performance
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

col_description is the method used to retrieve the comment in PG. @rafiss is still true that this is slower than using information_schema.columns ?

Comment on lines +38 to +42
ARRAY(
SELECT a.attname
FROM pg_attribute a
WHERE a.attrelid = d.indexrelid AND a.attishidden
) AS hidden_columns
Copy link
Collaborator Author

@BuonOmo BuonOmo Oct 30, 2025

Choose a reason for hiding this comment

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

@rafiss
I added this bit. I'm really unsure about performances, any better idea would be appreciated.

The goal is to remove hidden columns from the columns above. My main issue was that we are using pg_get_indexdef to retrieve the columns. I don't know edge cases so I was not eager to use another method, but if we were not to use it I feel like we could easily have a faster and simpler approach.

My second question is: do you have a simple routine to benchmark two slightly different columns in CRDB, and pitfalls I should avoid?


EDIT: one of the ideas I had:

ARRAY(
  SELECT a.attname
  FROM pg_attribute a
  JOIN unnest(d.indkey) AS k(attnum)
  ON a.attrelid = d.indrelid AND a.attnum = k.attnum
  AND NOT a.attishidden
) AS visible_columns

Copy link
Contributor

Choose a reason for hiding this comment

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

Using pg_get_indexdef should actually be fine. The CRDB implementation is a function with a SQL body (https://github.com/cockroachdb/cockroach/blob/08676709fc2c9ee4f9cdbfeb4601b71b273773d8/pkg/sql/sem/builtins/pg_builtins.go#L862), so what that means in practice is that all the SQL gets inlined into the query that's using the function.

do you have a simple routine to benchmark two slightly different columns in CRDB, and pitfalls I should avoid?

Hm, nothing is available externally, but we do have an internal benchmark suite in our codebase: https://github.com/cockroachdb/cockroach/blob/master/pkg/bench/rttanalysis/orm_queries_bench_test.go

I think that's overkill for what you're doing. If you are able to run locally, I think the best way to do a basic comparison is to create ~100 tables and then try executing the different queries to see what's faster.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Using pg_get_indexdef should actually be fine.

I actually wanted not to use it, to have better perf. Although from what I can see now, I doesn't seem trivial to replace it and improve perf

@BuonOmo BuonOmo force-pushed the feat/rails-8-1 branch 10 times, most recently from 4b74df4 to d477fd6 Compare November 4, 2025 16:25
@Maimer
Copy link
Contributor

Maimer commented Nov 13, 2025

Any timeline when this will get merged and released?

@BuonOmo
Copy link
Collaborator Author

BuonOmo commented Nov 13, 2025

@Maimer the PR should be ready during next week. @rafiss would you be able to make a release within two weeks from now?

@BuonOmo BuonOmo force-pushed the feat/rails-8-1 branch 5 times, most recently from aebefbe to f6aa445 Compare November 18, 2025 19:22
false
end

def supports_close_prepared?
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This seemed true by testing in CRDB 25.2, but I could not see any documentation on the CRDB side.

Here's the pg backend function as a reference : https://github.com/postgres/postgres/blob/master/src/backend/commands/prepare.c#L519

And libpq interface is PqMsg_Close with the Statement type ('C', 'S'). In the cockroach codebase I could find ClientMsgClose that maps to PqMsg_Close, but I did not find handling of close_prepared, yet it works... Any clue on that feature @rafiss ?

@BuonOmo BuonOmo force-pushed the feat/rails-8-1 branch 2 times, most recently from f5639cd to d3c0e76 Compare November 19, 2025 09:58
- update some outdated overrides.
- update override comments to know when
  was the last override.
- remove the `#column_names_from_column_numbers`
  method that was generating N+1 queries.
- update related methods.

See:
- rails/rails@249c367
- rails/rails@4e0546c
- rails/rails@c93d1b0
This would silently fail in the test
`test_bulk_insert_with_a_multi_statement_query_raises_an_exception_when_any_insert_fails`.
Removing some foreign keys and thus failing afterwards
in the test suite. Moreover, the `#disable_referential_integrity`
method is unfortunately public, so we need it to be robust.
Comment on lines +75 to +76
# prefixes and suffixes. The good thing is: if this happens, tests will crash
# hard, no way we miss it.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Well, the test retry logic was actually proving me wrong. But now that it is removed, we are fine (see later commit)

- remove `debugging?` utility
- use variables for `autocommit_before_ddl`:
  > This is the default intended approach, as it will
  > quote value if needed.
- remove spatial_column_info.rb and its calls
  as it is never used. See #391 to maybe include
  it back, properly.
- fix geos-config file fetching
- refactor for loop into any?
- unexclude working tests
- reset `test_create_fixtures` to the original,
  it works. And our version would corrupt foreign keys
If `#disable_referential_integrity` is ran within a
transaction, we may be able to still run the user's code.

Otherwise we warn the user why it failed.
Since CockroachDB does not support DDL transactions,
we adapt the test.
This fixes a bug where our `TestRetryHelper` logic combined
with `reset_fixtures` trying to reset a table without foreign
keys from another table.
It would first crash, removing the foreign key constraint (due
to how we handle `disable_referential_integrity`). And then pass,
since the foreign key constraint is gone. But we need that
constraint in later tests.
This is WIP and there is likely more to do. Hence
the reference #390.

Fixes the flaky test `PostGISTest#test_spatial_factory_attrs_parsing`
Tests that should fail would instead corrupt the
database state, causing subsequent tests to fail.
This is of course related to the unfortunate way
we disable referential integrity.

If we see tests failing again with `InvalidForeignKey`
errors, it will be a good time to reopen [cockroach#71496]

[cockroach#71496]: cockroachdb/cockroach#71496
@BuonOmo BuonOmo marked this pull request as ready for review November 19, 2025 10:45
@BuonOmo
Copy link
Collaborator Author

BuonOmo commented Nov 19, 2025

@rafiss the PR is finally ready for review. It is packed with various intentions, all separated in various commits (except two small exceptions that I was too lazy to fixup). I strongly suggest review commit by commit rather than the whole PR at once. Have a good read :)

About flaky tests, I've ran many times the flaky.yml CI. It seems like the only flaky tests we have left are one rare connection leaked, I don't have a local reproduction, and I think we can easily merge without this.

This PR also fixes some past bugs I found along the way, we might want to cherry-pick some of the commits later on.

If that's fine with you, I'd rather rebase and merge, not squash nor merge.

In our adapter, we rely on the sql type string for
spatial details to properly map the type to the correct
ActiveRecord type. It was not shared.
Minimal set of test reproduction:

```sh
COCKROACH_SKIP_LOAD_SCHEMA=1 SEED=13092 bundle exec rake test TESTOPTS="--name='/\A("\
"test_concurrent_insert_with_processes"\
"|test_uniqueness_validations_work_when_using_old_encryption_schemes"\
"|test_fixtures_get_encrypted_automatically"\
"|test_ciphertext_for_returns_the_ciphertext_of_a_value_when_the_record_is_new)\z/'"
```

It is important NOT TO run `ActiveRecord::Base.reset_column_information`.
Some tests are eagerly loading information to prevent side effects [^1].

We could run `Avenger.reset_column_information` but we actually do not
need it here.

[^1]: See https://github.com/rails/rails/blob/v8.1.1/activerecord/test/cases/encryption/helper.rb#L115-L120
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.

3 participants