Database::tableExists appears to be called repeatedly during normal use of a Drupal site. In PostgreSQL, this is implemented by querying the information_schema.tables view, which in our PostgreSQL 9.2 environment completes in the low-single-digit milliseconds; e.g.:

LOG:  duration: 5.150 ms  statement: SELECT 1 FROM information_schema.tables WHERE (table_catalog = 'drupal') AND  (table_schema = 'public') AND (table_name = 'node');

An equivalent query using the pg_tables view (see attached schema.inc.patch) is typically several times faster, however:

LOG:  duration: 0.966 ms  statement: SELECT 1 FROM pg_tables WHERE  (schemaname = 'public') AND (tablename = 'node');

(Note that in PostgreSQL, the information_schema.tables view essentiallydefines table_catalog as current_database(), so it should be unnecessary to specify it)

While several milliseconds would normally be unnoticeable to a user, I recently encountered a case where the performance of Database::tableExists significantly impacted the performance of the Tripal module during an operation where Drupal nodes are created for records in an external Chado schema. During this operation, which can take hours depending on the number of records in the Chado schema, roughly a third of the total run time was spent in Database::tableExists.

There is precedent for this optimization: the MySQL version of Database::tableExists has been overridden as well to bypass MySQL's relatively-slow information_schema.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

nathanweeks’s picture

FileSize
1.69 KB
bzrudi71’s picture

Version: 7.32 » 8.0.x-dev
Category: Bug report » Feature request
Issue tags: +PostgreSQL
Parent issue: » #2157455: [Meta] Make Drupal 8 work with PostgreSQL or remove support from core before release

Just stumbled over this while looking for remaining issues for #2157455: [Meta] Make Drupal 8 work with PostgreSQL or remove support from core before release. @nathanweeks, could you rewrite your patch for D8 please. To get this in D7 it needs back port from D8 branch. Adding a reference to the parent issue and tagging PostgreSQL.

daffie’s picture

Category: Feature request » Task
Status: Active » Needs review
Issue tags: +Perfomance
Parent issue: #2157455: [Meta] Make Drupal 8 work with PostgreSQL or remove support from core before release » #1744302: [meta] Resolve known performance regressions in Drupal 8
FileSize
999 bytes
daffie’s picture

bzrudi71’s picture

Patch looks good to me, but I think we will need a complete test run first before tagging RTBC. Will report results later...

daffie’s picture

@bzrudi71: Thank for the review!

bzrudi71’s picture

Status: Needs review » Reviewed & tested by the community

Bot run completed with no new fails or exceptions. This one seems ready to me and as even MySQL overrides tableExists() we can do the same I think.

  • webchick committed 7dad7c7 on 8.0.x
    Issue #2370593 by daffie, nathanweeks, bzrudi71: Database::tableExists...
webchick’s picture

Status: Reviewed & tested by the community » Patch (to be ported)
Issue tags: +Needs backport to D7

Committed and pushed to 8.0.x. Thanks!

Looks like this is a candidate for 7.x as well?

bzrudi71’s picture

Thanks @webchick. Yes, this is a candidate for D7 also.

daffie’s picture

Version: 8.0.x-dev » 7.x-dev
Sivaji_Ganesh_Jojodae’s picture

Status: Patch (to be ported) » Needs review
FileSize
934 bytes

Patch for Drupal 7.

  • webchick committed 7dad7c7 on 8.1.x
    Issue #2370593 by daffie, nathanweeks, bzrudi71: Database::tableExists...
nathanweeks’s picture

We have been using the Drupal 7 patch by @sivaji in production for two Drupal/Tripal sites running PostgreSQL 9.3.x for approximately five months. Is there anything formal testing that needs to be done before declaring the patch to be "Reviewed & tested by the community"?

  • webchick committed 7dad7c7 on 8.3.x
    Issue #2370593 by daffie, nathanweeks, bzrudi71: Database::tableExists...

  • webchick committed 7dad7c7 on 8.3.x
    Issue #2370593 by daffie, nathanweeks, bzrudi71: Database::tableExists...

  • webchick committed 7dad7c7 on 8.4.x
    Issue #2370593 by daffie, nathanweeks, bzrudi71: Database::tableExists...

  • webchick committed 7dad7c7 on 8.4.x
    Issue #2370593 by daffie, nathanweeks, bzrudi71: Database::tableExists...
joseph.olstad’s picture

joseph.olstad’s picture

Wow, great work poker10! First time I've seen so much green on these postgres test runs.

joseph.olstad’s picture

poker10’s picture

mcdruid’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Pending Drupal 7 commit

Looks like a good backport to D7.

I might tweak the comment about the strtolower a little (will do the same in #3265837: Backport DatabaseSchema_pgsql::fieldExists() improvements), but that can be done on commit.

Thanks!

Fabianx’s picture

Assigned: Unassigned » mcdruid

RTBC + 1, approved for Merge! Thanks all!

  • mcdruid committed da2cb44 on 7.x
    Issue #2370593 by poker10, daffie, joseph.olstad, nathanweeks,...
mcdruid’s picture

Assigned: mcdruid » Unassigned
Status: Reviewed & tested by the community » Fixed
Issue tags: -Needs backport to D7, -Pending Drupal 7 commit

Tweaked the comment on commit per #24

Thank you everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

edame’s picture

Note: This modifies the behavior of tableExists for PostgreSQL; it used to be that db_table_exists() also found views, but with this change that's no longer the case.