Re: hashjoins, index loops to retrieve pk/ux constrains in pg12

2021-09-29 Thread Arturas Mazeika
Hi Tom,

I agree that the query needs to be first correct, and second fast. I also
agree that this query works only if there are no duplicates among schemas
(if one chooses to create a table with the same names and index names and
constraint names in a different schema, this would not work). Provided the
assumptions are correct (what  it is on our customer systems), we use
intermediate liquibase scripts to keep track of our database (schema)
changes, those intermediate scripts fire queries as mentioned above, i.e.,
we cannot directly influence how the query looks like.

Given these very hard constraints (i.e., the query is formulated using
information_schema, and not directly) is it possible to assess why the hash
joins plan is chosen? At the end of the day, the io block hit rate of this
query in hash joins is 3-4 orders of magnitude higher compared to
sort/index joins? Is there anything one can do on the configuration side to
avoid such hash-join pitfalls?

Cheers,
Arturas

On Tue, Sep 28, 2021 at 4:13 PM Tom Lane  wrote:

> Arturas Mazeika  writes:
> > Thanks a lot for having a look at the query once again in more detail. In
> > short, you are right, I fired the liquibase scripts and observed the
> exact
> > query that was hanging in pg_stats_activity. The query was:
>
> > SELECT
> >   FK.TABLE_NAME   as "TABLE_NAME"
> >   , CU.COLUMN_NAMEas "COLUMN_NAME"
> >   , PK.TABLE_NAME as "REFERENCED_TABLE_NAME"
> >   , PT.COLUMN_NAMEas "REFERENCED_COLUMN_NAME"
> >   , C.CONSTRAINT_NAME as "CONSTRAINT_NAME"
> > FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
> > INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK ON
> > C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
> > INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK ON
> > C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
> > INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU ON C.CONSTRAINT_NAME
> > = CU.CONSTRAINT_NAME
> > INNER JOIN (
> >   SELECT
> >   i1.TABLE_NAME
> >   , i2.COLUMN_NAME
> >   FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
> >   INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2 ON
> > i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
> >   WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
> > ) PT ON PT.TABLE_NAME = PK.TABLE_NAME WHERE
> > lower(FK.TABLE_NAME)='secrole_condcollection'
>
> TBH, before worrying about performance you should be worrying about
> correctness.  constraint_name alone is not a sufficient join key
> for these tables, so who's to say whether you're even getting the
> right answers?
>
> Per SQL spec, the join key to use is probably constraint_catalog
> plus constraint_schema plus constraint_name.  You might say you
> don't need to compare constraint_catalog because that's fixed
> within any one Postgres database, and that observation would be
> correct.  But you can't ignore the schema.
>
> What's worse, the SQL-spec join keys are based on the assumption that
> constraint names are unique within schemas, which is not enforced in
> Postgres.  Maybe you're all right here, because you're only looking
> at primary key constraints, which are associated with indexes, which
> being relations do indeed have unique-within-schema names.  But you
> still can't ignore the schema.
>
> On the whole I don't think you're buying anything by going through
> the SQL-spec information views, because this query is clearly pretty
> dependent on Postgres-specific assumptions even if it looks like it's
> portable.  And you're definitely giving up a lot of performance, since
> those views have so many complications from trying to map the spec's
> view of whats-a-constraint onto the Postgres objects (not to mention
> the spec's arbitrary opinions about which objects you're allowed to
> see).  This query would be probably be simpler, more correct, and a
> lot faster if rewritten to query the Postgres catalogs directly.
>
> regards, tom lane
>


How to improve cockroach performance with pgbench?

2021-09-29 Thread Fabien COELHO



Hello,

I've been playing with CockroachDB, a distributed database system which is 
more or less compatible with Postgres because it implements the same 
network protocol. Because if this compatibility, I have used pgbench to 
setup and run some tests on various AWS VMs (5 identical VMs, going up to 
a total 80 vcpu in the system).


The general behavior and ease of use is great. Data are shared between 
nodes, adding a new node makes the system automatically replicate and 
balance the data, wow. Also, the provided web interface is quite nice and 
gives hints about what is happening. They implement an automatic retry 
feature so that when a transaction fails it is retried without the client 
needed to know about it.


All this is impressive, but performance wise I ran in a few issues and/or 
questions:


 - Loading data with a COPY (pgbench -i) is pretty slow, typically 3
   seconds per scale whereas on a basic postgres I would get 0.3 seconds
   per scale. Should I expect better performance, or is this the expected
   performance that can be achieved because of the automatic (automagic)
   replication performed by cockroach? Would it be better if I generated
   data from several connections (hmmm, pgbench does not know how to do
   that, but the tool could be improved if it is worth it)?

 - I'm at a loss at finding the right number of client connections to
   "maximise" tps under reasonable latency. Some of my tests suggest that
   maybe 4 clients per core is the best option. For a standard postgres,
   a typical client count would be larger, typically around 8-10 per
   core.
   Is this choice reasonable for cockroach?

 - The overall performance is a little bit disappointing. Ok, this is
   a distributed system which does automatic partitioning and replication
   on serializable transactions, so obviously this quality of service must
   cost something, but I'm typically running around 10 tps per core (with
   pgbench default transaction), so a pretty high latency, and even if
   it scales somehow, it which seems  quite low.
   What I am doing wrong? What should I check?

 - Another strange thing is that the steady state at full speed is quite
   unstable: looking at instantaneous performance, the tps varies a lot,
   eg between 0 and 4500 tps, more or less uniformly, i.e. the standard
   deviation is large, say 1000 tps stddev for a 2000 tps average
   performance.

Basically, any advice about cockroach configuration and running pgbench 
against it is welcome!


Thanks in advance,

--
Fabien.