umbrellio / laravel-pg-extensions

Laravel extensions for Postgres
MIT License
90 stars 18 forks source link

PDO Read or Write connection #68

Closed mayken closed 2 years ago

mayken commented 2 years ago

I have a problem, I use master and slave postgres. master is located far from the backend, and slave is nearby. This package is automatically connected by default to the master - write connection, thereby increasing the request time by more than 2 seconds! How can I fix this? Please help me.

After remove umbrellio/laravel-pg-extensions, all ok!

stack trace: PDO::construct Illuminate\Database\Connectors\Connector::createPdoConnection Illuminate\Database\Connectors\Connector::createConnection Illuminate\Database\Connectors\PostgresConnector::connect Illuminate\Database\Connectors\ConnectionFactory::Illuminate\Database\Connectors{closure} call_user_func Illuminate\Database\Connection::getPdo Illuminate\Database\Connection::getDoctrineConnection Umbrellio\Postgres\PostgresConnection::getDoctrineConnection Illuminate\Database\Connection::getDoctrineSchemaManager Illuminate\Database\Schema\Builder::registerCustomDoctrineType Umbrellio\Postgres\PostgresConnection::Umbrellio\Postgres{closure} Illuminate\Support\Collection::each Umbrellio\Postgres\PostgresConnection::registerExtensions Umbrellio\Postgres\PostgresConnection::useDefaultPostProcessor Illuminate\Database\Connection::construct Umbrellio\Postgres\Connectors\ConnectionFactory::createConnection Illuminate\Database\Connectors\ConnectionFactory::createSingleConnection Illuminate\Database\Connectors\ConnectionFactory::createReadWriteConnection Illuminate\Database\Connectors\ConnectionFactory::make

pvsaintpe commented 2 years ago

Please, send to me your composer.json, app/config (app.php, database* configs), your App/Providers, for analysis this problem.

mayken commented 2 years ago

Archive.zip

UPD: I also created a new empty laravel project, made different database connections for reading(on localhost) and writing(remote server), added umbrella/laravel-ltree dependencies. In routes/web.php contains:

Route::get('/', function () {
    Category::first();
    return view('welcome');
});

class Category extends Model implements LTreeModelInterface
{
    use LTreeModelTrait, LTreeTrait;
}

http request http://127.0.0.1:8000 the pg-extension package makes a request to the database for writing but not in read database, that is, which is located outside the backend server.

mayken commented 2 years ago

Any news? @pvsaintpe

pvsaintpe commented 2 years ago

@mayken

  1. Please try to delete Illuminate\Database\DatabaseServiceProvider::class, from config/app.PHP, because DatabaseProvider already registered in composer post-autoload from vendor
  2. which database provider are you using? from your config I see that you have MySQL by default, and our solution only works with Postgres.
pvsaintpe commented 2 years ago

Archive.zip

UPD: I also created a new empty laravel project, made different database connections for reading(on localhost) and writing(remote server), added umbrella/laravel-ltree dependencies. In routes/web.php contains:

Route::get('/', function () {
    Category::first();
    return view('welcome');
});

class Category extends Model implements LTreeModelInterface
{
    use LTreeModelTrait, LTreeTrait;
}

http request http://127.0.0.1:8000 the pg-extension package makes a request to the database for writing but not in read database, that is, which is located outside the backend server.

Also, you don’t need to use trait: LtreeTrait in your model, because LtreeModelTrait already using this trait.

mayken commented 2 years ago

@mayken

  1. Please try to delete Illuminate\Database\DatabaseServiceProvider::class, from config/app.PHP, because DatabaseProvider already registered in composer post-autoload from vendor
  2. which database provider are you using? from your config I see that you have MySQL by default, and our solution only works with Postgres.
  1. also didn't help
  2. from .env set default connection for PostgreSQL: DB_CONNECTION=pgsql
pvsaintpe commented 2 years ago

Archive.zip

