If you were a murder witness, there is a long line of preliminary matters that would have happened long before you ever get near the courtroom.
You will have made at least one statement to the police and/or the prosecutor - probably a lot more than one. These are not made under oath so you cannot commit perjury by lying through your teeth although you can commit obstruction of justice, attempting to pervert the course of justice and similar crimes. In some jurisdictions, lying to law enforcement is its own crime, most notably, telling lies to the FBI is.
If you are in the united-states or canada, you will probably have been deposed by the defence. These are under oath so lying about a material fact is perjury. In other common law jurisdictions, there is no right to deposition but you might have been asked to give an affidavit of your testimony-in-chief which is a written statement of what you would have said on the witness stand before examination and cross-examination and are used to save time in the courtroom. These are made on oath or affirmed so you can commit perjury in them.
During examination and cross-examination, you are required to answer all valid questions put to you honestly and completely. A question is valid if it conforms with the rules of evidence in the particular jurisdiction - the most common requirements are that it must be relevant and not hearsay. However, the witness does not get to decide if a question is valid or not, the judge does. This may be following an objection from one of the parties or of their own volition.
Most jurisdictions have a rule against self-incrimination, however, this only applies to offences - not civil matters or embarrassment. For example, if you were in the alley to meet your mistress then a) choose a nicer place and b) you have to answer the question. Assuming that adultery is not illegal in the jurisdiction: it is in Saudi Arabia for example.
If you do claim a right against self-incrimination, you may have to disclose the answer to the judge who will rule a) if it is, in fact, self-incriminatory under the law and b) if the evidence is so important and material that they will grant you immunity for the testimony and require you to answer anyway.