Google Summer of Code 2014 is wrapped up: Maxence Ahlouche did an excellent job implementing one new algorithm for MADlib and refactored the code base for another one.
I posted a more detailled explanation in the Pivotal blog.
Google Summer of Code 2014 is wrapped up: Maxence Ahlouche did an excellent job implementing one new algorithm for MADlib and refactored the code base for another one.
I posted a more detailled explanation in the Pivotal blog.
As mentioned in my earlier blog, I'm visiting several events in the US and Canada in October and November. The first of these, the talk about WebRTC in CRM at xTupleCon, has moved from the previously advertised timeslot to Wednesday, 15 October at 14:15.
Later that day, there will be a WebRTC/JavaScript meetup in Norfolk hosted at the offices of xTuple. It is not part of xTupleCon and free to attend. Please register using the Eventbrite page created by xTuple.
This will be a hands on event for developers and other IT professionals, especially those in web development, network administration and IP telephony. Please bring laptops and mobile devices with the latest versions of both Firefox and Chrome to experience WebRTC.
If you do want to attend xTupleCon itself, please contact xTuple directly through this form for details about the promotional tickets for free software developers.
The 2.1.4 release of PostGIS is now available.
The PostGIS development team is happy to release patch for PostGIS 2.1, the 2.1.4 release. As befits a patch release, the focus is on bugs, breakages, and performance issues
http://download.osgeo.org/postgis/source/postgis-2.1.4.tar.gz
Continue Reading by clicking title hyperlink ..After my Btree bloat estimation query, I found some time to work on a new query for tables. The goal here is still to have a better bloat estimation using dedicated queries for each kind of objects.
Compare to the well known bloat query, this query pay attention to:
You’ll find the queries here:
I created the file sql/bloat_tables.sql
with the 9.0 and more query version. I edited the query to add the bloat reported by pgstattuple
(free_percent + dead_tuple_percent) to compare both results and added the following filter:
-- remove Non Applicable tablesNOTis_na-- remove tables with real bloat < 1 blockANDtblpages*((pst).free_percent+(pst).dead_tuple_percent)::float4/100>=1-- filter on table name using the parameter :tblnameANDtblnameLIKE:'tblname'
Here is the result on a fresh pagila database:
postgres@pagila=#\settblname%postgres@pagila=#\isql/bloat_tables.sql current_database | schemaname | tblname | real_size | bloat_size | tblpages | is_na | bloat_ratio | real_frag ------------------+------------+----------------+-----------+------------+----------+-------+------------------+----------- pagila | pg_catalog | pg_description | 253952 | 8192 | 31 | f | 3.2258064516129 | 3.34 pagila | public | city | 40960 | 8192 | 5 | f | 20 | 20.01 pagila | public | customer | 73728 | 8192 | 9 | f | 11.1111111111111 | 11.47 pagila | public | film | 450560 | 8192 | 55 | f | 1.81818181818182 | 3.26 pagila | public | rental | 1228800 | 131072 | 150 | f | 10.6666666666667 | 0.67(5 rows)
Well, not too bad. Let’s consider the largest table, clone it and create some bloat:
postgres@pagila=#createtablefilm2asselect*fromfilm;SELECT 1000postgres@pagila=#analyzefilm2;ANALYZEpostgres@pagila=#\settblnamefilm%postgres@pagila=#\isql/bloat_tables.sql current_database | schemaname | tblname | real_size | bloat_size | tblpages | is_na | bloat_ratio | real_frag ------------------+------------+---------+-----------+------------+----------+-------+------------------+----------- pagila | public | film | 450560 | 8192 | 55 | f | 1.81818181818182 | 3.26 pagila | public | film2 | 450560 | 8192 | 55 | f | 1.81818181818182 | 3.26(2 rows)postgres@pagila=#deletefromfilm2wherefilm_id<250;DELETE 249postgres@pagila=#analyzefilm2;ANALYZEpostgres@pagila=#\settblnamefilm2postgres@pagila=#\isql/bloat_tables.sql current_database | schemaname | tblname | real_size | bloat_size | tblpages | is_na | bloat_ratio | real_frag ------------------+------------+---------+-----------+------------+----------+-------+------------------+----------- pagila | public | film2 | 450560 | 122880 | 55 | f | 27.2727272727273 | 27.29(1 row)
Again, the bloat reported here is pretty close to the reality!
Some more tests:
postgres@pagila=#deletefromfilm2wherefilm_id<333;DELETE 83postgres@pagila=#analyzefilm2;ANALYZEpostgres@pagila=#\isql/bloat_tables.sql current_database | schemaname | tblname | real_size | bloat_size | tblpages | is_na | bloat_ratio | real_frag ------------------+------------+---------+-----------+------------+----------+-------+------------------+----------- pagila | public | film2 | 450560 | 155648 | 55 | f | 34.5454545454545 | 35.08(1 row)postgres@pagila=#deletefromfilm2wherefilm_id<666;DELETE 333postgres@pagila=#analyzefilm2;ANALYZEpostgres@pagila=#\isql/bloat_tables.sql current_database | schemaname | tblname | real_size | bloat_size | tblpages | is_na | bloat_ratio | real_frag ------------------+------------+---------+-----------+------------+----------+-------+------------------+----------- pagila | public | film2 | 450560 | 303104 | 55 | f | 67.2727272727273 | 66.43(1 row)
Good, good, good. What next?
You might have noticed I did not mentioned this table with a large deviation between the statistical bloat and the real one, called “rental”:
postgres@pagila=#\settblnamerentalpostgres@pagila=#\isql/bloat_tables.sql current_database | schemaname | tblname | real_size | bloat_size | tblpages | is_na | bloat_ratio | real_frag ------------------+------------+---------+-----------+------------+----------+-------+------------------+----------- pagila | public | rental | 1228800 | 131072 | 150 | f | 10.6666666666667 | 0.67(1 row)
This particular situation is exactly why I loved writing these bloat queries (including the btree one), confronting the statistics and the reality and finding a logical answer or a fix.
Statistical and real bloat are actually both right here. The statistical one is just measuring here the bloat AND something else we usually don’t pay attention to. I’ll call it the alignment overhead.
Depending on the fields types, PostgreSQL adds some padding before the values to align them inside the row in regards to the CPU word size. This help ensuring a value fits in only one CPU register when possible. Alignment padding are given in this pg_type page from PostgreSQL document, see field typalign
.
So let’s demonstrate how it influence the bloat here. Back to the rental table, here is its definition:
postgres@pagila=#\drental Table "public.rental" Column | Type | Modifiers --------------+-----------------------------+------------------------------------------------------------ rental_id | integer | not null default nextval('rental_rental_id_seq'::regclass) rental_date | timestamp without time zone | not null inventory_id | integer | not null customer_id | smallint | not null return_date | timestamp without time zone | staff_id | smallint | not null last_update | timestamp without time zone | not null default now()
All the fields here are fixed-size types, so it is quite easy to compute the row size:
rental_id
and inventory_id
are 4-bytes integers, possible alignment is every 4 bytes from the begining of the rowcustomer_id
and staff_id
are 2-bytes integers, possible alignment is every 2 bytes from the begining of the rowrental_date
, return_date
and last_update
are 8-bytes timestamps, possible alignment is every 8 bytes from the begining of the rowThe minimum row size would be 2*4 + 2*2 + 3*8
, 36 bytes. Considering the alignment optimization and the order of the fields, we now have (ascii art is easier to explain):
|0 1 2 3 4 5 6 7 8 |
| rental_id |***********PADDING************|
| rental_date |
| inventory_id |customer_id|******PADDING*****|
| return_date |
| staff_id |*****************PADDING******************|
| last_update |
That makes 12 bytes of padding and a total row size of 48 bytes instead of 36. Here are the 10%! Let’s double check this by the experience:
postgres@pagila=#createtablerental2asselectrental_date,return_date,last_update,rental_id,inventory_id,customer_id,staff_idfrompublic.rental;SELECT 16044postgres@pagila=#\drental2 Table "public.rental2" Column | Type | Modifiers --------------+-----------------------------+----------- rental_date | timestamp without time zone | return_date | timestamp without time zone | last_update | timestamp without time zone | rental_id | integer | inventory_id | integer | customer_id | smallint | staff_id | smallint | postgres@pagila=#\dt+rental* List of relations Schema | Name | Type | Owner | Size | Description --------+---------+-------+----------+---------+------------- public | rental | table | postgres | 1200 kB | public | rental2 | table | postgres | 1072 kB | (2 rows)postgres@pagila=#select100*(1200-1072)::float4/1200; ?column? ------------------ 10.6666666666667(1 row)
Removing the “remove tables with real bloat < 1 block” filter from my demo query, we have now:
postgres@pagila=#\settblnamerental%postgres@pagila=#\isql/bloat_tables.sql current_database | schemaname | tblname | real_size | bloat_size | tblpages | is_na | bloat_ratio | real_frag ------------------+------------+---------+-----------+------------+----------+-------+------------------+----------- pagila | public | rental | 1228800 | 131072 | 150 | f | 10.6666666666667 | 0.67 pagila | public | rental2 | 1097728 | 0 | 134 | f | 0 | 0.41(2 rows)
Great!
Sadly, I couldn’t find a good way to measure this in the queries so far, so I will live with that. By the way, this alignment overhead might be a nice subject for a script measuring it per tables.
The same than for the Btree statistical bloat query: I’m pretty sure the query will have a pretty bad estimation with array types. I’ll investigate about that later.
Cheers, and happy monitoring!
A co-worker of mine did a blog post last year that I’ve found incredibly useful when assisting clients with getting shared_buffers tuned accurately.
Setting shared_buffers the hard way
You can follow his queries there for using pg_buffercache to find out how your shared_buffers are actually being used. But I had an incident recently that I thought would be interesting to share that shows how shared_buffers may not need to be set nearly as high as you believe it should. Or it can equally show you that you that you definitely need to increase it. Object names have been sanitized to protect the innocent.
To set the stage, the database total size is roughly 260GB and the use case is high data ingestion with some reporting done on just the most recent data at the time. shared_buffers is set to 8GB. The other thing to note is that this is the only database in the cluster. pg_buffercache is installed on a per database basis, so you’ll have to install it on each database in the cluster and do some additional totalling to figure out your optimal setting in the end.
database=# SELECT c.relname , pg_size_pretty(count(*) * 8192) as buffered , round(100.0 * count(*) / ( SELECT setting FROM pg_settings WHERE name='shared_buffers')::integer,1) AS buffers_percent , round(100.0 * count(*) * 8192 / pg_relation_size(c.oid),1) AS percent_of_relation FROM pg_class c INNER JOIN pg_buffercache b ON b.relfilenode = c.relfilenode INNER JOIN pg_database d ON (b.reldatabase = d.oid AND d.datname = current_database()) GROUP BY c.oid, c.relname ORDER BY 3 DESC LIMIT 10; relname | buffered | buffers_percent | percent_of_relation -------------------------------------+----------+-----------------+--------------------- table1 | 7479 MB | 91.3 | 9.3 table2 | 362 MB | 4.4 | 100.0 table3 | 311 MB | 3.8 | 0.8 table4 | 21 MB | 0.3 | 100.0 pg_attrdef_adrelid_adnum_index | 16 kB | 0.0 | 100.0 table4 | 152 kB | 0.0 | 7.7 index5 | 16 kB | 0.0 | 14.3 pg_index_indrelid_index | 40 kB | 0.0 | 8.8 pg_depend_depender_index | 56 kB | 0.0 | 1.0 pg_cast_source_target_index | 16 kB | 0.0 | 100.0
You can see that table1 is taking up a vast majority of the space here and it’s a large table, so only 9% of it is actually in shared_buffers. What’s more interesting though is how much of the space for that table is actually in high demand.
database=# SELECT pg_size_pretty(count(*) * 8192) FROM pg_class c INNER JOIN pg_buffercache b ON b.relfilenode = c.relfilenode INNER JOIN pg_database d ON (b.reldatabase = d.oid AND d.datname = current_database()) WHERE c.oid::regclass = 'table1'::regclass AND usagecount >= 2; pg_size_pretty ---------------------- 2016 kB
Data blocks that go into and come out of postgres all go through shared_buffers. Just to review the blog post I linked to, whenever a block is used in shared memory, it increments a clock-sweep algorithm that ranges from 1-5, 5 being extremely high use data blocks. This means high usage blocks are likely to be kept in shared_buffers (if there’s room) and low usage blocks will get moved out if space for higher usage ones is needed. We believe that a simple insert or update sets a usagecount of 1. So, now we look at the difference when usage count is dropped to that.
database=# SELECT pg_size_pretty(count(*) * 8192) FROM pg_class c INNER JOIN pg_buffercache b ON b.relfilenode = c.relfilenode INNER JOIN pg_database d ON (b.reldatabase = d.oid AND d.datname = current_database()) WHERE c.oid::regclass = 'public.ip_addresses_taggings'::regclass AND usagecount >= 1; pg_size_pretty ---------------------- 4946 MB
So the shared_buffers is actually getting filled mostly by the data ingestion process, but relatively very little of it is of any further use afterwards. If anything of greater importance was needed in shared_buffers, there’s plenty of higher priority space and that inserted data would quickly get flushed out of shared memory due to having a low usagecount.
So with having pg_buffercache installed, we’ve found that the below query seems to be a good estimate on an optimal, minimum shared_buffers setting
database=# SELECT pg_size_pretty(count(*) * 8192) as ideal_shared_buffers FROM pg_class c INNER JOIN pg_buffercache b ON b.relfilenode = c.relfilenode INNER JOIN pg_database d ON (b.reldatabase = d.oid AND d.datname = current_database()) WHERE usagecount >= 3; ideal_shared_buffers ---------------------- 640 MB
This is the sort of query you would run after you have had your database running through your expected workload for a while. Also, note my use of the key word minimal. This does not account for unexpected spikes in shared_buffers usage that may occur during a session of reporting queries or something like that. So you definitely want to set it higher than this, but it can at least show you how effectively postgres is using its shared memory. In general we’ve found the typical suggestion of 8GB to be a great starting point for shared_buffers.
So, in the end, the purpose of this post was to show that shared_buffers is something that needs further investigation to really set optimally and there is a pretty easy method to figuring it out once you know where to look.
If you weren't able to make it to FOSS4G 2014 this year, you can still experience the event Live. All the tracks are being televised live and its pretty good reception. https://2014.foss4g.org/live/. Lots of GIS users using PostGIS and PostgreSQL. People seem to love Node.JS too.
After hearing enough about Node.JS from all these people, and this guy (Bill Dollins), I decided to try this out for myself.
I created a node.js web application - which you can download from here: https://github.com/robe2/node_postgis_express . It's really a spin-off from my other viewers, but more raw. I borrowed the same ideas as Bill, but instead of having a native node Postgres driver, I went for the pure javascript one so its easier to install on all platforms. I also experimented with using base-64 encoding to embed raster output directly into the browser so I don't have to have that silly img src path reference thing to contend with.
We had about 50 folks attend the PDXPUGDay 2014 last week, between DjangoCon and Foss4g. A lot of folks were already in town for one of the other confs, but several folks also day tripped from SeaPUG! Thanks for coming on down.
Thanks again to our speakers:
Josh Drake
David Wheeler
Eric Hanson
Veronika Megler
Kristin Tufte
Josh Berkus
(Plus our lightning talk speakers: Josh B, Mark W, and Basil!)
And our sponsors:
2nd Quadrant
iovation
PGX
And of course, PSU for hosting us.
Videos are linked from the wiki.
A while ago I wrote about compiling PostgreSQL extensions under Visual Studio– without having to recompile the whole PostgreSQL source tree.
I just finished the pg_sysdatetime extension, which is mainly for Windows but also supports compilation with PGXS on *nix. It’s small enough that it serves as a useful example of how to support Windows compilation in your extension, so it’s something I think is worth sharing with the community.
The actual Visual Studio project creation process took about twenty minutes, and would’ve taken less if I wasn’t working remotely over Remote Desktop on an AWS EC2 instance. Most of the time was taken by the simple but fiddly and annoying process of adding the include paths and library path for the x86 and x64 configurations. That’s necessary because MSVC can’t just get them from pg_config and doesn’t have seem to have user-defined project variables to let you specify a $(PGINSTALLDIR) in one place.
Working on Windows isn’t always fun – but it’s not as hard as it’s often made out to be either. If you maintain an extension but haven’t added Windows support it might be easier than you expect to do so.
Packaging it for x86 and x64 versions of each major PostgreSQL release, on the other hand… well, lets just say we could still use PGXS support for Windows with a “make installer” target.
Postgres 9.5 will come up with an additional logging option making possible to log replication commands that are being received by a node. It has been introduced by this commit.
commit: 4ad2a548050fdde07fed93e6c60a4d0a7eba0622
author: Fujii Masao <fujii@postgresql.org>
date: Sat, 13 Sep 2014 02:55:45 +0900
Add GUC to enable logging of replication commands.
Previously replication commands like IDENTIFY_COMMAND were not logged
even when log_statements is set to all. Some users who want to audit
all types of statements were not satisfied with this situation. To
address the problem, this commit adds new GUC log_replication_commands.
If it's enabled, all replication commands are logged in the server log.
There are many ways to allow us to enable that logging. For example,
we can extend log_statement so that replication commands are logged
when it's set to all. But per discussion in the community, we reached
the consensus to add separate GUC for that.
Reviewed by Ian Barwick, Robert Haas and Heikki Linnakangas.
The new parameter is called log_replication_commands and needs to be set in postgresql.conf. Default is off to not log this new information that may surprise existing users after an upgrade to 9.5 and newer versions. And actually replication commands received by a node were already logged at DEBUG1 level by the server. A last thing to note is that if log_replication_commands is enabled, all the commands will be printed as LOG and not as DEBUG1, which is kept for backward-compatibility purposes.
Now, a server enabling this logging mode...
$ psql -At -c 'show log_replication_commands'
on
... Is able to show replication commands in LOG mode. Here is for example the set of commands set by a standby starting up:
LOG: received replication command: IDENTIFY_SYSTEM
LOG: received replication command: START_REPLICATION 0/3000000 TIMELINE 1
This will certainly help utilities and users running audit for replication, so looking forward to see log parsing tools like pgbadger make some nice outputs using this information.
Thanks for Bucardo team for responding my previous post. My cascaded slave replication works as expected.
Today I notice there is still something to do related with delta and track tables.
Single table replication scenario:
Db-A/Tbl-T1 (master) => Db-B/Tbl-T2 (slave) => Db-C/Tbl-T3 (cascaded slave)
Every change on Table T1 replicated to T2, then T2 to T3. After a while, VAC successfully cleans delta and track tables on Db-A. But not on Db-B.
I detect 2 issues:
1. If cascaded replication T2 to T3 successful, the delta table on Db-B is not be cleaned up by VAC.
2. If cascaded replication T2 to T3 failed before VAC schedule, the delta table on Db-B will be cleaned up by VAC. Then, cascaded replication from T2 to T3 losts.
I fix it by modifying SQL inside bucardo.bucardo_purge_delta(text, text):
Need advice from Bucardo team.
It has been a little quiet on the U.S. front of late. Alas, summer of 2014 has come and gone and it is time to strap on the gators and get a little muddy. Although we have been relatively quiet we have been doing some work. In 2013 the board appointed two new board members, Jonathan S. Katz and Jim Mlodgeski. We also affiliated with multiple PostgreSQL User Groups:
I have yet to run PostgreSQL on GCE in production. I am still testing it but I have learned the following:
Either disk can be provisioned as a raw device allowing you to use Linux Software Raid to build a RAID 10 which even further increases speed and reliability. Think about that, 4 SSD provisioned disks in a RAID 10...
The downside I see outside of the general arguments against cloud services (shared tenancy, all your data in a big brother, lack of control over your resources, general distaste for $vendor, or whatever else we in our right minds can think up) is that GCE is current limited to 16 virtual CPUS and 104GB of memory.
What does that mean? Well it means that it is likely that GCE is perfect for 99% of PostgreSQL workloads. By far the majority of PostgreSQL need less than 104GB of memory. Granted, we have customers that have 256GB, 512GB and even more but those are few and far between.
It also means that EC2 is no longer your only choice for dynamic cloud provisioned VMs for PostgreSQL. Give it a shot, the more competition in this space the better.
pv
# import to vertica
zcat data.sql | pv -s 16986105538 -p -t -r | vsql
ALTER TABLE
0:13:56 [4.22MB/s] [==============> ] 14%