UPD: I also created a new empty laravel project, made different database connections for reading(on localhost) and writing(remote server), added umbrella/laravel-ltree dependencies. In routes/web.php contains:

Route::get('/', function () {
    Category::first();
    return view('welcome');
});

class Category extends Model implements LTreeModelInterface
{
    use LTreeModelTrait, LTreeTrait;
}

http request http://127.0.0.1:8000 the pg-extension package makes a request to the database for writing but not in read database, that is, which is located outside the backend server.

regarding the question why our package uses a connection to the database in the composer, because the provider is registered in the vendor, and in it there is the registration of types for Doctrine, in particular LtreeType. you need to do this in any case, and in order not to explain to not everyone how to do this, we do this in our package automatically, delivering support in Schema for migrations.

pvsaintpe commented 2 years ago

I have a problem, I use master and slave postgres. master is located far from the backend, and slave is nearby. This package is automatically connected by default to the master - write connection, thereby increasing the request time by more than 2 seconds! How can I fix this? Please help me.

After remove umbrellio/laravel-pg-extensions, all ok!

stack trace: PDO::construct Illuminate\Database\Connectors\Connector::createPdoConnection Illuminate\Database\Connectors\Connector::createConnection Illuminate\Database\Connectors\PostgresConnector::connect Illuminate\Database\Connectors\ConnectionFactory::Illuminate\Database\Connectors{closure} call_user_func Illuminate\Database\Connection::getPdo Illuminate\Database\Connection::getDoctrineConnection Umbrellio\Postgres\PostgresConnection::getDoctrineConnection Illuminate\Database\Connection::getDoctrineSchemaManager Illuminate\Database\Schema\Builder::registerCustomDoctrineType Umbrellio\Postgres\PostgresConnection::Umbrellio\Postgres{closure} Illuminate\Support\Collection::each Umbrellio\Postgres\PostgresConnection::registerExtensions Umbrellio\Postgres\PostgresConnection::useDefaultPostProcessor Illuminate\Database\Connection::construct Umbrellio\Postgres\Connectors\ConnectionFactory::createConnection Illuminate\Database\Connectors\ConnectionFactory::createSingleConnection Illuminate\Database\Connectors\ConnectionFactory::createReadWriteConnection Illuminate\Database\Connectors\ConnectionFactory::make

completely forgot to ask, at what point does the error occur? at the time of composer install or in tests or when starting an application or in a migration?

pvsaintpe commented 2 years ago

although you know, try to do it like this:

DAD5587B-CD74-4312-8C0D-DDC8AC5216C3

mayken commented 2 years ago

Archive.zip UPD: I also created a new empty laravel project, made different database connections for reading(on localhost) and writing(remote server), added umbrella/laravel-ltree dependencies. In routes/web.php contains:

Route::get('/', function () {
    Category::first();
    return view('welcome');
});

class Category extends Model implements LTreeModelInterface
{
    use LTreeModelTrait, LTreeTrait;
}

http request http://127.0.0.1:8000 the pg-extension package makes a request to the database for writing but not in read database, that is, which is located outside the backend server.

regarding the question why our package uses a connection to the database in the composer, because the provider is registered in the vendor, and in it there is the registration of types for Doctrine, in particular LtreeType. you need to do this in any case, and in order not to explain to not everyone how to do this, we do this in our package automatically, delivering support in Schema for migrations.

unfortunately creates an additional connection to the database without even calling models using ltree...

I created a new class:

class CategoryManager extends Category {
    protected $connection = 'pgsql::write';
}

And in the parent class:

class Category extends Model implements LTreeModelInterface
{

    use Translateable, LTreeModelTrait, HasTreeRelationships, SoftDeletes;

    protected $connection = 'pgsql_read'; // --- NEW DATABASE SECTION config/database.php for manual connection for only read database
}

Also commented in Umbrellio\Postgres\UmbrellioPostgresProvider

// $this->app->singleton('db.factory', function ($app) {
//     return new ConnectionFactory($app);
// });

