Picking a Random Winning Entry When Entries Have a Weight

Assuming we have three entries for a drawing:

  • entry 1 has a weight of 3
  • entry 2 has a weight of 5
  • entry 3 has a weight of 2

We want to pick a random winning entry that takes into account the weights. This query will do that. 

How do entries get "weights"? Someone enters a drawing which results in an entry being created with a weight of 1. Later that day they are rewarded 5 bonus entries which just translates into us bumping their entry's weight to 6. 

Copying a Postgres Database

I often have to test things on a pretty large database that takes forever to import. Today I learned you can just copy it so you don't have to reimport!