Tuesday, November 18, 2014
pgDay SF 2015 Call For Speakers/Sponsors now open
pgDay SF 2015, to be held March 10th in Burlingame, CA, now has its Call for Speakers open. The event is also looking for sponsors. Come join us and meet even more Postgres folks!
Thursday, November 6, 2014
We need a webapp benchmark
I've been doing some comparative testing on different cloud platform's hosting of PostgreSQL. And one of the deficiencies in this effort is the only benchmarking tool I have is pgbench, which doesn't reflect the kinds of workloads people would want to run on cloud hosting. Don't get me wrong, pgbench does everything you could imagine with the simple Wisconsin benchmark, including statistics and sampling. But the core benchmark is still something which doesn't look much like the kind of Rails and Django apps I deal with on a daily basis.
There's also TPCC-js, which is more sophisticated, but is ultimately still a transactional, back-office OLTP benchmark.
So I'm thinking of developing a "webapp" benchmark. Here's what I see as concepts for such a benchmark:
There's also TPCC-js, which is more sophisticated, but is ultimately still a transactional, back-office OLTP benchmark.
So I'm thinking of developing a "webapp" benchmark. Here's what I see as concepts for such a benchmark:
- Read-mostly
- No multi-statement transactions
- Defined "users" concept with logins and new user registration
- Needs a "sessions" table which is frequently updated
- Read-write, read-only and session database connections should be separable, in order to test load-balancing optimization.
- Queries counting, sorting and modifying content
- Measured unit of work is the "user session" which would contain some content lookups and minor updates ("likes").
Saturday, November 1, 2014
Finding Foreign Keys with No Indexes
Unlike some other SQL databases, PostgreSQL does not automatically create indexes on the "child" (in formal language "referencing") side of a foreign key. There are some good reasons for this (see below), but it does give users another opportunity to forget something, which is indexing the foreign keys (FKs) that need it. But which ones need it? Well, fortunately, you can interrogate the system catalogs and find out.
I have added a query for finding foreign keys without indexes to pgx_scripts. Indexes on the "parent", or "referenced" side of the FK are automatically indexed (they have to be, because they need to be unique), so we won't talk about them further.
Now, in order to understand how to interpret this, you have to understand why you would or would not have an index on an FK, and what sort of indexes are valid. There's two times that indexes on the child side of FKs are used:
This means that it's important to have an index on the child side of the FK if any of the following are true:
Now you're ready to run the query on your own database and look at the results. The query tries to filter for the best indexing candidates, but it is just a query and you need to use your judgement on what you know about the tables. The query also filters for either the parent or child table being larger than 10MB.
Now, you might say "but I have an index on column Y" and wonder why it's appearing on the report. That's probably because the FK does match the first columns of the index. For example, an index on ( name, team_id ) cannot be used for an FK on team_id.
You may notice a 2nd section of the report called "questionable indexes". These are FKs which have an index available, but that index may not be usable for JOINs and constraint enforcement, or may be very inefficient. This includes:
I have added a query for finding foreign keys without indexes to pgx_scripts. Indexes on the "parent", or "referenced" side of the FK are automatically indexed (they have to be, because they need to be unique), so we won't talk about them further.
Now, in order to understand how to interpret this, you have to understand why you would or would not have an index on an FK, and what sort of indexes are valid. There's two times that indexes on the child side of FKs are used:
- when doing JOIN and lookup queries using the FK column
- when updating or deleting a row from the "parent" table
This means that it's important to have an index on the child side of the FK if any of the following are true:
- The child table is large and the parent table gets updates/deletes
- The parent table is large and the FK is used for JOINs
- The child table is large and the FK is used to filter (WHERE clause) records on the child table
Now you're ready to run the query on your own database and look at the results. The query tries to filter for the best indexing candidates, but it is just a query and you need to use your judgement on what you know about the tables. The query also filters for either the parent or child table being larger than 10MB.
Now, you might say "but I have an index on column Y" and wonder why it's appearing on the report. That's probably because the FK does match the first columns of the index. For example, an index on ( name, team_id ) cannot be used for an FK on team_id.
You may notice a 2nd section of the report called "questionable indexes". These are FKs which have an index available, but that index may not be usable for JOINs and constraint enforcement, or may be very inefficient. This includes:
- Non-BTree indexes. Currently other types of indexes can't be used for FK enforcement, although that is likely to change in future Postgres versions. But they can sometimes be used for joins.
- Indexes with more than one column in addition to the FK columns. These indexes can be used for FKs, but they may be very inefficient at it due to the bigger size and extra index levels.
- Partial indexes (i.e. INDEX ... WHERE). In general, these cannot be used for FK enforcement, but they can sometimes be used for joins.
Thursday, October 30, 2014
Upcoming Seattle Visit
I will be in Seattle soon on business. This will include two opportunties to do PostgreSQL community stuff:
- A meetup with SEAPUG on the 12th, where I will talk about the upcoming 9.4 features.
- A BOF at Usenix LISA where I talk about the same, only to a different crowd.
Tuesday, October 21, 2014
Introducing Flexible Freeze
One of the things I mentioned in my series on VACUUM FREEZE was that we really needed a Postgres utility which would opportunistically freeze tables during low traffic periods. Today I'm announcing the Flexible Freeze project, our first attempt at designing such a utility.
All that's there right now is a simple Python script. However, that script is already a useful tool, installed at multiple production sites. Here's how the script works:
Of course, I have a tanker-truck full of desired improvements/expansions to this. So, pull requests welcome.
If you're more into Ruby, Wanelo has rewritten flexible freeze for Rails and incorporated it into their Postmodern tool.
All that's there right now is a simple Python script. However, that script is already a useful tool, installed at multiple production sites. Here's how the script works:
- identify your active databases and daily/weekly low traffic periods.
- create a cron job which calls flexible_freeze.py with a time limit to keep it inside your low traffic window.
- flexible_freeze.py will loop through your tables with the oldest XIDs, freezing them until it runs out of time or out of tables
Of course, I have a tanker-truck full of desired improvements/expansions to this. So, pull requests welcome.
If you're more into Ruby, Wanelo has rewritten flexible freeze for Rails and incorporated it into their Postmodern tool.
Friday, October 10, 2014
New Table Bloat Query
To accompany the New Index Bloat Query, I've written a New Table Bloat Query. This also involves the launch of the pgx_scripts project on GitHub, which will include most of the "useful scripts" I talk about here, as well as some scripts from my co-workers.
The new table bloat query is different from the check_postgres.pl version in several ways:
Results look like this:
pct_bloat is how much of the table (0 to 100) is estimated to be dead space. MB_bloat is how many megabytes of bloat are estimated to exist. Table_mb is the actual size of the table in megabytes.
The suggested criteria is to list tables which are either more than 50% bloat and bigger than 10MB, or more than 25% bloat and bigger than 1GB. However, you should calibrate this according to your own database.
The new table bloat query is different from the check_postgres.pl version in several ways:
- Rewritten to use WITH statements for better maintainability and clarity
- Conditional logic for old Postgres versions and 32-bit platforms taken out
- Index bloat removed, since we have a separate query for that
- Columns formatted to be more immediately comprehensible
- Some attributes (such as JSON and polygon fields) have no stats, so those tables can't be estimated.
- There's no good way to estimate bloat for compressed (TOAST) attributes and rows.
Results look like this:
databasename | schemaname | tablename | pct_bloat | mb_bloat | table_mb
--------------+------------+-------------------+-----------+----------+----------
members_2014 | public | current_member | 92 | 16.98 | 18.547
members_2014 | public | member_response | 87 | 17.46 | 20.000
members_2014 | public | archive_member | 84 | 35.16 | 41.734
members_2014 | public | survey | 57 | 28.59 | 50.188
pct_bloat is how much of the table (0 to 100) is estimated to be dead space. MB_bloat is how many megabytes of bloat are estimated to exist. Table_mb is the actual size of the table in megabytes.
The suggested criteria is to list tables which are either more than 50% bloat and bigger than 10MB, or more than 25% bloat and bigger than 1GB. However, you should calibrate this according to your own database.
Thursday, October 2, 2014
JSONB and 9.4: Move Slow and Break Things
If you've been paying any attention at all, you're probably wondering why 9.4 isn't out yet. The answer is that we had to change JSONB at the last minute, in a way that breaks compatibility with earlier betas.
In August, a beta-testing user reported that we had an issue with JSONB not compressing well. This was because of the binary structure of key offsets at the beginning of the JSONB value, and the affects were dramatic; in worst cases, JSONB values were 150% larger than comparable JSON values. We spent August through September revising the data structure and Heikki and Tom eventually developed one which gives better compressibility without sacrificing extraction speed.
I did a few benchmarks on the various JSONB types. We're getting a JSONB which is both faster and smaller than competing databases, so it'll be worth the wait.
However, this means that we'll be releasing an 9.4beta3 next week, whose JSONB type will be incompatible with prior betas; you'll have to dump and reload if you were using Beta 1 or Beta 2 and have JSONB data. It also means a delay in final release of 9.4.
In August, a beta-testing user reported that we had an issue with JSONB not compressing well. This was because of the binary structure of key offsets at the beginning of the JSONB value, and the affects were dramatic; in worst cases, JSONB values were 150% larger than comparable JSON values. We spent August through September revising the data structure and Heikki and Tom eventually developed one which gives better compressibility without sacrificing extraction speed.
I did a few benchmarks on the various JSONB types. We're getting a JSONB which is both faster and smaller than competing databases, so it'll be worth the wait.
However, this means that we'll be releasing an 9.4beta3 next week, whose JSONB type will be incompatible with prior betas; you'll have to dump and reload if you were using Beta 1 or Beta 2 and have JSONB data. It also means a delay in final release of 9.4.
Subscribe to:
Posts (Atom)