// $this->app->singleton('db', function ($app) {
//     return new DatabaseManager($app, $app['db.factory']);
// });

$this->app->bind('db.connection', function ($app) {
            return $app['db']->connection();
});

as temporary solution it is work for all models

mayken commented 2 years ago

I have a problem, I use master and slave postgres. master is located far from the backend, and slave is nearby. This package is automatically connected by default to the master - write connection, thereby increasing the request time by more than 2 seconds! How can I fix this? Please help me. After remove umbrellio/laravel-pg-extensions, all ok! stack trace: PDO::construct Illuminate\Database\Connectors\Connector::createPdoConnection Illuminate\Database\Connectors\Connector::createConnection Illuminate\Database\Connectors\PostgresConnector::connect Illuminate\Database\Connectors\ConnectionFactory::Illuminate\Database\Connectors{closure} call_user_func Illuminate\Database\Connection::getPdo Illuminate\Database\Connection::getDoctrineConnection Umbrellio\Postgres\PostgresConnection::getDoctrineConnection Illuminate\Database\Connection::getDoctrineSchemaManager Illuminate\Database\Schema\Builder::registerCustomDoctrineType Umbrellio\Postgres\PostgresConnection::Umbrellio\Postgres{closure} Illuminate\Support\Collection::each Umbrellio\Postgres\PostgresConnection::registerExtensions Umbrellio\Postgres\PostgresConnection::useDefaultPostProcessor Illuminate\Database\Connection::construct Umbrellio\Postgres\Connectors\ConnectionFactory::createConnection Illuminate\Database\Connectors\ConnectionFactory::createSingleConnection Illuminate\Database\Connectors\ConnectionFactory::createReadWriteConnection Illuminate\Database\Connectors\ConnectionFactory::make

completely forgot to ask, at what point does the error occur? at the time of composer install or in tests or when starting an application or in a migration?

this is not error, it Profiler info. Me was need Information what very slow execute any http request for website

pvsaintpe commented 2 years ago

Archive.zip UPD: I also created a new empty laravel project, made different database connections for reading(on localhost) and writing(remote server), added umbrella/laravel-ltree dependencies. In routes/web.php contains:

Route::get('/', function () {
    Category::first();
    return view('welcome');
});

class Category extends Model implements LTreeModelInterface
{
    use LTreeModelTrait, LTreeTrait;
}

http request http://127.0.0.1:8000 the pg-extension package makes a request to the database for writing but not in read database, that is, which is located outside the backend server.

regarding the question why our package uses a connection to the database in the composer, because the provider is registered in the vendor, and in it there is the registration of types for Doctrine, in particular LtreeType. you need to do this in any case, and in order not to explain to not everyone how to do this, we do this in our package automatically, delivering support in Schema for migrations.

unfortunately creates an additional connection to the database without even calling models using ltree...

I created a new class:

class CategoryManager extends Category {
    protected $connection = 'pgsql::write';
}

And in the parent class:

class Category extends Model implements LTreeModelInterface
{

    use Translateable, LTreeModelTrait, HasTreeRelationships, SoftDeletes;

    protected $connection = 'pgsql_read'; // --- NEW DATABASE SECTION config/database.php for manual connection for only read database
}

Also commented in Umbrellio\Postgres\UmbrellioPostgresProvider

// $this->app->singleton('db.factory', function ($app) {
//     return new ConnectionFactory($app);
// });

// $this->app->singleton('db', function ($app) {
//     return new DatabaseManager($app, $app['db.factory']);
// });

$this->app->bind('db.connection', function ($app) {
            return $app['db']->connection();
});

as temporary solution it is work for all models

Today I will try in our project to test your case with a read only flag for postgres, most likely we will release a fix as I understand what it is, because I can’t just replace the code, these factories are needed for other cases

github-actions[bot] commented 2 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 5 days

mayken commented 2 years ago

I think there will be no solution?