If timing is one dimension of your data and you want to index a timestamptz column. How do you index it? What solution is the best for your case?

Suppose that you have table_a, which has a timestamptz column named column_tz. Possibly, column_tz has numerous values of timestamp. By any reason you need to index column_tz by date because you don’t want to build a huge B-tree with many nodes in your memory unnecessarily. Unfortunately you will recognize that you can not create the index like this

CREATE INDEX index_table_a_column_tz_by_date ON table_a (column_tz::DATE);

Can magic parentheses help in this case?

CREATE INDEX index_table_a_column_tz_by_date ON table_a…

As you know PostgresSQL introduced index-only scan in version 9.2 like other DBMS to speed up queries by using index data only if possible, in other words PostgreSQL uses index to produce result for the queries without IO cost of accessing table tuples on disk. Before going to detail of slow count query let’s take a look on related concepts first.

  • Heap is the main table data on disk. Usually the data is stored on multiple pages.
  • MVCC stands for Multi version Concurrency Control. Basically each SQL statement sees a snapshot of data as it was some time ago, regardless of the current state of the underlying data. …

Does the following code output “Sum = 45”? The answer is NO.

package mainimport (
"fmt"
"sync"
)
func ConcurrentSum(l int) int {
var wg sync.WaitGroup
c := make(chan int, l)
for i := 0; i < l; i++ {
wg.Add(1)
go func() {
c <- i
wg.Done()
}()
}
wg.Wait()
close(c)

return processResult(c)
}
func processResult(c <-chan int) int {
total := 0
for n := range c {
total += n
}
return total
}
func main() {
sum := ConcurrentSum(10);
//What is the output here?
fmt.Println("Sum = ", sum)
}

If you want to have an output of “Sum = 45”, you have to pass the loop variable into the go routine as a parameter as below. …


Deadlock

It is a very common practice that people use channel to receive data from Go routine in order to implement a concurrency solution. Yeah using channel in Go routine is super convenient but if you are not really familiar with it, you may get some unexpected behaviors. Let’s take a look on the following example.

package mainimport (
"fmt"
"time"
"sync"
)
func ConcurrentSum(l int) uint64 {
var wg sync.WaitGroup
c := make(chan uint64)
for i := 0; i < l; i++ {
wg.Add(1)
go func(n int) {
//Suppose here we do a heavy API call instead
c <- uint64(n)
time.Sleep(time.Millisecond)
wg.Done()
}(i)
}
wg.Wait() …

Basically, Golang is not an OOP language so there is no inheritance concept in Golang. However, structs still can be extended through struct embedding. But keep in mind that if you redefine a property in your sub struct, struct embedding will not inherit the property from the embedded struct any more.

Now let’s take a look on the following example of struct embedding.

package mainimport (
"fmt"
)
type Animal struct {
Age int
Leg int
}
func (a *Animal) Move() {
fmt.Println("An animal moves by ", a.Leg, " legs.")
}
type Dog struct {
Animal
Leg int
}
func (d *Dog) Move() {
fmt.Println("A dog moves by ", d.Leg, " legs.") …

Several years ago, I built a service by Golang and unluckily I made two serious performance issues with regular expression and slice. The issues look not complicated but they took me a lot of time to investigate and fix. In this article, I would like to share my mistakes along with solutions and benchmarks. Hopefully, it is helpful for you to avoid such cases in your work.

1. Using Regular expression

Mistake

In a release, I used regular expression to validate whether a date time string meets RFC3339 standard or not. …


Follow up the previous article, this is one of the series “Pessimistic Programming”, monitoring and alert. Generally, Prometheus is a great tool to monitor your application, for me it is extremely good. As I mentioned in previous article, a mistake is a mistake, a tiny mistake may cause a big issue and we can not avoid mistakes completely. Needless to say, It is terrible if suddenly you recognize that your system is suffering a very bad performance issue but you do not have any metric monitoring board to investigate. Then it takes 12 hours with a ton of effort and 14 pizzas to fix just a small bug. Keeping in mind that monitoring is an important part of a system will help you react a problem quickly and effectively. …


On the early days of my coding life, I implemented a small feature in a given ticket with totally wrong code which will be in my mind forever for sure. Particularly, the code completely contrasted with the business logic of the feature. But somehow the ticket passed both my test and QA test to go through to staging in a release along with some other tickets. Technically, the wrong code can be illustrated as below:

if (!anObject.isRightSide) {
//Do something
}

In expectation, it should be implemented as:

if (anObject.isRightSide) {
//Do something
}

Honestly, this is just one real story in many mistakes which I made or knew as a developer up to now. A mistake is just a mistake and it always exists around us. The fault looks stupid but it proves that a mistake can appear anytime, anywhere in programming, called a bug. So that, personally I think a developer should be a bit “pessimistic” because we are not sure that a program is perfect and it does not have any bug. We have to accept that our code may have bugs and we must prepare ways to deal with them. Simply, it is “Pessimistic Programming”. …


Overview

Commonly, to display images in HTML or CSS we have to indicate URLs located to certain image files. Technically, this procedure requires one separated HTTP request for each image to load the file into client browser. Consequently, we have to store and manage the images somewhere in web servers, CDN or image storage services.

Image for post
Image for post

Fig 1: The above figure demonstrates that a web browser makes 3 different HTTP requests for 3 resource files.

In fact, there is a completely different way to display images on a web without any extra HTTP request for image files, called displaying images with data URI. Specifically, we will embed image data directly into HTML or CSS files by base64 encoded strings instead of declaring URLs to image files. In other words, base64 encoded image data is packaged in HTML or CSS payloads. …


Some friends asked me how MySQL uses index in join queries. Absolutely, this is a truly interesting question, which many people concern. Some days ago, I discovered that MySQL Workbench can generate a very informative graph of execution plan for each query executed [1]. So I decided to write this article to share some experience about using index in join queries.

Database

First things first, following is the database, which we will do some experiments on it later.

Image for post
Image for post

Figure 1: The database has three tables: `user`, `question` and `answer`.

`user` table has 512.038 rows. Each user is identified by an unique ID in `id` column. …

About

Tung Nguyen

A coding lover. Mouse and keyboard are my friends all day long. Computer is a part of my life and coding is my cup of tea.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store