All posts by kortsi

Upsert Methods for PostgreSQL

3.62 avg. rating (73% score) - 8 votes

Update: PostgreSQL 9.5 has been released with INSERT .. ON CONFLICT UPDATE support.


PostgreSQL has no “upsert” or “replace” or “insert .. on duplicate key update” or “merge into” construct to conditionally either insert a new row, or if a row with the key already exists, to either update the existing row with the new values, or first delete the old row and then insert a new one. This has been discussed many times in the posgresql developers mailing lists, and plans to implement the SQL standard MERGE operation have been devised. But there is still no such functionality in PostgreSQL as of version 9.3. Here are some examples on how to implement the functionality in different ways, using either a function, a trigger function or a rule. None of them are perfect. There are trade-offs to be made. Continue reading Upsert Methods for PostgreSQL

SQLAlchemy Declarative Class Reflector

3.00 avg. rating (63% score) - 5 votes

SQLAlchemy has a nice reflection facility which takes for example a database table name as argument and produces a Table object out of it for manipulation. However, those objects do not behave like the objects produced by declarative classes, which are easier to work with. Here’s a little class that helps to bridge that gap by reflecting proper declarative classes from database tables. It has only been tested with PostgreSQL, but it may work with other databases as well. Continue reading SQLAlchemy Declarative Class Reflector

Editing Tabular Data in Vim

3.33 avg. rating (68% score) - 6 votes

Helpful settings for editing tabular data:

Noexpandtab disables expanding of inputted tabs to spaces. Adjust the number of spaces to your liking. Tabstop sets the visual appearance of tab stops (here 16 spaces). Continue reading Editing Tabular Data in Vim

Developer Mode for the Samsung Galaxy S4

3.43 avg. rating (70% score) - 7 votes

From: Developer Mode for the Samsung Galaxy S4

Get to the phone’s settings by tapping ‘menu’ from the home screen and then tapping ‘settings’

  • tap on ‘more’
  • tap on ‘software information’
  • tap on ‘About Device’
  • find the build number, and tap it 7 times
  • on the 7th tap, a prompt should appear alerting you that Developer Options has been unlocked

You may also wish to:

And after that you must accept the USB connection by tapping OK in the prompt on the device after you connect it.

How to prefer IPv4 over IPv6 for some hosts

2.86 avg. rating (60% score) - 7 votes

If an IPv6 address is unreachable, but you can reach the IPv4 one, you can set the preference to IPv4 for that particular address by adding a line such as this in /etc/gai.conf:

That will set the preference for host 198.145.11.105 to IPv4.

See gai.conf(5) for details.